cohere-ai/cohere-typescript

No paramters retreived

Closed this issue · 8 comments

Hello,

I tried to call chat function with tools parameters. it's works but I don't receive parameters. To compare I tested the same code but in python and it's works. For this exemple, I'm based on cohere exemple without database connexion.

Javascipt (native, not with typescript)

const { CohereClient } = require("cohere-ai");

const cohere = new CohereClient({
    token: "KEY",
});

function query_daily_sales_report (day) {
    console.log("query_daily_sales_report called : ", day);
}

function query_product_catalog (category) {
    console.log("query_product_catalog called ", category);
}

const mapping = {
    query_daily_sales_report : query_daily_sales_report,
    query_product_catalog : query_product_catalog
}

const tools = [
    {
        name : "query_daily_sales_report",
        description : "Connects to a database to retrieve overall sales volumes and sales information for a given day.",
        parameter_definitions : {
            date : {
                description : "Retrieves sales data for this day, formatted as YYYY-MM-DD.",
                type: "srt",
                required : true
            }
        }
    },
    {
        name : "query_product_catalog",
        description : "Connects to a a product catalog with information about all the products being sold, including categories, prices, and stock levels.",
        parameter_definitions : {
            date : {
                description : "Retrieves product information data for all products in this category.",
                type: "srt",
                required : true
            }
        }
    }
]

const preamble = `
## Task & Context
You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging.

## Style Guide
Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.
`

async function callCohere () {
    const response = await cohere.chat({
        model    : "command-r",
        tools    : tools,
        preamble : preamble,
        message  : "Can you provide a sales summary for 29th September 2023, and also give me some details about the products in the 'Electronics' category, for example their prices and stock levels?"
    });

    if (response && Array.isArray(response.toolCalls)) {
        response.toolCalls.forEach(tool => {
            if (tool.name) {
                //console.log(tool.name, tool.parameters)
                //mapping[tool.name](tool.parameters);
            }
        })
    }

   console.log(response);
}

console.log("The model recommends doing the following tool calls:")
callCohere();

Response :

{
  response_id: '103ca753-d5fc-478d-a71a-e24dfb8512c7',
  text: '',
  generationId: '5c5e4039-cf20-4616-86f5-92f2b64e40d8',
  chatHistory: [],
  finishReason: 'COMPLETE',
  meta: {
    api_version: { version: '1' },
    billed_units: { input_tokens: 98, output_tokens: 14 }
  },
  toolCalls: [
    { name: 'query_daily_sales_report', parameters: {} },
    { name: 'query_product_catalog', parameters: {} }
  ]
}

Py code :

import cohere, json, os, sqlite3

co = cohere.Client("KEY")


def query_daily_sales_report(day: str) -> dict:
    """
    Function to retrieve the sales report for the given day from the SQLite database
    """
    print(f"query_daily_sales_report called : {day}")

def query_product_catalog(category: str) -> dict:
    """
    Function to retrieve products for the given category from the SQLite database
    """
    print(f"query_product_catalog called : {category}")

functions_map = {
    "query_daily_sales_report": query_daily_sales_report,
    "query_product_catalog": query_product_catalog
}

tools = [
    {
        "name": "query_daily_sales_report",
        "description": "Connects to a database to retrieve overall sales volumes and sales information for a given day.",
        "parameter_definitions": {
            "day": {
                "description": "Retrieves sales data for this day, formatted as YYYY-MM-DD.",
                "type": "str",
                "required": True
            }
        }
    },
    {
        "name": "query_product_catalog",
        "description": "Connects to a a product catalog with information about all the products being sold, including categories, prices, and stock levels.",
        "parameter_definitions": {
            "category": {
                "description": "Retrieves product information data for all products in this category.",
                "type": "str",
                "required": True
            }
        }
    }
]

preamble = """
## Task & Context
You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging.

## Style Guide
Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.
"""

message = """Can you provide a sales summary for 29th September 2023,
and also give me some details about the products in the 'Electronics' category,
for example their prices and stock levels?"""

# 2. The model smartly decides which tool(s) to use and how
response = co.chat(
    message=message,
    tools=tools,
    preamble=preamble,
    model="command-r"
)
print(response)

Response :

text='' generation_id='a733e782-cc2b-455c-8d59-ed06d2349d92' citations=None documents=None is_search_required=None search_queries=None search_results=None finish_reason='COMPLETE' tool_calls=[ToolCall(name='query_daily_sales_report', parameters={'day': '2023-09-29'}), ToolCall(name='query_product_catalog', parameters={'category': 'Electronics'})] chat_history=[] meta={'api_version': {'version': '1'}, 'billed_units': {'input_tokens': 129, 'output_tokens': 32}} response_id='b114eb77-d18b-4a6b-9fa9-97b5e5836301'

I forgot something ?

please can you try parameter_definitions -> parameterDefinitions ?

Ho yeah, it works better :D

But I don't receive the same response: in case of javascript, by uncomment (console.log(tool.name, tool.parameters)) :

query_daily_sales_report { date: '2023-09-29' }
query_product_catalog { date: '2023-09-29' }

or

query_daily_sales_report { date: '2023-09-29' }
query_product_catalog { date: '2023-09-30' }

With some ids (maybe it's usefull for you)
{"response_id":"f666efaf-af5c-4fb4-8142-81d30ad057a3","text":"","generationId":"b65d49f7-116a-4d5c-9949-fe7cfdddb870","chatHistory":[],"finishReason":"COMPLETE","meta":{"api_version":{"version":"1"},"billed_units":{"input_tokens":129,"output_tokens":40}},"toolCalls":[{"name":"query_daily_sales_report","parameters":{"date":"2023-09-29"}},{"name":"query_product_catalog","parameters":{"date":"2023-09-29"}}]}

glad that improved things, we want to add runtime validation to arguments to fix your issue in future. For now i recommend using typescript because that would have given you feedback on the request shape.

I'm not sure i understand your subsequent issue? is it that the date is different?

oh I see are you asking why you get query_product_catalog { date: '2023-09-30' } not {'category': 'Electronics'}? Looking into this

ok I will try with typescript tomorrow.

For the other issue, I receive { date: '2023-09-29' } as parameter for the name "query_product_catalog" but I expect {'category': 'Electronics'}. I tried several time but always same response with javascript. No issue with Python.

I think the issue is in the tool definition, you need to change the field name to category

    {
        name : "query_product_catalog",
        description : "Connects to a a product catalog with information about all the products being sold, including categories, prices, and stock levels.",
        parameter_definitions : {
            category : { // <-- here 
                description : "Retrieves product information data for all products in this category.",
                type: "str", // <-- also fixed this srt -> str
                required : true
            }
        }
    }

Wow sad copy paste : /

ok I will be very careful with keys. Sorry for my clumsiness, I was eager to do a test.

Thx a lot

no problem! thanks for trying it out and providing such a reproducible code snippet. let us know if you have any more issues :)