Configure and interact with Agents¶
You can build an agent in Snowsight or using the REST API, and then integrate the agent into your application to perform tasks or respond to queries. You must first create an agent object that contains information such as the metadata, tools, and orchestration instructions that the agent can use to perform a task or answer questions. You can then reference the agent object in your application to integrate the agent's functionality. You can configure a thread to maintain the context in memory, so that the client does not have to send the context at every turn of the conversation.
备注
Snowflake REST APIs 支持通过编程访问令牌 (PATs) 进行身份验证、使用 JSON Web 令牌 (JWTs) 进行密钥对身份验证,以及 OAuth。有关详细信息,请参阅 使用 Snowflake 对 Snowflake REST APIs 进行身份验证。
创建代理¶
Create an agent object by specifying the database and schema where the agent should be located, along with a name and description for the agent. In addition, specify the display name, avatar, and the color. These attributes are used by the client application to display the agent. The display name is also used as the handle to reference the agent in conversations.
The following examples show how to create an agent object from Snowsight or using the REST API:
Sign in to Snowsight.
In the navigation menu, select AI & ML » Agents.
选择 Create agent。
对于 Agent object name,请指定代理的名称,该名称将在 UI 中向用户显示。
对于 Display name,指定代理的名称,该名称将在代理列表中向管理员显示。
选择 Create agent。
用常识请求提示代理。
Create an agent object by specifying the database and schema where the agent will be created, as well as the parameters needed for the agent. You can also specify tool fields when creating the agent object.
curl -X POST "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/databases/<database-name>/schemas/<schema-name>/agents" \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header "Authorization: Bearer $PAT" \ --data '{ "name": "TransportationAgent", "comment": "This agent handles queries related to transportation methods and costs.", "models": { "orchestration": "llama3.3-70B" } }'
Add tools¶
After you've created the agent, you need to add tools and provide instructions on how to orchestrate across the tools. Agents support the following tool types:
Cortex Analyst: You specify the semantic views so that Cortex Analyst can use these to retrieve structured data. The Agents can route across multiple semantic views to provide the response.
Cortex Search: You provide the Cortex Search indices as tools, including related information like search filters, column names, and metadata. The Cortex Agent uses the Cortex Search indices to retrieve unstructured data.
Custom tools: You can implement code for a specific business logic as a stored procedure or user defined function (UDF). Alternatively, you can use the custom tools to retrieve data from your backend systems using APIs.
You also specify the resources used by each tool. For example, on Cortex Analyst you specify the warehouse along with the timeout for SQL query execution. Similarly for Cortex Search, you specify the filters and column names used in the search query, along with the max results in the search response. For custom tools, you will provide the warehouse details.
要修改现有代理的配置,请完成以下步骤:
选择 Edit。
对于 Description,请描述代理以及用户与之交互的方式。
要添加用户可以向代理询问的示例问题,请输入示例问题并选择 Add a question。
Select Tools. Add one or more of the following tools.
To add a semantic view in Cortex Analyst to the agent: This section assumes that you already have a semantic view created. For information about semantic views and how to create one, see 语义视图概述.
找到 Cortex Analyst 并选择相应的 + Add 按钮。
对于 Name,请输入语义视图的名称。
选择 Semantic view。
选择代理使用的语义视图。
对于 Warehouse,请选择代理用来运行查询的仓库。
对于 Query timeout (seconds),请指定代理在超时之前等待查询完成的最长时间(以秒为单位)。
对于 Description,请描述语义视图。
选择 Add。
To add a Cortex Search service to the agent: This section assumes that you've already created a Cortex Search service. For information about creating a Cortex Search service, see Cortex Search. You can also use a Cortex Knowledge Extension (CKE) that is shared with you. For a tutorial that uses a CKE, see 故障排除.
找到 Cortex Search Services 并选择相应的 + Add 按钮。
对于 Name,请输入 Cortex Search Service 的名称。
对于 Description,请描述 Cortex Search Service。
对于 Search service,请选择代理使用的 Cortex Search Service。
选择 Add。
To add a custom tool to the agent: By adding custom tools, you can extend the functionality of your agents. With custom tools, the agent can call stored procedures and functions that you have defined to perform actions or do computations. This section assumes that you've already created a custom tool. For information about procedures and functions, see 使用函数和过程扩展 Snowflake.
找到 Custom tools 并选择相应的 + Add 按钮。
对于 Name,请输入自定义工具的名称。
对于 Resource type,请选择自定义工具是函数还是过程。有关是否使用函数或过程的信息,请参阅 选择是编写存储过程还是用户定义函数。
对于 Custom tool identifier,请选择要添加为自定义工具的现有函数或过程。
函数或过程的相关参数会自动显示。您可以通过添加名称、类型、描述以及选择是否需要参数,来手动为自定义工具添加参数。您也可以修改自动填充的参数。
备注
Snowflake Cortex does not support stored procedures and custom tools with a parameter of type
object.对于 Warehouse,请选择代理用来运行自定义工具的仓库。必须手动选择仓库。
对于 Description,请描述自定义工具及其使用方法。
选择 Add。
After creating the custom tool, make sure users are granted USAGE privileges to the function or procedure that you added as a custom tool. When using stored procedures, agents maintain whether the procedure runs with owner's or caller's rights. For information about owner's and caller's rights, see 了解调用方权限和所有者权限存储过程.
选择 Save。
To add tools to an agent using the REST API, add the following payloads as part of a request to Update Cortex Agent. You can also specify these fields when creating the agent object.
Add Cortex Analyst tool and tool resources: The following example shows how to add a Cortex Analyst tool and tool resources to an existing agent object.
Add a Cortex Analyst tool
curl -X PUT "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/databases/<database-name>/schemas/<schema-name>/agents/<agent-name>" \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header "Authorization: Bearer $PAT" \ --data '{ "tools": [ { "tool_spec": { "description": "Analyst to analyze price", "type": "cortex_analyst_text_to_sql", "name": "Analyst1" } } ] }'Add a Cortex Analyst tool resource
curl -X PUT "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/databases/<database-name>/schemas/<schema-name>/agents/<agent-name>" \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header "Authorization: Bearer $PAT" \ --data '{ "tool_resources": { "Analyst1": { "semantic_model_file": "stage1", "semantic_view": "The name of the Snowflake native semantic model object", "execution_environment": {"type":"warehouse", "warehouse":"my_wh"} } } }'Add Cortex Search tool and tool resources: The following example shows how to add a Cortex Search tool and tool resources to an existing agent object.
Add a Cortex Search tool
curl -X PUT "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/databases/<database-name>/schemas/<schema-name>/agents/<agent-name>" \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header "Authorization: Bearer $PAT" \ --data '{ "tool_spec": { "type": "cortex_search", "name": "Search1" } }'Add a Cortex Search tool resource
curl -X PUT "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/databases/<database-name>/schemas/<schema-name>/agents/<agent-name>" \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header "Authorization: Bearer $PAT" \ --data '{ "tool_resources": { "Search1": { "search_service": "db.schema.service_name", "filter": {"@eq": {"region": "North America"} }, "max_results": 5 } } }'Add custom tool and tool resources: The following example shows how to add a custom tool and tool resources to an existing agent object.
Add a custom tool
curl -X PUT "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/databases/<database-name>/schemas/<schema-name>/agents/<agent-name>" \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header "Authorization: Bearer $PAT" \ --data '{ "tools": [ { "tool_spec": { "description": "Custom tool", "type": "generic", "name": "custom1" } } ] }'Add a custom tool resource
curl -X PUT "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/databases/<database-name>/schemas/<schema-name>/agents/<agent-name>" \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header "Authorization: Bearer $PAT" \ --data '{ "tool_resources": { "Custom1": { "user-defined-function-argument": "argument1" } } }'
Specify orchestration¶
Cortex Agents orchestrate the task by breaking it into a sequence of sub-tasks and identifying the right tool for each sub-task. You specify the LLM that the Agent should use to conduct this orchestration. You can also influence the orchestration by providing instructions. For example, consider an agent built to respond to retail product questions. You can use the orchestration instruction "Use the search tool for all requests related to refunds" to ensure the Agent only provides refund policy details (using Cortex Search) and does not actually calculate the refund amounts (using Cortex Analyst). You can also specify instructions to align the response to a brand or a tone, such as "Always provide provide a concise response; maintain a friendly tone".
选择 Orchestration。
For the Orchestration model, select the model that the agent uses to handle orchestration.
对于 Planning instructions,请根据用户提供的输入,提供影响代理选择工具的说明。其中可能包括有关每种工具何时使用的具体说明,甚至包括在响应开始或结束时始终使用工具的说明。
对于 Response instruction,请提供模型用于生成响应的说明。例如,指定您是希望代理优先创建图表,还是要对用户保持某种语气。
选择 Save。
To update an agent using the REST API, add the following payloads as part of a request to Update Cortex Agent. You can also specify these fields when creating the agent object. The following procedure shows how to update the agent with planning and response instructions, and specify the LLM model used for orchestration.
Update the LLM model
curl -X PUT "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/databases/<database-name>/schemas/<schema-name>/agents/<agent-name>" \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header "Authorization: Bearer $PAT" \ --data '{ "models": { "orchestration": "llama3.3-70B" }'
Specify the planning and response instructions
curl -X PUT "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/databases/<database-name>/schemas/<schema-name>/agents/<agent-name>" \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header "Authorization: Bearer $PAT" \ --data '{ "instructions": { "response": "Always provide a concise response and maintain a friendly tone.", "orchestration": "<orchestration instructions>", "system": "You are a helpful data agent." } }'
Set up access to the agent¶
Set up access policies from Snowsight UI or using SQL so that users can access the Agent. Specify the role to provide access to the Agent.
选择 Access。
要向代理授予角色访问权限,请选择 Add role,然后从下拉菜单中选择相应角色。
选择 Save。
GRANT USAGE ON AGENT my_agent TO ROLE test_rl;
Review the agent¶
After you have built the Agent, you can review the Agent to verify all parameters.
备注
When reviewing agents from Snowsight, you can only view agents in the Agent Admin UI. You cannot view agents in the database object explorer.
In the navigation menu, select AI & ML » Agents.
From the list of agents, select the agent that you want to view the details for. This opens a new page that gives an overview of the agent details.
To review all agent details, select Next.
You can list and describe agents using the REST APIs.
List all agents.
curl -X GET "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/databases/{database}/schemas/{schema}/agents:" \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header "Authorization: Bearer $PAT" \
Describe the desired agent.
curl -X GET "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/databases/{database}/schemas/{schema}/agents/{name}:" \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header "Authorization: Bearer $PAT" \
Test the agent¶
After you've created the agent, you can test it to see how it responds to user queries. You can also test the agent using 带有代理对象的代理运行请求.
To test the agent, follow these steps:
Sign in to Snowsight.
In the navigation menu, select AI & ML » Agents.
从代理列表中选择代理。
On the agent details page, enter a query in the agent playground.
Verify that the agent responds to the query as expected. If the agent does not respond as expected, modify the agent's configuration by following the steps in Add tools.
Interact with the agent¶
After creating the agent object, you can integrate the agent directly into your application using the REST API. To maintain context during the interaction, use a thread. The agent object and thread combined simplify the client application code.
Create a thread¶
Create a thread to maintain the context during a conversation. When the thread is created successfully, the system returns a Thread ID.
curl -X POST "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/cortex/threads" \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header "Authorization: Bearer $PAT" \
--data '{
"origin_application": <application_name>,
}'
Send a request to the agent¶
To interact with the Agent, you must pass the agent object, thread ID, and a unique parent_message_id as part of your REST API request. The initial parent_message_id should be 0.
curl -X POST "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/databases/{database}/schemas/{schema}/agents/{name}:run" \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header "Authorization: Bearer $PAT" \
--data '{
"thread_id": <thread id for context>,
"parent_message_id": <parent message id>,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What are the projected transportation costs for the next three quarters? "
}
]
}
],
"tool_choice": {
"type": "required",
"name": [
"Analyst1",
"Search1"
]
}
}'
Collect feedback about the agent¶
You can collect feedback from users about the responses given by the agent. This feedback can help you refine the agent as you iterate on your use case. Users can provide an objective rating (postive/negative), as well as more subjective detail with a message. Also, users can classify the feedback across one of many categories.
curl -X POST "$SNOWFLAKE_ACCOUNT_BASE_URL/api/v2/databases/<database-name>/schemas/<schema-name>/agents/<agent-name>:feedback:" \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header "Authorization: Bearer $PAT" \
--data '{
"request_id": "<request-id>",
"positive": true
"feedback_message": "This answer was great",
"categories":[
"category1", "category2", "category3"
],
"thread_id": "<thread-id>"
}'
Interact without an agent object¶
In some cases, you may want to get started with Cortex Agents by using agent:run without an agent object. For example, this may be useful when you want to quickly try out a use case. For more information about the REST API, see 代理在没有代理对象的情况下运行.
备注
When interacting with an agent without creating an agent object, you must manually maintain the context for the agent with every request.