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