Skip to content

A programming framework for agentic AI

docs: enhance human-in-the-loop tutorial with FastAPI websocket example (#5455)

Eric Zhu15891e8cef18
diff --git a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/human-in-the-loop.ipynb b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/human-in-the-loop.ipynb
index a7c0121d2..bcabe6958 100644
--- a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/human-in-the-loop.ipynb
+++ b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/human-in-the-loop.ipynb
@@ -129,7 +129,28 @@
"through `user_proxy` to approve the generated poem.\n",
"\n",
"You can provide your own input function to the {py:class}`~autogen_agentchat.agents.UserProxyAgent`\n",
- "to customize the feedback process."
+ "to customize the feedback process.\n",
+ "For example, when the team is running as a web service, you can use a custom\n",
+ "input function to wait for message from a web socket connection.\n",
+ "The following code snippet shows an example of custom input function\n",
+ "when using the [FastAPI](https://fastapi.tiangolo.com/) web framework:\n",
+ "\n",
+ "```python\n",
+ "@app.websocket(\"/ws/chat\")\n",
+ "async def chat(websocket: WebSocket):\n",
+ " await websocket.accept()\n",
+ "\n",
+ " async def _user_input(prompt: str, cancellation_token: CancellationToken | None) -> str:\n",
+ " data = await websocket.receive_json() # Wait for user message from websocket.\n",
+ " message = TextMessage.model_validate(data) # Assume user message is a TextMessage.\n",
+ " return message.content\n",
+ " \n",
+ " # Create user proxy with custom input function\n",
+ " # Run the team with the user proxy\n",
+ " # ...\n",
+ "```\n",
+ "\n",
+ "See the [AgentChat FastAPI sample](https://github.com/microsoft/autogen/blob/main/python/samples/agentchat_fastapi) for a complete example."
]
},
{