Skip to content

Commit d34a31b

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

28 files changed

+2874
-0
lines changed

.gitignore

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
.mastra/
44+
45+
# lock files
46+
package-lock.json
47+
yarn.lock
48+
pnpm-lock.yaml
49+
bun.lockb
50+
51+
# python
52+
agent/venv/
53+
agent/__pycache__/
54+
.venv/

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

agent/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Llama Index Agent Starter: Python
2+
3+
This package is a quick starter example for building AG-UI agents with Llama Index and CopilotKit.
4+
5+
## Running the Backend
6+
7+
```bash
8+
export OPENAI_API_KEY=...
9+
uv sync
10+
uv run dev
11+
```
12+
13+
## Running the Frontend
14+
15+
```bash
16+
cd ../ui
17+
npm install
18+
npm run dev
19+
```

agent/agent/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import uvicorn
2+
from .server import app
3+
4+
def main():
5+
uvicorn.run(app, host="127.0.0.1", port=9000)
6+
7+
if __name__ == "__main__":
8+
main()
9+
10+
__all__ = ["app"]
516 Bytes
Binary file not shown.
2.32 KB
Binary file not shown.
346 Bytes
Binary file not shown.

agent/agent/agent.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from typing import Annotated
2+
3+
from llama_index.core.workflow import Context
4+
from llama_index.llms.openai import OpenAI
5+
from llama_index.protocols.ag_ui.events import StateSnapshotWorkflowEvent
6+
from llama_index.protocols.ag_ui.router import get_ag_ui_workflow_router
7+
8+
9+
# This tool has a client-side version that is actually called to change the background
10+
def change_theme_color(
11+
theme_color: Annotated[str, "The hex color value. i.e. '#123456''"],
12+
) -> str:
13+
"""Change the background color of the chat. Can be any hex color value."""
14+
return f"Changing background to {theme_color}"
15+
16+
async def add_proverb(
17+
ctx: Context,
18+
proverb: Annotated[str, "The proverb to add. Make it witty, short and concise."],
19+
) -> str:
20+
"""Add a proverb to the list of proverbs."""
21+
state = await ctx.get("state", default={})
22+
23+
if "proverbs" not in state:
24+
state["proverbs"] = []
25+
26+
state["proverbs"].append(proverb)
27+
ctx.write_event_to_stream(StateSnapshotWorkflowEvent(snapshot=state))
28+
29+
await ctx.set(state)
30+
31+
return f"Added proverb: {proverb}"
32+
33+
async def get_weather(
34+
location: Annotated[str, "The location to get the weather for."],
35+
) -> str:
36+
"""Get the weather for a given location."""
37+
return f"The weather in {location} is sunny and 70 degrees."
38+
39+
agentic_chat_router = get_ag_ui_workflow_router(
40+
llm=OpenAI(model="gpt-4.1"),
41+
frontend_tools=[change_theme_color, add_proverb],
42+
backend_tools=[get_weather],
43+
system_prompt="You are a helpful assistant that can add proverbs to a list, get the weather for a given location, and change the background color of the chat/app background.",
44+
initial_state={
45+
"proverbs": [
46+
"CopilotKit may be new, but its the best thing since sliced bread.",
47+
],
48+
},
49+
)

agent/agent/server.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from fastapi import FastAPI
2+
from .agent import agentic_chat_router
3+
4+
app = FastAPI()
5+
app.include_router(agentic_chat_router)

0 commit comments

Comments
 (0)