Files
python----/智慧校园/智能客服.ipynb
T
admin aac9f5934d feat: 添加多个功能模块和工具脚本
- 新增websocket客户端和服务端实现
- 添加图片压缩工具和快速压缩脚本
- 实现学生信息处理相关API
- 添加MQTT客户端和消息处理功能
- 更新.gitignore忽略更多文件类型
- 添加数据库操作工具和示例
- 实现多个测试脚本和工具类
2026-04-13 14:47:50 +08:00

22 KiB

In [1]:
from requests import get,post
In [2]:
headers = {
    "authorization":"Bearer pat_OHegGl8V4T04IUBZ9KRoldFHKegGX48kQKTfyskZJWMRUbQMNHD0AuqGtvGYZ0q9",
    "content-type":"application/json"
}
ip = "https://api.coze.cn"
In [3]:
data = post("https://api.coze.cn/open_api/knowledge/document/list",headers=headers,json={"dataset_id":"7567253415145144335"}).text
print(data)
{"code":700012006,"msg":"access token expired"}
In [ ]:
In [27]:
data = get(f"{ip}/v1/conversation/message/list?conversation_id=7483161006881980479",json={"limit":20},headers=headers).json()
In [24]:
for line in data.iter_lines():
    if line:
        print(line.decode("utf-8"))  # 处理每一行数据
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[24], line 1
----> 1 for line in data.iter_lines():
      2     if line:
      3         print(line.decode("utf-8"))

AttributeError: 'str' object has no attribute 'iter_lines'
In [15]:
req = post(
    f"{ip}/v1/workflows/chat",
    json={"additional_messages":[{"role":"user","content":"打开1101门锁","content_type":"text"}],"connector_id":"10000010","conversation_id":"7506806371942301737","workflow_id":"7505969113844613147","parameters":{"token":"ce975c9e-6d6a-4d63-b069-79501b1b7038"},"execute_mode":"DEBUG","bot_id":"7484071067645198387","ext":{"_caller":"CANVAS","user_id":"288532740445212"},"suggest_reply_info":{}},
    headers=headers,
    stream=True,
)
try:
    for line in req.iter_lines():
        if line:
            print(line.decode("utf-8"))  # 处理每一行数据
finally:
    req.close()
