-
Notifications
You must be signed in to change notification settings - Fork 8k
feat: Add LangChain vector store adapter for CrateDB #6011
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,90 @@ | ||||||||||||||||||||||||||||||||||
| import typing as t | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| from langchain_cratedb import CrateDBVectorStore | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| from langflow.base.vectorstores.model import LCVectorStoreComponent, check_cached_vector_store | ||||||||||||||||||||||||||||||||||
| from langflow.helpers import docs_to_data | ||||||||||||||||||||||||||||||||||
| from langflow.io import HandleInput, IntInput, SecretStrInput, StrInput | ||||||||||||||||||||||||||||||||||
| from langflow.schema import Data | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class CrateDBVectorStoreComponent(LCVectorStoreComponent): | ||||||||||||||||||||||||||||||||||
| display_name = "CrateDBVector" | ||||||||||||||||||||||||||||||||||
| description = "CrateDB Vector Store with search capabilities" | ||||||||||||||||||||||||||||||||||
| name = "CrateDB" | ||||||||||||||||||||||||||||||||||
| icon = "CrateDB" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| inputs = [ | ||||||||||||||||||||||||||||||||||
| SecretStrInput(name="server_url", display_name="CrateDB SQLAlchemy URL", required=True), | ||||||||||||||||||||||||||||||||||
| StrInput(name="collection_name", display_name="Table", required=True), | ||||||||||||||||||||||||||||||||||
| *LCVectorStoreComponent.inputs, | ||||||||||||||||||||||||||||||||||
| HandleInput(name="embedding", display_name="Embedding", input_types=["Embeddings"], required=True), | ||||||||||||||||||||||||||||||||||
| IntInput( | ||||||||||||||||||||||||||||||||||
| name="number_of_results", | ||||||||||||||||||||||||||||||||||
| display_name="Number of Results", | ||||||||||||||||||||||||||||||||||
| info="Number of results to return.", | ||||||||||||||||||||||||||||||||||
| value=4, | ||||||||||||||||||||||||||||||||||
| advanced=True, | ||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+17
to
+29
Contributor
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. 🛠️ Refactor suggestion Missing
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @check_cached_vector_store | ||||||||||||||||||||||||||||||||||
| def build_vector_store(self) -> CrateDBVectorStore: | ||||||||||||||||||||||||||||||||||
| documents = [] | ||||||||||||||||||||||||||||||||||
| for _input in self.ingest_data or []: | ||||||||||||||||||||||||||||||||||
| if isinstance(_input, Data): | ||||||||||||||||||||||||||||||||||
| documents.append(_input.to_lc_document()) | ||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||
| documents.append(_input) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| connection_string = self.server_url or "crate://" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if documents: | ||||||||||||||||||||||||||||||||||
| store = CrateDBVectorStore.from_documents( | ||||||||||||||||||||||||||||||||||
| embedding=self.embedding, | ||||||||||||||||||||||||||||||||||
| documents=documents, | ||||||||||||||||||||||||||||||||||
| collection_name=self.collection_name, | ||||||||||||||||||||||||||||||||||
| connection=connection_string, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||
| store = CrateDBVectorStore.from_existing_index( | ||||||||||||||||||||||||||||||||||
| embedding=self.embedding, | ||||||||||||||||||||||||||||||||||
| collection_name=self.collection_name, | ||||||||||||||||||||||||||||||||||
| connection=connection_string, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return store | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def search_documents(self) -> list[Data]: | ||||||||||||||||||||||||||||||||||
| vector_store = self.build_vector_store() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if self.search_query and isinstance(self.search_query, str) and self.search_query.strip(): | ||||||||||||||||||||||||||||||||||
| docs = vector_store.similarity_search( | ||||||||||||||||||||||||||||||||||
| query=self.search_query, | ||||||||||||||||||||||||||||||||||
| k=self.number_of_results, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| data = docs_to_data(docs) | ||||||||||||||||||||||||||||||||||
| self.status = data | ||||||||||||||||||||||||||||||||||
| return data | ||||||||||||||||||||||||||||||||||
| return [] | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+58
to
+70
Contributor
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. 🛠️ Refactor suggestion Duplicated search logic bypasses MMR & score-threshold paths This override always calls - def search_documents(self) -> list[Data]:
- vector_store = self.build_vector_store()
- ...
- docs = vector_store.similarity_search(
- query=self.search_query,
- k=self.number_of_results,
- )
+ # Remove this override; the base implementation already handles
+ # caching and dispatches to vector_store.search with the chosen
+ # search_type.This instantly enables MMR and score-threshold searches without extra code. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def cratedb_collection_to_data(embedding_documents: list[t.Any]): | ||||||||||||||||||||||||||||||||||
| """Converts a collection of CrateDB vectors into a list of data. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||
| embedding_documents (dict): A list of EmbeddingStore instances. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||
| list: A list of data, where each record represents a document in the collection. | ||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||
| data = [] | ||||||||||||||||||||||||||||||||||
| for doc in embedding_documents: | ||||||||||||||||||||||||||||||||||
| data_dict = { | ||||||||||||||||||||||||||||||||||
| "id": doc.id, | ||||||||||||||||||||||||||||||||||
| "text": doc.document, | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| data_dict.update(doc.cmetadata) | ||||||||||||||||||||||||||||||||||
| data.append(Data(**data_dict)) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+84
to
+89
Contributor
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. Guard against
- data_dict.update(doc.cmetadata)
+ if doc.cmetadata:
+ data_dict.update(doc.cmetadata)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| return data | ||||||||||||||||||||||||||||||||||
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.
Incorrect helper import path
docs_to_datalives inlangflow.helpers.data, not the package root.This avoids ImportError in environments where
langflow.helpers.__init__doesn’t re-export the symbol.📝 Committable suggestion
🤖 Prompt for AI Agents