From 31775ce4cb1b49c7d2eed7c5e655d4f1970e18b3 Mon Sep 17 00:00:00 2001 From: Toni Date: Thu, 14 Dec 2023 16:18:26 +0100 Subject: [PATCH] add vision --- README.md | 13 +++++++++++++ src/Ollama.php | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9c727ff..336ac69 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,19 @@ $response = Ollama::agent('You are a weather expert...') ->ask(); ``` + +### Vision support + +```php +$response = Ollama::model('llava:13b') + ->prompt('What is in this picture?') + ->image(public_path('images/example.jpg')) + ->ask(); + +// "The image features a close-up of a person's hand, wearing bright pink fingernail polish and blue nail polish. In addition to the colorful nails, the hand has two tattoos – one is a cross and the other is an eye." + +``` + ### Chat completion ```php diff --git a/src/Ollama.php b/src/Ollama.php index 7335ede..e306d1b 100755 --- a/src/Ollama.php +++ b/src/Ollama.php @@ -4,6 +4,7 @@ use Cloudstudio\Ollama\Services\ModelService; use Cloudstudio\Ollama\Traits\MakesHttpRequests; +use Illuminate\Support\Facades\Storage; /** * Ollama class for integration with Laravel. @@ -75,6 +76,13 @@ class Ollama */ protected $agent; + /** + * Base64 encoded image. + * + * @var string|null + */ + protected $image = null; + /** * Ollama class constructor. */ @@ -226,6 +234,24 @@ public function pull() return $this; } + /** + * Sets an image for generation. + * + * @param string $imagePath + * @return $this + * @throws \Exception + */ + public function image(string $imagePath) + { + if (!file_exists($imagePath)) { + throw new \Exception("Image file does not exist: $imagePath"); + } + + $this->image = base64_encode(file_get_contents($imagePath)); + return $this; + } + + /** * Generates embeddings from the selected model. * @@ -245,7 +271,7 @@ public function embeddings(string $prompt) */ public function ask() { - return $this->sendRequest('/api/generate', [ + $requestData = [ 'model' => $this->model, 'system' => $this->agent, 'prompt' => $this->prompt, @@ -253,7 +279,13 @@ public function ask() 'options' => $this->options, 'stream' => $this->stream, 'raw' => $this->raw, - ]); + ]; + + if ($this->image) { + $requestData['images'] = [$this->image]; + } + + return $this->sendRequest('/api/generate', $requestData); } /**