chore: rename multiple files to improve importability and module structure
This commit is contained in:
@@ -1,87 +0,0 @@
|
||||
from config import OPENAI_API_KEY
|
||||
from openai import OpenAI
|
||||
import json
|
||||
|
||||
|
||||
def create_client():
|
||||
return OpenAI(api_key=OPENAI_API_KEY, base_url="https://api.deepseek.com")
|
||||
|
||||
def get_weather(location):
|
||||
# 模拟获得天气信息
|
||||
return f"{location}当前天气:23℃,晴,风力2级"
|
||||
|
||||
|
||||
# 以下格式为固定写法,一般仅需改description与name
|
||||
get_weather_func = {
|
||||
"name": "get_weather", # 函数名称
|
||||
"description": "获取指定城市的天气情况", # 对该函数的描述
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"location": {
|
||||
"type": "string", # 该参数类型
|
||||
"description": "城市名称,如北京、上海等" # 对该参数的描述
|
||||
}
|
||||
},
|
||||
"required": ["location"] # 声明必填
|
||||
}
|
||||
}
|
||||
|
||||
# 固定写法,有多少个tool就往里追加多少个
|
||||
tools = [
|
||||
{
|
||||
"type": "function",
|
||||
"function": get_weather_func
|
||||
}
|
||||
]
|
||||
|
||||
def chat_loop(agent_client, tools):
|
||||
messages = [
|
||||
{"role": "system",
|
||||
"content": "你是一个善解人意会热心回答人问题的助手。如果你感觉你回答不了当前问题,就会调用函数来回答。"},
|
||||
{"role": "user", "content": "北京今天的天气怎么样?"}
|
||||
]
|
||||
response = agent_client.chat.completions.create(
|
||||
model="deepseek-chat",
|
||||
messages=messages,
|
||||
tools=tools, # 调用工具
|
||||
tool_choice="auto" # 模型自主选择是否调用工具
|
||||
)
|
||||
message = response.choices[0].message
|
||||
# 如果有该参数,证明ai调用了工具
|
||||
if message.tool_calls:
|
||||
# 对每个可能要调用的工具进行循环
|
||||
for tool_call in message.tool_calls:
|
||||
if tool_call.function.name == "get_weather":
|
||||
# 解析参数
|
||||
args = json.loads(tool_call.function.arguments) # 获取用户关键词的参数
|
||||
location = args.get("location", "未知地点")
|
||||
|
||||
# 调用真实参数
|
||||
weather_info = get_weather(location)
|
||||
|
||||
# 将函数执行结果以"tool"角色传给模型,等待后面二次调用
|
||||
messages.append(message) # 先添加模型的原始响应
|
||||
messages.append({
|
||||
"role": "tool",
|
||||
"tool_call_id": tool_call.id,
|
||||
"name": tool_call.function.name,
|
||||
"content": weather_info
|
||||
})
|
||||
# 第二次调用,让模型基于工具返回的结果再生成最终答案
|
||||
final_res = agent_client.chat.completions.create(
|
||||
model="deepseek-chat",
|
||||
messages=messages
|
||||
)
|
||||
print('已调用工具...')
|
||||
print(f'回答:{final_res.choices[0].message.content}')
|
||||
else:
|
||||
# 模型没有要调用工具, 直接返回
|
||||
print('未调用工具...')
|
||||
print(f'回答:{response.choices[0].message.content}')
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
client = create_client()
|
||||
chat_loop(client, tools)
|
||||
@@ -1,79 +0,0 @@
|
||||
from config import OPENAI_API_KEY
|
||||
from openai import OpenAI
|
||||
import requests
|
||||
|
||||
|
||||
def create_client():
|
||||
return OpenAI(api_key=OPENAI_API_KEY, base_url="https://api.deepseek.com")
|
||||
|
||||
# 通过api调用获得当前ip与位置
|
||||
def get_addr():
|
||||
res_ip = requests.get('https://ipapi.co/ip/').text
|
||||
res_city = requests.get('https://ipapi.co/city/').text
|
||||
return f'ip地址:{res_ip},所在城市:{res_city}'
|
||||
|
||||
|
||||
get_addr_func = {
|
||||
"name": "get_addr", # 函数名称
|
||||
"description": "获取用户的ip地址与城市", # 对该函数的描述
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {}, # 参数为空,那么这两个地方也是空的
|
||||
"required": []
|
||||
}
|
||||
}
|
||||
|
||||
tools = [
|
||||
{
|
||||
"type":"function",
|
||||
"function":get_addr_func
|
||||
}
|
||||
]
|
||||
|
||||
def chat_loop(agent_client,tools):
|
||||
messages = [
|
||||
{"role": "system","content": "你是一个善解人意会热心回答人问题的助手。如果你感觉你回答不了当前问题,就会调用函数来回答。"},
|
||||
{"role": "user", "content": "我现在在哪个城市,ip地址是多少?"}
|
||||
]
|
||||
response = agent_client.chat.completions.create(
|
||||
model="deepseek-chat",
|
||||
messages=messages,
|
||||
tools=tools,
|
||||
tool_choice="auto"
|
||||
)
|
||||
message = response.choices[0].message
|
||||
if message.tool_calls:
|
||||
for tool_call in message.tool_calls:
|
||||
if tool_call.function.name == "get_addr":
|
||||
# 解析参数
|
||||
# args = json.loads(tool_call.function.arguments) # 获取用户关键词的参数
|
||||
# location = args.get("location")
|
||||
# 调用真实参数
|
||||
# your_info = get_weather(location)
|
||||
|
||||
# 这里无参数,直接调用函数就行
|
||||
your_info = get_addr()
|
||||
messages.append(message)
|
||||
messages.append({
|
||||
"role": "tool",
|
||||
"tool_call_id": tool_call.id,
|
||||
"name": tool_call.function.name,
|
||||
"content": your_info
|
||||
})
|
||||
final_res = agent_client.chat.completions.create(
|
||||
model="deepseek-chat",
|
||||
messages=messages
|
||||
)
|
||||
print('已调用工具...')
|
||||
print(f'回答:{final_res.choices[0].message.content}')
|
||||
else:
|
||||
# 模型没有要调用工具,直接返回
|
||||
print('未调用工具...')
|
||||
print(f'回答:{response.choices[0].message.content}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
client = create_client()
|
||||
chat_loop(client, tools)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user