57 lines
1.8 KiB
Python
57 lines
1.8 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(content):
|
||
# 设置Authorization和URL
|
||
authorization = "46a5674a-e978-491b-a810-5d54605f2c36" # 请替换为真实的Authorization token
|
||
url = 'http://127.0.0.1:8080/v1/chat/completions'
|
||
|
||
prompt = ('你是一个个人助理,请根据用户给的任务要求及时安排日程。需要你理解用户的自然语言,并即时反馈在什么时候加入了日程提醒。提醒时间为什么时候。请只返回json格式的内容用于提取数据。格式如下:{\"task\": \"洗澡\", \"reminder_time\":\"00 20 * * *\", \"reason\": \"用户今天晚上八点要洗澡\"}')
|
||
# 设置请求的payload
|
||
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('提醒我明天晚上9点20要去游泳')) |