Notebooks
C
Cohere
Vanilla Tool Use

Vanilla Tool Use

agentsnotebookscohere-developer-experience

Tool Use

Tool use allows customers to connect their large language models to external tools like search engines, APIs, functions, databases, etc.

This allows the customer's model to unlock a richer set of behaviors by leveraging data stored in tools, taking actions through APIs, interacting with a vector database, querying a search engine, etc.

This is particularly valuable for enterprise customers, since a lot of enterprise data lives in external sources.

Tool Use consists of 4 steps:

  • Step 1: the user configures the request to the model
  • Step 2: the model smartly decides which tool(s) to use and how
  • Step 3: the tool calls are executed to mock database
  • Step 4: the model generates a final answer with precise citations based on the tool results
[1]
Note: you may need to restart the kernel to use updated packages.
[ ]

Step 0: Create a mock database

First, we'll define the mock data that our tools will query. This data represents sales reports and a product catalog.

[3]

Now, we'll define the tools that simulate querying this database.
You could for example use the API of an enterprise sales platform.

[4]

Step 1 - the user configures the request to the model

The developer provides a few things to the model:

  • A preamble containing instructions about the task and the desired style for the output.
  • The user request.
  • A list of tools to the model.
  • (Optionally) a chat history for the model to work with.

You can specify one or many tools to the model. Every tool needs to be described with a JSON schema, indicating the tool name, description, and parameters (code snippets below).

In our example, we provide two tools to the model: daily_sales_report and product_catalog.

[5]

Now let's define the user request.

In our example we'll use: "Can you provide a sales summary for 29th September 2023, and also give me the details of all products in the 'Electronics' category that were sold that day, including their prices and stock levels?"

Only a langage model with Tool Use can answer this request: it requires looking up information in the right external tools (step 2), and then providing a final answer based on the tool results (step 4).

[6]

Step 2 โ€“ the model smartly decides which tool(s) to use and how

The model intelligently selects the right tool(s) to call -- and the right parameters for each tool call -- based on the content of the user message.

[7]
The model recommends doing the following tool calls:
name='query_daily_sales_report' parameters={'day': '2023-09-29'}
name='query_product_catalog' parameters={'category': 'Electronics'}

Step 3 โ€“ the tool calls are executed

You can then execute the appropriate calls, using the tool calls and tool parameters generated by the model.
These tool calls return tool results that will be fed to the model in Step 4.

[8]
= running tool query_daily_sales_report, with parameters: {'day': '2023-09-29'}
== tool results: [{'date': '2023-09-29', 'summary': 'Total Sales Amount: 10000, Total Units Sold: 250'}]
= running tool query_product_catalog, with parameters: {'category': 'Electronics'}
== tool results: [{'category': 'Electronics', 'products': [{'product_id': 'E1001', 'name': 'Smartphone', 'price': 500, 'stock_level': 20}, {'product_id': 'E1002', 'name': 'Laptop', 'price': 1000, 'stock_level': 15}, {'product_id': 'E1003', 'name': 'Tablet', 'price': 300, 'stock_level': 25}]}]
Tool results that will be fed back to the model in step 4:
[
    {
        "call": {
            "name": "query_daily_sales_report",
            "parameters": {
                "day": "2023-09-29"
            }
        },
        "outputs": [
            {
                "date": "2023-09-29",
                "summary": "Total Sales Amount: 10000, Total Units Sold: 250"
            }
        ]
    },
    {
        "call": {
            "name": "query_product_catalog",
            "parameters": {
                "category": "Electronics"
            }
        },
        "outputs": [
            {
                "category": "Electronics",
                "products": [
                    {
                        "product_id": "E1001",
                        "name": "Smartphone",
                        "price": 500,
                        "stock_level": 20
                    },
                    {
                        "product_id": "E1002",
                        "name": "Laptop",
                        "price": 1000,
                        "stock_level": 15
                    },
                    {
                        "product_id": "E1003",
                        "name": "Tablet",
                        "price": 300,
                        "stock_level": 25
                    }
                ]
            }
        ]
    }
]

Step 4 - the model generates a final answer based on the tool results

Finally, the developer calls the Cohere model, providing the tools results, in order to generate the model's final answer.

