From 1c7ee4f78704d15e58269355ed725f0c68180061 Mon Sep 17 00:00:00 2001 From: Toni Date: Mon, 11 Dec 2023 10:29:53 +0100 Subject: [PATCH] Add chat completion feature --- README.md | 17 +++++++++++++ src/Ollama.php | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/README.md b/README.md index d30fb93..a5cb085 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Ollama.php b/src/Ollama.php index 8be71be..7335ede 100755 --- a/src/Ollama.php +++ b/src/Ollama.php @@ -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; /** @@ -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, + ]); + } }