Commit f2cc04df authored by 何家明's avatar 何家明

AI回答图标

parent c63a8355
......@@ -19,6 +19,19 @@ from model.entity.AiChatRecommendQuestionEntity import AiChatRecommendQuestionEn
from model.entity.AiChatRecordEntity import AiChatRecordEntity
async def update_fixed_answer(_id, default_answer):
"""AI回答完成后,异步更新固定问题的回答"""
await db_util.update(
AiChatRecommendQuestionEntity,
{
"id": _id
},
{
"default_answer": default_answer,
"latest_answer_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
)
async def record_answer(_id, _completion_answer):
"""AI回答完成后,异步更新回答数据"""
await db_util.update(
......@@ -152,15 +165,33 @@ class McpClient:
AiChatRecommendQuestionEntity,
AiChatRecommendQuestionEntity.question.__eq__(message),
)
if db_question and len(db_question) > 0 and (default_answer := db_question[0].default_answer):
fixed_question_id = None
if db_question and len(db_question) > 0 and db_question[0].question_type == "fixed": # 固定问题
q = db_question[0]
# 更新频率小于等于0,或者默认回答没有数据,那都要重新调用AI
call_ai = q.answer_update_frequency <= 0 or q.default_answer is None
fixed_question_id = q.id
if not call_ai:
if q.latest_answer_time:
date_delta = datetime.now() - q.latest_answer_time
logger.info(f"Current date delta is: {date_delta}")
# 回答时间差比更新频率大了,需要重新更新回答,意味着要去调AI重新生成
call_ai = date_delta.days > q.answer_update_frequency
else: # 没有回答最近更新时间,但是却有回答,可能是脏数据或其他原因导致没更新
call_ai = True
logger.info(f"Need to call ai: {call_ai}")
if not call_ai:
# 固定问题,模拟AI回答
step = 2 # 两个字符两个字符的输出
for i in range(0, len(default_answer), step):
for i in range(0, len(q.default_answer), step):
await asyncio.sleep(0.05) # 50毫秒延迟
yield json.dumps({"content": default_answer[i:i + step]})
yield json.dumps({"content": q.default_answer[i:i + step]})
yield "[DONE]"
asyncio.create_task(record_answer(chat_record_entity.id, default_answer))
asyncio.create_task(record_answer(chat_record_entity.id, q.default_answer))
else:
call_ai = True
if call_ai:
messages = [
{"role": "system", "content": self.default_system_prompt},
{"role": "system", "content": self.deal_customer_permission(customer_id)},
......@@ -214,6 +245,8 @@ class McpClient:
completion_answer = ""
async for chunk in ai_stream_response:
if chunk.choices[0].finish_reason == "stop":
if fixed_question_id:
asyncio.create_task(update_fixed_answer(fixed_question_id, completion_answer))
asyncio.create_task(record_answer(chat_record_entity.id, completion_answer))
yield "[DONE]"
else:
......
from sqlalchemy import Column, Integer, String
from sqlalchemy import Column, Integer, String, DateTime
from model.entity.BaseEntity import BaseEntity
......@@ -7,4 +7,14 @@ class AiChatRecommendQuestionEntity(BaseEntity):
__tablename__ = "t_ai_chat_recommend_question"
customer_id = Column(Integer)
question = Column(String)
question_type = Column(String)
"""fixed:固定问题 mcp:需要调用mcp服务的问题"""
default_answer = Column(String)
"""部分长期不会发生改变的问题默认回答"""
latest_answer_time = Column(DateTime)
"""默认回答最近一次更新时间"""
answer_update_frequency = Column(Integer)
"""默认回答的更新频率,单位为天,比如30,则表示每30天更新一次默认回答"""
\ No newline at end of file
......@@ -7,7 +7,7 @@ from mcp.server.fastmcp import FastMCP
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
from config.system import config
mcp = FastMCP("BME-MCP")
mcp = FastMCP("BME-MCP", log_level="ERROR")
remote = config["remote"]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment