-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GriptapeCloudVectorStoreDriver #885
Merged
Merged
Changes from 79 commits
Commits
Show all changes
91 commits
Select commit
Hold shift + click to select a range
a2f25ab
Add raw_query fn to GriptapeCloudKnowledgeBaseClient
cjkindel eb3c47e
Re-order Workflow tasks on every task execution wave
cjkindel 1859a75
changelog update
cjkindel 4aa2eb1
unit tests
cjkindel fbc9fdd
Merge branch 'dev' into cjkindel/raw_kb_query
cjkindel 8470635
Merge branch 'dev' into cjkindel/raw_kb_query
cjkindel 82f3b46
Consolidate raw into query()
cjkindel 27c080d
change to query_results in response
cjkindel 53f87fa
meta is dict
cjkindel d045fc5
add bonus to artifact json
cjkindel a95ddb9
fix bonus
cjkindel 546725e
bonus again
cjkindel 3f156f8
remove keywords from returned artifact
cjkindel 1fa5fe1
debug
cjkindel ebdfb40
fix keyword del
cjkindel 1a32edc
try/except keywords delete
cjkindel e56124c
Merge branch 'dev' into cjkindel/raw_kb_query
cjkindel e80674c
Merge branch 'dev' into cjkindel/raw_kb_query
cjkindel faf20ff
Initial griptape-cloud-vector-store-driver
cjkindel a9f9008
revert knowledge_base tool changes
cjkindel 5ddf15c
Rename metadata to meta
cjkindel 71aed95
fix changelog
cjkindel 0dad40c
formatting
cjkindel 09d7ffa
DC in changelog
cjkindel 0c2898f
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel 7539d66
change to KB specific
cjkindel bad5064
meta changelog
cjkindel 74b146a
require kb_id
cjkindel 19f5cd4
add to driver init
cjkindel 537e635
meta to kw_only
cjkindel fa38100
tests
cjkindel 306fa0f
doc improvement
cjkindel 6ab7890
use dummy embed driver
cjkindel 159d7b6
test
cjkindel 60532e3
only include optional params if defined
cjkindel e14ee75
remove converter from meta
cjkindel 6fefbea
debug
cjkindel bde14fe
remove debug
cjkindel 35ac22a
debug
cjkindel ab8da26
debug 2
cjkindel 750f675
fix?
cjkindel 1d2d6ad
fix ??
cjkindel 3f3c622
json load
cjkindel 373d353
debug part 176
cjkindel a9d857e
moar debug
cjkindel ee0dafa
get entry
cjkindel 6ea54bb
remove json loads
cjkindel 654155a
not-implemented upserts
cjkindel 6669f45
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel 432bb11
Update for RAGEngine
cjkindel 586fd25
remove meta change
cjkindel 8183f26
fix tests
cjkindel 5e0e7ff
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel a61180d
more tests
cjkindel 851736e
change default count
cjkindel 53101c1
debug
cjkindel 88cd303
remove debug
cjkindel f693b14
append meta to value post-query
cjkindel af6d13a
debug
cjkindel 6b6389a
moar debug
cjkindel 5d0cd17
remove debug
cjkindel e593506
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel b337972
remove DC from comment
cjkindel a48ab02
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel b0463ec
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel dbeae6d
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel 03582e4
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel c91b539
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel d0872a0
update changelog
cjkindel 2e7c3d6
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel 02092a4
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel 80e0aa2
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel fa89918
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel 0966448
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel fc05195
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel 6e13686
clean up request gen
cjkindel cbeb422
reformat tests
cjkindel a33b337
add docs
cjkindel 58ca7fc
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel 298e543
address comments
cjkindel b1b6c88
Update doc formatting
cjkindel 6f27f38
another docs format
cjkindel e7b3018
another d to D
cjkindel 11ba4e3
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel dc0938b
single summary lines
cjkindel d4860e6
back to multi-line with space
cjkindel 3bac8d1
I want a single command to run all checks that isn't the pre-commit hook
cjkindel c23bc50
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel 6bc8e42
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel 3165299
doc integ test fix
cjkindel cb8e4da
Merge branch 'dev' into cjkindel/gtc_vector_store_driver
cjkindel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
107 changes: 107 additions & 0 deletions
107
griptape/drivers/vector/griptape_cloud_knowledge_base_vector_store_driver.py
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,107 @@ | ||
from urllib.parse import urljoin | ||
import requests | ||
from typing import Optional, Any | ||
from attrs import Factory, define, field | ||
from griptape.artifacts import TextArtifact, ListArtifact | ||
from griptape.drivers import BaseEmbeddingDriver, BaseVectorStoreDriver, DummyEmbeddingDriver | ||
|
||
|
||
@define | ||
class GriptapeCloudKnowledgeBaseVectorStoreDriver(BaseVectorStoreDriver): | ||
"""A vector store driver for Griptape Cloud Knowledge Bases. | ||
|
||
Attributes: | ||
api_key: API Key for Griptape Cloud. | ||
knowledge_base_id: Knowledge Base ID for Griptape Cloud. | ||
base_url: Base URL for Griptape Cloud. | ||
headers: Headers for Griptape Cloud. | ||
""" | ||
|
||
api_key: str = field(kw_only=True, metadata={"serializable": True}) | ||
knowledge_base_id: str = field(kw_only=True, metadata={"serializable": True}) | ||
base_url: str = field(default="https://cloud.griptape.ai", kw_only=True) | ||
headers: dict = field( | ||
default=Factory(lambda self: {"Authorization": f"Bearer {self.api_key}"}, takes_self=True), kw_only=True | ||
) | ||
embedding_driver: BaseEmbeddingDriver = field( | ||
default=Factory(lambda: DummyEmbeddingDriver()), metadata={"serializable": True}, kw_only=True, init=False | ||
) | ||
|
||
def upsert_vector( | ||
self, | ||
vector: list[float], | ||
vector_id: Optional[str] = None, | ||
namespace: Optional[str] = None, | ||
meta: Optional[dict] = None, | ||
**kwargs, | ||
) -> str: | ||
raise NotImplementedError(f"{self.__class__.__name__} does not support vector upsert.") | ||
|
||
def upsert_text_artifact( | ||
self, | ||
artifact: TextArtifact, | ||
namespace: Optional[str] = None, | ||
meta: Optional[dict] = None, | ||
vector_id: Optional[str] = None, | ||
**kwargs, | ||
) -> str: | ||
raise NotImplementedError(f"{self.__class__.__name__} does not support text artifact upsert.") | ||
|
||
def upsert_text( | ||
self, | ||
string: str, | ||
vector_id: Optional[str] = None, | ||
namespace: Optional[str] = None, | ||
meta: Optional[dict] = None, | ||
**kwargs, | ||
) -> str: | ||
raise NotImplementedError(f"{self.__class__.__name__} does not support text upsert.") | ||
|
||
def load_entry(self, vector_id: str, namespace: Optional[str] = None) -> BaseVectorStoreDriver.Entry: | ||
"""Retrieves a specific vector entry from the collection based on its identifier and optional namespace.""" | ||
raise NotImplementedError(f"{self.__class__.__name__} does not support entry loading.") | ||
|
||
def load_entries(self, namespace: Optional[str] = None) -> list[BaseVectorStoreDriver.Entry]: | ||
"""Retrieves all vector entries from the collection, optionally filtering to only | ||
those that match the provided namespace. | ||
""" | ||
raise NotImplementedError(f"{self.__class__.__name__} does not support entry loading.") | ||
|
||
def load_artifacts(self, namespace: Optional[str] = None) -> ListArtifact: | ||
"""Retrieves all Artifacts from the collection, optionally filtering to only | ||
those that match the provided namespace. | ||
""" | ||
raise NotImplementedError(f"{self.__class__.__name__} does not support Artifact loading.") | ||
|
||
def query( | ||
self, | ||
query: str, | ||
count: Optional[int] = None, | ||
namespace: Optional[str] = None, | ||
include_vectors: Optional[bool] = None, | ||
distance_metric: Optional[str] = None, | ||
# GriptapeCloudKnowledgeBaseVectorStoreDriver-specific params: | ||
filter: Optional[dict] = None, | ||
**kwargs, | ||
) -> list[BaseVectorStoreDriver.Entry]: | ||
"""Performs a search on the Knowledge Base to find vectors similar to the provided input vector, | ||
optionally filtering to only those that match the provided filter(s). | ||
""" | ||
url = urljoin(self.base_url.strip("/"), f"/api/knowledge-bases/{self.knowledge_base_id}/query") | ||
|
||
request: dict[str, Any] = { | ||
"query": query, | ||
"count": count, | ||
"distance_metric": distance_metric, | ||
"filter": filter, | ||
"include_vectors": include_vectors, | ||
} | ||
request = {k: v for k, v in request.items() if v is not None} | ||
|
||
response = requests.post(url, json=request, headers=self.headers).json() | ||
entries = response.get("entries", []) | ||
entry_list = [BaseVectorStoreDriver.Entry.from_dict(entry) for entry in entries] | ||
return entry_list | ||
|
||
def delete_vector(self, vector_id: str): | ||
raise NotImplementedError(f"{self.__class__.__name__} does not support deletion.") |
79 changes: 79 additions & 0 deletions
79
tests/unit/drivers/vector/test_griptape_cloud_knowledge_base_vector_store_driver.py
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,79 @@ | ||
import pytest | ||
import uuid | ||
|
||
|
||
class MockResponse: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused? |
||
def __init__(self, json_data, status_code): | ||
self.json_data = json_data | ||
self.status_code = status_code | ||
|
||
def json(self): | ||
return self.json_data | ||
|
||
|
||
class TestGriptapeCloudKnowledgeBaseVectorStoreDriver: | ||
test_ids = [str(uuid.uuid4()), str(uuid.uuid4())] | ||
test_vecs = [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]] | ||
test_namespaces = [str(uuid.uuid4()), str(uuid.uuid4())] | ||
test_metas = [{"key": "value1"}, {"key": "value2"}] | ||
test_scores = [0.7, 0.8] | ||
|
||
@pytest.fixture | ||
def driver(self, mocker): | ||
from griptape.drivers import GriptapeCloudKnowledgeBaseVectorStoreDriver | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to top level import |
||
|
||
test_entries = { | ||
"entries": [ | ||
{ | ||
"id": self.test_ids[0], | ||
"vector": self.test_vecs[0], | ||
"namespace": self.test_namespaces[0], | ||
"meta": self.test_metas[0], | ||
"score": self.test_scores[0], | ||
}, | ||
{ | ||
"id": self.test_ids[1], | ||
"vector": self.test_vecs[1], | ||
"namespace": self.test_namespaces[1], | ||
"meta": self.test_metas[1], | ||
"score": self.test_scores[1], | ||
}, | ||
] | ||
} | ||
|
||
mock_response = mocker.Mock() | ||
mock_response.status_code = 200 | ||
mock_response.json.return_value = test_entries | ||
mocker.patch("requests.post", return_value=mock_response) | ||
|
||
return GriptapeCloudKnowledgeBaseVectorStoreDriver(api_key="foo bar", knowledge_base_id="1") | ||
|
||
def test_query(self, driver): | ||
result = driver.query( | ||
"some query", count=10, namespace="foo", include_vectors=True, distance_metric="bar", filter={"foo": "bar"} | ||
) | ||
|
||
assert result[0].id == self.test_ids[0] | ||
assert result[1].id == self.test_ids[1] | ||
assert result[0].vector == self.test_vecs[0] | ||
assert result[1].vector == self.test_vecs[1] | ||
assert result[0].namespace == self.test_namespaces[0] | ||
assert result[1].namespace == self.test_namespaces[1] | ||
assert result[0].meta == self.test_metas[0] | ||
assert result[1].meta == self.test_metas[1] | ||
assert result[0].score == self.test_scores[0] | ||
assert result[1].score == self.test_scores[1] | ||
|
||
def test_query_defaults(self, driver): | ||
result = driver.query("some query") | ||
|
||
assert result[0].id == self.test_ids[0] | ||
assert result[1].id == self.test_ids[1] | ||
assert result[0].vector == self.test_vecs[0] | ||
assert result[1].vector == self.test_vecs[1] | ||
assert result[0].namespace == self.test_namespaces[0] | ||
assert result[1].namespace == self.test_namespaces[1] | ||
assert result[0].meta == self.test_metas[0] | ||
assert result[1].meta == self.test_metas[1] | ||
assert result[0].score == self.test_scores[0] | ||
assert result[1].score == self.test_scores[1] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is not support
->is not supported at this time
.driver
->Driver
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want Driver capitalized, it isn't for rest of entries (can update all if we want)