Skip to content

Commit 2a3427e

Browse files
authored
fix(docs): Refer to the OpenAI documentation to update the openai-functions docu… (#3317)
* 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: 四少爷 <[email protected]> * 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: 四少爷 <[email protected]> * add python example Signed-off-by: 四少爷 <[email protected]> --------- Signed-off-by: 四少爷 <[email protected]>
1 parent 7ec02ba commit 2a3427e

File tree

1 file changed

+101
-23
lines changed

1 file changed

+101
-23
lines changed

docs/content/docs/features/openai-functions.md

Lines changed: 101 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,43 +40,121 @@ parameters:
4040
To use the functions with the OpenAI client in python:
4141
4242
```python
43-
import openai
43+
from openai import OpenAI
44+
4445
# ...
4546
# Send the conversation and available functions to GPT
46-
messages = [{"role": "user", "content": "What's the weather like in Boston?"}]
47-
functions = [
47+
messages = [{"role": "user", "content": "What is the weather like in Beijing now?"}]
48+
tools = [
4849
{
49-
"name": "get_current_weather",
50-
"description": "Get the current weather in a given location",
51-
"parameters": {
52-
"type": "object",
53-
"properties": {
54-
"location": {
55-
"type": "string",
56-
"description": "The city and state, e.g. San Francisco, CA",
50+
"type": "function",
51+
"function": {
52+
"name": "get_current_weather",
53+
"description": "Return the temperature of the specified region specified by the user",
54+
"parameters": {
55+
"type": "object",
56+
"properties": {
57+
"location": {
58+
"type": "string",
59+
"description": "User specified region",
60+
},
61+
"unit": {
62+
"type": "string",
63+
"enum": ["celsius", "fahrenheit"],
64+
"description": "temperature unit"
65+
},
5766
},
58-
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
67+
"required": ["location"],
5968
},
60-
"required": ["location"],
6169
},
6270
}
6371
]
64-
response = openai.ChatCompletion.create(
65-
model="gpt-3.5-turbo",
72+
73+
client = OpenAI(
74+
# This is the default and can be omitted
75+
api_key="test",
76+
base_url="http://localhost:8080/v1/"
77+
)
78+
79+
response =client.chat.completions.create(
6680
messages=messages,
67-
functions=functions,
68-
function_call="auto",
81+
tools=tools,
82+
tool_choice ="auto",
83+
model="gpt-4",
6984
)
70-
# ...
85+
#...
7186
```
7287

73-
{{% alert note %}}
74-
When running the python script, be sure to:
88+
For example, with curl:
7589

76-
- Set `OPENAI_API_KEY` environment variable to a random string (the OpenAI api key is NOT required!)
77-
- Set `OPENAI_API_BASE` to point to your LocalAI service, for example `OPENAI_API_BASE=http://localhost:8080`
90+
```bash
91+
curl http://localhost:8080/v1/chat/completions -H "Content-Type: application/json" -d '{
92+
"model": "gpt-4",
93+
"messages": [{"role": "user", "content": "What is the weather like in Beijing now?"}],
94+
"tools": [
95+
{
96+
"type": "function",
97+
"function": {
98+
"name": "get_current_weather",
99+
"description": "Return the temperature of the specified region specified by the user",
100+
"parameters": {
101+
"type": "object",
102+
"properties": {
103+
"location": {
104+
"type": "string",
105+
"description": "User specified region"
106+
},
107+
"unit": {
108+
"type": "string",
109+
"enum": ["celsius", "fahrenheit"],
110+
"description": "temperature unit"
111+
}
112+
},
113+
"required": ["location"]
114+
}
115+
}
116+
}
117+
],
118+
"tool_choice":"auto"
119+
}'
120+
```
78121

79-
{{% /alert %}}
122+
Return data:
123+
124+
```json
125+
{
126+
"created": 1724210813,
127+
"object": "chat.completion",
128+
"id": "16b57014-477c-4e6b-8d25-aad028a5625e",
129+
"model": "gpt-4",
130+
"choices": [
131+
{
132+
"index": 0,
133+
"finish_reason": "tool_calls",
134+
"message": {
135+
"role": "assistant",
136+
"content": "",
137+
"tool_calls": [
138+
{
139+
"index": 0,
140+
"id": "16b57014-477c-4e6b-8d25-aad028a5625e",
141+
"type": "function",
142+
"function": {
143+
"name": "get_current_weather",
144+
"arguments": "{\"location\":\"Beijing\",\"unit\":\"celsius\"}"
145+
}
146+
}
147+
]
148+
}
149+
}
150+
],
151+
"usage": {
152+
"prompt_tokens": 221,
153+
"completion_tokens": 26,
154+
"total_tokens": 247
155+
}
156+
}
157+
```
80158

81159
## Advanced
82160

0 commit comments

Comments
 (0)