Cortex Analyst¶
cortex_agents.analyst.CortexAnalyst ¶
CortexAnalyst(account_url: str | None = None, pat: str | None = None, enable_logging: bool = True, token_type: str | None = None)
Bases: BaseAgent
Client for Snowflake Cortex Analyst.
Provides a simple interface for generating SQL from natural language questions using semantic models or semantic views. Supports multi-turn conversations, streaming responses, and user feedback submission.
Examples:
from cortex_agents import CortexAnalyst
# Use with context manager for automatic cleanup
with CortexAnalyst() as analyst:
response = analyst.message(
"Which company had the most revenue?",
semantic_model_file="@my_stage/model.yaml"
)
Initialize the Cortex Analyst client.
Source code in cortex_agents/analyst.py
close ¶
message ¶
message(question: str, semantic_model_file: str | None = None, semantic_model: str | None = None, semantic_view: str | None = None, semantic_models: list[dict[str, Any]] | None = None, messages: list[dict[str, Any]] | None = None, stream: bool = True) -> AnalystResponse
Send a message to Cortex Analyst to generate SQL from natural language.
You must specify ONE of: semantic_model_file, semantic_model, semantic_view, or semantic_models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The natural language question (str) |
required |
semantic_model_file
|
str | None
|
Path to semantic model YAML file on stage (e.g., "@my_db.my_schema.my_stage/model.yaml") |
None
|
semantic_model
|
str | None
|
Full semantic model YAML as string |
None
|
semantic_view
|
str | None
|
Fully qualified semantic view name (e.g., "MY_DB.MY_SCHEMA.MY_VIEW") |
None
|
semantic_models
|
list[dict[str, Any]] | None
|
List of semantic model/view dicts for multi-model selection (e.g., [{"semantic_view": "DB.SCH.VIEW1"}, {"semantic_model_file": "@stage/model.yaml"}]) |
None
|
messages
|
list[dict[str, Any]] | None
|
Full messages array for multi-turn conversations (if not provided, will be built from question) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
AnalystResponse |
AnalystResponse
|
Response object with SQL, text, and streaming support |
Examples:
# With semantic model file
response = analyst.message(
"Which company had most revenue?",
semantic_model_file="@my_stage/model.yaml"
)
# Multi-turn conversation
response1 = analyst.message(
"What's total revenue?",
semantic_model_file="@stage/model.yaml"
)
response2 = analyst.message(
"How does that compare to last year?",
semantic_model_file="@stage/model.yaml",
messages=response1.conversation_messages
)
Source code in cortex_agents/analyst.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
submit_feedback ¶
submit_feedback(request_id: str, positive: bool, feedback_message: str | None = None) -> dict[str, Any]
Submit feedback about an Analyst response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_id
|
str
|
The request ID from response.request_id. |
required |
positive
|
bool
|
Whether the feedback is positive (True) or negative (False). |
required |
feedback_message
|
str | None
|
Optional detailed feedback message. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Confirmation response (typically empty dict on success). |
Examples:
analyst.submit_feedback(
request_id=response.request_id,
positive=True,
feedback_message="Perfect SQL generation!"
)
Source code in cortex_agents/analyst.py
suggest_questions ¶
suggest_questions(semantic_model_file: str | None = None, semantic_view: str | None = None, max_questions: int = 3) -> list[str]
Get suggested questions based on semantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
semantic_model_file
|
str | None
|
Path to semantic model file |
None
|
semantic_view
|
str | None
|
Name of semantic view |
None
|
max_questions
|
int
|
Maximum number of questions to return |
3
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Suggested questions |
Source code in cortex_agents/analyst.py
validate_semantic_model ¶
validate_semantic_model(semantic_model_file: str | None = None, semantic_view: str | None = None) -> dict[str, Any]
Validate a semantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
semantic_model_file
|
str | None
|
Path to semantic model file |
None
|
semantic_view
|
str | None
|
Name of semantic view |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
dict[str, Any]
|
Validation results |