FunctionCalling作业(QQ号测试吉凶POST推送模式) #45
Labels
No Label
bug
duplicate
enhancement
help wanted
invalid
question
wontfix
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: HswOAuth/llm_course#45
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
import json
import requests
import yaml
from openai import OpenAI
client = OpenAI()
定义查询QQ运势的方法
def get_qq_number_jixiong(qq_number):
#读取配置
with open('config.yml', 'r') as file:
config = yaml.safe_load(file)
#print(config)
url = config['config']['myurl']
request_data = {
"api_key": config['config']['myquerykey'],
"qq": qq_number,
}
#print(request_data)
#使用POST请求
response = requests.post(url, data=request_data)
result = json.loads(response.text)
if result['errcode'] == 0:
qq_jixiong_info = {
"xiongji": result['data']['xiongji'],
"desc": result['data']['desc']
}
return json.dumps(qq_jixiong_info, ensure_ascii=False)
else:
return None
test_qq_number = input("请输入你的QQ号码:")
messages = []
messages.append({"role": "system", "content": "你是测试运势的小助手,你需要根据用户提供的QQ号码来查询今日运势"})
messages.append({"role": "user", "content": "这是我的QQ号{test_qq_number},我的今日运势如何?".format(test_qq_number=test_qq_number)})
tools = [
{"type": "function",
"function": {
"name": "get_qq_number_jixiong",
"description": "获取该号码的今日运势",
"parameters": {
"type": "object",
"properties": {
"test_qq_number": {
"type": "string",
"description": "QQ号码"
}
},
"required": ["test_qq_number"]
},
}
}
]
def get_completion(messages, tools=None, model="gpt-3.5-turbo"):
response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools,
)
return response
response = get_completion(messages, tools)
#print(response)
messages.append(response.choices[0].message)
function_name = response.choices[0].message.tool_calls[0].function.name
#print(function_name)
function_id = response.choices[0].message.tool_calls[0].id
#print(function_id)
messages.append(
{
"tool_call_id": function_id,
"role": "tool",
"name": function_name,
"content": get_qq_number_jixiong(test_qq_number),
}
)
response = get_completion(messages)
print(response.choices[0].message.content)