Tutorial 1
# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
def get_current_weather(location, unit="celsius"):
"""Get the current weather in a given location"""
weather_info = {
"location": location,
"temperature": "16",
"unit": unit,
"forecast": ["sunny", "windy"],
}
return json.dumps(weather_info)functions = [
{
"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. Stuttgart, BW",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
]{ “id”: “chatcmpl-8EzjVmo4zPOq7Z70XEV47HcjF3FBb”, “object”: “chat.completion”, “created”: 1698584585, “model”: “gpt-3.5-turbo-0613”, “choices”: [ { “index”: 0, “message”: { “role”: “assistant”, “content”: null, “function_call”: { “name”: “get_current_weather”, “arguments”: “{"location": "Stuttgart"}” } }, “finish_reason”: “function_call” } ], “usage”: { “prompt_tokens”: 81, “completion_tokens”: 17, “total_tokens”: 98 } } ## Response message
{‘location’: ‘Stuttgart’}
{ “id”: “chatcmpl-8GqJyabUr85mp4mGjFLZJuKGOoBu6”, “object”: “chat.completion”, “created”: 1699025062, “model”: “gpt-3.5-turbo-0613”, “choices”: [ { “index”: 0, “message”: { “role”: “assistant”, “content”: “Hello! How can I assist you today?” }, “finish_reason”: “stop” } ], “usage”: { “prompt_tokens”: 75, “completion_tokens”: 10, “total_tokens”: 85 } }
{ “id”: “chatcmpl-8Ezl8M209h6uqOXuFrGzVwAoSjXVI”, “object”: “chat.completion”, “created”: 1698584686, “model”: “gpt-3.5-turbo-0613”, “choices”: [ { “index”: 0, “message”: { “role”: “assistant”, “content”: “Hello! How can I assist you today?” }, “finish_reason”: “stop” } ], “usage”: { “prompt_tokens”: 75, “completion_tokens”: 10, “total_tokens”: 85 } }
Don’t use the function
{ “id”: “chatcmpl-8GqKcx0cYK0MWb0ONpWqTTbU1hWlJ”, “object”: “chat.completion”, “created”: 1699025102, “model”: “gpt-3.5-turbo-0613”, “choices”: [ { “index”: 0, “message”: { “role”: “assistant”, “content”: “Hello! How can I assist you today?” }, “finish_reason”: “stop” } ], “usage”: { “prompt_tokens”: 76, “completion_tokens”: 9, “total_tokens”: 85 } }
{ “id”: “chatcmpl-8GqKwA2jwWb8PmgrXGZANDyxcsO0l”, “object”: “chat.completion”, “created”: 1699025122, “model”: “gpt-3.5-turbo-0613”, “choices”: [ { “index”: 0, “message”: { “role”: “assistant”, “content”: null, “function_call”: { “name”: “get_current_weather”, “arguments”: “{"location": "Stuttgart, BW"}” } }, “finish_reason”: “stop” } ], “usage”: { “prompt_tokens”: 82, “completion_tokens”: 12, “total_tokens”: 94 } }
{ “id”: “chatcmpl-8EzqG43iC6CWCOqCQiSWxngMHJNIQ”, “object”: “chat.completion”, “created”: 1698585004, “model”: “gpt-3.5-turbo-0613”, “choices”: [ { “index”: 0, “message”: { “role”: “assistant”, “content”: null, “function_call”: { “name”: “get_current_weather”, “arguments”: “{"location": "Boston, MA"}” } }, “finish_reason”: “stop” } ], “usage”: { “prompt_tokens”: 88, “completion_tokens”: 11, “total_tokens”: 99 } }
Append to list of messages
{ “id”: “chatcmpl-8EzsXbeQiRTRNHnGV9eCzlfCZrNU2”, “object”: “chat.completion”, “created”: 1698585145, “model”: “gpt-3.5-turbo-0613”, “choices”: [ { “index”: 0, “message”: { “role”: “assistant”, “content”: “The current temperature in Boston is 16 degrees Celsius. The weather is sunny and windy.” }, “finish_reason”: “stop” } ], “usage”: { “prompt_tokens”: 77, “completion_tokens”: 18, “total_tokens”: 95 } }
Congratulations! You have completed this tutorial 👍
Next, you may want to go back to the lab’s website
Jan Kirenz