import openai
import json
def add(a, b):
return a + b
def sub(a, b):
return a - b
openai.api_key = "demo"
openai.base_url = "http://localhost:8000/v1/"
funcs = {
"add": add,
"sub": sub
}
tools = [{
"type": "function",
"function": {
"name": "add",
"description": "Add two numbers together",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "First number"},
"b": {"type": "number", "description": "Second number"}
},
"required": ["a", "b"]
}
}
},
{
"type": "function",
"function": {
"name": "sub",
"description": "计算两个数的差",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "First number"},
"b": {"type": "number", "description": "Second number"}
},
"required": ["a", "b"]
}
}
}
]
response = openai.chat.completions.create(
stream=False,
model="qwen2:7b",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Calculate the subtraction of 500 and 70 using the sub function."}
],
tools=tools,
tool_choice={"type": "function", "function": {
"name": "sub"}},
)
message = response.choices[0].message
print(message)
if message.function_call:
result = response.choices[0].message.function_call
print(result)
func = funcs.get(result.name)
params = json.loads(result.arguments)
print(func(**params))
else:
print(message.content)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.