1. Go to this page and download the library: Download ardagnsrn/ollama-php 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/ */
ardagnsrn / ollama-php example snippets
// with default base URL
$client = \ArdaGnsrn\Ollama\Ollama::client();
// or with custom base URL
$client = \ArdaGnsrn\Ollama\Ollama::client('http://localhost:11434');
$completions = $client->completions()->create([
'model' => 'llama3.1',
'prompt' => 'Once upon a time',
]);
$completions->response; // '...in a land far, far away...'
$response->toArray(); // ['model' => 'llama3.1', 'response' => '...in a land far, far away...', ...]
$response = $client->chat()->create([
'model' => 'llama3.1',
'messages' => [
['role' => 'system', 'content' => 'You are a llama.'],
['role' => 'user', 'content' => 'Hello!'],
['role' => 'assistant', 'content' => 'Hi! How can I help you today?'],
['role' => 'user', 'content' => 'I need help with my taxes.'],
],
]);
$response->message->content; // 'Ah, taxes... *chew chew* Hmm, not really sure how to help with that.'
$response->toArray(); // ['model' => 'llama3.1', 'message' => ['role' => 'assistant', 'content' => 'Ah, taxes...'], ...]
$response = $client->chat()->create([
'model' => 'llama3.1',
'messages' => [
['role' => 'user', 'content' => 'What is the weather today in Paris?'],
],
'tools' => [
[
'type' => 'function',
'function' => [
'name' => 'get_current_weather',
'description' => 'Get the current weather',
'parameters' => [
'type' => 'object',
'properties' => [
'location' => [
'type' => 'string',
'description' => 'The location to get the weather for, e.g. San Francisco, CA',
],
'format' => [
'type' => 'string',
'description' => 'The location to get the weather for, e.g. San Francisco, CA',
'enum' => ['celsius', 'fahrenheit']
],
],
'
$responses = $client->chat()->createStreamed([
'model' => 'llama3.1',
'messages' => [
['role' => 'system', 'content' => 'You are a llama.'],
['role' => 'user', 'content' => 'Hello!'],
['role' => 'assistant', 'content' => 'Hi! How can I help you today?'],
['role' => 'user', 'content' => 'I need help with my taxes.'],
],
]);
foreach ($responses as $response) {
echo $response->message->content;
}
// 1. Iteration: 'Ah,'
// 2. Iteration: ' taxes'
// 3. Iteration: '... '
// 4. Iteration: ' *chew,'
// ...
$response = $client->models()->create([
'name' => 'mario',
'modelfile' => "FROM llama3.1\nSYSTEM You are mario from Super Mario Bros."
]);
$response->status; // 'success'
$responses = $client->models()->createStreamed([
'name' => 'mario',
'modelfile' => "FROM llama3.1\nSYSTEM You are mario from Super Mario Bros."
]);
foreach ($responses as $response) {
echo $response->status;
}