"tools": [
{
'type': 'function',
'function': {
'name': '对应到实际执行的函数名称',
'description': '此处是函数相关描述',
'parameters': {
'_comments': '此处是函数参数相关描述'
},
}
},
{
'_comments': '其他函数相关说明'
}
]
payload = {
"model": "deepseek-ai/DeepSeek-V2.5",
"messages": [
{
"role": "user",
"content": "中国大模型行业2025年将会迎来哪些机遇和挑战"
}
],
"tools": [
{
'type': 'function',
'function': {
'name': '对应到实际执行的函数名称',
'description': '此处是函数相关描述',
'parameters': {
'_comments': '此处是函数参数相关描述'
},
}
},
{
'_comments': '其他函数相关说明'
}
]
'_comments': '其他函数列表'
}
tools=[对应的 tools]
比如:
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V2.5",
messages = messages,
tools=[
{
'type': 'function',
'function': {
'name': '对应到实际执行的函数名称',
'description': '此处是函数相关描述',
'parameters': {
// 此处是函数参数相关描述
},
}
},
{
// 其他函数相关说明
}
]
// chat.completions 其他参数
)
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))
strawberry中有3个r。
9.11 比 9.9 小。
import requests
from openai import OpenAI
client = OpenAI(
api_key="您的 APIKEY", # 从https://cloud.siliconflow.cn/account/ak获取
base_url="https://api.siliconflow.cn/v1"
)
# 使用 WeatherAPI 的天气查询函数
def get_weather(city: str):
# 使用 WeatherAPI 的 API 来获取天气信息
api_key = "您的WeatherAPI APIKEY" # 替换为你自己的 WeatherAPI APIKEY
base_url = "http://api.weatherapi.com/v1/current.json"
params = {
'key': api_key,
'q': city,
'aqi': 'no' # 不需要空气质量数据
}
# 调用天气 API
response = requests.get(base_url, params=params)
if response.status_code == 200:
data = response.json()
weather = data['current']['condition']['text']
temperature = data['current']['temp_c']
return f"The weather in {city} is {weather} with a temperature of {temperature}°C."
else:
return f"Could not retrieve weather information for {city}."
# 定义 OpenAI 的 function calling tools
tools = [
{
'type': 'function',
'function': {
'name': 'get_weather',
'description': 'Get the current weather for a given city.',
'parameters': {
'type': 'object',
'properties': {
'city': {
'type': 'string',
'description': 'The name of the city to query weather for.',
},
},
'required': ['city'],
},
}
}
]
# 发送请求并处理 function calling
def function_call_playground(prompt):
messages = [{'role': 'user', 'content': prompt}]
# 发送请求到 OpenAI API
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V2.5",
messages=messages,
temperature=0.01,
top_p=0.95,
stream=False,
tools=tools
)
# 处理 API 返回的工具调用请求
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})')
# 将结果添加到对话中并返回
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
})
# 返回模型响应
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
# 示例使用
prompt = "how is the weather today in beijing?"
print(function_call_playground(prompt))
The weather in Beijing today is sunny with a temperature of 21.4°C.