-
Notifications
You must be signed in to change notification settings - Fork 1k
feature: add OpenAI ChatKit UI integration #478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bonk1t
wants to merge
2
commits into
main
Choose a base branch
from
feat/chatkit-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| --- | ||
| title: "ChatKit Integration" | ||
| description: "Add a chat UI to your agency with OpenAI ChatKit." | ||
| icon: "comments" | ||
| --- | ||
|
|
||
| [OpenAI ChatKit](https://platform.openai.com/docs/guides/chatkit) is a React-based chat UI. Agency Swarm includes a ready-to-use demo. | ||
|
|
||
| <Warning> | ||
| Requires [Node.js](https://nodejs.org/) v18+ and npm. | ||
| </Warning> | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```python | ||
| from agency_swarm import Agency, Agent | ||
|
|
||
| agent = Agent(name="Assistant", instructions="You are helpful.") | ||
| agency = Agency(agent, name="my_agency") | ||
|
|
||
| agency.chatkit_demo() # Opens browser at http://localhost:3000 | ||
| ``` | ||
|
|
||
| <Accordion title="Parameters"> | ||
| ```python | ||
| agency.chatkit_demo( | ||
| host="0.0.0.0", # Backend host | ||
| port=8000, # Backend port | ||
| frontend_port=3000, # Frontend port | ||
| cors_origins=None, # CORS origins list | ||
| open_browser=True, # Auto-open browser | ||
| ) | ||
| ``` | ||
| </Accordion> | ||
|
|
||
| --- | ||
|
|
||
| ## Backend Only | ||
|
|
||
| Use `enable_chatkit=True` to expose the ChatKit endpoint without the demo frontend: | ||
|
|
||
| ```python | ||
| from agency_swarm import Agency, Agent, run_fastapi | ||
|
|
||
| def create_agency(**kwargs): | ||
| agent = Agent(name="Assistant", instructions="You are helpful.") | ||
| return Agency(agent, name="my_agency") | ||
|
|
||
| run_fastapi(agencies={"my_agency": create_agency}, enable_chatkit=True) | ||
| ``` | ||
|
|
||
| This exposes `/{agency_name}/chatkit` for your own ChatKit frontend. | ||
|
|
||
| <Accordion title="Connecting Your Frontend"> | ||
|
|
||
| Point your ChatKit React app to the backend: | ||
|
|
||
| ```typescript | ||
| const chatkit = useChatKit({ | ||
| api: { url: "http://localhost:8000/my_agency/chatkit" }, | ||
| }); | ||
| ``` | ||
|
|
||
| Or use a Vite proxy: | ||
|
|
||
| ```typescript | ||
| // vite.config.ts | ||
| export default defineConfig({ | ||
| server: { | ||
| proxy: { | ||
| "/chatkit": { | ||
| target: "http://localhost:8000", | ||
| rewrite: (path) => `/my_agency${path}`, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| ``` | ||
| </Accordion> | ||
|
|
||
| --- | ||
|
|
||
| ## Persistence | ||
|
|
||
| By default, ChatKit is stateless. For conversation persistence, pass custom `RunHooks` via `hooks_override` parameter in `get_response` or `get_response_stream`: | ||
|
|
||
| ```python | ||
| from agents import RunHooks | ||
|
|
||
| class MyPersistenceHooks(RunHooks): | ||
| async def on_agent_end(self, context, agent, output): | ||
| messages = context.context.thread_manager.get_all_messages() | ||
| db.save(thread_id, messages) # Save to your database | ||
|
|
||
| result = await agency.get_response( | ||
| message=user_message, | ||
| hooks_override=MyPersistenceHooks(), | ||
| ) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """ | ||
| Agency Swarm ChatKit Demo | ||
|
|
||
| This example demonstrates the ChatKit UI capabilities of Agency Swarm v1.x. | ||
| Sets up a frontend and backend server for the OpenAI ChatKit UI chat demo. | ||
| """ | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| # Add the src directory to the path so we can import agency_swarm | ||
| sys.path.insert(0, str(Path(__file__).parent.parent / "src")) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example file has incorrect module path calculationMedium Severity The path |
||
|
|
||
| from agency_swarm import Agency, Agent, RunContextWrapper, function_tool | ||
|
|
||
|
|
||
| @function_tool() | ||
| async def example_tool(wrapper: RunContextWrapper) -> str: | ||
| """Example tool for chatkit demo""" | ||
| return "Example tool executed" | ||
|
|
||
|
|
||
| def create_demo_agency(): | ||
| """Create a demo agency for chatkit demo""" | ||
|
|
||
| # Create agents using v1.x pattern (direct instantiation) | ||
| ceo = Agent( | ||
| name="CEO", | ||
| description="Chief Executive Officer - oversees all operations", | ||
| instructions="You are the CEO responsible for high-level decision making and coordination.", | ||
| tools=[example_tool], | ||
| ) | ||
|
|
||
| worker = Agent( | ||
| name="Worker", | ||
| description="Worker - performs tasks", | ||
| instructions="Follow instructions given by the CEO.", | ||
| tools=[example_tool], | ||
| ) | ||
|
|
||
| # Create agency with communication flows (v1.x pattern) | ||
| agency = Agency( | ||
| ceo, # Entry point agent (positional argument) | ||
| communication_flows=[ceo > worker], | ||
| name="ChatKitDemoAgency", | ||
| ) | ||
|
|
||
| return agency | ||
|
|
||
|
|
||
| def main(): | ||
| """Launch interactive ChatKit demo""" | ||
| print("Agency Swarm ChatKit Demo") | ||
| print("=" * 50) | ||
| print() | ||
|
|
||
| try: | ||
| agency = create_demo_agency() | ||
| # Launch the ChatKit UI demo with backend and frontend servers. | ||
| agency.chatkit_demo() | ||
|
|
||
| except Exception as e: | ||
| print(f"❌ Demo failed with error: {e}") | ||
| import traceback | ||
|
|
||
| traceback.print_exc() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
examples/interactive/chatkit_demo.pylacks an opening triple‑quote before the intended module docstring, leaving bare words (Agency Swarm ChatKit Demo) at the top level. Importing or running the demo raises aSyntaxErrorbefore any code executes, so the documented ChatKit demo cannot be launched.Useful? React with 👍 / 👎.