from openai import OpenAI
client = OpenAI(
api_key="您的 APIKEY", # 从https://cloud.siliconflow.cn/account/ak获取
base_url="https://api.siliconflow.cn/v1"
)
def add(a: float, b: float):
return a + b
def mul(a: float, b: float):
return a * b
def compare(a: float, b: float):
if a > b:
return f'{a} is greater than {b}'
elif a < b:
return f'{b} is greater than {a}'
else:
return f'{a} is equal to {b}'
def count_letter_in_string(a: str, b: str):
string = a.lower()
letter = b.lower()
count = string.count(letter)
return(f"The letter '{letter}' appears {count} times in the string.")
tools = [
{
'type': 'function',
'function': {
'name': 'add',
'description': 'Compute the sum of two numbers',
'parameters': {
'type': 'object',
'properties': {
'a': {
'type': 'int',
'description': 'A number',
},
'b': {
'type': 'int',
'description': 'A number',
},
},
'required': ['a', 'b'],
},
}
},
{
'type': 'function',
'function': {
'name': 'mul',
'description': 'Calculate the product of two numbers',
'parameters': {
'type': 'object',
'properties': {
'a': {
'type': 'int',
'description': 'A number',
},
'b': {
'type': 'int',
'description': 'A number',
},
},
'required': ['a', 'b'],
},
}
},
{
'type': 'function',
'function': {
'name': 'count_letter_in_string',
'description': 'Count letter number in a string',
'parameters': {
'type': 'object',
'properties': {
'a': {
'type': 'str',
'description': 'source string',
},
'b': {
'type': 'str',
'description': 'letter',
},
},
'required': ['a', 'b'],
},
}
},
{
'type': 'function',
'function': {
'name': 'compare',
'description': 'Compare two number, which one is bigger',
'parameters': {
'type': 'object',
'properties': {
'a': {
'type': 'float',
'description': 'A number',
},
'b': {
'type': 'float',
'description': 'A number',
},
},
'required': ['a', 'b'],
},
}
}
]
def function_call_playground(prompt):
messages = [{'role': 'user', 'content': prompt}]
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V2.5",
messages = messages,
temperature=0.01,
top_p=0.95,
stream=False,
tools=tools)
# print(response)
func1_name = response.choices[0].message.tool_calls[0].function.name
func1_args = response.choices[0].message.tool_calls[0].function.arguments
func1_out = eval(f'{func1_name}(**{func1_args})')
# print(func1_out)
messages.append(response.choices[0].message)
messages.append({
'role': 'tool',
'content': f'{func1_out}',
'tool_call_id': response.choices[0].message.tool_calls[0].id
})
# print(messages)
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V2.5",
messages=messages,
temperature=0.01,
top_p=0.95,
stream=False,
tools=tools)
return response.choices[0].message.content
prompts = [
"用中文回答:strawberry中有多少个r?",
"用中文回答:9.11和9.9,哪个小?"
]
for prompt in prompts:
print(function_call_playground(prompt))