68 lines
2.5 KiB
Python
68 lines
2.5 KiB
Python
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(prompt, content):
|
||
# 设置Authorization和URL
|
||
authorization = "46a5674a-e978-491b-a810-5d54605f2c36" # 请替换为真实的Authorization token
|
||
url = 'http://127.0.0.1:8080/v1/chat/completions'
|
||
|
||
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)
|
||
|
||
|
||
def game_question_json(question):
|
||
prompt = "你是一个益智百科问答大师,可以随时提出百科类的问题,问题需要有一定的难度,回答完毕之后用户能有所收获,并且对问题进行打分,同时根据问题难度告知答对之后给多少分(1-10)请只返回JSON格式的内容:格式要求如下:{\"question\": \"哪个国家最早将玫瑰与爱情联系起来?\", \"score\":\"1\", \"answer\": \"波斯\",\"description\":\"描述问题答案的原因\"}"
|
||
return message_task_json(prompt, question)
|
||
|
||
|
||
def game_answer_json(answar):
|
||
prompt = "你是一个益智百科问答大师,可以根据用户回答的答案进行判断,并且对问题(question)答案(answer)进行打分,打分时请参考最高分要求(top_score),告知用户能获得多少分,请在description中描述打分理由,请只返回JSON格式的内容:格式要求如下:{\"question\": \"哪个国家最早将玫瑰与爱情联系起来?\", \"score\":\"1\", \"answer\": \"波斯\",\"description\":\"描述问题答案的原因\"}"
|
||
return message_task_json(prompt, answar)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
|
||
print(game_question_json('请出题!'))
|
||
print(game_answer_json('question:哪个国家的节日与裸体狂欢有关?,answer:古罗马,top_score:3'))
|