-
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
15045bd
commit 872779e
Showing
4 changed files
with
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from __future__ import annotations | ||
from collections.abc import Iterator | ||
from attrs import define, field | ||
from griptape.artifacts import TextArtifact | ||
from griptape.drivers import BasePromptDriver | ||
from griptape.utils import PromptStack, import_optional_dependency | ||
|
||
|
||
@define | ||
class OllamaPromptDriver(BasePromptDriver): | ||
""" | ||
Attributes: | ||
api_key: Cohere API key. | ||
model: Cohere model name. | ||
client: Custom `cohere.Client`. | ||
""" | ||
|
||
model: str = field(kw_only=True, metadata={"serializable": True}) | ||
|
||
def try_run(self, prompt_stack: PromptStack) -> TextArtifact: | ||
ollama = import_optional_dependency("ollama") | ||
|
||
response = ollama.chat(**self._base_params(prompt_stack)) | ||
|
||
return response["message"]["content"] | ||
|
||
def try_stream(self, prompt_stack: PromptStack) -> Iterator[TextArtifact]: | ||
ollama = import_optional_dependency("ollama") | ||
|
||
stream = ollama.chat(**self._base_params(prompt_stack), stream=True) | ||
|
||
for chunk in stream: | ||
yield TextArtifact(value=chunk["message"]["content"]) | ||
|
||
def _base_params(self, prompt_stack: PromptStack) -> dict: | ||
messages = [{"role": input.role, "content": input.content} for input in prompt_stack.inputs] | ||
|
||
return { | ||
"message": messages, | ||
"temperature": self.temperature, | ||
"stop": self.tokenizer.stop_sequences, | ||
"num_predict": self.max_tokens, | ||
} |
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