Tuesday, April 30, 2024

How to Implement Tool Use with Command R Plus Model Locally

 This video is a simple hands-on tutorial to show how to implement Tool Use with your own data locally by using Cohere Comand R+ model.





Code:




# pip install cohere
# export COHERE_API_KEY="<>"

import cohere
import os
import json

# Mock database containing daily weather reports
weather_database = {
    'Sydney': {
        '2023-09-28': {
            'weather': 'Sunny',
            'temperature': 22,
        },
        '2023-09-29': {
            'weather': 'Rainy',
            'temperature': 18,
        },
        '2023-09-30': {
            'weather': 'Cloudy',
            'temperature': 20,
        }
    },
    'Tokyo': {
        '2023-09-28': {
            'weather': 'Sunny',
            'temperature': 25,
        },
        '2023-09-29': {
            'weather': 'Cloudy',
            'temperature': 22,
        },
        '2023-09-30': {
            'weather': 'Rainy',
            'temperature': 20,
        }
    }
}

# Mock city catalog
city_catalog = {
    'Sydney': {
        'country': 'Australia',
        'population': 5000000,
    },
    'Tokyo': {
        'country': 'Japan',
        'population': 10000000,
    }
}

def query_daily_weather_report(city: str, date: str) -> dict:
    """
    Function to retrieve the weather report for the given city and date
    """
    report = weather_database.get(city, {}).get(date, {})
    if report:
        return {
            'city': city,
            'date': date,
            'summary': f"Weather: {report['weather']}, Temperature: {report['temperature']}°C"
        }
    else:
        return {
            'city': city,
            'date': date,
            'summary': 'No weather data available for this city and date.'
        }

def query_city_catalog(city: str) -> dict:
    """
    Function to retrieve information about the given city
    """
    info = city_catalog.get(city, {})
    return {
        'city': city,
        'info': info
    }

functions_map = {
    "query_daily_weather_report": query_daily_weather_report,
    "query_city_catalog": query_city_catalog
}

tools = [
    {
        "name": "query_daily_weather_report",
        "description": "Connects to a database to retrieve weather information for a given city and date.",
        "parameter_definitions": {
            "city": {
                "description": "Retrieves weather data for this city.",
                "type": "str",
                "required": True
            },
            "date": {
                "description": "Retrieves weather data for this date, formatted as YYYY-MM-DD.",
                "type": "str",
                "required": True
            }
        }
    },
    {
        "name": "query_city_catalog",
        "description": "Connects to a city catalog with information about cities, countries, and populations.",
        "parameter_definitions": {
            "city": {
                "description": "Retrieves city information data for this city.",
                "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 weather summary for Sydney on 29th September 2023, and also give me some details about the city of Tokyo, such as its country and population?"

api_key = os.getenv('COHERE_API_KEY')
co = cohere.Client(api_key)

response = co.chat(
    message=message,
    tools=tools,
    preamble=preamble,
    model="command-r"
)

print("The model recommends doing the following tool calls:")
print("\n".join(str(tool_call) for tool_call in response.tool_calls))

tool_results = []
# Iterate over the tool calls generated by the model
for tool_call in response.tool_calls:
    # here is where you would call the tool recommended by the model, using the parameters recommended by the model
    print(f"= running tool {tool_call.name}, with parameters: {tool_call.parameters}")
    output = functions_map[tool_call.name](**tool_call.parameters)
    # store the output in a list
    outputs = [output]
    print(f"== tool results: {outputs}")
    # create a new dictionary with the relevant information
    tool_result = {
        "call": {"name": tool_call.name},
        "outputs": outputs
    }
    # add the new dictionary to the list
    tool_results.append(tool_result)

print("Tool results that will be fed back to the model in step 4:")
print(json.dumps(tool_results, indent=4))

response = co.chat(
    message=message,
    tools=tools,
    tool_results=tool_results,
    preamble=preamble,
    model="command-r",
    temperature=0.3
)

print("Final answer:")
print(response.text)

No comments: