Skip to content

Commit b12faea

Browse files
committed
initial commit
Signed-off-by: Tyler Slaton <tyler@copilotkit.ai>
0 parents  commit b12faea

23 files changed

+708
-0
lines changed

.gitignore

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.*
7+
.yarn/*
8+
!.yarn/patches
9+
!.yarn/plugins
10+
!.yarn/releases
11+
!.yarn/versions
12+
13+
# testing
14+
/coverage
15+
16+
# next.js
17+
/.next/
18+
/out/
19+
20+
# production
21+
/build
22+
23+
# misc
24+
.DS_Store
25+
*.pem
26+
27+
# debug
28+
npm-debug.log*
29+
yarn-debug.log*
30+
yarn-error.log*
31+
.pnpm-debug.log*
32+
33+
# env files (can opt-in for committing if needed)
34+
.env*
35+
36+
# vercel
37+
.vercel
38+
39+
# typescript
40+
*.tsbuildinfo
41+
next-env.d.ts
42+
43+
# lock files
44+
package-lock.json
45+
yarn.lock
46+
pnpm-lock.yaml
47+
bun.lockb
48+
49+
# LangGraph API
50+
.langgraph_api

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) Atai Barkai
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# CopilotKit <> LangGraph Starter
2+
3+
This is a starter template for building AI agents using [LangGraph](https://www.langchain.com/langgraph) and [CopilotKit](https://copilotkit.ai). It provides a modern Next.js application with an integrated LangGraph agent to be built on top of.
4+
5+
## Prerequisites
6+
7+
- Node.js 18+
8+
- Python 3.8+
9+
- Any of the following package managers:
10+
- [pnpm](https://pnpm.io/installation) (recommended)
11+
- npm
12+
- [yarn](https://classic.yarnpkg.com/lang/en/docs/install/#mac-stable)
13+
- [bun](https://bun.sh/)
14+
- OpenAI API Key (for the LangGraph agent)
15+
16+
> **Note:** This repository ignores lock files (package-lock.json, yarn.lock, pnpm-lock.yaml, bun.lockb) to avoid conflicts between different package managers. Each developer should generate their own lock file using their preferred package manager. After that, make sure to delete it from the .gitignore.
17+
18+
## Getting Started
19+
20+
1. Install dependencies using your preferred package manager:
21+
```bash
22+
# Using pnpm (recommended)
23+
pnpm install
24+
25+
# Using npm
26+
npm install
27+
28+
# Using yarn
29+
yarn install
30+
31+
# Using bun
32+
bun install
33+
```
34+
35+
2. Install Python dependencies for the LangGraph agent:
36+
```bash
37+
# Using pnpm
38+
pnpm install:agent
39+
40+
# Using npm
41+
npm run install:agent
42+
43+
# Using yarn
44+
yarn install:agent
45+
46+
# Using bun
47+
bun run install:agent
48+
```
49+
50+
3. Set up your OpenAI API key:
51+
```bash
52+
export OPENAI_API_KEY="your-openai-api-key-here"
53+
```
54+
55+
4. Start the development server:
56+
```bash
57+
# Using pnpm
58+
pnpm dev
59+
60+
# Using npm
61+
npm run dev
62+
63+
# Using yarn
64+
yarn dev
65+
66+
# Using bun
67+
bun run dev
68+
```
69+
70+
This will start both the UI and agent servers concurrently.
71+
72+
## Available Scripts
73+
The following scripts can also be run using your preferred package manager:
74+
- `dev` - Starts both UI and agent servers in development mode
75+
- `dev:debug` - Starts development servers with debug logging enabled
76+
- `dev:ui` - Starts only the Next.js UI server
77+
- `dev:agent` - Starts only the LangGraph agent server
78+
- `build` - Builds the Next.js application for production
79+
- `start` - Starts the production server
80+
- `lint` - Runs ESLint for code linting
81+
- `install:agent` - Installs Python dependencies for the agent
82+
83+
## Documentation
84+
85+
The main UI component is in `src/app/page.tsx`. You can:
86+
- Modify the theme colors and styling
87+
- Add new frontend actions
88+
- Customize the CopilotKit sidebar appearance
89+
90+
## 📚 Documentation
91+
92+
- [LangGraph Documentation](https://langchain-ai.github.io/langgraph/) - Learn more about LangGraph and its features
93+
- [CopilotKit Documentation](https://docs.copilotkit.ai) - Explore CopilotKit's capabilities
94+
- [Next.js Documentation](https://nextjs.org/docs) - Learn about Next.js features and API
95+
- [YFinance Documentation](https://pypi.org/project/yfinance/) - Financial data tools
96+
97+
## Contributing
98+
99+
Feel free to submit issues and enhancement requests! This starter is designed to be easily extensible.
100+
101+
## License
102+
103+
This project is licensed under the MIT License - see the LICENSE file for details.
104+
105+
## Troubleshooting
106+
107+
### Agent Connection Issues
108+
If you see "I'm having trouble connecting to my tools", make sure:
109+
1. The LangGraph agent is running on port 8000
110+
2. Your OpenAI API key is set correctly
111+
3. Both servers started successfully
112+
113+
### Python Dependencies
114+
If you encounter Python import errors:
115+
```bash
116+
cd agent
117+
pip install -r requirements.txt
118+
```

agent/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
venv/
2+
__pycache__/
3+
*.pyc
4+
.env
5+
.vercel
6+
7+
# python
8+
.venv/
9+
.langgraph_api/

agent/agent.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""
2+
This is the main entry point for the agent.
3+
It defines the workflow graph, state, tools, nodes and edges.
4+
"""
5+
6+
from typing import Any, List
7+
from typing_extensions import Literal
8+
from langchain_openai import ChatOpenAI
9+
from langchain_core.messages import SystemMessage
10+
from langchain_core.runnables import RunnableConfig
11+
from langchain.tools import tool
12+
from langgraph.graph import StateGraph, END
13+
from langgraph.types import Command
14+
from langgraph.graph import MessagesState
15+
from langgraph.prebuilt import ToolNode
16+
17+
class AgentState(MessagesState):
18+
"""
19+
Here we define the state of the agent
20+
21+
In this instance, we're inheriting from CopilotKitState, which will bring in
22+
the CopilotKitState fields. We're also adding a custom field, `language`,
23+
which will be used to set the language of the agent.
24+
"""
25+
proverbs: List[str] = []
26+
tools: List[Any]
27+
# your_custom_agent_state: str = ""
28+
29+
@tool
30+
def get_weather(location: str):
31+
"""
32+
Get the weather for a given location.
33+
"""
34+
return f"The weather for {location} is 70 degrees."
35+
36+
# @tool
37+
# def your_tool_here(your_arg: str):
38+
# """Your tool description here."""
39+
# print(f"Your tool logic here")
40+
# return "Your tool response here."
41+
42+
tools = [
43+
get_weather
44+
# your_tool_here
45+
]
46+
47+
async def chat_node(state: AgentState, config: RunnableConfig) -> Command[Literal["tool_node", "__end__"]]:
48+
"""
49+
Standard chat node based on the ReAct design pattern. It handles:
50+
- The model to use (and binds in CopilotKit actions and the tools defined above)
51+
- The system prompt
52+
- Getting a response from the model
53+
- Handling tool calls
54+
55+
For more about the ReAct design pattern, see:
56+
https://www.perplexity.ai/search/react-agents-NcXLQhreS0WDzpVaS4m9Cg
57+
"""
58+
59+
# 1. Define the model
60+
model = ChatOpenAI(model="gpt-4o")
61+
62+
print(state)
63+
64+
# 2. Bind the tools to the model
65+
model_with_tools = model.bind_tools(
66+
[
67+
*state["tools"], # bind tools defined by ag-ui
68+
get_weather,
69+
# your_tool_here
70+
],
71+
72+
# 2.1 Disable parallel tool calls to avoid race conditions,
73+
# enable this for faster performance if you want to manage
74+
# the complexity of running tool calls in parallel.
75+
parallel_tool_calls=False,
76+
)
77+
78+
# 3. Define the system message by which the chat model will be run
79+
system_message = SystemMessage(
80+
content=f"You are a helpful assistant. The current proverbs are {state.get('proverbs', [])}."
81+
)
82+
83+
# 4. Run the model to generate a response
84+
response = await model_with_tools.ainvoke([
85+
system_message,
86+
*state["messages"],
87+
], config)
88+
89+
# 5. We've handled all tool calls, so we can end the graph.
90+
return Command(
91+
goto=END,
92+
update={
93+
"messages": response
94+
}
95+
)
96+
97+
# Define the workflow graph
98+
workflow = StateGraph(AgentState)
99+
workflow.add_node("chat_node", chat_node)
100+
workflow.add_node("tool_node", ToolNode(tools=tools))
101+
workflow.add_edge("tool_node", "chat_node")
102+
workflow.set_entry_point("chat_node")
103+
104+
graph = workflow.compile()

agent/langgraph.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"python_version": "3.12",
3+
"dockerfile_lines": [],
4+
"dependencies": ["."],
5+
"graphs": {
6+
"sample_agent": "./agent.py:graph"
7+
},
8+
"env": ".env"
9+
}

agent/requirements.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
langchain==0.3.26
2+
langgraph==0.4.10
3+
langsmith==0.4.4
4+
openai>=1.68.2,<2.0.0
5+
fastapi>=0.115.5,<1.0.0
6+
uvicorn>=0.29.0,<1.0.0
7+
python-dotenv>=1.0.0,<2.0.0
8+
langgraph-cli[inmem]==0.3.3
9+
langchain-openai>=0.0.1

eslint.config.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { dirname } from "path";
2+
import { fileURLToPath } from "url";
3+
import { FlatCompat } from "@eslint/eslintrc";
4+
5+
const __filename = fileURLToPath(import.meta.url);
6+
const __dirname = dirname(__filename);
7+
8+
const compat = new FlatCompat({
9+
baseDirectory: __dirname,
10+
});
11+
12+
const eslintConfig = [
13+
...compat.extends("next/core-web-vitals", "next/typescript"),
14+
];
15+
16+
export default eslintConfig;

next.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { NextConfig } from "next";
2+
3+
const nextConfig: NextConfig = {
4+
/* config options here */
5+
};
6+
7+
export default nextConfig;

