Skip to content

Commit

Permalink
Add chat completion feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni authored and Toni committed Dec 11, 2023
1 parent df93959 commit 1c7ee4f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ $response = Ollama::agent('You are a weather expert...')
->ask();
```

### Chat completion

```php
$messages = [
['role' => 'user', 'content' => 'My name is Toni Soriano and I live in Spain'],
['role' => 'assistant', 'content' => 'Nice to meet you , Toni Soriano'],
['role' => 'user', 'content' => 'where I live ?'],
];

$response = Ollama::agent('You know me really well!')
->model('llama2')
->chat($messages);

// "You mentioned that you live in Spain."

```

### Show Model Information

```php
Expand Down
69 changes: 69 additions & 0 deletions src/Ollama.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,67 @@ class Ollama
{
use MakesHttpRequests;

/**
* modelService
*
* @var mixed
*/
protected $modelService;

/**
* selectedModel
*
* @var mixed
*/
protected $selectedModel;

/**
* model
*
* @var mixed
*/
protected $model;

/**
* prompt
*
* @var mixed
*/
protected $prompt;

/**
* format
*
* @var mixed
*/
protected $format;

/**
* options
*
* @var mixed
*/
protected $options;

/**
* stream
*
* @var bool
*/
protected $stream = false;

/**
* raw
*
* @var mixed
*/
protected $raw;

/**
* agent
*
* @var mixed
*/
protected $agent;

/**
Expand Down Expand Up @@ -203,4 +255,21 @@ public function ask()
'raw' => $this->raw,
]);
}

/**
* Generates a chat completion using the specified model and conversation.
*
* @param array $conversation
* @return array
*/
public function chat(array $conversation)
{
return $this->sendRequest('/api/chat', [
'model' => $this->model,
'messages' => $conversation,
'format' => $this->format,
'options' => $this->options,
'stream' => $this->stream,
]);
}
}

0 comments on commit 1c7ee4f

Please sign in to comment.