Files
abot/task/message_task_json.py

60 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import requests
import json
# 解析JSON
def extract_content(data_string):
try:
data = json.loads(data_string)
# 提取content字段
content = data["choices"][0]["message"].get("content", "")
return content
except json.JSONDecodeError:
print("Invalid JSON")
return None
def message_task_json(content):
# 设置Authorization和URL
authorization = "46a5674a-e978-491b-a810-5d54605f2c36" # 请替换为真实的Authorization token
url = 'http://127.0.0.1:8080/v1/chat/completions'
prompt = "你是一个益智百科问答大师可以随时提出百科类的问题问题需要有一定的难度回答完毕之后用户能有所收获并且对问题进行打分同时根据问题难度告知答对之后给多少分1-10请只返回JSON格式的内容格式要求如下{\"question\": \"哪个国家最早将玫瑰与爱情联系起来?\", \"score\":\"1\", \"answer\": \"波斯\",\"description\":\"描述问题答案的原因\"}"
# 设置请求的payload
# prompt ="你是一个益智百科问答大师,可以根据用户回答的答案进行判断,并且对问题答案进行打分,打分时请参考最高分要求告知用户能获得多少分请只返回JSON格式的内容格式要求如下{\"question\": \"哪个国家最早将玫瑰与爱情联系起来?\", \"score\":\"1\", \"answer\": \"波斯\",\"description\":\"描述问题答案的原因\"}"
data = {
# "stream": True,
"model": "windsurf/gpt4o",
"messages": [
{
"role": "system",
"content": f"{prompt}"
},
{
"role": "user",
"content": f"{content}"
}
]
}
# 设置请求头
headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": authorization
}
# 发送POST请求
response = requests.post(url, headers=headers, data=json.dumps(data), )
response.encoding = 'utf-8'
# 输出响应内容
print(response.status_code)
print(response.text)
return extract_content(response.text)
if __name__ == '__main__':
# print(message_task_json('question:哪个国家的节日与裸体狂欢有关?,answer:古罗马,top_score:3'))
print(message_task_json('请出题!'))