Skip to content

Commit 2170f41

Browse files
committed
use AG-UI's LangGraph FastAPI integration
1 parent 56e70ba commit 2170f41

File tree

8 files changed

+2016
-9
lines changed

8 files changed

+2016
-9
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This is a starter template for building AI agents using [LangGraph](https://www.
66

77
- Node.js 18+
88
- Python 3.8+
9+
- Poetry 2+
910
- Any of the following package managers:
1011
- [pnpm](https://pnpm.io/installation) (recommended)
1112
- npm
@@ -114,5 +115,5 @@ If you see "I'm having trouble connecting to my tools", make sure:
114115
If you encounter Python import errors:
115116
```bash
116117
cd agent
117-
pip install -r requirements.txt
118+
poetry lock && poetry install
118119
```

agent/poetry.lock

Lines changed: 1943 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agent/pyproject.toml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[tool.poetry]
2+
name = "agent"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["CopilotKit"]
6+
readme = "README.md"
7+
packages = [{include = "sample_agent"}]
8+
package-mode = false
9+
10+
[tool.poetry.dependencies]
11+
python = ">=3.10,<3.13"
12+
copilotkit = "0.1.55"
13+
langchain = "0.3.26"
14+
langchain-openai = "0.3.28"
15+
langgraph = "0.4.10"
16+
langsmith = "0.4.4"
17+
openai = "^1.68.2"
18+
fastapi = "^0.115.5"
19+
uvicorn = "^0.29.0"
20+
python-dotenv = "^1.0.0"
21+
22+
[tool.poetry.scripts]
23+
demo = "sample_agent.demo:main"
24+
25+
[build-system]
26+
requires = ["poetry-core"]
27+
build-backend = "poetry.core.masonry.api"

agent/sample_agent/__init__.py

Whitespace-only changes.

agent/agent.py renamed to agent/sample_agent/agent.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from langgraph.types import Command
1414
from langgraph.graph import MessagesState
1515
from langgraph.prebuilt import ToolNode
16+
from langgraph.checkpoint.memory import MemorySaver
1617

1718
class AgentState(MessagesState):
1819
"""
@@ -99,4 +100,5 @@ async def chat_node(state: AgentState, config: RunnableConfig) -> Command[Litera
99100
workflow.add_edge("tool_node", "chat_node")
100101
workflow.set_entry_point("chat_node")
101102

102-
graph = workflow.compile()
103+
checkpointer = MemorySaver()
104+
graph = workflow.compile(checkpointer=checkpointer)

agent/sample_agent/demo.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
This serves the "sample_agent" agent. This is an example of self-hosting an agent
3+
through our FastAPI integration. However, you can also host in LangGraph platform.
4+
"""
5+
6+
import os
7+
from dotenv import load_dotenv
8+
load_dotenv() # pylint: disable=wrong-import-position
9+
10+
from fastapi import FastAPI
11+
import uvicorn
12+
from copilotkit import LangGraphAGUIAgent
13+
from sample_agent.agent import graph
14+
from ag_ui_langgraph import add_langgraph_fastapi_endpoint
15+
16+
app = FastAPI()
17+
18+
add_langgraph_fastapi_endpoint(
19+
app=app,
20+
agent=LangGraphAGUIAgent(
21+
name="sample_agent",
22+
description="An example agent to use as a starting point for your own agent.",
23+
graph=graph
24+
),
25+
path="/"
26+
)
27+
28+
def main():
29+
"""Run the uvicorn server."""
30+
port = int(os.getenv("PORT", "8123"))
31+
uvicorn.run(
32+
"sample_agent.demo:app",
33+
host="0.0.0.0",
34+
port=port,
35+
reload=True,
36+
)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"scripts": {
66
"dev": "concurrently \"npm run dev:ui\" \"npm run dev:agent\" --names ui,agent --prefix-colors blue,green --kill-others",
77
"dev:debug": "LOG_LEVEL=debug npm run dev",
8-
"dev:agent": "cd agent && npx @langchain/langgraph-cli dev --port 8123 --no-browser",
8+
"dev:agent": "cd agent && poetry run demo",
99
"dev:ui": "next dev --turbopack",
1010
"build": "next build",
1111
"start": "next start",
1212
"lint": "next lint",
13-
"install:agent": "cd agent && pip install -r requirements.txt"
13+
"install:agent": "cd agent && poetry install"
1414
},
1515
"dependencies": {
1616
"@ag-ui/langgraph": "0.0.6",

src/app/api/copilotkit/route.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
copilotRuntimeNextJSAppRouterEndpoint,
55
} from "@copilotkit/runtime";
66

7-
import { LangGraphAgent } from "@ag-ui/langgraph"
7+
import { LangGraphHttpAgent } from "@ag-ui/langgraph"
88
import { NextRequest } from "next/server";
99

1010
// 1. You can use any service adapter here for multi-agent support. We use
@@ -15,10 +15,8 @@ const serviceAdapter = new ExperimentalEmptyAdapter();
1515
// integration to setup the connection.
1616
const runtime = new CopilotRuntime({
1717
agents: {
18-
"sample_agent": new LangGraphAgent({
19-
deploymentUrl: process.env.LANGGRAPH_DEPLOYMENT_URL || "http://localhost:8123",
20-
graphId: "sample_agent",
21-
langsmithApiKey: process.env.LANGSMITH_API_KEY || "",
18+
"sample_agent": new LangGraphHttpAgent({
19+
url: process.env.AGENT_URL || "http://localhost:8123",
2220
}),
2321
}
2422
});

0 commit comments

Comments
 (0)