|
| 1 | +from typing import List, Optional |
| 2 | + |
| 3 | +from langchain_core.embeddings import Embeddings |
| 4 | + |
| 5 | +from langflow.components.vectorstores.base.model import LCVectorStoreComponent |
| 6 | +from langflow.components.vectorstores.Upstash import UpstashVectorStoreComponent |
| 7 | +from langflow.field_typing import Text |
| 8 | +from langflow.schema import Record |
| 9 | + |
| 10 | + |
| 11 | +class UpstashSearchComponent(UpstashVectorStoreComponent, LCVectorStoreComponent): |
| 12 | + """ |
| 13 | + A custom component for implementing a Vector Store using Upstash. |
| 14 | + """ |
| 15 | + |
| 16 | + display_name: str = "Upstash Search" |
| 17 | + description: str = "Search an Upstash Vector Store for similar documents." |
| 18 | + |
| 19 | + def build_config(self): |
| 20 | + """ |
| 21 | + Builds the configuration for the component. |
| 22 | +
|
| 23 | + Returns: |
| 24 | + - dict: A dictionary containing the configuration options for the component. |
| 25 | + """ |
| 26 | + return { |
| 27 | + "search_type": { |
| 28 | + "display_name": "Search Type", |
| 29 | + "options": ["Similarity", "MMR"], |
| 30 | + }, |
| 31 | + "input_value": {"display_name": "Input"}, |
| 32 | + "inputs": {"display_name": "Input", "input_types": ["Document", "Record"]}, |
| 33 | + "embedding": { |
| 34 | + "display_name": "Embedding", |
| 35 | + "input_types": ["Embeddings"], |
| 36 | + "info": "To use Upstash's embeddings, don't provide an embedding.", |
| 37 | + }, |
| 38 | + "index_url": { |
| 39 | + "display_name": "Index URL", |
| 40 | + "info": "The URL of the Upstash index.", |
| 41 | + }, |
| 42 | + "index_token": { |
| 43 | + "display_name": "Index Token", |
| 44 | + "info": "The token for the Upstash index.", |
| 45 | + }, |
| 46 | + "number_of_results": { |
| 47 | + "display_name": "Number of Results", |
| 48 | + "info": "Number of results to return.", |
| 49 | + "advanced": True, |
| 50 | + }, |
| 51 | + "text_key": { |
| 52 | + "display_name": "Text Key", |
| 53 | + "info": "The key in the record to use as text.", |
| 54 | + "advanced": True, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + def build( # type: ignore[override] |
| 59 | + self, |
| 60 | + input_value: Text, |
| 61 | + search_type: str, |
| 62 | + text_key: str = "text", |
| 63 | + index_url: Optional[str] = None, |
| 64 | + index_token: Optional[str] = None, |
| 65 | + embedding: Optional[Embeddings] = None, |
| 66 | + number_of_results: int = 4, |
| 67 | + ) -> List[Record]: |
| 68 | + vector_store = super().build( |
| 69 | + embedding=embedding, |
| 70 | + text_key=text_key, |
| 71 | + index_url=index_url, |
| 72 | + index_token=index_token, |
| 73 | + ) |
| 74 | + if not vector_store: |
| 75 | + raise ValueError("Failed to load the Upstash Vector Store.") |
| 76 | + |
| 77 | + return self.search_with_vector_store( |
| 78 | + input_value=input_value, search_type=search_type, vector_store=vector_store, k=number_of_results |
| 79 | + ) |
0 commit comments