|
@@ -118,8 +118,15 @@ def call_llm(prompt: str, bot_id: str,coze_access_token:str):
|
|
|
return coze_response
|
|
|
|
|
|
if status == "completed":
|
|
|
- res_message = requests.get(f"https://api.coze.cn/v3/chat/message/list?chat_id={chat_id}&conversation_id={conversation_id}", headers=req_head)
|
|
|
- coze_response = res_message.json()['data'][1]['content'].replace(" ", "")
|
|
|
+ res_message = requests.get(
|
|
|
+ f"https://api.coze.cn/v3/chat/message/list?chat_id={chat_id}&conversation_id={conversation_id}",
|
|
|
+ headers=req_head)
|
|
|
+
|
|
|
+ data = res_message.json()['data']
|
|
|
+
|
|
|
+
|
|
|
+ last_answer_record = next((record for record in reversed(data) if record['type'] == 'answer'), None)
|
|
|
+ coze_response = last_answer_record['content'].replace(" ", "")
|
|
|
|
|
|
return coze_response
|
|
|
time.sleep(1)
|
|
@@ -155,12 +162,11 @@ def qiwei_post(username: str, answer: str,agentid:str):
|
|
|
|
|
|
|
|
|
|
|
|
-def post_consumer_api(user_query, decrypt_data, request_id, bot_id):
|
|
|
+def post_consumer_api(user_query, decrypt_data, request_id):
|
|
|
data = {
|
|
|
"user_query": user_query,
|
|
|
"decrypt_data": decrypt_data,
|
|
|
"request_id": request_id,
|
|
|
- "bot_id": bot_id,
|
|
|
}
|
|
|
request_id_context.set(request_id)
|
|
|
url = "https://101.126.81.2:18088/consumer"
|
|
@@ -168,7 +174,6 @@ def post_consumer_api(user_query, decrypt_data, request_id, bot_id):
|
|
|
logger.info(f"post_consumer_api 被执行{user_query}")
|
|
|
t1 = threading.Thread(target=requests.post, kwargs={"url": url, "json": data, "verify": False})
|
|
|
t1.start()
|
|
|
- logger.info(f"post_consumer_api 请求成功: {t1}")
|
|
|
|
|
|
|
|
|
|
|
@@ -176,31 +181,75 @@ def post_consumer_api(user_query, decrypt_data, request_id, bot_id):
|
|
|
logger.error(f"post_consumer_api 请求失败: {e}")
|
|
|
|
|
|
|
|
|
+
|
|
|
+user_bot_id_mapping = {}
|
|
|
+
|
|
|
+
|
|
|
+user_welcome_status = {}
|
|
|
+
|
|
|
+
|
|
|
@app.post("/consumer")
|
|
|
def consumer(
|
|
|
request_id: str = Body(...),
|
|
|
user_query: str = Body(...),
|
|
|
decrypt_data: dict = Body(...),
|
|
|
- bot_id: str = Body(...),
|
|
|
):
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
request_id_context.set(request_id)
|
|
|
- user_query = user_query if user_query else "回答图片中的问题"
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
username = decrypt_data.get('FromUserName', '')
|
|
|
agentid = decrypt_data.get('AgentID', '')
|
|
|
msgtype = decrypt_data.get('MsgType', '')
|
|
|
picurl = decrypt_data.get('PicUrl', '')
|
|
|
- qiwei_post(username, "正在加载,请稍后...", agentid)
|
|
|
- logger.info("正在加载,请稍后...")
|
|
|
- logger.info(f"consumer 请求:{user_query}")
|
|
|
+ event = decrypt_data.get('Event')
|
|
|
+ msg_type = decrypt_data.get('MsgType')
|
|
|
+ event_key = decrypt_data.get('EventKey')
|
|
|
+ print(f"event_key: {event_key}")
|
|
|
+
|
|
|
+ if msg_type == 'event' and event == 'enter_agent':
|
|
|
+ if not user_welcome_status.get(username, False):
|
|
|
+ welcome_message = "Hi,我是小微AI助手~你可以在屏幕底部“产品选择”菜单栏选择想咨询的产品,我会随时为你解答问题~"
|
|
|
+ qiwei_post(username, welcome_message, agentid)
|
|
|
+ user_welcome_status[username] = True
|
|
|
+ return Response(content="")
|
|
|
|
|
|
+
|
|
|
+ if msg_type == 'event' and event == 'click' and event_key == '#sendmsg#_0_0#7599826213209000':
|
|
|
+ bot_id = "7456977536891846697"
|
|
|
+
|
|
|
+
|
|
|
+ user_bot_id_mapping[username] = bot_id
|
|
|
+ return Response(content="")
|
|
|
+ elif msg_type == 'event' and event == 'click' and event_key == '#sendmsg#_0_1#7599826213209001':
|
|
|
+ bot_id = "7445101065005154313"
|
|
|
+
|
|
|
+
|
|
|
+ user_bot_id_mapping[username] = bot_id
|
|
|
+ return Response(content="")
|
|
|
+ else:
|
|
|
+
|
|
|
+ bot_id = user_bot_id_mapping.get(username, "7456977536891846697")
|
|
|
+
|
|
|
+
|
|
|
+ qiwei_post(username, "我正在思考,请稍等...", agentid)
|
|
|
+ logger.info("我正在思考,请稍等...")
|
|
|
+ logger.info(f"consumer 请求:{user_query}")
|
|
|
+ user_query = user_query if user_query else "回答图片中的问题"
|
|
|
multimodal_content = [
|
|
|
{"type": "text", "text": user_query},
|
|
|
{"type": msgtype, "file_url": picurl},
|
|
@@ -208,6 +257,7 @@ def consumer(
|
|
|
|
|
|
]
|
|
|
user_query = json.dumps(multimodal_content, ensure_ascii=False)
|
|
|
+
|
|
|
|
|
|
coze_response = call_llm(prompt=user_query, bot_id=bot_id, coze_access_token=coze_access_token)
|
|
|
|
|
@@ -237,6 +287,7 @@ def consumer(
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
@app.get("/ok")
|
|
|
async def ok():
|
|
|
return "ok"
|
|
@@ -251,12 +302,11 @@ async def verify(msg_signature: str, timestamp: str, nonce: str, echostr: str):
|
|
|
|
|
|
logger.info(sEchoStr)
|
|
|
|
|
|
-
|
|
|
-user_bot_id_mapping = {}
|
|
|
+
|
|
|
|
|
|
@app.post("/bot")
|
|
|
-async def recv(msg_signature: str, timestamp: str, nonce: str, request: Request):
|
|
|
-
|
|
|
+async def recv(msg_signature: str, timestamp: str, nonce: str, request: Request, background_tasks: BackgroundTasks):
|
|
|
+
|
|
|
body = await request.body()
|
|
|
request_id = str(uuid.uuid4())
|
|
|
request_id_context.set(request_id)
|
|
@@ -265,47 +315,22 @@ async def recv(msg_signature: str, timestamp: str, nonce: str, request: Request)
|
|
|
for node in list(fromstring(sMsg.decode('utf-8'))):
|
|
|
decrypt_data[node.tag] = node.text
|
|
|
|
|
|
+
|
|
|
+ print(decrypt_data)
|
|
|
|
|
|
user_query = decrypt_data.get('Content', '')
|
|
|
|
|
|
- username = decrypt_data.get('FromUserName', '')
|
|
|
- agentid = decrypt_data.get('AgentID', '')
|
|
|
- event = decrypt_data.get('Event')
|
|
|
- msg_type = decrypt_data.get('MsgType')
|
|
|
- event_key = decrypt_data.get('EventKey')
|
|
|
-
|
|
|
|
|
|
-
|
|
|
- if msg_type == 'event' and event == 'enter_agent':
|
|
|
-
|
|
|
-
|
|
|
- return Response(content="")
|
|
|
+
|
|
|
|
|
|
-
|
|
|
- if msg_type == 'event' and event == 'click' and event_key == '#sendmsg#_0_0#7599827683207428':
|
|
|
- bot_id = "7456977536891846697"
|
|
|
-
|
|
|
-
|
|
|
- user_bot_id_mapping[username] = bot_id
|
|
|
- return Response(content="")
|
|
|
- elif msg_type == 'event' and event == 'click' and event_key == '#sendmsg#_0_1#7599827683207427':
|
|
|
- bot_id = "7445101065005154313"
|
|
|
-
|
|
|
-
|
|
|
- user_bot_id_mapping[username] = bot_id
|
|
|
- return Response(content="")
|
|
|
- else:
|
|
|
-
|
|
|
- bot_id = user_bot_id_mapping.get(username, "7456977536891846697")
|
|
|
|
|
|
|
|
|
|
|
|
logger.info(f"start: {user_query}")
|
|
|
|
|
|
- post_consumer_api(user_query, decrypt_data, request_id, bot_id)
|
|
|
- return Response(content="")
|
|
|
-
|
|
|
|
|
|
+ post_consumer_api(user_query, decrypt_data, request_id)
|
|
|
+ return Response(content="")
|
|
|
|
|
|
|
|
|
|