SnowflakeCortexAnalyst(
self,
**kwargs = {},
)Snowflake Cortex Analyst integration for Text2SQL.
Cortex Analyst is Snowflake's AI-powered tool that converts natural language questions into SQL queries and executes them against your Snowflake data.
Setup:
Install langchain-snowflake and configure Snowflake connection.
.. code-block:: bash
pip install -U langchain-snowflake
Key init args:
session: Optional[Session] Active Snowflake session account: str Snowflake account identifier user: str Snowflake username database: str Database to query schema: str Schema to query warehouse: Optional[str] Warehouse to use for queries use_rest_api: bool Whether to use REST API (default: True) or SQL function fallback semantic_model_file: Optional[str] Path to semantic model file on Snowflake stage
Instantiate:
.. code-block:: python
from . import SnowflakeCortexAnalyst
analyst = SnowflakeCortexAnalyst( session=session, semantic_model_file="@my_stage/semantic_model.yaml" )
Use within an agent:
.. code-block:: python
from langchain.agents import AgentExecutor, create_tool_calling_agent from langchain_core.prompts import ChatPromptTemplate from .. import ChatSnowflake
llm = ChatSnowflake(model="claude-3-5-sonnet", session=session) tools = [analyst]
prompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful data analyst assistant."), ("human", "{input}"), ("placeholder", "{agent_scratchpad}"), ])
agent = create_tool_calling_agent(llm, tools, prompt) agent_executor = AgentExecutor(agent=agent, tools=tools)
response = agent_executor.invoke({ "input": "What were our top 5 products by revenue last quarter?" })