1. 发布APICoze的API功能需要通过应用发布功能启用。2. 调试2.1 入口发布成功后在工作流画布页面可以看到API调试入口2.2 查看工作流ID和应用ID通过上述入口进入API playground选择右侧的Shell请求可以看到工作流IDworkflow_id应用IDapp_id2.3 授权API key左侧窗口下滑可以看到token这就是API Key点击授权点击“授权”后自动生成并填充 API Key右侧的Shell命令窗口同步更新2.4 添加参数parameters属性下的JSON用于向工作流传递参数。该JSON对象的每个属性对应一个工作流的输入变量2.5 运行可以直接点击Shell命令窗口右上角的“运行”按钮2.6 完整命令curl-XPOSThttps://api.coze.cn/v3/chat?\-HAuthorization: xxxxxx\-HContent-Type: application/json\-d{ bot_id: 7669363834150862858, user_id: 123456789, stream: true, additional_messages: [ { content: hello, content_type: text, role: user, type: question } ], parameters: { input: 你是 } }2.7 运行结果右下角可以看到运行结果200表示通信正常PING是用于维持通信连接的心跳响应Message是携带数据的响应此处的content是工作流的输出。Done是最后一个响应表示工作流执行完毕在Message响应之后。3.通过python代码调用工作流3.1 官方提供了调用API的Python代码3.2 源码如下 This example is about how to use the streaming interface to start a chat request and handle chat events importos# Our official coze sdk for Python [cozepy](https://github.com/coze-dev/coze-py)fromcozepyimportCOZE_CN_BASE_URL# Get an access_token through personal access token or oauth.coze_api_tokencztei_qwL8CVSDyZxxxxxJxxxx7mLZ5uQA3r5vI# The default access is api.coze.com, but if you need to access api.coze.cn,# please use base_url to configure the api endpoint to accesscoze_api_baseCOZE_CN_BASE_URLfromcozepyimportCoze,TokenAuth,Message,ChatStatus,MessageContentType,ChatEventType# noqa# Init the Coze client through the access_token.cozeCoze(authTokenAuth(tokencoze_api_token),base_urlcoze_api_base)# Create a bot instance in Coze, copy the last number from the web link as the bots ID.bot_id7669363834150862858# The user id identifies the identity of a user. Developers can use a custom business ID# or a random string.user_id123456789# Call the coze.chat.stream method to create a chat. The create method is a streaming# chat and will return a Chat Iterator. Developers should iterate the iterator to get# chat event and handle them.foreventincoze.chat.stream(bot_idbot_id,user_iduser_id,additional_messages[Message.build_user_question_text(Tell a 500-word story.),],):ifevent.eventChatEventType.CONVERSATION_MESSAGE_DELTA:print(event.message.content,end,flushTrue)ifevent.eventChatEventType.CONVERSATION_CHAT_COMPLETED:print()print(token usage:,event.chat.usage.token_count)将源码粘贴到PyCharm。3.3 在本地PyCharm中运行需要安装cozepypipinstallcozepy代码粘贴后运行3.4 优化代码完善代码具备交互功能: This example is about how to use the streaming interface to start a chat request and handle chat events, with interactive multi-turn conversation support. importosfromcozepyimportCOZE_CN_BASE_URL coze_api_tokencztei_qZqYvHQ411YIrxxxxxxxxxxxxxxxKIacoze_api_baseCOZE_CN_BASE_URLfromcozepyimportCoze,TokenAuth,Message,ChatStatus,MessageContentType,ChatEventType# noqacozeCoze(authTokenAuth(tokencoze_api_token),base_urlcoze_api_base)bot_id7669363834150862858user_id123456789conversation_idNoneprint( Coze 智能助手 )print(输入消息开始对话输入 exit 或 退出 结束程序\n)whileTrue:user_inputinput(你: ).strip()ifnotuser_input:continueifuser_input.lower()in(exit,quit,退出,q):print(再见对话已结束。)breaktry:chat_params{bot_id:bot_id,user_id:user_id,additional_messages:[Message.build_user_question_text(user_input),],}ifconversation_id:chat_params[conversation_id]conversation_idprint(助手: ,end,flushTrue)foreventincoze.chat.stream(**chat_params):ifevent.eventChatEventType.CONVERSATION_MESSAGE_DELTA:print(event.message.content,end,flushTrue)ifevent.eventChatEventType.CONVERSATION_CHAT_COMPLETED:conversation_idevent.chat.conversation_idprint()print(f[token usage:{event.chat.usage.token_count}])exceptExceptionase:print(f\n发生错误:{e})运行结果