Skip to content

Commit

Permalink
Merge pull request #3 from cloudstudio/vision
Browse files Browse the repository at this point in the history
Add vision
  • Loading branch information
cloudstudio authored Dec 14, 2023
2 parents 66546f3 + 31775ce commit 2dd7464
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 34 additions & 2 deletions src/Ollama.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -75,6 +76,13 @@ class Ollama
*/
protected $agent;

/**
* Base64 encoded image.
*
* @var string|null
*/
protected $image = null;

/**
* Ollama class constructor.
*/
Expand Down Expand Up @@ -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.
*
Expand All @@ -245,15 +271,21 @@ public function embeddings(string $prompt)
*/
public function ask()
{
return $this->sendRequest('/api/generate', [
$requestData = [
'model' => $this->model,
'system' => $this->agent,
'prompt' => $this->prompt,
'format' => $this->format,
'options' => $this->options,
'stream' => $this->stream,
'raw' => $this->raw,
]);
];

if ($this->image) {
$requestData['images'] = [$this->image];
}

return $this->sendRequest('/api/generate', $requestData);
}

/**
Expand Down

0 comments on commit 2dd7464

Please sign in to comment.