Bonus: At Cohere, all Tool Use calls come with... precise citations! ๐ŸŽ‰ The model cites which tool results were used to generate the final answer.
These citations make it easy to check where the modelโ€™s generated response claims are coming from.
They help users gain visibility into the model reasoning, and sanity check the final model generation.
These citations are optional โ€” you can decide to ignore them.

[9]
[10]
Final answer:
On the 29th September 2023, the total sales amount was 10,000 and the total units sold was 250.

Here are the details of the products in the 'Electronics' category:

| Product Name | Price | Product ID | Stock Level |
|---|---|---|---|
| Smartphone | 500 | E1001 | 20 |
| Laptop | 1000 | E1002 | 15 |
| Tablet | 300 | E1003 | 25 |

Bonus: Citations come for free with Cohere! ๐ŸŽ‰

At Cohere, model generations come with... precise citations! ๐ŸŽ‰ The model cites which groups of words, in the tool results, were used to generate the final answer.
These citations make it easy to check where the modelโ€™s generated response claims are coming from.
They help users gain visibility into the model reasoning, and sanity check the final model generation.
These citations are optional โ€” you can decide to ignore them.

[12]
Citations that support the final answer:
start=32 end=61 text='total sales amount was 10,000' document_ids=['query_daily_sales_report:0:0'] type='TEXT_CONTENT'
start=70 end=95 text='total units sold was 250.' document_ids=['query_daily_sales_report:0:0'] type='TEXT_CONTENT'
start=168 end=180 text='Product Name' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=183 end=188 text='Price' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=191 end=201 text='Product ID' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=204 end=215 text='Stock Level' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=238 end=248 text='Smartphone' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=251 end=254 text='500' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=257 end=262 text='E1001' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=265 end=267 text='20' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=272 end=278 text='Laptop' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=281 end=285 text='1000' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=288 end=293 text='E1002' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=296 end=298 text='15' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=303 end=309 text='Tablet' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=312 end=315 text='300' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=318 end=323 text='E1003' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
start=326 end=328 text='25' document_ids=['query_product_catalog:1:0'] type='TEXT_CONTENT'
[14]
On the 29th September 2023, the **total sales amount was 10,000**[1] and the **total units sold was 250.**[1]

Here are the details of the products in the 'Electronics' category:

| **Product Name**[2] | **Price**[2] | **Product ID**[2] | **Stock Level**[2] |
|---|---|---|---|
| **Smartphone**[2] | **500**[2] | **E1001**[2] | **20**[2] |
| **Laptop**[2] | **1000**[2] | **E1002**[2] | **15**[2] |
| **Tablet**[2] | **300**[2] | **E1003**[2] | **25**[2] |

[1] source: [{'date': '2023-09-29', 'summary': 'Total Sales Amount: 10000, Total Units Sold: 250'}] 
    based on tool call: {'name': 'query_daily_sales_report', 'parameters': {'day': '2023-09-29'}}
[2] source: [{'category': 'Electronics', 'products': [{'product_id': 'E1001', 'name': 'Smartphone', 'price': 500, 'stock_level': 20}, {'product_id': 'E1002', 'name': 'Laptop', 'price': 1000, 'stock_level': 15}, {'product_id': 'E1003', 'name': 'Tablet', 'price': 300, 'stock_level': 25}]}] 
    based on tool call: {'name': 'query_product_catalog', 'parameters': {'category': 'Electronics'}}

Yiha. You've used Cohere for Tool Use. Tool use opens up a wide range of new use cases. Here are a few examples:

  • Function calling: It's now possible to ask the model to output a JSON object with specific function parameters. For instance, this allows your chatbot to interact with your CRM to change the status of a deal, or to engage with a Python interpreter to conduct data science analyses.

  • Query transformation: You can transform a user message into a search query for a vector database or any search engine. For instance, this enables your work assistant to automatically retrieve the appropriate data from your company's documentation by creating the right query for your vector database.

  • Advanced searches: You can transform a user message into one-or-many queries, to do multiple subtasks based on the content of the message. For instance, this allows your chatbot to search across different databases and platforms to retrieve relevant information or to conduct comparative analysis.

[ ]