Skip to content

Commit

Permalink
fix(docs): Refer to the OpenAI documentation to update the openai-fun…
Browse files Browse the repository at this point in the history
…ctions 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]>
  • Loading branch information
jermeyhu authored Aug 21, 2024
1 parent 7ec02ba commit 2a3427e
Showing 1 changed file with 101 additions and 23 deletions.
124 changes: 101 additions & 23 deletions docs/content/docs/features/openai-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",

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,
functions=functions,
function_call="auto",
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

Expand Down

0 comments on commit 2a3427e

Please sign in to comment.