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.

67 lines
2.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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())