Files
tw/legacy/websocket_summary_flow.py

24 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
async def save_conversation_summary_flow(client, customer_id: str, buyer_msg: str, agent_reply: str):
"""用 AI 生成一句话对话摘要并持久化。"""
try:
from db.customer_db import db
from openai import AsyncOpenAI
api_client = AsyncOpenAI(
api_key=client.agent.api_key if client.agent else None,
base_url=client.agent.base_url if client.agent else None,
)
resp = await api_client.chat.completions.create(
model=client.agent.model_name if client.agent else "gpt-4o-mini",
messages=[
{"role": "system", "content": "用一句话15字以内总结这段对话的核心内容只输出摘要文字。"},
{"role": "user", "content": f"买家:{buyer_msg}\n客服:{agent_reply}"},
],
max_tokens=30,
temperature=0.3,
)
summary = resp.choices[0].message.content.strip()
db.save_conversation_summary(customer_id, summary)
except Exception:
client.logger.debug("保存对话摘要失败(不影响主流程)", exc_info=True)