2024-01-02 17:54:37 +00:00
{
"cells": [
2024-01-12 00:55:57 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Function Calling with OpenAI APIs"
]
},
2024-01-02 17:54:37 +00:00
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 1,
2024-01-02 17:54:37 +00:00
"metadata": {},
2024-01-04 22:17:36 +00:00
"outputs": [],
2024-01-02 17:54:37 +00:00
"source": [
"import os\n",
"import openai\n",
"import json\n",
"from dotenv import load_dotenv\n",
"\n",
2024-01-04 22:17:36 +00:00
"load_dotenv()\n",
"\n",
"# set openai api key\n",
2024-01-02 17:54:37 +00:00
"openai.api_key = os.environ['OPENAI_API_KEY']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define a Get Completion Function"
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 2,
2024-01-02 17:54:37 +00:00
"metadata": {},
"outputs": [],
"source": [
2024-01-04 22:17:36 +00:00
"def get_completion(messages, model=\"gpt-3.5-turbo-1106\", temperature=0, max_tokens=300, tools=None, tool_choice=None):\n",
2024-01-02 17:54:37 +00:00
" response = openai.chat.completions.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=temperature,\n",
" max_tokens=max_tokens,\n",
2024-01-04 22:17:36 +00:00
" tools=tools,\n",
" tool_choice=tool_choice\n",
2024-01-02 17:54:37 +00:00
" )\n",
" return response.choices[0].message"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define Dummy Function"
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 3,
2024-01-02 17:54:37 +00:00
"metadata": {},
"outputs": [],
"source": [
"# Defines a dummy function to get the current weather\n",
"def get_current_weather(location, unit=\"fahrenheit\"):\n",
" \"\"\"Get the current weather in a given location\"\"\"\n",
" weather = {\n",
" \"location\": location,\n",
" \"temperature\": \"50\",\n",
" \"unit\": unit,\n",
" }\n",
"\n",
" return json.dumps(weather)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define Functions\n",
"\n",
"As demonstrated in the OpenAI documentation, here is a simple example of how to define the functions that are going to be part of the request. \n",
"\n",
"The descriptions are important because these are passed directly to the LLM and the LLM will use the description to determine whether to use the functions or how to use/call."
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 4,
2024-01-02 17:54:37 +00:00
"metadata": {},
"outputs": [],
"source": [
"# define a function as tools\n",
"tools = [\n",
" {\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"get_current_weather\",\n",
" \"description\": \"Get the current weather in a given location\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"location\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city and state, e.g. San Francisco, CA\",\n",
" },\n",
" \"unit\": {\n",
" \"type\": \"string\", \n",
" \"enum\": [\"celsius\", \"fahrenheit\"]},\n",
" },\n",
" \"required\": [\"location\"],\n",
" },\n",
" }, \n",
" }\n",
"]"
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 5,
2024-01-02 17:54:37 +00:00
"metadata": {},
"outputs": [],
"source": [
"# define a list of messages\n",
"\n",
"messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"What is the weather like in London?\"\n",
" }\n",
"]"
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 6,
2024-01-02 17:54:37 +00:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-01-12 00:55:57 +00:00
"ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_GYg3yhSh1bbMabne9brwTqmA', function=Function(arguments='{\"location\":\"London\",\"unit\":\"celsius\"}', name='get_current_weather'), type='function')])\n"
2024-01-02 17:54:37 +00:00
]
}
],
"source": [
"response = get_completion(messages, tools=tools)\n",
"print(response)"
]
},
2024-01-04 22:17:36 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"response.tool_calls[0].function.arguments"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now capture the arguments:"
]
},
2024-01-02 17:54:37 +00:00
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 7,
2024-01-04 22:17:36 +00:00
"metadata": {},
"outputs": [],
"source": [
"args = json.loads(response.tool_calls[0].function.arguments)"
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 8,
2024-01-02 17:54:37 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2024-01-04 22:17:36 +00:00
"'{\"location\": \"London\", \"temperature\": \"50\", \"unit\": \"celsius\"}'"
2024-01-02 17:54:37 +00:00
]
},
2024-01-12 00:55:57 +00:00
"execution_count": 8,
2024-01-02 17:54:37 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2024-01-04 22:17:36 +00:00
"get_current_weather(**args)"
2024-01-02 17:54:37 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-01-04 22:17:36 +00:00
"### Controlling Function Calling Behavior"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's say we were interested in designing this `function_calling` functionality in the context of an LLM-powered conversational agent. Your solution should then know what function to call or if it needs to be called at all. Let's try a simple example of a greeting message:"
2024-01-02 17:54:37 +00:00
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 9,
2024-01-02 17:54:37 +00:00
"metadata": {},
"outputs": [],
"source": [
2024-01-04 22:17:36 +00:00
"messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"Hello! How are you?\",\n",
" }\n",
"]"
2024-01-02 17:54:37 +00:00
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 10,
2024-01-02 17:54:37 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2024-01-04 22:17:36 +00:00
"ChatCompletionMessage(content=\"Hello! I'm here and ready to assist you. How can I help you today?\", role='assistant', function_call=None, tool_calls=None)"
2024-01-02 17:54:37 +00:00
]
},
2024-01-12 00:55:57 +00:00
"execution_count": 10,
2024-01-02 17:54:37 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2024-01-04 22:17:36 +00:00
"get_completion(messages, tools=tools)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can specify the behavior you want from function calling, which is desired to control the behavior of your system. By default, the model decide on its own whether to call a function and which function to call. This is achieved by setting `tool_choice: \"auto\"` which is the default setting. "
2024-01-02 17:54:37 +00:00
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 11,
2024-01-04 22:17:36 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ChatCompletionMessage(content=\"Hello! I'm here and ready to assist you. How can I help you today?\", role='assistant', function_call=None, tool_calls=None)"
]
},
2024-01-12 00:55:57 +00:00
"execution_count": 11,
2024-01-04 22:17:36 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_completion(messages, tools=tools, tool_choice=\"auto\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Setting `tool_choice: \"none\"` forces the model to not use any of the functions provided. "
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 12,
2024-01-04 22:17:36 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ChatCompletionMessage(content=\"Hello! I'm here and ready to assist you. How can I help you today?\", role='assistant', function_call=None, tool_calls=None)"
]
},
2024-01-12 00:55:57 +00:00
"execution_count": 12,
2024-01-04 22:17:36 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_completion(messages, tools=tools, tool_choice=\"none\")"
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 13,
2024-01-04 22:17:36 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ChatCompletionMessage(content='I will check the current weather in London for you.', role='assistant', function_call=None, tool_calls=None)"
]
},
2024-01-12 00:55:57 +00:00
"execution_count": 13,
2024-01-04 22:17:36 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"What's the weather like in London?\",\n",
" }\n",
"]\n",
"get_completion(messages, tools=tools, tool_choice=\"none\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also force the model to choose a function if that's the behavior you want in your application. Example:"
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 14,
2024-01-04 22:17:36 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2024-01-12 00:55:57 +00:00
"ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_ctUCZtwxZYhfF3sirByNY2qC', function=Function(arguments='{\"location\":\"London\",\"unit\":\"celsius\"}', name='get_current_weather'), type='function')])"
2024-01-04 22:17:36 +00:00
]
},
2024-01-12 00:55:57 +00:00
"execution_count": 14,
2024-01-04 22:17:36 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"What's the weather like in London?\",\n",
" }\n",
"]\n",
"get_completion(messages, tools=tools, tool_choice={\"type\": \"function\", \"function\": {\"name\": \"get_current_weather\"}})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The OpenAI APIs also support parallel function calling that can call multiple functions in one turn. "
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 15,
2024-01-04 22:17:36 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2024-01-12 00:55:57 +00:00
"ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_GhyrfrY9QWkrJs9vKqxpGW22', function=Function(arguments='{\"location\": \"London\", \"unit\": \"celsius\"}', name='get_current_weather'), type='function'), ChatCompletionMessageToolCall(id='call_Cl9fqGh1AZgi4yD8n5Lrc6NE', function=Function(arguments='{\"location\": \"Belmopan\", \"unit\": \"celsius\"}', name='get_current_weather'), type='function')])"
2024-01-04 22:17:36 +00:00
]
},
2024-01-12 00:55:57 +00:00
"execution_count": 15,
2024-01-04 22:17:36 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"What's the weather like in London and Belmopan in the coming days?\",\n",
" }\n",
"]\n",
"get_completion(messages, tools=tools)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can see in the response above that the response contains information from the function calls for the two locations queried. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-01-12 00:55:57 +00:00
"### Function Calling Response for Model Feedback\n",
2024-01-04 22:17:36 +00:00
"\n",
"You might also be interested in developing an agent that passes back the result obtained after calling your APIs with the inputs generated from function calling. Let's look at an example next:\n"
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 16,
2024-01-04 22:17:36 +00:00
"metadata": {},
"outputs": [],
"source": [
"messages = []\n",
"messages.append({\"role\": \"user\", \"content\": \"What's the weather like in Boston!\"})\n",
"assistant_message = get_completion(messages, tools=tools, tool_choice=\"auto\")\n",
"assistant_message = json.loads(assistant_message.model_dump_json())\n",
"assistant_message[\"content\"] = str(assistant_message[\"tool_calls\"][0][\"function\"])\n",
"\n",
"#a temporary patch but this should be handled differently\n",
"# remove \"function_call\" from assistant message\n",
"del assistant_message[\"function_call\"]"
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 17,
2024-01-04 22:17:36 +00:00
"metadata": {},
"outputs": [],
"source": [
"messages.append(assistant_message)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We then append the results of the `get_current_weather` function and pass it back to the model using a `tool` role."
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 18,
2024-01-04 22:17:36 +00:00
"metadata": {},
"outputs": [],
"source": [
"# get the weather information to pass back to the model\n",
"weather = get_current_weather(messages[1][\"tool_calls\"][0][\"function\"][\"arguments\"])\n",
"\n",
"messages.append({\"role\": \"tool\",\n",
" \"tool_call_id\": assistant_message[\"tool_calls\"][0][\"id\"],\n",
" \"name\": assistant_message[\"tool_calls\"][0][\"function\"][\"name\"],\n",
" \"content\": weather})"
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 19,
2024-01-02 17:54:37 +00:00
"metadata": {},
"outputs": [],
2024-01-04 22:17:36 +00:00
"source": [
"final_response = get_completion(messages, tools=tools)"
]
},
{
"cell_type": "code",
2024-01-12 00:55:57 +00:00
"execution_count": 20,
2024-01-04 22:17:36 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2024-01-12 00:55:57 +00:00
"ChatCompletionMessage(content='The current temperature in Boston, MA is 50°F.', role='assistant', function_call=None, tool_calls=None)"
2024-01-04 22:17:36 +00:00
]
},
2024-01-12 00:55:57 +00:00
"execution_count": 20,
2024-01-04 22:17:36 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"final_response"
]
2024-01-02 17:54:37 +00:00
}
],
"metadata": {
"kernelspec": {
"display_name": "papersql",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 2
}