Skip to content

Commit

Permalink
Clean up Vector Store Driver examples
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter committed Oct 1, 2024
1 parent 2676077 commit 2bc3da3
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **BREAKING**: Removed `loaders-dataframe` and `loaders-audio` extras as they are no longer needed.
- **BREKING**: `TextLoader`, `PdfLoader`, `ImageLoader`, and `AudioLoader` now take a `str | PathLike` instead of `bytes`. Passing `bytes` is still supported but deprecated.
- **BREAKING**: Removed `DataframeLoader`.
- `BaseVectorStoreDriver.upsert_text_artifacts` now returns a list or dictionary of upserted vector ids.
- `LocalFileManagerDriver.workdir` is now optional.
- `filetype` is now a core dependency.
- `FileManagerTool` now uses `filetype` for more accurate file type detection.
Expand Down
3 changes: 1 addition & 2 deletions docs/examples/src/query_webpage_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
artifacts = WebLoader().load("https://www.griptape.ai")
chunks = TextChunker().chunk(artifacts)

for chunk in chunks:
vector_store.upsert_text_artifact(chunk, namespace="griptape")
vector_store.upsert_text_artifacts({"griptape": chunks})

results = vector_store.query("creativity", count=3, namespace="griptape")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
chunks = TextChunker(max_tokens=100).chunk(artifact)

# Upsert Artifacts into the Vector Store Driver
[vector_store_driver.upsert_text_artifact(chunk, namespace="griptape") for chunk in chunks]
vector_store_driver.upsert_text_artifacts({"griptape": chunks})

results = vector_store_driver.query(query="What is griptape?")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
)

# Upsert Artifacts into the Vector Store Driver
[vector_store_driver.upsert_text_artifact(chunk, namespace="griptape") for chunk in chunks]
vector_store_driver.upsert_text_artifacts({"griptape": chunks})

results = vector_store_driver.query(query="What is griptape?")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
chunks = TextChunker().chunk(artifact)

# Upsert Artifacts into the Vector Store Driver
[vector_store_driver.upsert_text_artifact(chunk, namespace="griptape") for chunk in chunks]
vector_store_driver.upsert_text_artifacts({"griptape": chunks})

results = vector_store_driver.query(query="What is griptape?")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
chunks = TextChunker(max_tokens=100).chunk(artifact)

# Upsert Artifacts into the Vector Store Driver
[vector_store_driver.upsert_text_artifact(chunk, namespace="griptape") for chunk in chunks]
vector_store_driver.upsert_text_artifacts({"griptape": chunks})

results = vector_store_driver.query(query="What is griptape?")

Expand Down
6 changes: 1 addition & 5 deletions docs/griptape-framework/engines/src/summary_engines_1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import requests

from griptape.chunkers import TextChunker
from griptape.drivers import OpenAiChatPromptDriver
from griptape.engines import PromptSummaryEngine
from griptape.loaders import PdfLoader
Expand All @@ -11,8 +10,5 @@
)

artifact = PdfLoader().parse(response.content)
chunks = TextChunker().chunk(artifact)

text = "\n\n".join([chunk.value for chunk in chunks])

engine.summarize_text(text)
engine.summarize_artifacts(artifact)
28 changes: 8 additions & 20 deletions griptape/drivers/vector/base_vector_store_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def upsert_text_artifacts(
*,
meta: Optional[dict] = None,
**kwargs,
) -> None:
) -> list[str] | dict[str, list[str]]:
if isinstance(artifacts, list):
utils.execute_futures_list(
return utils.execute_futures_list(
[
self.futures_executor.submit(self.upsert_text_artifact, a, namespace=None, meta=meta, **kwargs)
for a in artifacts
Expand All @@ -65,7 +65,7 @@ def upsert_text_artifacts(
)
)

utils.execute_futures_list_dict(futures_dict)
return utils.execute_futures_list_dict(futures_dict)

def upsert_text_artifact(
self,
Expand All @@ -89,32 +89,20 @@ def upsert_text_artifact(

vector = artifact.embedding or artifact.generate_embedding(self.embedding_driver)

if isinstance(vector, list):
return self.upsert_vector(vector, vector_id=vector_id, namespace=namespace, meta=meta, **kwargs)
else:
raise ValueError("Vector must be an instance of 'list'.")
return self.upsert_vector(vector, vector_id=vector_id, namespace=namespace, meta=meta, **kwargs)

def upsert_text(
self,
string: str,
*,
vector_id: Optional[str] = None,
namespace: Optional[str] = None,
meta: Optional[dict] = None,
vector_id: Optional[str] = None,
**kwargs,
) -> str:
vector_id = self._get_default_vector_id(string) if vector_id is None else vector_id

if self.does_entry_exist(vector_id, namespace=namespace):
return vector_id
else:
return self.upsert_vector(
self.embedding_driver.embed_string(string),
vector_id=vector_id,
namespace=namespace,
meta=meta or {},
**kwargs,
)
return self.upsert_text_artifact(
TextArtifact(string), vector_id=vector_id, namespace=namespace, meta=meta, **kwargs
)

def does_entry_exist(self, vector_id: str, *, namespace: Optional[str] = None) -> bool:
try:
Expand Down

0 comments on commit 2bc3da3

Please sign in to comment.