Skip to content

Commit

Permalink
feat: add support in vector_store for server-side embeddings (#22)
Browse files Browse the repository at this point in the history
* Adds vectorize functionality
  • Loading branch information
jordanrfrazier authored Apr 29, 2024
1 parent 27dafbf commit 3149f79
Show file tree
Hide file tree
Showing 6 changed files with 674 additions and 128 deletions.
3 changes: 3 additions & 0 deletions libs/astradb/langchain_astradb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from astrapy.info import CollectionVectorServiceOptions

from langchain_astradb.cache import AstraDBCache, AstraDBSemanticCache
from langchain_astradb.chat_message_histories import AstraDBChatMessageHistory
from langchain_astradb.document_loaders import AstraDBLoader
Expand All @@ -12,4 +14,5 @@
"AstraDBChatMessageHistory",
"AstraDBLoader",
"AstraDBVectorStore",
"CollectionVectorServiceOptions",
]
16 changes: 16 additions & 0 deletions libs/astradb/langchain_astradb/utils/astradb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import langchain_core
from astrapy.api import APIRequestError
from astrapy.db import AstraDB, AstraDBCollection, AsyncAstraDB, AsyncAstraDBCollection
from astrapy.info import CollectionVectorServiceOptions


class SetupMode(Enum):
Expand Down Expand Up @@ -94,6 +95,9 @@ def __init__(
metric: Optional[str] = None,
requested_indexing_policy: Optional[Dict[str, Any]] = None,
default_indexing_policy: Optional[Dict[str, Any]] = None,
collection_vector_service_options: Optional[
CollectionVectorServiceOptions
] = None,
) -> None:
super().__init__(
token, api_endpoint, astra_db_client, async_astra_db_client, namespace
Expand Down Expand Up @@ -126,12 +130,18 @@ async def _setup_db() -> None:
else:
dimension = embedding_dimension

# Used for enabling $vectorize on the collection
service_dict: Optional[Dict[str, Any]] = None
if collection_vector_service_options is not None:
service_dict = collection_vector_service_options.as_dict()

try:
await async_astra_db.create_collection(
collection_name,
dimension=dimension,
metric=metric,
options=_options,
service_dict=service_dict,
)
except (APIRequestError, ValueError):
# possibly the collection is preexisting and may have legacy,
Expand Down Expand Up @@ -161,12 +171,18 @@ async def _setup_db() -> None:
"set to False"
)
else:
# Used for enabling $vectorize on the collection
service_dict: Optional[Dict[str, Any]] = None
if collection_vector_service_options is not None:
service_dict = collection_vector_service_options.as_dict()

try:
self.astra_db.create_collection(
collection_name,
dimension=embedding_dimension, # type: ignore[arg-type]
metric=metric,
options=_options,
service_dict=service_dict,
)
except (APIRequestError, ValueError):
# possibly the collection is preexisting and may have legacy,
Expand Down
Loading

0 comments on commit 3149f79

Please sign in to comment.