From 2a3427e533d7c8d0347e572744633eb2b78ea9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9B=9B=E5=B0=91=E7=88=B7?= Date: Wed, 21 Aug 2024 19:09:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(docs):=20Refer=20to=20the=20OpenAI=20docume?= =?UTF-8?q?ntation=20to=20update=20the=20openai-functions=20docu=E2=80=A6?= =?UTF-8?q?=20(#3317)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refer to the OpenAI documentation to update the openai-functions documentation I saw the openai official website, apIn the description: The parameters `function_call` and `functions` have been replaced by `tool_choice` and `tools`.So I submitted this update;But I haven't read the code of localai, so I'm not sure if it also applies to localai. Signed-off-by: 四少爷 * Update Usage Example The original usage example was too outdated, and calling with the new version of the openai python package would result in errors. Therefore, the curl example was rewritten (as curl examples are also used elsewhere). Signed-off-by: 四少爷 * add python example Signed-off-by: 四少爷 --------- Signed-off-by: 四少爷 --- .../content/docs/features/openai-functions.md | 126 ++++++++++++++---- 1 file changed, 102 insertions(+), 24 deletions(-) diff --git a/docs/content/docs/features/openai-functions.md b/docs/content/docs/features/openai-functions.md index cb667815..5d43ece0 100644 --- a/docs/content/docs/features/openai-functions.md +++ b/docs/content/docs/features/openai-functions.md @@ -40,43 +40,121 @@ parameters: To use the functions with the OpenAI client in python: ```python -import openai +from openai import OpenAI + # ... # Send the conversation and available functions to GPT -messages = [{"role": "user", "content": "What's the weather like in Boston?"}] -functions = [ +messages = [{"role": "user", "content": "What is the weather like in Beijing now?"}] +tools = [ { - "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", + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Return the temperature of the specified region specified by the user", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "User specified region", + }, + "unit": { + "type": "string", + "enum": ["celsius", "fahrenheit"], + "description": "temperature unit" + }, }, - "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + "required": ["location"], }, - "required": ["location"], }, } ] -response = openai.ChatCompletion.create( - model="gpt-3.5-turbo", - messages=messages, - functions=functions, - function_call="auto", + +client = OpenAI( + # This is the default and can be omitted + api_key="test", + base_url="http://localhost:8080/v1/" ) -# ... + +response =client.chat.completions.create( + messages=messages, + tools=tools, + tool_choice ="auto", + model="gpt-4", +) +#... ``` -{{% alert note %}} -When running the python script, be sure to: +For example, with curl: -- Set `OPENAI_API_KEY` environment variable to a random string (the OpenAI api key is NOT required!) -- Set `OPENAI_API_BASE` to point to your LocalAI service, for example `OPENAI_API_BASE=http://localhost:8080` +```bash +curl http://localhost:8080/v1/chat/completions -H "Content-Type: application/json" -d '{ + "model": "gpt-4", + "messages": [{"role": "user", "content": "What is the weather like in Beijing now?"}], + "tools": [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Return the temperature of the specified region specified by the user", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "User specified region" + }, + "unit": { + "type": "string", + "enum": ["celsius", "fahrenheit"], + "description": "temperature unit" + } + }, + "required": ["location"] + } + } + } + ], + "tool_choice":"auto" +}' +``` -{{% /alert %}} +Return data: + +```json +{ + "created": 1724210813, + "object": "chat.completion", + "id": "16b57014-477c-4e6b-8d25-aad028a5625e", + "model": "gpt-4", + "choices": [ + { + "index": 0, + "finish_reason": "tool_calls", + "message": { + "role": "assistant", + "content": "", + "tool_calls": [ + { + "index": 0, + "id": "16b57014-477c-4e6b-8d25-aad028a5625e", + "type": "function", + "function": { + "name": "get_current_weather", + "arguments": "{\"location\":\"Beijing\",\"unit\":\"celsius\"}" + } + } + ] + } + } + ], + "usage": { + "prompt_tokens": 221, + "completion_tokens": 26, + "total_tokens": 247 + } +} +``` ## Advanced