-
Notifications
You must be signed in to change notification settings - Fork 182
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
Showing
11 changed files
with
136 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Optional | ||
|
||
from attrs import define, field | ||
|
||
from griptape.artifacts import BaseArtifact | ||
|
||
if TYPE_CHECKING: | ||
from griptape.drivers import BaseEmbeddingDriver | ||
from griptape.tokenizers import BaseTokenizer | ||
|
||
|
||
@define | ||
class BaseTextArtifact(BaseArtifact): | ||
encoding: str = field(default="utf-8", kw_only=True) | ||
encoding_error_handler: str = field(default="strict", kw_only=True) | ||
_embedding: list[float] = field(factory=list, kw_only=True) | ||
|
||
@property | ||
def embedding(self) -> Optional[list[float]]: | ||
return None if len(self._embedding) == 0 else self._embedding | ||
|
||
def generate_embedding(self, driver: BaseEmbeddingDriver) -> Optional[list[float]]: | ||
self._embedding.clear() | ||
self._embedding.extend(driver.embed_string(str(self.value))) | ||
|
||
return self.embedding | ||
|
||
def token_count(self, tokenizer: BaseTokenizer) -> int: | ||
return tokenizer.count_tokens(str(self.value)) | ||
|
||
def to_bytes(self) -> bytes: | ||
return str(self.value).encode(encoding=self.encoding, errors=self.encoding_error_handler) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "griptape" | ||
version = "0.28.2" | ||
version = "0.29.0" | ||
description = "Modular Python framework for LLM workflows, tools, memory, and data." | ||
authors = ["Griptape <[email protected]>"] | ||
license = "Apache 2.0" | ||
|
@@ -59,7 +59,7 @@ opentelemetry-api = {version = "^1.25.0", optional = true} | |
opentelemetry-instrumentation = {version = "^0.46b0", optional = true} | ||
opentelemetry-instrumentation-threading = {version = "^0.46b0", optional = true} | ||
opentelemetry-exporter-otlp-proto-http = {version = "^1.25.0", optional = true} | ||
diffusers = {git = "https://github.com/griptape-ai/diffusers.git", branch = "main", optional = true} | ||
diffusers = {version = "^0.29.1", optional = true} | ||
accelerate = {version = "^0.32.1", optional = true} | ||
sentencepiece = {version = "^0.2.0", optional = true} | ||
torch = {version = "^2.3.1", optional = true} | ||
|
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,35 @@ | ||
import pytest | ||
|
||
from griptape.artifacts import TextArtifact | ||
from griptape.tokenizers import OpenAiTokenizer | ||
from tests.mocks.mock_embedding_driver import MockEmbeddingDriver | ||
|
||
|
||
class TestBaseTextArtifact: | ||
def test_generate_embedding(self): | ||
assert TextArtifact("foobar").generate_embedding(MockEmbeddingDriver()) == [0, 1] | ||
|
||
def test_embedding(self): | ||
artifact = TextArtifact("foobar") | ||
|
||
assert artifact.embedding is None | ||
assert artifact.generate_embedding(MockEmbeddingDriver()) == [0, 1] | ||
assert artifact.embedding == [0, 1] | ||
|
||
def test_to_bytes_encoding(self): | ||
assert ( | ||
TextArtifact("ß", name="foobar.txt", encoding="ascii", encoding_error_handler="backslashreplace").to_bytes() | ||
== b"\\xdf" | ||
) | ||
|
||
def test_to_bytes_encoding_error(self): | ||
with pytest.raises(ValueError): | ||
assert TextArtifact("ß", encoding="ascii").to_bytes() | ||
|
||
def test_token_count(self): | ||
assert ( | ||
TextArtifact("foobarbaz").token_count( | ||
OpenAiTokenizer(model=OpenAiTokenizer.DEFAULT_OPENAI_GPT_3_CHAT_MODEL) | ||
) | ||
== 2 | ||
) |
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