SnowflakeCortexSearchRetriever(
self,
**kwargs: Any = {},
)Snowflake Cortex Search retriever using REST API exclusively.
This retriever integrates with Snowflake's Cortex Search service exclusively through the REST API. Cortex Search is a managed service that provides enterprise-grade semantic search capabilities.
Note: This retriever uses Cortex Search, which is different from Search Preview. Cortex Search only supports REST API access, not SQL functions.
Setup:
Install langchain-snowflake and configure Snowflake connection.
.. code-block:: bash
pip install -U langchain-snowflake
Key init args:
service_name: str Fully qualified name of the Cortex Search service session: Optional[Session] Active Snowflake session k: int Number of documents to retrieve (default: 4) search_columns: Optional[List[str]] Columns to return in search results filter_dict: Optional[Dict[str, Any]] Filter criteria for search results content_field: str Metadata field containing the actual content (default: "TRANSCRIPT_TEXT") join_separator: str String to join multiple documents (default: "\n\n") fallback_to_page_content: bool Fall back to page_content when metadata field is empty (default: True)
Instantiate:
.. code-block:: python
from . import SnowflakeCortexSearchRetriever
retriever = SnowflakeCortexSearchRetriever( service_name="mydb.myschema.my_search_service", session=session, k=5 )
retriever = SnowflakeCortexSearchRetriever( service_name="mydb.myschema.my_search_service", account="your-account", user="your-user", password="your-password", warehouse="your-warehouse", k=3 )
retriever_custom = SnowflakeCortexSearchRetriever( service_name="mydb.myschema.my_search_service", session=session, k=5, content_field="CHUNK", # Extract from metadata["CHUNK"] instead of "TRANSCRIPT_TEXT" join_separator="\n---\n", # Custom separator fallback_to_page_content=True )
Usage:
.. code-block:: python
query = "What is machine learning?" docs = retriever.invoke(query) for doc in docs: print(doc.page_content)
Use within a chain:
.. code-block:: python
from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate from langchain_core.runnables import RunnablePassthrough from . import ChatSnowflake
prompt = ChatPromptTemplate.from_template( """Answer the question based only on the context provided.
Context: {context}
Question: {question}""" )
llm = ChatSnowflake(model="llama3.1-70b", session=session)
chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser() )
response = chain.invoke("What is the capital of France?")