-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into feature/native-functions
- Loading branch information
Showing
7 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import annotations | ||
from typing import Optional, TYPE_CHECKING | ||
from attrs import define, field, Factory | ||
from griptape.utils import import_optional_dependency | ||
from griptape.drivers import BaseEmbeddingDriver | ||
|
||
if TYPE_CHECKING: | ||
from ollama import Client | ||
|
||
|
||
@define | ||
class OllamaEmbeddingDriver(BaseEmbeddingDriver): | ||
""" | ||
Attributes: | ||
model: Ollama embedding model name. | ||
host: Optional Ollama host. | ||
client: Ollama `Client`. | ||
""" | ||
|
||
model: str = field(kw_only=True, metadata={"serializable": True}) | ||
host: Optional[str] = field(default=None, kw_only=True, metadata={"serializable": True}) | ||
client: Client = field( | ||
default=Factory(lambda self: import_optional_dependency("ollama").Client(host=self.host), takes_self=True), | ||
kw_only=True, | ||
) | ||
|
||
def try_embed_chunk(self, chunk: str) -> list[float]: | ||
return list(self.client.embeddings(model=self.model, prompt=chunk)["embedding"]) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
tests/unit/drivers/embedding/test_ollama_embedding_driver.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pytest | ||
from griptape.drivers import OllamaEmbeddingDriver | ||
|
||
|
||
class TestOllamaEmbeddingDriver: | ||
@pytest.fixture(autouse=True) | ||
def mock_client(self, mocker): | ||
mock_client = mocker.patch("ollama.Client") | ||
|
||
mock_client.return_value.embeddings.return_value = {"embedding": [0, 1, 0]} | ||
|
||
return mock_client | ||
|
||
def test_init(self): | ||
assert OllamaEmbeddingDriver(model="foo") | ||
|
||
def test_try_embed_chunk(self): | ||
assert OllamaEmbeddingDriver(model="foo").try_embed_chunk("foobar") == [0, 1, 0] |