Spaces:
Sleeping
Sleeping
File size: 3,044 Bytes
1d4c295 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
from openai import OpenAI
import json
from event_trigger import event_trigger
import os
# 设置OpenAI API密钥和基础URL
api_key = os.getenv("OPENAI_API_KEY")
base_url = os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1")
model_name = os.getenv("OPENAI_MODEL_NAME", "gpt-3.5-turbo")
tools = [
{
"type": "function",
"function": {
'name': 'generate_complaint_chain',
'description': '根据角色信息和近期遭遇的事件,生成一个患者的主诉请求认知变化链',
'parameters': {
"type": "object",
"properties": {
"chain": {
"type": "array",
"items": {
"type": "object",
"properties": {
"stage": {
"type": "integer"
},
"content": {
"type": "string"
}
},
"additionalProperties": False,
"required": [
"stage",
"content"
]
},
"minItems": 3,
"maxItems": 7
}
},
"required": ["chain"]
},
}
}
]
# 根据profile和event生成主诉启发链
def gen_complaint_chain(profile):
# 提取患者信息
patient_info = f"### 患者信息\n年龄:{profile['age']}\n性别:{profile['gender']}\n职业:{profile['occupation']}\n婚姻状况:{profile['marital_status']}\n症状:{profile['symptoms']}"
event = event_trigger(profile)
client = OpenAI(
api_key=api_key,
base_url=base_url
)
response = client.chat.completions.create(
model=model_name,
messages=[
{"role": "user", "content": f"### 任务\n根据患者情况及近期遭遇事件生成患者的主诉认知变化链。请注意,事件可能与患者信息冲突,如果发生这种情况,以患者的信息为准。\n{patient_info}\n### 近期遭遇事件\n{event}"}
],
tools=tools,
tool_choice={"type": "function", "function": {"name": "generate_complaint_chain"}}
)
chain = json.loads(response.choices[0].message.tool_calls[0].function.arguments)["chain"]
return chain
# unit test
# while True:
# # 模拟患者信息
# profile = {
# "drisk": 3,
# "srisk": 2,
# "age": "42",
# "gender": "女",
# "marital_status": "离婚",
# "occupation": "教师",
# "symptoms": "缺乏自信心,自我价值感低,有自罪感,无望感;体重剧烈增加;精神运动性激越;有自杀想法"
# }
# print(gen_complaint_chain(profile))
|