Преглед на файлове

modify:coze的api接口由V2改为V3

zhusc преди 4 месеца
родител
ревизия
8681f61200
променени са 1 файла, в които са добавени 57 реда и са изтрити 9 реда
  1. 57 9
      coze_bot_api.py

+ 57 - 9
coze_bot_api.py

@@ -9,6 +9,9 @@ Email: me@gentlecp.com
 Create Date: 2021/6/19
 -----------------End-----------------------------
 """
+import time
+
+from django.db.models.fields import return_None
 from fastapi import FastAPI, Response, Request, BackgroundTasks
 from WXBizMsgCrypt3 import WXBizMsgCrypt
 from xml.etree.ElementTree import fromstring
@@ -44,21 +47,66 @@ wxcpt = WXBizMsgCrypt(token, aeskey, corpid)
 app = FastAPI()
 
 
+# def call_llm(prompt: str, bot_id: str,coze_access_token:str):
+#     req_head = {
+#         "Authorization":f"Bearer {coze_access_token}",
+#         "Content-Type": "application/json",
+#     }
+#     req_data = {
+#         "conversation_id": "123",
+#         "bot_id": bot_id,
+#         "user": "test",
+#         "query": prompt,
+#         "stream": False
+#     }
+#     res = requests.post("https://api.coze.cn/open_api/v2/chat", headers=req_head, json=req_data)
+#     res.raise_for_status()  # 检查响应状态码是否为200
+#     return res.json()
+
 def call_llm(prompt: str, bot_id: str,coze_access_token:str):
     req_head = {
         "Authorization":f"Bearer {coze_access_token}",
         "Content-Type": "application/json",
     }
-    req_data = {
-        "conversation_id": "123",
+    # req_data = {
+    #     "conversation_id": "123",
+    #     "bot_id": bot_id,
+    #     "user": "test",
+    #     "query": prompt,
+    #     "stream": False
+    # }
+
+    req_data ={
         "bot_id": bot_id,
-        "user": "test",
-        "query": prompt,
-        "stream": False
+        "user_id": "123456789",
+        "stream": False,
+        "auto_save_history": True,
+        "additional_messages": [
+            {
+                "role": "user",
+                "content": prompt,
+                "content_type": "text"
+            }
+        ]
     }
-    res = requests.post("https://api.coze.cn/open_api/v2/chat", headers=req_head, json=req_data)
-    res.raise_for_status()  # 检查响应状态码是否为200
-    return res.json()
+    res_create = requests.post(" https://api.coze.cn/v1/conversation/create", headers=req_head)
+    # print(res_create.json()["data"]["id"])
+    coversition_id = res_create.json()["data"]["id"]
+    res_chat = requests.post(f" https://api.coze.cn/v3/chat?conversation_id={coversition_id}", headers=req_head,json=req_data)
+    # print(res_chat.json()["data"]["id"])
+    chat_id = res_chat.json()["data"]["id"]
+    while True:
+        res_retrieve = requests.get(f" https://api.coze.cn/v3/chat/retrieve?chat_id={chat_id}&conversation_id={coversition_id}", headers=req_head)
+        print(res_retrieve.json()["data"]["status"])
+        status = res_retrieve.json()["data"]["status"]
+        if status == "completed":
+            res_message = requests.get(f" https://api.coze.cn/v3/chat/message/list?chat_id={chat_id}&conversation_id={coversition_id}", headers=req_head)
+            # print(res_message.json())
+            return res_message.json()
+        time.sleep(1)
+    # res.raise_for_status()  # 检查响应状态码是否为200
+    # return res.json()
+
 
 def qiwei_get():
     res = requests.get(f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}")
@@ -92,7 +140,7 @@ def consumer(user_query,decrypt_data):
     agentid = decrypt_data.get('AgentID', '')
     # 返回coze结果
     coze_response = call_llm(prompt=user_query,bot_id=bot_id,coze_access_token = coze_access_token)
-    answer = coze_response['messages'][1]['content']
+    answer = coze_response['data'][1]['content']
     print(f"结果:{answer}")
     # 主动发结果给qiwei
     qiwei_post(username, answer, agentid)