Function Calling With Vand.io and OpenAI Models

Tools Photo by Oxana Melis

Augmenting language models with tools dramatically increases their usefulness, accuracy, and capabilities.

OpenAI’s GPT models have had the ability to leverage external tools via Function Calls and with the improved function calling announced with gpt-3.5-turbo-1106 they are more reliable.

What is Vand

Vand is a directory of tools and a simple Python package (vand-python) that makes it easy to find and augment your model with tools via with OpenAI’s models. Choose from the thousands of tools at Vand.io including Web Reader, Weather, Wolfram Alpha for scientific knowledge, and many ChatGPT Plugins. Support for other models is in development.

Installation

  • Install directly from GitHub with: pip install --upgrade https://github.com/vand-io/vand-python/tarball/master
  • Note: you can uninstall with : pip uninstall vand-python

Quick Demo

Make sure you have your OpenAI key set (export OPENAI_API_KEY=‘sk-…’) and execute python simple_tool_usage_example.py to run a simple demo that will:

  1. Start a chat with the model and ask about the weather in Honolulu.
  2. Provide a Weather tool to the model
  3. Receive the function call from the model
  4. Execute the function call by making a http request to a weather service
  5. Providing the weather information back to the model
  6. Getting the model response.

The output should look similar to the sample below.

system: Answer user questions as best you can.  Find and use a tool to increase accuracy or add capabilities if needed.

user: What's the weather like in Honolulu today?

assistant: {'name': 'getWeatherNow', 'arguments': '{\n  "city": "Honolulu",\n  "country": "US"\n}'}

function (getWeatherNow): {
  "base": "stations",
  "clouds": {
    "all": 20
  },
  "cod": 200,
  "coord": {
    "lat": 21.3045,
    "lon": -157.8557
  },
  "dt": 1699041629,
  "id": 5856195,
  "main": {
    "feels_like": 302.93,
    "humidity": 69,
    "pressure": 1017,
    "temp": 300.74,
    "temp_max": 302.1,
    "temp_min": 299.57
  },
  "name": "Honolulu",
  "sys": {
    "country": "US",
    "id": 47742,
    "sunrise": 1699029323,
    "sunset": 1699070070,
    "type": 2
  },
  "timezone": -36000,
  "visibility": 10000,
  "weather": [
    {
      "description": "few clouds",
      "icon": "02d",
      "id": 801,
      "main": "Clouds"
    }
  ],
  "wind": {
    "deg": 50,
    "speed": 2.06
  }
}

assistant: The weather in Honolulu today is partly cloudy with a temperature of approximately 300.74 Kelvin (27.59 degrees Celsius). The humidity is around 69% and there's a light breeze from the east-northeast with a speed of 2.06 m/s.

Using vand-python in your own apps

You can use the simple_tool_usage_example.py file as an example to follow from (vand-python).

  1. Import vand-python
 from vand import VandBasicAPITool
  1. Fetch the tool you want. Note you can find the ID (e.g. “vand-.*”) by searching for tools at Vand.io. Just swap out the Vand ID for the tool you want to use. In this example we’ll be using a weather tool to get the current conditions in Honolulu.
get_weather = VandBasicAPITool.get_toolpack("vand-6657ac86-b112-4776-a5cc-fae3aa80ba56") 
  1. Write the ChatCompletion call
completion = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "What's the weather like in Honolulu?"}],
    functions=get_weather.functions,
    function_call="auto",)

assistant_message = completion.choices[0].message.content

Which should return a response similar to below in this example.

{
    "id": "chatcmpl-7GCF1pk8pmsgtLZNyTC9LNqT4y5k8",
    "object": "chat.completion",
    "created": 1698470877,
    "model": "gpt-3.5-turbo-0613",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": null,
                "function_call": {
                    "name": "getWeatherNow",
                    "arguments": "{\n  \"city\": \"Honolulu\",\n  \"state\": \"HI\",\n  \"country\": \"US\"\n}"
                }
            },
            "finish_reason": "function_call"
        }
    ],
    "usage": {
        "prompt_tokens": 331,
        "completion_tokens": 31,
        "total_tokens": 362
    }
}
  1. Execute the function call from the model in the chat completion response by calling VandBasicAPITool with the function call message.
if assistant_message.get("function_call"):
    toolMessage, toolPack = VandBasicAPITool.execute_function_call(assistant_message)
  1. toolMessage is the result of the API call. toolPack is any additional tool that the function_call might have returned to be (optionally) passed to the model.

In this case toolMessage contained the weather in Honolulu:


print(f"tool message: {toolMessage}")

tool message: {
  "base": "stations",
  "clouds": {
    "all": 0
  },
  "cod": 200,
  "coord": {
    "lat": 34.0537,
    "lon": -118.2428
  },
  "dt": 1698870346,
  "id": 5368361,
  "main": {
    "feels_like": 300.63,
    "humidity": 11,
    "pressure": 1017,
    "temp": 302.31,
    "temp_max": 304.59,
    "temp_min": 299.77
  },
  "name": "Los Angeles",
  "sys": {
    "country": "US",
    "id": 3694,
    "sunrise": 1698847942,
    "sunset": 1698886837,
    "type": 1
  },
  "timezone": -25200,
  "visibility": 10000,
  "weather": [
    {
      "description": "clear sky",
      "icon": "01d",
      "id": 800,
      "main": "Clear"
    }
  ],
  "wind": {
    "deg": 130,
    "speed": 3.6
  }
}
  1. You then might want to pass the toolMessage and chat history back to the model.

completion = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "What's the weather like in Honolulu?"}, 
               "role": "function", "name": assistant_message["function_call"]["name"], 
                    "content": toolMessage}],
    )

assistant_message = completion.choices[0].message.content

Which should return a response similar to:

assistant: The weather in Honolulu today is partly cloudy with a temperature of approximately 300.74 Kelvin (27.59 degrees Celsius). The humidity is around 69% and there's a light breeze from the east-northeast with a speed of 2.06 m/s.