Skip to content

Plugins ‐ ChatGPT Function Calling

Eliran Wong edited this page Sep 10, 2023 · 5 revisions

ChatGPT Function Calling with Plugins

To know about ChatGPT "Function Calling", read https://platform.openai.com/docs/guides/gpt/function-calling

ChatGPT-GUI users can use function calling with plugins.

Enable Function Calling

First, enable "Function Calling" in settings:

  1. Open "Settings" dialog

  2. Select "auto" right next to "Function Calling".

enable_function_calling

Remarks: Enable this feature disables streaming response. That is, with function calling enabled, response is displayed in one go.

Write a plugin with function calling

Write a plugin to modify config.chatGPTApiFunctionSignatures and config.chatGPTApiAvailableFunctions to work with function calling.

We modified the official example at https://platform.openai.com/docs/guides/gpt/function-calling as an example to write a ChatGPT-GUI plugin that work with ChatGPT function calling.

In this example,

There are two essential elements:

  1. Write a function signature and add to config.chatGPTApiFunctionSignatures

  2. Write a function and add to config.chatGPTApiAvailableFunctions

import config, json

# modified from source: https://platform.openai.com/docs/guides/gpt/function-calling

def get_current_weather(function_args):
    # retrieve argument values from a dictionary
    location = function_args.get("location") # required
    unit=function_args.get("unit", "fahrenheit") # optional

    """Get the current weather in a given location"""
    weather_info = {
        "location": location,
        "temperature": "72",
        "unit": unit,
        "forecast": ["sunny", "windy"],
    }
    return json.dumps(weather_info)

functionSignature = {
    "name": "get_current_weather",
    "description": "Get the current weather in a given location",
    "parameters": {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA",
            },
            "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
        },
        "required": ["location"],
    },
}

config.chatGPTApiFunctionSignatures.append(functionSignature)
config.chatGPTApiAvailableFunctions["get_current_weather"] = get_current_weather

You may enable 'testing_function_calling' in the plugin menu to test this example.

Example:

What's the weather like in Boston?

The current weather in Boston, MA is 72°F and sunny. It is also windy.

Plugin to Integrate Google Searches

We also created a plugin to integrate Google search results into ChatGPT responses:

import config, json, googlesearch

# Use google https://pypi.org/project/googlesearch-python/ to search internet for information, about which ChatGPT doesn't know.

def get_internet_info(function_args):
    # retrieve argument values from a dictionary
    #print(function_args)
    keywords = function_args.get("keywords") # required

    news = {}
    for index, item in enumerate(googlesearch.search(keywords, advanced=True, num_results=config.maximumInternetSearchResults)):
        news[f"result {index}"] = {
            "title": item.title,
            "url": item.url,
            "description": item.description,
        }
    return json.dumps(news)

functionSignature = {
    "name": "get_internet_info",
    "description": "Search internet for keywords when ChatGPT does not have information",
    "parameters": {
        "type": "object",
        "properties": {
            "keywords": {
                "type": "string",
                "description": "keywords for searches, e.g. ChatGPT",
            },
        },
        "required": ["keywords"],
    },
}

config.chatGPTApiFunctionSignatures.append(functionSignature)
config.chatGPTApiAvailableFunctions["get_internet_info"] = get_internet_info

Read more at https://github.com/eliranwong/ChatGPT-GUI/wiki/Include-Latest-Internet-Search-Results