event: conversation.chat.created
data: {"id":"7506806634484744230","conversation_id":"7506806371942301737","created_at":1747814621,"last_error":{"code":0,"msg":""},"status":"created","usage":{"token_count":0,"output_count":0,"input_count":0},"section_id":"7506806371942301737","execute_id":"7506806633684779017","inserted_additional_messages":[{"id":"7506806606261403657"}]}
event: conversation.chat.in_progress
data: {"id":"7506806634484744230","conversation_id":"7506806371942301737","created_at":1747814621,"last_error":{"code":0,"msg":""},"status":"in_progress","usage":{"token_count":0,"output_count":0,"input_count":0},"section_id":"7506806371942301737","execute_id":"7506806633684779017","inserted_additional_messages":[{"id":"7506806606261403657"}]}
event: conversation.message.completed
data: {"id":"7506806654885756938","conversation_id":"7506806371942301737","role":"assistant","type":"answer","content":"{\"card_type\":3,\"content_type\":50,\"data\":\"{\\\"elements\\\":{\\\"1AsSz8ZKfU\\\":{\\\"id\\\":\\\"1AsSz8ZKfU\\\",\\\"name\\\":\\\"FlowpdCiciComponentsColumnLayout\\\",\\\"type\\\":\\\"@flowpd/cici-components/ColumnLayout\\\",\\\"props\\\":{\\\"Columns\\\":[{\\\"children\\\":[\\\"eY8Fbk67Ce\\\"],\\\"config\\\":{\\\"columnElementGap\\\":8,\\\"columnElementPadding\\\":0,\\\"vertical\\\":\\\"top\\\",\\\"weight\\\":1,\\\"width\\\":\\\"weighted\\\"},\\\"type\\\":\\\"slot\\\"}],\\\"action\\\":\\\"enableUrl\\\",\\\"backgroundColor\\\":\\\"transparent\\\",\\\"enableClickEvent\\\":false,\\\"layoutColumnGap\\\":4,\\\"layoutPaddingGap\\\":0},\\\"directives\\\":{}},\\\"FtgVj9otIh\\\":{\\\"id\\\":\\\"FtgVj9otIh\\\",\\\"name\\\":\\\"FlowpdCiciComponentsText\\\",\\\"type\\\":\\\"@flowpd/cici-components/Text\\\",\\\"props\\\":{\\\"action\\\":\\\"enableUrl\\\",\\\"color\\\":\\\"neutral-100\\\",\\\"content\\\":{\\\"type\\\":\\\"expression\\\",\\\"value\\\":\\\"{{AzTmd88pyB.description}}\\\"},\\\"enableClickEvent\\\":false,\\\"enableLines\\\":false,\\\"enableMaxWidth\\\":false,\\\"fixedRowHeightLines\\\":1,\\\"fontSize\\\":\\\"large\\\",\\\"fontWeight\\\":\\\"normal\\\",\\\"lines\\\":1,\\\"maxWidth\\\":424,\\\"positionStyle\\\":{\\\"type\\\":\\\"default\\\"},\\\"textAlign\\\":\\\"left\\\"},\\\"directives\\\":{}},\\\"eY8Fbk67Ce\\\":{\\\"id\\\":\\\"eY8Fbk67Ce\\\",\\\"name\\\":\\\"FlowpdCiciComponentsButton\\\",\\\"type\\\":\\\"@flowpd/cici-components/Button\\\",\\\"props\\\":{\\\"actionType\\\":\\\"enableMessage\\\",\\\"content\\\":{\\\"type\\\":\\\"expression\\\",\\\"value\\\":\\\"{{item.key}}\\\"},\\\"enableLines\\\":true,\\\"lines\\\":6,\\\"message\\\":{\\\"type\\\":\\\"expression\\\",\\\"value\\\":\\\"{{item.key}}\\\"},\\\"positionStyle\\\":{\\\"type\\\":\\\"default\\\"},\\\"size\\\":\\\"small\\\",\\\"textAlign\\\":\\\"left\\\",\\\"type\\\":\\\"secondary\\\",\\\"width\\\":\\\"fill\\\"},\\\"directives\\\":{\\\"repeat\\\":{\\\"replaceMap\\\":{\\\"AzTmd88pyB\\\":\\\"data\\\"},\\\"type\\\":\\\"expression\\\",\\\"value\\\":\\\"{{AzTmd88pyB.list}}\\\"}}},\\\"root\\\":{\\\"id\\\":\\\"root\\\",\\\"name\\\":\\\"Root\\\",\\\"type\\\":\\\"@flowpd/cici-components/PageContainer\\\",\\\"props\\\":{\\\"backgroundColor\\\":\\\"grey\\\",\\\"containerPadding\\\":16,\\\"containerRowGap\\\":12},\\\"children\\\":[\\\"FtgVj9otIh\\\",\\\"1AsSz8ZKfU\\\"],\\\"directives\\\":{}}},\\\"rootID\\\":\\\"root\\\",\\\"variables\\\":{\\\"AzTmd88pyB\\\":{\\\"ID\\\":\\\"AzTmd88pyB\\\",\\\"name\\\":\\\"data\\\",\\\"defaultValue\\\":{\\\"description\\\":\\\"请选择要打开的门锁:\\\",\\\"list\\\":[{\\\"key\\\":\\\"1101前门\\\"},{\\\"key\\\":\\\"1101后门\\\"}]}}}}\",\"response_type\":\"card\",\"template_id\":7383997384420261899,\"template_url\":\"aweme://lynxview/?need_reload=0&surl=https%3A%2F%2Flf-sourcecdn-tos.bytegecko.com%2Fobj%2Fbyte-gurd-source%2Fcard%2Flynx%2Fruntime%2Fcard_lynx_runtime%2Ftemplate.js\",\"x_properties\":{\"question_readonly_afterclick\":\"true\",\"workflow_card_info\":\"{\\\"card_type\\\":\\\"QUESTION\\\",\\\"input_card_data\\\":null,\\\"question_card_data\\\":{\\\"Title\\\":\\\"请选择要打开的门锁:\\\",\\\"Options\\\":[{\\\"name\\\":\\\"1101前门\\\"},{\\\"name\\\":\\\"1101后门\\\"}]}}\"}}","content_type":"card","chat_id":"7506806634484744230","section_id":"7506806371942301737","created_at":1747814626,"updated_at":1747814626}
event: conversation.message.completed
data: {"id":"7506806654885773322","conversation_id":"7506806371942301737","role":"assistant","type":"verbose","content":"{\"msg_type\":\"interrupt\",\"data\":\"\",\"from_module\":null,\"from_unit\":null}","content_type":"text","chat_id":"7506806634484744230","section_id":"7506806371942301737","created_at":1747814626,"updated_at":1747814626}
event: conversation.message.completed
data: {"id":"7506806654885806090","conversation_id":"7506806371942301737","role":"assistant","type":"verbose","content":"{\"msg_type\":\"generate_answer_finish\",\"data\":\"{\\\"finish_reason\\\":1,\\\"FinData\\\":\\\"\\\"}\",\"from_module\":null,\"from_unit\":null}","content_type":"text","chat_id":"7506806634484744230","section_id":"7506806371942301737","created_at":1747814626,"updated_at":1747814626}
event: conversation.chat.requires_action
data: {"id":"7506806634484744230","conversation_id":"7506806371942301737","created_at":1747814621,"completed_at":1747814626,"last_error":{"code":0,"msg":""},"status":"requires_action","usage":{"token_count":0,"output_count":0,"input_count":0},"required_action":{"type":"submit_tool_outputs","submit_tool_outputs":{"tool_calls":[{"id":"","type":"reply_message","function":null,"require_info":null}]}},"section_id":"7506806371942301737","execute_id":"7506806633684779017","inserted_additional_messages":[{"id":"7506806606261403657"}]}
event: done
data: {"debug_url":"https://www.coze.cn/work_flow?execute_id=7506806633684779017&space_id=7402807793096359947&workflow_id=7505969113844613147&execute_mode=2"}
In [20]:
req = post(
    f"{ip}/v1/workflows/chat",
    json={
        "additional_messages": [
            {"role": "user", "content": "打开1101门锁", "content_type": "text"}
        ],
        "workflow_id": "7506108197824987190",
        "execute_mode":"DEBUG",
        "parameters": {"token": "ebd37b76-a2f7-4657-895e-a1170d5a86cf"},
        "bot_id": "7484071067645460531",
        "ext":{"_caller":"CANVAS","user_id":"288532740445212"}
    },
    headers=headers,
    stream=True,
)
try:
    for line in req.iter_lines():
        if line:
            print(line.decode("utf-8"))  # 处理每一行数据
