1. Go to this page and download the library: Download sentience/ai library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sentience / ai example snippets
use Sentience\Ai\Api;
use Sentience\Ai\Ai;
$ai = Ai::connect(
Api::OpenAI,
baseUri: 'https://api.openai.com/v1/',
apiKey: getenv('OPENAI_API_KEY')
);
foreach ($ai
->prompt('gpt-4o-mini', 'Summarise the plot of Moby-Dick in two sentences.')
->execute() as $response
) {
echo $response->getContent();
}
foreach ($ai
->prompt('claude-3-5-sonnet-latest', 'What did I just ask you about?')
->withSystemPrompt('You are a terse assistant. Answer in one sentence.')
->withPreviousMessages($priorTurns)
->execute() as $response
) {
echo $response->getContent();
}
foreach ($ai
->prompt('gpt-4o', 'What is in this image?')
->withAttachment('/path/to/photo.png')
->withAttachment('/path/to/notes.txt')
->execute() as $response
) {
echo $response->getContent();
}
foreach ($ai
->prompt('gpt-4o', 'What is the weather in Amsterdam?')
->withTool(
name: 'get_weather',
tool: function (string $city): string {
return "{$city}: 14C, light rain";
},
description: 'Get the current weather for a city.'
)
->execute() as $response
) {
echo $response->getContent();
}
final class SearchTool implements ToolInterface
{
public function name(): string { return 'search'; }
public function description(): string { return 'Search the knowledge base.'; }
public function schema(): array { /* ... */ }
public function execute(array $arguments): string { /* ... */ }
}
foreach ($ai
->prompt('claude-3-5-sonnet-latest', '...')
->withToolInterface(new SearchTool())
->execute() as $response
) {
echo $response->getContent();
}
use Sentience\Ai\Apis\Length;
$generator = $ai
->prompt('gpt-4o-mini', 'Write a short essay on tide pools.')
->withStream()
->execute();
foreach ($generator as $response) {
$response->read(Length::Small);
echo $response->getContent();
}
$generator = $ai
->prompt('gpt-4o-mini', 'Write a short essay on tide pools.')
->execute();
foreach ($generator as $response) {
$response->readAll();
}
echo $response->getContent();
$generator = $ai
->prompt('gpt-4o', 'I have two dogs: Buster and Daisy.')
->execute();
foreach ($generator as $response) {
echo $response->getContent();
}
$newGenerator = $ai->continue('Can you think of cool last names for these dogs', 'gpt-3.5');
foreach ($newGenerator as $response) {
echo $response->getContent();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.