-
Notifications
You must be signed in to change notification settings - Fork 186
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
d93e1fa
commit 370a5ac
Showing
11 changed files
with
33 additions
and
98 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
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
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 |
---|---|---|
@@ -1,55 +1,32 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Optional, cast | ||
from typing import cast | ||
|
||
from attrs import Factory, define, field | ||
from attrs import define, field | ||
|
||
from griptape.artifacts import TextArtifact | ||
from griptape.chunkers import TextChunker | ||
from griptape.loaders import BaseTextLoader | ||
from griptape.tokenizers import OpenAiTokenizer | ||
|
||
if TYPE_CHECKING: | ||
from griptape.drivers import BaseEmbeddingDriver | ||
|
||
|
||
@define | ||
class TextLoader(BaseTextLoader): | ||
MAX_TOKEN_RATIO = 0.5 | ||
|
||
tokenizer: OpenAiTokenizer = field( | ||
default=Factory(lambda: OpenAiTokenizer(model=OpenAiTokenizer.DEFAULT_OPENAI_GPT_3_CHAT_MODEL)), | ||
kw_only=True, | ||
) | ||
max_tokens: int = field( | ||
default=Factory(lambda self: round(self.tokenizer.max_input_tokens * self.MAX_TOKEN_RATIO), takes_self=True), | ||
kw_only=True, | ||
) | ||
chunker: TextChunker = field( | ||
default=Factory( | ||
lambda self: TextChunker(tokenizer=self.tokenizer, max_tokens=self.max_tokens), | ||
takes_self=True, | ||
), | ||
kw_only=True, | ||
) | ||
embedding_driver: Optional[BaseEmbeddingDriver] = field(default=None, kw_only=True) | ||
encoding: str = field(default="utf-8", kw_only=True) | ||
|
||
def load(self, source: bytes | str, *args, **kwargs) -> list[TextArtifact]: | ||
def load(self, source: str | bytes, *args, **kwargs) -> TextArtifact: | ||
if isinstance(source, bytes): | ||
source = source.decode(encoding=self.encoding) | ||
elif isinstance(source, (bytearray, memoryview)): | ||
raise ValueError(f"Unsupported source type: {type(source)}") | ||
|
||
return self._text_to_artifacts(source) | ||
return TextArtifact(source) | ||
|
||
def load_collection( | ||
self, | ||
sources: list[bytes | str], | ||
*args, | ||
**kwargs, | ||
) -> dict[str, list[TextArtifact]]: | ||
) -> dict[str, TextArtifact]: | ||
return cast( | ||
dict[str, list[TextArtifact]], | ||
dict[str, TextArtifact], | ||
super().load_collection(sources, *args, **kwargs), | ||
) |
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