Files
python----/智慧校园/智能客服.ipynb
T
2025-10-20 12:32:18 +08:00

265 lines
20 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from requests import get,post"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"headers = {\n",
" \"authorization\":\"Bearer czs_h7L9OSgejkWbzwau7RfDzO35tWFNnXA3URoe1xtz1WYhyvfxDWuUTDhUGHPREjBxn\",\n",
" \"content-type\":\"application/json\"\n",
"}\n",
"ip = \"https://api.coze.cn\""
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"data = get(f\"{ip}/v1/conversation/message/list?conversation_id=7483161006881980479\",json={\"limit\":20},headers=headers).json()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"event: conversation.chat.created\n",
"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\"}]}\n",
"event: conversation.chat.in_progress\n",
"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\"}]}\n",
"event: conversation.message.completed\n",
"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}\n",
"event: conversation.message.completed\n",
"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}\n",
"event: conversation.message.completed\n",
"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}\n",
"event: conversation.chat.requires_action\n",
"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\"}]}\n",
"event: done\n",
"data: {\"debug_url\":\"https://www.coze.cn/work_flow?execute_id=7506806633684779017&space_id=7402807793096359947&workflow_id=7505969113844613147&execute_mode=2\"}\n"
]
}
],
"source": [
"req = post(\n",
" f\"{ip}/v1/workflows/chat\",\n",
" 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\":{}},\n",
" headers=headers,\n",
" stream=True,\n",
")\n",
"try:\n",
" for line in req.iter_lines():\n",
" if line:\n",
" print(line.decode(\"utf-8\")) # 处理每一行数据\n",
"finally:\n",
" req.close()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"event: conversation.chat.created\n",
"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\"}]}\n",
"event: conversation.chat.in_progress\n",
"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\"}]}\n",
"event: conversation.message.delta\n",
"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}\n",
"event: conversation.message.completed\n",
"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}\n",
"event: conversation.message.completed\n",
"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}\n",
"event: conversation.message.completed\n",
"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}\n",
"event: conversation.chat.requires_action\n",
"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\"}]}\n",
"event: done\n",
"data: {\"debug_url\":\"https://www.coze.cn/work_flow?execute_id=7506807711906005033&space_id=7402807793096359947&workflow_id=7506108197824987190&execute_mode=2\"}\n"
]
}
],
"source": [
"req = post(\n",
" f\"{ip}/v1/workflows/chat\",\n",
" json={\n",
" \"additional_messages\": [\n",
" {\"role\": \"user\", \"content\": \"打开1101门锁\", \"content_type\": \"text\"}\n",
" ],\n",
" \"workflow_id\": \"7506108197824987190\",\n",
" \"execute_mode\":\"DEBUG\",\n",
" \"parameters\": {\"token\": \"ebd37b76-a2f7-4657-895e-a1170d5a86cf\"},\n",
" \"bot_id\": \"7484071067645460531\",\n",
" \"ext\":{\"_caller\":\"CANVAS\",\"user_id\":\"288532740445212\"}\n",
" },\n",
" headers=headers,\n",
" stream=True,\n",
")\n",
"try:\n",
" for line in req.iter_lines():\n",
" if line:\n",
" print(line.decode(\"utf-8\")) # 处理每一行数据\n",
"finally:\n",
" req.close()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"event:conversation.chat.created\n",
"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\"}\n",
"event:conversation.chat.in_progress\n",
"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\"}\n",
"event:conversation.message.delta\n",
"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}\n",
"event:conversation.message.completed\n",
"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}\n",
"event:conversation.message.completed\n",
"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}\n",
"event:conversation.message.completed\n",
"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}\n",
"event:conversation.chat.requires_action\n",
"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\"}\n",
"event:done\n",
"data:\"[DONE]\"\n"
]
}
],
"source": [
"req = post(\n",
" f\"{ip}/v3/chat\",\n",
" 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}},\n",
" headers=headers,\n",
" stream=True,\n",
")\n",
"try:\n",
" for line in req.iter_lines():\n",
" if line:\n",
" print(line.decode(\"utf-8\")) # 处理每一行数据\n",
"finally:\n",
" req.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"json.loads(data[\"data\"][0][\"content\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"import base64\n",
" \n",
"def encode_file_to_base64(file_path):\n",
" with open(file_path, \"rb\") as file:\n",
" encoded_bytes = base64.b64encode(file.read())\n",
" encoded_string = encoded_bytes.decode(\"utf-8\")\n",
" return encoded_string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = post(\n",
" \"https://api.coze.cn/open_api/knowledge/document/create\",\n",
" headers=headers,\n",
" data={\n",
" \"document_bases\": [\n",
" {\n",
" \"source_info\": {\n",
" \"file_base64\": \"5rWL6K+V5LiA5LiL5ZOm\",\n",
" \"document_source\": 0,\n",
" \"file_type\": \"pdf\",\n",
" \"source_file_id\": 7491624184251531000\n",
" },\n",
" \"name\": \"Coze.pdf\",\n",
" \"update_rule\": {\n",
" \"update_type\": 0\n",
" }\n",
" }\n",
" ],\n",
" \"chunk_strategy\": {\n",
" \"caption_type\": 0,\n",
" \"chunk_type\": 0,\n",
" \"max_tokens\": 1000,\n",
" \"remove_extra_spaces\": True,\n",
" \"remove_urls_emails\": False\n",
" },\n",
" \"dataset_id\": \"7491611456598114316\",\n",
" \"format_type\": 0\n",
"},\n",
")\n",
"print(data.text)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}