Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bme-mcp
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
何家明
bme-mcp
Commits
f2cc04df
Commit
f2cc04df
authored
May 19, 2025
by
何家明
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AI回答图标
parent
c63a8355
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
11 deletions
+54
-11
client.py
client/client.py
+41
-8
AiChatRecommendQuestionEntity.py
model/entity/AiChatRecommendQuestionEntity.py
+12
-2
server.py
server/server.py
+1
-1
No files found.
client/client.py
View file @
f2cc04df
...
...
@@ -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
):
# 固定问题,模拟AI回答
step
=
2
# 两个字符两个字符的输出
for
i
in
range
(
0
,
len
(
default_answer
),
step
):
await
asyncio
.
sleep
(
0.05
)
# 50毫秒延迟
yield
json
.
dumps
({
"content"
:
default_answer
[
i
:
i
+
step
]})
yield
"[DONE]"
asyncio
.
create_task
(
record_answer
(
chat_record_entity
.
id
,
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
(
q
.
default_answer
),
step
):
await
asyncio
.
sleep
(
0.05
)
# 50毫秒延迟
yield
json
.
dumps
({
"content"
:
q
.
default_answer
[
i
:
i
+
step
]})
yield
"[DONE]"
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
:
...
...
model/entity/AiChatRecommendQuestionEntity.py
View file @
f2cc04df
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
)
default_answer
=
Column
(
String
)
\ No newline at end of file
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
server/server.py
View file @
f2cc04df
...
...
@@ -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"
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment