-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ceadf6
commit 3cc531e
Showing
10 changed files
with
218 additions
and
9 deletions.
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,69 @@ | ||
from __future__ import annotations | ||
from collections.abc import Iterator | ||
from typing import TYPE_CHECKING, Optional | ||
from attrs import define, field, Factory | ||
from griptape.artifacts import TextArtifact | ||
from griptape.drivers import BasePromptDriver | ||
from griptape.tokenizers.base_tokenizer import BaseTokenizer | ||
from griptape.utils import PromptStack, import_optional_dependency | ||
from griptape.tokenizers import SimpleTokenizer | ||
|
||
if TYPE_CHECKING: | ||
from ollama import Client | ||
|
||
|
||
@define | ||
class OllamaPromptDriver(BasePromptDriver): | ||
""" | ||
Attributes: | ||
model: Model name. | ||
""" | ||
|
||
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, | ||
) | ||
tokenizer: BaseTokenizer = field( | ||
default=Factory( | ||
lambda self: SimpleTokenizer( | ||
characters_per_token=4, max_input_tokens=2000, max_output_tokens=self.max_tokens | ||
), | ||
takes_self=True, | ||
), | ||
kw_only=True, | ||
) | ||
options: dict = field( | ||
default=Factory( | ||
lambda self: { | ||
"temperature": self.temperature, | ||
"stop": self.tokenizer.stop_sequences, | ||
"num_predict": self.max_tokens, | ||
}, | ||
takes_self=True, | ||
), | ||
kw_only=True, | ||
) | ||
|
||
def try_run(self, prompt_stack: PromptStack) -> TextArtifact: | ||
response = self.client.chat(**self._base_params(prompt_stack)) | ||
|
||
if isinstance(response, dict): | ||
return TextArtifact(value=response["message"]["content"]) | ||
else: | ||
raise Exception("invalid model response") | ||
|
||
def try_stream(self, prompt_stack: PromptStack) -> Iterator[TextArtifact]: | ||
stream = self.client.chat(**self._base_params(prompt_stack), stream=True) | ||
|
||
if isinstance(stream, Iterator): | ||
for chunk in stream: | ||
yield TextArtifact(value=chunk["message"]["content"]) | ||
else: | ||
raise Exception("invalid model response") | ||
|
||
def _base_params(self, prompt_stack: PromptStack) -> dict: | ||
messages = [{"role": input.role, "content": input.content} for input in prompt_stack.inputs] | ||
|
||
return {"messages": messages, "model": self.model, "options": self.options} |
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
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
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,96 @@ | ||
from griptape.drivers import OllamaPromptDriver | ||
from griptape.utils import PromptStack | ||
import pytest | ||
|
||
|
||
class TestOllamaPromptDriver: | ||
@pytest.fixture | ||
def mock_client(self, mocker): | ||
mock_client = mocker.patch("ollama.Client") | ||
|
||
mock_client.return_value.chat.return_value = {"message": {"content": "model-output"}} | ||
|
||
return mock_client | ||
|
||
@pytest.fixture | ||
def mock_stream_client(self, mocker): | ||
mock_stream_client = mocker.patch("ollama.Client") | ||
mock_stream_client.return_value.chat.return_value = iter([{"message": {"content": "model-output"}}]) | ||
|
||
return mock_stream_client | ||
|
||
def test_init(self): | ||
assert OllamaPromptDriver(model="llama") | ||
|
||
def test_try_run(self, mock_client): | ||
# Given | ||
prompt_stack = PromptStack() | ||
prompt_stack.add_generic_input("generic-input") | ||
prompt_stack.add_system_input("system-input") | ||
prompt_stack.add_user_input("user-input") | ||
prompt_stack.add_assistant_input("assistant-input") | ||
driver = OllamaPromptDriver(model="llama") | ||
expected_messages = [ | ||
{"role": "generic", "content": "generic-input"}, | ||
{"role": "system", "content": "system-input"}, | ||
{"role": "user", "content": "user-input"}, | ||
{"role": "assistant", "content": "assistant-input"}, | ||
] | ||
|
||
# When | ||
text_artifact = driver.try_run(prompt_stack) | ||
|
||
# Then | ||
mock_client.return_value.chat.assert_called_once_with( | ||
messages=expected_messages, | ||
model=driver.model, | ||
options={"temperature": driver.temperature, "stop": [], "num_predict": driver.max_tokens}, | ||
) | ||
assert text_artifact.value == "model-output" | ||
|
||
def test_try_run_bad_response(self, mock_client): | ||
# Given | ||
prompt_stack = PromptStack() | ||
driver = OllamaPromptDriver(model="llama") | ||
mock_client.return_value.chat.return_value = "bad-response" | ||
|
||
# When/Then | ||
with pytest.raises(Exception, match="invalid model response"): | ||
driver.try_run(prompt_stack) | ||
|
||
def test_try_stream_run(self, mock_stream_client): | ||
# Given | ||
prompt_stack = PromptStack() | ||
prompt_stack.add_generic_input("generic-input") | ||
prompt_stack.add_system_input("system-input") | ||
prompt_stack.add_user_input("user-input") | ||
prompt_stack.add_assistant_input("assistant-input") | ||
expected_messages = [ | ||
{"role": "generic", "content": "generic-input"}, | ||
{"role": "system", "content": "system-input"}, | ||
{"role": "user", "content": "user-input"}, | ||
{"role": "assistant", "content": "assistant-input"}, | ||
] | ||
driver = OllamaPromptDriver(model="llama", stream=True) | ||
|
||
# When | ||
text_artifact = next(driver.try_stream(prompt_stack)) | ||
|
||
# Then | ||
mock_stream_client.return_value.chat.assert_called_once_with( | ||
messages=expected_messages, | ||
model=driver.model, | ||
options={"temperature": driver.temperature, "stop": [], "num_predict": driver.max_tokens}, | ||
stream=True, | ||
) | ||
assert text_artifact.value == "model-output" | ||
|
||
def test_try_stream_bad_response(self, mock_stream_client): | ||
# Given | ||
prompt_stack = PromptStack() | ||
driver = OllamaPromptDriver(model="llama", stream=True) | ||
mock_stream_client.return_value.chat.return_value = "bad-response" | ||
|
||
# When/Then | ||
with pytest.raises(Exception, match="invalid model response"): | ||
next(driver.try_stream(prompt_stack)) |
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