diff --git a/cookbooks/end2end_application/agent/chatflow.ipynb b/cookbooks/end2end_application/agent/chatflow.ipynb index 5b054a24..c0d022d3 100644 --- a/cookbooks/end2end_application/agent/chatflow.ipynb +++ b/cookbooks/end2end_application/agent/chatflow.ipynb @@ -57,7 +57,7 @@ "

\n", "\n", "## 2.3 使用AppBuilder-SDK进行对话\n", - "### 2.3.1 方式一:使用SDK直接对话" + "### 2.3.1 方式一:使用SDK直接对话(不推荐)" ] }, { @@ -146,7 +146,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### 2.3.2 方式二 实现自己的EventHandler,更方便地进行对话\n", + "### 2.3.2 方式二 实现自己的EventHandler,更方便地进行对话(推荐)\n", "直接调用处理较为繁琐。SDK提供了使用AppBuilderEventHandler简化tool_call操作的功能。**继承AppBuilderEventHandler类,并实现针对各类型event的处理方法。**" ] }, @@ -238,7 +238,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### 2.3.3 方式三、实现自己的EventHandler,更方便地进行多轮对话\n", + "### 2.3.3 方式三、实现自己的EventHandler,更方便地进行多轮对话(推荐)\n", "SDK提供了run_multiple_dialog_with_handler方法来进行多轮对话,按需传入iterable对象(queries、actions等参数),即可一次调用完成多轮对话。" ] }, diff --git a/docs/BasisModule/Platform/Application/appbuilder_client.md b/docs/BasisModule/Platform/Application/appbuilder_client.md index 126324bc..f562fc71 100644 --- a/docs/BasisModule/Platform/Application/appbuilder_client.md +++ b/docs/BasisModule/Platform/Application/appbuilder_client.md @@ -450,7 +450,9 @@ answer = app_builder_client.run( #### Run方法回复工作流Agent “信息收集节点”使用示例: -使用[“飞行客服小助手”](https://cloud.baidu.com/doc/AppBuilder/s/cm38k8nqr)作为工作流Agent的示例应用 +使用[“飞行客服小助手”](https://cloud.baidu.com/doc/AppBuilder/s/cm38k8nqr)作为工作流Agent的示例应用。 + +**方式1:** SDK直接进行对话(不推荐) ```python import appbuilder @@ -532,6 +534,154 @@ for ans in msg4.content: ``` +**方式2: **实现自己的EventHandler,更方便地进行对话(推荐) + +```python +import os +import appbuilder +from appbuilder.core.console.appbuilder_client.event_handler import ( + AppBuilderEventHandler, +) + + +class MyEventHandler(AppBuilderEventHandler): + def __init__(self): + super().__init__() + self.interrupt_ids = [] + + def handle_content_type(self, run_context, run_response): + interrupt_event_id = None + event = run_response.events[-1] + if event.content_type == "chatflow_interrupt": + interrupt_event_id = event.detail.get("interrupt_event_id") + if interrupt_event_id is not None: + self.interrupt_ids.append(interrupt_event_id) + + def _create_action(self): + if len(self.interrupt_ids) == 0: + return None + event_id = self.interrupt_ids.pop() + return { + "action_type": "resume", + "parameters": {"interrupt_event": {"id": event_id, "type": "chat"}}, + } + + def run(self, query=None): + super().new_dialog( + query=query, + action=self._create_action(), + ) + + +def main(): + # 请前往千帆AppBuilder官网创建密钥,流程详见:https://cloud.baidu.com/doc/AppBuilder/s/Olq6grrt6#1%E3%80%81%E5%88%9B%E5%BB%BA%E5%AF%86%E9%92%A5 + # 设置环境变量 + os.environ["APPBUILDER_TOKEN"] = "..." + appbuilder.logger.setLoglevel("DEBUG") + + # 飞行客服小助手的应用id + app_id = "..." + # 初始化智能体 + client = appbuilder.AppBuilderClient(app_id) + conversation_id = client.create_conversation() + + event_handler = MyEventHandler() + event_handler.init( + appbuilder_client=client, + conversation_id=conversation_id, + stream=True, + query="查天气", + ) + for data in event_handler: + pass + event_handler.run( + query="查航班", + ) + for data in event_handler: + pass + event_handler.run( + query="CA1234", + ) + for data in event_handler: + pass + event_handler.run( + query="北京的", + ) + for data in event_handler: + pass + + +if __name__ == "__main__": + main() +``` + +**方式3:** 实现自己的EventHandler,更方便地进行多轮对话(推荐) + +```python +import os +import appbuilder +from appbuilder.core.console.appbuilder_client.event_handler import ( + AppBuilderEventHandler, +) + + +class MyEventHandler(AppBuilderEventHandler): + def __init__(self): + super().__init__() + self.interrupt_ids = [] + + def handle_content_type(self, run_context, run_response): + interrupt_event_id = None + event = run_response.events[-1] + if event.content_type == "chatflow_interrupt": + interrupt_event_id = event.detail.get("interrupt_event_id") + if interrupt_event_id is not None: + self.interrupt_ids.append(interrupt_event_id) + + def _create_action(self): + if len(self.interrupt_ids) == 0: + return None + event_id = self.interrupt_ids.pop() + return { + "action_type": "resume", + "parameters": {"interrupt_event": {"id": event_id, "type": "chat"}}, + } + + def gen_action(self): + while True: + yield self._create_action() + + +def main(): + # 请前往千帆AppBuilder官网创建密钥,流程详见:https://cloud.baidu.com/doc/AppBuilder/s/Olq6grrt6#1%E3%80%81%E5%88%9B%E5%BB%BA%E5%AF%86%E9%92%A5 + # 设置环境变量 + os.environ["APPBUILDER_TOKEN"] = "..." + appbuilder.logger.setLoglevel("DEBUG") + + # 飞行客服小助手的应用id + app_id = "..." + # 初始化智能体 + client = appbuilder.AppBuilderClient(app_id) + conversation_id = client.create_conversation() + + queries = ["查天气", "查航班", "CA1234", "北京的"] + event_handler = MyEventHandler() + event_handler = client.run_multiple_dialog_with_handler( + conversation_id=conversation_id, + queries=queries, + event_handler=event_handler, + stream=True, + actions=event_handler.gen_action(), + ) + for data in event_handler: + for ans in data: + pass + + +if __name__ == "__main__": + main() +``` + ## Java基本用法