You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
import asyncio
|
|
|
|
|
from openai import AsyncOpenAI
|
|
|
|
|
from WxMini.Milvus.Config.MulvusConfig import *
|
|
|
|
|
from WxMini.Utils.MySQLUtil import init_mysql_pool, get_chat_log_by_session
|
|
|
|
|
|
|
|
|
|
# 初始化异步 OpenAI 客户端
|
|
|
|
|
client = AsyncOpenAI(
|
|
|
|
|
api_key=MODEL_API_KEY,
|
|
|
|
|
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
|
# 哪个人员
|
|
|
|
|
session_id = 1
|
|
|
|
|
# 哪一页
|
|
|
|
|
page = 1
|
|
|
|
|
# 一页多少个
|
|
|
|
|
page_size = 100
|
|
|
|
|
|
|
|
|
|
# 初始化 MySQL 连接池
|
|
|
|
|
mysql_pool = await init_mysql_pool()
|
|
|
|
|
|
|
|
|
|
# 调用
|
|
|
|
|
result = await get_chat_log_by_session(mysql_pool, session_id, page, page_size)
|
|
|
|
|
|
|
|
|
|
# 我只关心 user_input 与 model_response
|
|
|
|
|
# 把这些拼接出问题与回答
|
|
|
|
|
history = ""
|
|
|
|
|
for row in result['data']: # 注意:result 是一个字典,包含 'data' 字段
|
|
|
|
|
user_input = row['user_input']
|
|
|
|
|
model_response = row['model_response']
|
|
|
|
|
history = f"{history}\n问题:{user_input}\n回答:{model_response}"
|
|
|
|
|
|
|
|
|
|
# 将历史聊天记录发给大模型,让它帮我分析一下
|
|
|
|
|
prompt = (
|
|
|
|
|
"我将把用户与AI大模型交流的记录发给你,帮我分析一下这个用户是否存在心理健康方面的问题,"
|
|
|
|
|
"参考:1、PHQ-9抑郁症筛查量表和2、Beck自杀意念评量表(BSI-CV)。"
|
|
|
|
|
"如果没有健康问题请回复: OK;否则回复:NO,换行后再输出是什么问题。"
|
|
|
|
|
f"\n\n历史聊天记录:{history}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 调用大模型进行分析
|
|
|
|
|
try:
|
|
|
|
|
response = await client.chat.completions.create(
|
|
|
|
|
model=MODEL_NAME,
|
|
|
|
|
messages=[
|
|
|
|
|
{"role": "system", "content": "你是一个心理健康分析助手,负责分析用户的心理健康状况。"},
|
|
|
|
|
{"role": "user", "content": prompt}
|
|
|
|
|
],
|
|
|
|
|
max_tokens=1000
|
|
|
|
|
)
|
|
|
|
|
# 提取大模型的回复
|
|
|
|
|
if response.choices and response.choices[0].message.content:
|
|
|
|
|
analysis_result = response.choices[0].message.content.strip()
|
|
|
|
|
print("大模型分析结果:")
|
|
|
|
|
print(analysis_result)
|
|
|
|
|
else:
|
|
|
|
|
print("大模型未返回有效结果。")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"调用大模型失败: {str(e)}")
|
|
|
|
|
|
|
|
|
|
# 关闭连接池
|
|
|
|
|
mysql_pool.close()
|
|
|
|
|
await mysql_pool.wait_closed()
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
asyncio.run(main())
|