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.

Note

Snowflake REST APIs support authentication via programmatic access tokens (PATs), key pair authentication using JSON Web Tokens (JWTs), and OAuth. For details, see Authenticating Snowflake REST APIs with Snowflake.

Create an agent

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:

  1. Sign in to Snowsight.

  2. From the left-hand navigation, select AI & ML.

  3. Select Agents.

  4. Select Create agent.

  5. For Agent object name, specify a name for the agent that is displayed to users in the UI.

  6. For Display name, specify a name for the agent that is displayed to admins in the agent list.

  7. Select Create agent.

  8. Prompt the agent with general knowledge requests.

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.

To modify the configuration for an existing agent, complete the following steps:

  1. Select Edit.

  2. For Description, describe the agent and how users can interact with it.

  3. To add sample questions that users can ask the agent, enter a sample question and select Add a question.

  4. 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 Overview of semantic views.

      1. Find Cortex Analyst and select the respective + Add button.

      2. For Name, enter a name for the semantic view.

      3. Select Semantic view.

      4. Select the semantic view that the agent uses.

      5. For Warehouse, select the warehouse that the agent uses to run queries.

      6. For Query timeout (seconds), specify the maximum time in seconds that the agent waits for a query to complete before timing out.

      7. For Description, describe the semantic view.

      8. Select 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 Troubleshooting.

      1. Find Cortex Search Services and select the respective + Add button.

      2. For Name, enter a name for the Cortex Search service.

      3. For Description, describe the Cortex Search service.

      4. For Search service, select the Cortex Search service that the agent uses.

      5. Select 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 Extending Snowflake with Functions and Procedures.

      1. Find Custom tools and select the respective + Add button.

      2. For Name, enter a name for the custom tool.

      3. For Resource type, select whether the custom tool is a function or a procedure. For information about whether to use a function or procedure, see Choosing whether to write a stored procedure or a user-defined function.

      4. For Custom tool identifier, select the existing function or procedure that you want to add as a custom tool.

      5. The related parameters for the function or procedure automatically appear. You can manually add parameters for the custom tool by adding a name, type, description, and selecting whether the parameter is required. You can also modify parameters that automatically populate.

      6. For Warehouse, select the warehouse that the agent uses to run the custom tool. You must manually select a warehouse.

      7. For Description, describe the custom tool and how to use it.

      8. Select Add.

      9. 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 Understanding caller’s rights and owner’s rights stored procedures.

  5. Select Save.

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".

  1. Select Orchestration.

  2. For the Orchestration model, select the model that the agent uses to handle orchestration.

  3. For Planning instructions, provide instructions that influence tool selection by the agent based on user-provided input. These can include specific instructions about when to use each tool, or even to always use a tool at the beginning or end of a response.

  4. For Response instruction, provide instructions that the model uses for response generation. For example, specify if you want the agent to prioritize chart creation, or to keep a certain tone with users.

  5. Select Save.

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.

  1. Select Access.

  2. To give a role access to the agent, select Add role, then select the role from the dropdown menu.

  3. Select Save.

Review the agent

After you have built the Agent, you can review the Agent to verify all parameters.

  1. Select Agents.

  2. 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.

  3. To review all agent details, select Next.

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 Run agent object.

To test the agent, follow these steps:

  1. Sign in to Snowsight.

  2. From the left-hand navigation, select AI & ML.

  3. Select Agents.

  4. Select the agent from the list of agents.

  5. On the agent details page, enter a query in the agent playground.

  6. 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>,
}'
Copy

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"
       ]
     }
 }'
Copy

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>"
}'
Copy