-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support tool call in openai api server
- Loading branch information
Showing
3 changed files
with
113 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,46 @@ | ||
import argparse | ||
|
||
import openai | ||
from openai import OpenAI | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--stream", action="store_true") | ||
parser.add_argument("--prompt", default="你好", type=str) | ||
parser.add_argument("--tool_call", action="store_true") | ||
args = parser.parse_args() | ||
|
||
client = OpenAI() | ||
|
||
tools = None | ||
if args.tool_call: | ||
tools = [ | ||
{ | ||
"type": "function", | ||
"function": { | ||
"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", | ||
}, | ||
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, | ||
}, | ||
"required": ["location"], | ||
}, | ||
}, | ||
} | ||
] | ||
|
||
messages = [{"role": "user", "content": args.prompt}] | ||
if args.stream: | ||
response = openai.ChatCompletion.create(model="default-model", messages=messages, stream=True) | ||
response = client.chat.completions.create(model="default-model", messages=messages, stream=True, tools=tools) | ||
for chunk in response: | ||
content = chunk["choices"][0]["delta"].get("content", "") | ||
print(content, end="", flush=True) | ||
content = chunk.choices[0].delta.content | ||
if content is not None: | ||
print(content, end="", flush=True) | ||
print() | ||
else: | ||
response = openai.ChatCompletion.create(model="default-model", messages=messages) | ||
print(response["choices"][0]["message"]["content"]) | ||
response = client.chat.completions.create(model="default-model", messages=messages, tools=tools) | ||
print(response.choices[0].message.content) |