finally:
    req.close()
event: conversation.chat.created
data: {"id":"7506807717307203647","conversation_id":"7506807701910077479","created_at":1747814873,"last_error":{"code":0,"msg":""},"status":"created","usage":{"token_count":0,"output_count":0,"input_count":0},"section_id":"7506807701910077479","execute_id":"7506807711906005033","inserted_additional_messages":[{"id":"7506807711254724647"}]}
event: conversation.chat.in_progress
data: {"id":"7506807717307203647","conversation_id":"7506807701910077479","created_at":1747814873,"last_error":{"code":0,"msg":""},"status":"in_progress","usage":{"token_count":0,"output_count":0,"input_count":0},"section_id":"7506807701910077479","execute_id":"7506807711906005033","inserted_additional_messages":[{"id":"7506807711254724647"}]}
event: conversation.message.delta
data: {"id":"7506807736336662591","conversation_id":"7506807701910077479","role":"assistant","type":"answer","content":"请选择要打开的门锁:\n- 1101前门\n- 1101后门\n","content_type":"text","chat_id":"7506807717307203647","section_id":"7506807701910077479","created_at":1747814877}
event: conversation.message.completed
data: {"id":"7506807736336662591","conversation_id":"7506807701910077479","role":"assistant","type":"answer","content":"请选择要打开的门锁:\n- 1101前门\n- 1101后门\n","content_type":"text","chat_id":"7506807717307203647","section_id":"7506807701910077479","created_at":1747814877}
event: conversation.message.completed
data: {"id":"7506807736336678975","conversation_id":"7506807701910077479","role":"assistant","type":"verbose","content":"{\"msg_type\":\"interrupt\",\"data\":\"\",\"from_module\":null,\"from_unit\":null}","content_type":"text","chat_id":"7506807717307203647","section_id":"7506807701910077479","created_at":1747814877,"updated_at":1747814877}
event: conversation.message.completed
data: {"id":"7506807736336711743","conversation_id":"7506807701910077479","role":"assistant","type":"verbose","content":"{\"msg_type\":\"generate_answer_finish\",\"data\":\"{\\\"finish_reason\\\":1,\\\"FinData\\\":\\\"\\\"}\",\"from_module\":null,\"from_unit\":null}","content_type":"text","chat_id":"7506807717307203647","section_id":"7506807701910077479","created_at":1747814877,"updated_at":1747814877}
event: conversation.chat.requires_action
data: {"id":"7506807717307203647","conversation_id":"7506807701910077479","created_at":1747814873,"completed_at":1747814877,"last_error":{"code":0,"msg":""},"status":"requires_action","usage":{"token_count":0,"output_count":0,"input_count":0},"required_action":{"type":"submit_tool_outputs","submit_tool_outputs":{"tool_calls":[{"id":"","type":"reply_message","function":null,"require_info":null}]}},"section_id":"7506807701910077479","execute_id":"7506807711906005033","inserted_additional_messages":[{"id":"7506807711254724647"}]}
event: done
data: {"debug_url":"https://www.coze.cn/work_flow?execute_id=7506807711906005033&space_id=7402807793096359947&workflow_id=7506108197824987190&execute_mode=2"}
In [10]:
req = post(
    f"{ip}/v3/chat",
    json={"bot_id":"7484071067645198387","user_id":"邱宇航","stream":True,"additional_messages":[{"content":"打开1101门锁","content_type":"text","role":"user","type":"question"}],"parameters":{"role":"教师","userId":537,"token":"ebd37b76-a2f7-4657-895e-a1170d5a86cf","teamId":0}},
    headers=headers,
    stream=True,
)
try:
    for line in req.iter_lines():
        if line:
            print(line.decode("utf-8"))  # 处理每一行数据