package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "langgraph-python-starter",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "concurrently \"npm run dev:ui\" \"npm run dev:agent\" --names ui,agent --prefix-colors blue,green --kill-others",
7+
"dev:debug": "LOG_LEVEL=debug npm run dev",
8+
"dev:agent": "cd agent && npx @langchain/langgraph-cli dev --port 8123 --no-browser",
9+
"dev:ui": "next dev --turbopack",
10+
"build": "next build",
11+
"start": "next start",
12+
"lint": "next lint",
13+
"install:agent": "cd agent && pip install -r requirements.txt"
14+
},
15+
"dependencies": {
16+
"@ag-ui/langgraph": "^0.0.4",
17+
"@ai-sdk/openai": "^1.3.22",
18+
"@copilotkit/react-core": "^1.9.1",
19+
"@copilotkit/react-ui": "^1.9.1",
20+
"@copilotkit/runtime": "^1.9.1",
21+
"next": "15.3.2",
22+
"react": "^19.0.0",
23+
"react-dom": "^19.0.0",
24+
"zod": "^3.24.4"
25+
},
26+
"devDependencies": {
27+
"@eslint/eslintrc": "^3",
28+
"@langchain/langgraph-cli": "0.0.40",
29+
"@tailwindcss/postcss": "^4",
30+
"@types/node": "^20",
31+
"@types/react": "^19",
32+
"@types/react-dom": "^19",
33+
"concurrently": "^9.1.2",
34+
"eslint": "^9",
35+
"eslint-config-next": "15.3.2",
36+
"tailwindcss": "^4",
37+
"typescript": "^5"
38+
}
39+
}

0 commit comments

Comments
 (0)