AtomStorm/01_creativity/tool_use.ipynb
2025-02-05 17:01:10 +08:00

129 lines
4.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Openai Function Call \n",
"https://platform.openai.com/docs/guides/function-calling"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"User>\t How's the weather in Hangzhou?\n",
"[ChatCompletionMessageToolCall(id='call_zvxBS1zoCa6X9fJzE48BYo2z', function=Function(arguments='{\"location\":\"Hangzhou, China\"}', name='get_weather', parameters=None), type='function')]\n",
"https://api.weatherapi.com/v1/current.json?key=b7e69154e60b427586983619250502&q=Hangzhou, China\n",
"Tool call>\t Temperature: 13.0°C, Condition: Partly cloudy\n",
"Model>\t The current weather in Hangzhou, China is partly cloudy with a temperature of 13.0°C.\n"
]
}
],
"source": [
"from openai import OpenAI\n",
"import json\n",
"import requests\n",
"\n",
"# 定义获取天气的工具\n",
"def get_weather(location: str):\n",
" \"\"\"调用天气 API 获取指定地点的天气信息\"\"\"\n",
" url = f\"https://api.weatherapi.com/v1/current.json?key=b7e69154e60b427586983619250502&q={location}\"\n",
" print(url)\n",
" response = requests.get(url)\n",
" if response.status_code == 200:\n",
" data = response.json()\n",
" return f\"Temperature: {data['current']['temp_c']}°C, Condition: {data['current']['condition']['text']}\"\n",
" return \"Weather data not found\"\n",
"\n",
"def send_messages(messages):\n",
" response = client.chat.completions.create(\n",
" model=\"gpt-4o\",\n",
" messages=messages,\n",
" tools=tools, # 指定可调用的函数\n",
" )\n",
" return response.choices[0].message\n",
"\n",
"client = OpenAI(\n",
" api_key=\"sk-lXFW7Bl1ruw2qmHu287e979847354601A07fE2D85a567bD7\", # pass litellm proxy key, if you're using virtual keys\n",
" base_url=\"https://yunwu.ai/v1/\" # litellm-proxy-base url\n",
")\n",
"\n",
"tools = [{\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"get_weather\",\n",
" \"description\": \"Get current temperature for a given location.\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"location\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"City and country e.g. Bogotá, Colombia\"\n",
" }\n",
" },\n",
" \"required\": [\n",
" \"location\"\n",
" ],\n",
" \"additionalProperties\": False\n",
" },\n",
" \"strict\": True\n",
" }\n",
"}]\n",
"# step1\n",
"messages = [{\"role\": \"user\", \"content\": \"How's the weather in Hangzhou?\"}]\n",
"message = send_messages(messages)\n",
"print(f\"User>\\t {messages[0]['content']}\")\n",
"print(message.tool_calls)\n",
"\n",
"# step2\n",
"tool_call = message.tool_calls[0]\n",
"\n",
"#step3 tool call chioce to local function call\n",
"args = json.loads(tool_call.function.arguments)\n",
"result = get_weather(args[\"location\"])\n",
"print(\"Tool call>\\t\", result)\n",
"\n",
"# step4\n",
"messages.append(message) # append model's function call message\n",
"messages.append({ # append result message\n",
" \"role\": \"tool\",\n",
" \"tool_call_id\": tool_call.id,\n",
" \"content\": result\n",
"})\n",
"\n",
"message = send_messages(messages)\n",
"\n",
"# step5\n",
"print(\"Model>\\t\", message.content)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}