From 31c177b67d8becd469341e671e23f6c743c07bd4 Mon Sep 17 00:00:00 2001 From: wangyanfei Date: Wed, 26 Mar 2025 16:26:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9AOpenAI=E6=9C=80?= =?UTF-8?q?=E6=96=B0=E7=89=88=E6=9C=AC=E7=9A=84tools=E3=80=81tool=5Fchoice?= =?UTF-8?q?=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- openai_api/function_call.ipynb | 221 ++++++++++++++++++++++----------- 1 file changed, 146 insertions(+), 75 deletions(-) diff --git a/openai_api/function_call.ipynb b/openai_api/function_call.ipynb index a05ceadd..d1fc0e6c 100644 --- a/openai_api/function_call.ipynb +++ b/openai_api/function_call.ipynb @@ -132,12 +132,14 @@ }, { "cell_type": "code", + "execution_count": null, "id": "eb420fcd-1a4d-4cc8-90cf-39a92cf28060", "metadata": { "jupyter": { "is_executing": true } }, + "outputs": [], "source": [ "# 使用了retry库,指定在请求失败时的重试策略。\n", "# 这里设定的是指数等待(wait_random_exponential),时间间隔的最大值为40秒,并且最多重试3次(stop_after_attempt(3))。\n", @@ -187,9 +189,7 @@ " print(\"Unable to generate ChatCompletion response\")\n", " print(f\"Exception: {e}\")\n", " return e\n" - ], - "outputs": [], - "execution_count": null + ] }, { "cell_type": "code", @@ -342,12 +342,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", - "\u001B[0m\n", - "\u001B[32muser: What's the weather like today\n", - "\u001B[0m\n", - "\u001B[34massistant[content]: Sure, could you please provide me with the location for which you would like to know the current weather?\n", - "\u001B[0m\n" + "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", + "\u001b[0m\n", + "\u001b[32muser: What's the weather like today\n", + "\u001b[0m\n", + "\u001b[34massistant[content]: Sure, could you please provide me with the location for which you would like to know the current weather?\n", + "\u001b[0m\n" ] } ], @@ -443,16 +443,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", - "\u001B[0m\n", - "\u001B[32muser: What's the weather like today\n", - "\u001B[0m\n", - "\u001B[34massistant[content]: Sure, could you please provide me with the location for which you would like to know the current weather?\n", - "\u001B[0m\n", - "\u001B[32muser: I'm in Shanghai, China.\n", - "\u001B[0m\n", - "\u001B[34massistant[function_call]: {'name': 'get_current_weather', 'arguments': '{\"location\":\"Shanghai, China\",\"format\":\"celsius\"}'}\n", - "\u001B[0m\n" + "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", + "\u001b[0m\n", + "\u001b[32muser: What's the weather like today\n", + "\u001b[0m\n", + "\u001b[34massistant[content]: Sure, could you please provide me with the location for which you would like to know the current weather?\n", + "\u001b[0m\n", + "\u001b[32muser: I'm in Shanghai, China.\n", + "\u001b[0m\n", + "\u001b[34massistant[function_call]: {'name': 'get_current_weather', 'arguments': '{\"location\":\"Shanghai, China\",\"format\":\"celsius\"}'}\n", + "\u001b[0m\n" ] } ], @@ -502,12 +502,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", - "\u001B[0m\n", - "\u001B[32muser: what is the weather going to be like in Shanghai, China over the next x days\n", - "\u001B[0m\n", - "\u001B[34massistant[content]: Sure! Please provide the number of days you would like to know the weather forecast for in Shanghai, China.\n", - "\u001B[0m\n" + "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", + "\u001b[0m\n", + "\u001b[32muser: what is the weather going to be like in Shanghai, China over the next x days\n", + "\u001b[0m\n", + "\u001b[34massistant[content]: Sure! Please provide the number of days you would like to know the weather forecast for in Shanghai, China.\n", + "\u001b[0m\n" ] } ], @@ -569,16 +569,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", - "\u001B[0m\n", - "\u001B[32muser: what is the weather going to be like in Shanghai, China over the next x days\n", - "\u001B[0m\n", - "\u001B[34massistant[content]: Sure! Please provide the number of days you would like to know the weather forecast for in Shanghai, China.\n", - "\u001B[0m\n", - "\u001B[32muser: 5 days\n", - "\u001B[0m\n", - "\u001B[34massistant[function_call]: {'name': 'get_n_day_weather_forecast', 'arguments': '{\"location\":\"Shanghai, China\",\"format\":\"celsius\",\"num_days\":5}'}\n", - "\u001B[0m\n" + "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", + "\u001b[0m\n", + "\u001b[32muser: what is the weather going to be like in Shanghai, China over the next x days\n", + "\u001b[0m\n", + "\u001b[34massistant[content]: Sure! Please provide the number of days you would like to know the weather forecast for in Shanghai, China.\n", + "\u001b[0m\n", + "\u001b[32muser: 5 days\n", + "\u001b[0m\n", + "\u001b[34massistant[function_call]: {'name': 'get_n_day_weather_forecast', 'arguments': '{\"location\":\"Shanghai, China\",\"format\":\"celsius\",\"num_days\":5}'}\n", + "\u001b[0m\n" ] } ], @@ -634,12 +634,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", - "\u001B[0m\n", - "\u001B[32muser: Give me a weather report for San Diego, USA.\n", - "\u001B[0m\n", - "\u001B[34massistant[function_call]: {'name': 'get_n_day_weather_forecast', 'arguments': '{\"location\":\"San Diego, USA\",\"format\":\"celsius\",\"num_days\":1}'}\n", - "\u001B[0m\n" + "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", + "\u001b[0m\n", + "\u001b[32muser: Give me a weather report for San Diego, USA.\n", + "\u001b[0m\n", + "\u001b[34massistant[function_call]: {'name': 'get_n_day_weather_forecast', 'arguments': '{\"location\":\"San Diego, USA\",\"format\":\"celsius\",\"num_days\":1}'}\n", + "\u001b[0m\n" ] } ], @@ -692,12 +692,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", - "\u001B[0m\n", - "\u001B[32muser: Give me a weather report for San Diego, USA.\n", - "\u001B[0m\n", - "\u001B[34massistant[function_call]: {'name': 'get_current_weather', 'arguments': '{\"location\":\"San Diego, USA\",\"format\":\"celsius\"}'}\n", - "\u001B[0m\n" + "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", + "\u001b[0m\n", + "\u001b[32muser: Give me a weather report for San Diego, USA.\n", + "\u001b[0m\n", + "\u001b[34massistant[function_call]: {'name': 'get_current_weather', 'arguments': '{\"location\":\"San Diego, USA\",\"format\":\"celsius\"}'}\n", + "\u001b[0m\n" ] } ], @@ -759,12 +759,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "\u001B[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", - "\u001B[0m\n", - "\u001B[32muser: Give me the current weather (use Celcius) for Toronto, Canada.\n", - "\u001B[0m\n", - "\u001B[34massistant[content]: Sure! Let me retrieve the current weather for Toronto, Canada in Celsius.\n", - "\u001B[0m\n" + "\u001b[31msystem: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\n", + "\u001b[0m\n", + "\u001b[32muser: Give me the current weather (use Celcius) for Toronto, Canada.\n", + "\u001b[0m\n", + "\u001b[34massistant[content]: Sure! Let me retrieve the current weather for Toronto, Canada in Celsius.\n", + "\u001b[0m\n" ] } ], @@ -1119,14 +1119,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "\u001B[31msystem: Answer user questions by generating SQL queries against the Chinook Music Database.\n", - "\u001B[0m\n", - "\u001B[32muser: Hi, who are the top 5 artists by number of tracks?\n", - "\u001B[0m\n", - "\u001B[34massistant[function_call]: {'name': 'ask_database', 'arguments': '{\"query\":\"SELECT artists.Name, COUNT(tracks.TrackId) AS TrackCount\\\\nFROM artists\\\\nJOIN albums ON artists.ArtistId = albums.ArtistId\\\\nJOIN tracks ON albums.AlbumId = tracks.AlbumId\\\\nGROUP BY artists.Name\\\\nORDER BY TrackCount DESC\\\\nLIMIT 5;\"}'}\n", - "\u001B[0m\n", - "\u001B[35mfunction (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n", - "\u001B[0m\n" + "\u001b[31msystem: Answer user questions by generating SQL queries against the Chinook Music Database.\n", + "\u001b[0m\n", + "\u001b[32muser: Hi, who are the top 5 artists by number of tracks?\n", + "\u001b[0m\n", + "\u001b[34massistant[function_call]: {'name': 'ask_database', 'arguments': '{\"query\":\"SELECT artists.Name, COUNT(tracks.TrackId) AS TrackCount\\\\nFROM artists\\\\nJOIN albums ON artists.ArtistId = albums.ArtistId\\\\nJOIN tracks ON albums.AlbumId = tracks.AlbumId\\\\nGROUP BY artists.Name\\\\nORDER BY TrackCount DESC\\\\nLIMIT 5;\"}'}\n", + "\u001b[0m\n", + "\u001b[35mfunction (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n", + "\u001b[0m\n" ] } ], @@ -1254,20 +1254,20 @@ "name": "stdout", "output_type": "stream", "text": [ - "\u001B[31msystem: Answer user questions by generating SQL queries against the Chinook Music Database.\n", - "\u001B[0m\n", - "\u001B[32muser: Hi, who are the top 5 artists by number of tracks?\n", - "\u001B[0m\n", - "\u001B[34massistant[function_call]: {'name': 'ask_database', 'arguments': '{\"query\":\"SELECT artists.Name, COUNT(tracks.TrackId) AS TrackCount\\\\nFROM artists\\\\nJOIN albums ON artists.ArtistId = albums.ArtistId\\\\nJOIN tracks ON albums.AlbumId = tracks.AlbumId\\\\nGROUP BY artists.Name\\\\nORDER BY TrackCount DESC\\\\nLIMIT 5;\"}'}\n", - "\u001B[0m\n", - "\u001B[35mfunction (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n", - "\u001B[0m\n", - "\u001B[32muser: What is the name of the album with the most tracks?\n", - "\u001B[0m\n", - "\u001B[34massistant[function_call]: {'name': 'ask_database', 'arguments': '{\"query\":\"SELECT albums.Title, COUNT(tracks.TrackId) AS TrackCount\\\\nFROM albums\\\\nJOIN tracks ON albums.AlbumId = tracks.AlbumId\\\\nGROUP BY albums.Title\\\\nORDER BY TrackCount DESC\\\\nLIMIT 1;\"}'}\n", - "\u001B[0m\n", - "\u001B[35mfunction (ask_database): [('Greatest Hits', 57)]\n", - "\u001B[0m\n" + "\u001b[31msystem: Answer user questions by generating SQL queries against the Chinook Music Database.\n", + "\u001b[0m\n", + "\u001b[32muser: Hi, who are the top 5 artists by number of tracks?\n", + "\u001b[0m\n", + "\u001b[34massistant[function_call]: {'name': 'ask_database', 'arguments': '{\"query\":\"SELECT artists.Name, COUNT(tracks.TrackId) AS TrackCount\\\\nFROM artists\\\\nJOIN albums ON artists.ArtistId = albums.ArtistId\\\\nJOIN tracks ON albums.AlbumId = tracks.AlbumId\\\\nGROUP BY artists.Name\\\\nORDER BY TrackCount DESC\\\\nLIMIT 5;\"}'}\n", + "\u001b[0m\n", + "\u001b[35mfunction (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n", + "\u001b[0m\n", + "\u001b[32muser: What is the name of the album with the most tracks?\n", + "\u001b[0m\n", + "\u001b[34massistant[function_call]: {'name': 'ask_database', 'arguments': '{\"query\":\"SELECT albums.Title, COUNT(tracks.TrackId) AS TrackCount\\\\nFROM albums\\\\nJOIN tracks ON albums.AlbumId = tracks.AlbumId\\\\nGROUP BY albums.Title\\\\nORDER BY TrackCount DESC\\\\nLIMIT 1;\"}'}\n", + "\u001b[0m\n", + "\u001b[35mfunction (ask_database): [('Greatest Hits', 57)]\n", + "\u001b[0m\n" ] } ], @@ -1303,6 +1303,77 @@ "outputs": [], "source": [] }, + { + "cell_type": "markdown", + "id": "3dbcb0e6", + "metadata": {}, + "source": [ + "## OpenAI最新版本的tools、tool_choice使用\n", + "[新增]最新OpenAI版本的functions和function_call已经被tools、tool_choice所替代\n", + "参考: https://platform.openai.com/docs/guides/function-calling" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a24ad343", + "metadata": {}, + "outputs": [], + "source": [ + "from openai import OpenAI\n", + "import os\n", + "\n", + "# DeepSeek API调用(deepseek-chat)\n", + "client = OpenAI(\n", + " api_key=\"XXX\",\n", + " base_url=\"https://api.deepseek.com\"\n", + ")\n", + "\n", + "response = client.chat.completions.create(\n", + " model=\"deepseek-chat\", # 替换为您使用的模型\n", + " messages=[\n", + " {\"role\": \"user\", \"content\": \"今天北京天气怎么样?\"}\n", + " ],\n", + " tools=[\n", + " {\n", + " \"type\": \"function\",\n", + " \"function\": {\n", + " \"name\": \"get_n_day_weather_forecast\",\n", + " \"description\": \"获取未来N天的天气预报\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"location\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"要查询天气的城市名称\"\n", + " },\n", + " \"days\": {\n", + " \"type\": \"integer\",\n", + " \"description\": \"要查询的天数\"\n", + " }\n", + " },\n", + " \"required\": [\"location\", \"days\"]\n", + " }\n", + " }\n", + " }\n", + " ],\n", + " tool_choice={\"type\": \"function\", \"function\": {\"name\": \"get_n_day_weather_forecast\"}}\n", + ")\n", + "\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2247fe31", + "metadata": {}, + "outputs": [], + "source": [ + "print(response.choices[0].finish_reason)\n", + "print(response.choices[0].message.tool_calls)" + ] + }, { "cell_type": "markdown", "id": "b5c5dc6e-0a61-46a1-b04f-03f23c9161b2", From 22d69cbf4d5df5ccc4f8bb5e02d9b076439d97b5 Mon Sep 17 00:00:00 2001 From: wangyanfei Date: Wed, 26 Mar 2025 16:57:22 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E3=80=90FIX=E3=80=91=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=98=8E=E7=A1=AE=EF=BC=9B=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E6=90=AD=E5=BB=BA=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- openai-translator/README-CN.md | 16 +++++++++++++--- openai-translator/README.md | 17 +++++++++++++++-- openai-translator/requirements.txt | 18 +++++++++--------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/openai-translator/README-CN.md b/openai-translator/README-CN.md index 00d890c1..c4dc469f 100644 --- a/openai-translator/README-CN.md +++ b/openai-translator/README-CN.md @@ -46,9 +46,20 @@ OpenAI 翻译器目前还处于早期开发阶段,我正在积极地添加更 1.克隆仓库 `git clone git@github.com:DjangoPeng/openai-translator.git`。 -2.OpenAI-翻译器 需要 Python 3.6 或更高版本。使用 `pip install -r requirements.txt` 安装依赖项。 +2.OpenAI-翻译器 需要 Python 3.6 或更高版本。建议新建一个 Python 虚拟环境,命名为 `translater`。 -3.设置您的 OpenAI API 密钥(`$OPENAI_API_KEY`)或 ChatGLM 模型 URL(`$GLM_MODEL_URL`)。您可以将其添加到环境变量中,或者在 config.yaml 文件中指定。 +```shell +conda create -n translater python=3.10 + +# 激活环境 +conda activate translater +``` + +之后每次使用需要激活此环境。 + +3.使用 `pip install -r requirements.txt` 安装依赖项。 + +4.设置您的 OpenAI API 密钥(`$OPENAI_API_KEY`)或 ChatGLM 模型 URL(`$GLM_MODEL_URL`)。您可以将其添加到环境变量中,或者在 config.yaml 文件中指定。 ### 使用示例 @@ -104,4 +115,3 @@ python ai_translator/main.py --model_type GLMModel --glm_model_url $GLM_MODEL_UR - diff --git a/openai-translator/README.md b/openai-translator/README.md index 11a42c20..1829d8f9 100644 --- a/openai-translator/README.md +++ b/openai-translator/README.md @@ -47,9 +47,22 @@ The OpenAI Translator is still in its early stages of development, and I'm activ 1.Clone the repository `git clone git@github.com:DjangoPeng/openai-translator.git`. -2.The `OpenAI-Translator` requires Python 3.6 or later. Install the dependencies with `pip install -r requirements.txt`. +2.The `OpenAI-Translator` requires Python 3.6 or later. -3.Set up your OpenAI API key(`$OPENAI_API_KEY`) or ChatGLM Model URL(`$GLM_MODEL_URL`). You can either add it to your environment variables or specify it in the config.yaml file. +After installation, it is recommended to create a new Python virtual environment named `translater`. + +``` +conda create -n translater python=3.10 + +# Activate the environment +conda activate translater +``` + +This environment needs to be activated each time before use. + +3.Install the dependencies with `pip install -r requirements.txt`. + +4.Set up your OpenAI API key(`$OPENAI_API_KEY`) or ChatGLM Model URL(`$GLM_MODEL_URL`). You can either add it to your environment variables or specify it in the config.yaml file. ### Usage diff --git a/openai-translator/requirements.txt b/openai-translator/requirements.txt index 59246f88..8d270a55 100644 --- a/openai-translator/requirements.txt +++ b/openai-translator/requirements.txt @@ -1,9 +1,9 @@ -openai==1.14.2 -pdfplumber -simplejson -requests -PyYAML -pillow -reportlab -pandas -loguru \ No newline at end of file +openai==1.65.4 +pdfplumber==0.11.5 +simplejson==3.20.1 +requests==2.32.3 +PyYAML==6.0.2 +pillow==11.1.0 +reportlab==4.3.1 +pandas==2.2.3 +loguru==0.7.3 \ No newline at end of file