finally:
    req.close()
event:conversation.chat.created
data:{"id":"7506803685134172194","conversation_id":"7506803685134155810","bot_id":"7484071067645198387","created_at":1747813934,"last_error":{"code":0,"msg":""},"status":"created","usage":{"token_count":0,"output_count":0,"input_count":0},"section_id":"7506803685134155810"}
event:conversation.chat.in_progress
data:{"id":"7506803685134172194","conversation_id":"7506803685134155810","bot_id":"7484071067645198387","created_at":1747813934,"last_error":{"code":0,"msg":""},"status":"in_progress","usage":{"token_count":0,"output_count":0,"input_count":0},"section_id":"7506803685134155810"}
event:conversation.message.delta
data:{"id":"7506803791748923392","conversation_id":"7506803685134155810","bot_id":"7484071067645198387","role":"assistant","type":"answer","content":"请选择要打开的门锁:\n- 1101前门\n- 1101后门\n","content_type":"text","chat_id":"7506803685134172194","section_id":"7506803685134155810","created_at":1747813959}
event:conversation.message.completed
data:{"id":"7506803791748923392","conversation_id":"7506803685134155810","bot_id":"7484071067645198387","role":"assistant","type":"answer","content":"请选择要打开的门锁:\n- 1101前门\n- 1101后门\n","content_type":"text","chat_id":"7506803685134172194","section_id":"7506803685134155810","created_at":1747813959}
event:conversation.message.completed
data:{"id":"7506803791748972544","conversation_id":"7506803685134155810","bot_id":"7484071067645198387","role":"assistant","type":"verbose","content":"{\"msg_type\":\"interrupt\",\"data\":\"\",\"from_module\":null,\"from_unit\":null}","content_type":"text","chat_id":"7506803685134172194","section_id":"7506803685134155810","created_at":1747813959,"updated_at":1747813959}
event:conversation.message.completed
data:{"id":"7506803791748988928","conversation_id":"7506803685134155810","bot_id":"7484071067645198387","role":"assistant","type":"verbose","content":"{\"msg_type\":\"generate_answer_finish\",\"data\":\"{\\\"finish_reason\\\":1,\\\"FinData\\\":\\\"\\\"}\",\"from_module\":null,\"from_unit\":null}","content_type":"text","chat_id":"7506803685134172194","section_id":"7506803685134155810","created_at":1747813959,"updated_at":1747813959}
event:conversation.chat.requires_action
data:{"id":"7506803685134172194","conversation_id":"7506803685134155810","bot_id":"7484071067645198387","created_at":1747813934,"completed_at":1747813959,"last_error":{"code":0,"msg":""},"status":"requires_action","usage":{"token_count":0,"output_count":0,"input_count":0},"required_action":{"type":"submit_tool_outputs","submit_tool_outputs":{"tool_calls":[{"id":"","type":"reply_message","function":null,"require_info":null}]}},"section_id":"7506803685134155810"}
event:done
data:"[DONE]"
In [ ]:
import json
In [ ]:
json.loads(data["data"][0]["content"])
In [ ]:

import base64
 
def encode_file_to_base64(file_path):
    with open(file_path, "rb") as file:
        encoded_bytes = base64.b64encode(file.read())
        encoded_string = encoded_bytes.decode("utf-8")
    return encoded_string
In [ ]:
data = post(
    "https://api.coze.cn/open_api/knowledge/document/create",
    headers=headers,
    data={
  "document_bases": [
    {
      "source_info": {
        "file_base64": "5rWL6K+V5LiA5LiL5ZOm",
        "document_source": 0,
        "file_type": "pdf",
        "source_file_id": 7491624184251531000
      },
      "name": "Coze.pdf",
      "update_rule": {
        "update_type": 0
      }
    }
  ],
  "chunk_strategy": {
    "caption_type": 0,
    "chunk_type": 0,
    "max_tokens": 1000,
    "remove_extra_spaces": True,
    "remove_urls_emails": False
  },
  "dataset_id": "7491611456598114316",
  "format_type": 0
},
)
print(data.text)
In [6]:
import json
In [ ]:
data = post(f"https://api.coze.cn/open_api/knowledge/document/delete",json={"document_ids":["7567325098534502435"]},headers=headers)
print(data.text)
{"code":700012006,"msg":"cannot get access token from Authorization header"}
In [ ]: