Skip to content

Commit 25b5181

Browse files
committed
getting started
1 parent 0584115 commit 25b5181

3 files changed

Lines changed: 102 additions & 409 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from "react";
2+
3+
export const CoAgentsDiagram: React.FC = (): JSX.Element => {
4+
return (
5+
<div className="flex flex-col items-center">
6+
<div className="flex items-center">
7+
<DiagramNode title="Frontend (via CopilotKit provider)" />
8+
<DiagramArrow />
9+
<DiagramNode title="Copilot Runtime" />
10+
<DiagramArrow />
11+
<DiagramNode title="Remote Endpoint (FastAPI + CopilotKit Python SDK)" />
12+
<DiagramArrow />
13+
<DiagramNode title="LangGraph Agent" />
14+
</div>
15+
</div>
16+
);
17+
};
18+
19+
interface DiagramNodeProps {
20+
title: string;
21+
}
22+
23+
const DiagramNode: React.FC<DiagramNodeProps> = ({ title }): JSX.Element => {
24+
return (
25+
<div className="bg-white dark:bg-neutral-800 shadow-lg rounded-lg p-4 m-2 text-center">
26+
<span className="text-gray-800 dark:text-gray-200 font-medium">{title}</span>
27+
</div>
28+
);
29+
};
30+
31+
const DiagramArrow: React.FC = (): JSX.Element => {
32+
return (
33+
<div className="mx-2">
34+
<svg
35+
className="w-6 h-6 text-gray-400 dark:text-gray-500"
36+
fill="none"
37+
stroke="currentColor"
38+
viewBox="0 0 24 24"
39+
>
40+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
41+
</svg>
42+
</div>
43+
);
44+
};

docs/content/docs/coagents/getting-started.mdx

Lines changed: 53 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -5,271 +5,102 @@ description: "Get started with CoAgents in just a few minutes."
55
import { CoAgentsEnterpriseCTA } from "@/components/react/coagents/coagents-enterprise-cta.tsx";
66
import SelfHostingCopilotRuntimeCreateEndpoint from "@/snippets/self-hosting-copilot-runtime-create-endpoint.mdx";
77
import LLMAdapters from "@/snippets/llm-adapters.mdx";
8+
import { CoAgentsDiagram } from "@/components/react/coagents/coagents-diagram.tsx";
9+
810

911
## Before you start
1012

11-
These docs assume you’ve been introduced to CopilotKit’s core concepts and components. If you’re new to CopilotKit, take a minute to go through the quickstart and [learn how to integrate CopilotKit into a React app](/quickstart).
13+
At the moment CoAgents integrates with LangGraph-powered agents using **LangGraph-Python**. <br/>
14+
(Support for LangGraph.js and LangGraph Cloud is coming soon!)
1215

13-
Also, at the moment CoAgents integrates with LangGraph-powered agents using their Python SDK. (Support for LangGraph JS and LangGraph Cloud are coming soon!)
1416

15-
This guide assumes you’re familiar with using LangGraph to build agent workflows. If you need a quick introduction, check out [this brief example from the LangGraph docs](https://docs.langgraph.ai/quickstart).
17+
**For orientation, here's how CoAgents are wired:**
1618

17-
<Steps>
19+
<CoAgentsDiagram />
1820

1921

22+
<Callout type="info">
23+
This guide assumes you’re familiar with using LangGraph to build agent workflows. If you need a quick introduction, check out [this brief example from the LangGraph docs](https://docs.langgraph.ai/quickstart).
24+
</Callout>
2025

21-
<Step>
2226

23-
## Setup a remote endpoint in Python + FastAPI
2427

25-
CoAgents require you to use a remote endpoint using Python, FastAPI, and the CopilotKit Python SDK; here's a basic example to get you started.
2628

27-
<Callout>
28-
If you're starting from scratch or need more information, check out our [remote endpoint docs](/guides/backend-actions/remote-backend-endpoint) for a complete walkthrough.
29-
</Callout>
3029

31-
In your Python project, install the CopilotKit SDK:
30+
## Getting started
3231

33-
```bash
34-
pip install copilotkit --extra-index-url https://copilotkit.gateway.scarf.sh/simple/
35-
```
36-
37-
Then set up or update your FastAPI server to initialize our SDK and connect it to your LangGraph agent. In this example, we’re assuming a basic agent workflow is being exported from `agent.py` in the same directory as this server script.
32+
<Steps>
3833

39-
```python filename="server.py" showLineNumbers
40-
from fastapi import FastAPI
41-
app = FastAPI()
42-
43-
# Import CopilotKit SDK and integrations
44-
from copilotkit.integrations.fastapi import add_fastapi_endpoint
45-
from copilotkit import CopilotKitSDK, LangGraphAgent
46-
47-
# Import your LangGraph agent; in this example, it's the variable
48-
# named `basic_agent_graph` in ./agent.py
49-
from .agent import basic_agent_graph
50-
51-
# Initialize the agent for use by CopilotKit; we'll name it "basic_agent"
52-
basic_agent = LangGraphAgent(
53-
name="basic_agent",
54-
description="Agent that asks about the weather",
55-
agent=basic_agent_graph,
56-
)
57-
58-
# Next, initialize the SDK, passing in the agent
59-
sdk = CopilotKitSDK(
60-
agents=[
61-
basic_agent
62-
],
63-
)
64-
65-
# Finally, add the remote actions endpoint to this API
66-
add_fastapi_endpoint(app, sdk, "/copilotkit_remote")
67-
```
34+
<Step>
6835

69-
Remember the name `basic_agent` as well as our API endpoint URL (`http://localhost:8000/copilotkit_remote`), we'll need them as we move on to integrating this agent into the frontend.
36+
First, take a minute to **[go through the CopilotKit quickstart](/quickstart), and integrate CopilotKit into your React app.**
37+
This should only take a minute.
7038

7139
</Step>
7240

73-
7441
<Step>
75-
## Self-host the Copilot Runtime
42+
Then, setup a CopilotKit Remote-Endpoint using FastAPI. This endpoint will serve your LangGraph agent.
43+
Follow the [Remote Endpoint quickstart](/guides/backend-actions/remote-backend-endpoint).
44+
</Step>
7645

77-
To use your remote agent service, you’ll also need to self-host the CopilotRuntime service. (Support for CopilotCloud is coming soon.)
7846

79-
### Configure an AI service adapter
47+
<Step>
8048

81-
First, we’ll create a new module file to initialize a runtime adapter for our LLM of choice, which we’ll put at `src/lib/serviceAdapter.ts`.
8249

50+
## Connect your FastAPI Remote Endpoint to a LangGraph agent
8351

84-
<Tabs items={['OpenAI', 'Anthropic', 'Other LLM Providers']}>
8552

86-
<Tab value="OpenAI">
53+
At this point you should have CopilotKit hooked into your application, connected to a FastAPI Remote Endpoint.
8754

88-
Create a `.env` file in the root of your project, if you haven't already, and add your OpenAI API key.
55+
The next step is to configure the Remote Endpoint to serve LangGraph agents.
8956

90-
```plaintext filename=".env"
91-
OPENAI_API_KEY=your_api_key_here
92-
```
57+
**Modify your server.py file (setup in step (2)) to serve LangGraph agents:**
9358

94-
<Callout type="warning" title="Do you have a paid API key?">
95-
Please note that the code below uses GPT-4o, which requires a paid OpenAI API key.
96-
**If you are using a free OpenAI API key**, change the model to a different option such as "gpt-3.5-turbo".
97-
</Callout>
59+
```python title="server.py"
60+
from fastapi import FastAPI
61+
from copilotkit.integrations.fastapi import add_fastapi_endpoint
62+
from copilotkit import CopilotKitSDK, LangGraphAgent
9863

99-
Then create a new file called `src/lib/llmAdapter.ts`:
64+
app = FastAPI()
10065

101-
```ts filename="src/lib/llmAdapter.ts"
102-
import OpenAI from 'openai';
103-
import { OpenAIAdapter } from '@copilotkit/runtime';
66+
# Import your LangGraph agent; in this example, it's the variable
67+
# named `basic_agent_graph` in ./agent.py
68+
from .agent import basic_agent_graph
10469

105-
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
70+
# Initialize the agent for use by CopilotKit; we'll name it "basic_agent"
71+
basic_agent = LangGraphAgent( // [!code highlight:4]
72+
name="basic_agent",
73+
description="Agent that asks about the weather",
74+
agent=basic_agent_graph,
75+
)
10676

107-
const openaiAdapter = new OpenAIAdapter({
108-
openai,
109-
model: "gpt-4o"
110-
});
77+
# Initialize the CopilotKit SDK
78+
sdk = CopilotKitSDK(agents=[basic_agent]) [!code highlight:1]
11179

112-
export default openaiAdapter
113-
```
114-
</Tab>
80+
# Add the CopilotKit endpoint to your FastAPI app
81+
add_fastapi_endpoint(app, sdk, "/copilotkit_remote")
11582

116-
<Tab value="Anthropic">
117-
Create a `.env` file in the root of your project, if you haven't already, and add your Anthropic API key.
83+
def main():
84+
"""Run the uvicorn server."""
85+
import uvicorn
86+
uvicorn.run("server:app", host="127.0.0.1", port=8000, reload=True)
11887

119-
```plaintext filename=".env"
120-
ANTHROPIC_API_KEY=your_api_key_here
12188
```
89+
Remember the name `basic_agent` as well as our API endpoint URL (`http://localhost:8000/copilotkit_remote`), we'll need them as we move on to integrating this agent into the frontend.
12290

123-
Then create a new file called `src/lib/llmAdapter.ts`:
124-
125-
```ts filename="src/lib/llmAdapter.ts"
126-
import Anthropic from '@anthropic-ai/sdk';
127-
import { AnthropicAdapter } from '@copilotkit/runtime';
128-
129-
const anthropic = new Anthropic({
130-
apiKey: 'my_api_key', // defaults to process.env["ANTHROPIC_API_KEY"]
131-
});
132-
133-
const anthropicAdapter = new AnthropicAdapter({ anthropic });
91+
</Step>
13492

135-
export default anthropicAdapter
136-
```
137-
</Tab>
138-
139-
<Tab value="Other LLM Providers">
140-
To use a different LLM provider, you can follow the `OpenAI` instructions, but adapt them to use one of the other available LLM adapters listed below:
141-
142-
<LLMAdapters />
143-
</Tab>
144-
145-
</Tabs>
146-
147-
### Add a CopilotRuntime endpoint to your app
148-
149-
Next, you'll use the service adapter you created above to initialize a `CopilotRuntime` instance and add a new route to your app. Here are some examples for the most common Node server frameworks:
150-
151-
<Tabs items={['Next.js App Router', 'Next.js Pages Router', 'Node.js Express']}>
152-
153-
{/* Next.js App Router */}
154-
<Tab value="Next.js App Router">
155-
Next, create a new server route which will make the `CopilotRuntime` available at `/api/copilotkit` and ready to work with remote actions provided by our Python app.
156-
157-
Create a new file at `src/app/api/copilotkit/route.ts` and add this code:
158-
159-
```ts filename="app/api/copilotkit/route.ts" showLineNumbers
160-
import {
161-
CopilotRuntime,
162-
copilotRuntimeNextJSAppRouterEndpoint,
163-
} from '@copilotkit/runtime';
164-
import { NextRequest } from 'next/server';
165-
import serviceAdapter from '@/lib/llmAdapter';
166-
167-
const runtime = new CopilotRuntime({
168-
remoteActions: [
169-
{
170-
url: `http://localhost:8000/copilotkit_remote`,
171-
}
172-
]
173-
});
174-
175-
export const POST = async (req: NextRequest) => {
176-
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
177-
runtime,
178-
serviceAdapter,
179-
endpoint: '/api/copilotkit',
180-
});
181-
182-
return handleRequest(req);
183-
};
184-
```
185-
</Tab>
186-
187-
{/* Next.js Pages Router */}
188-
<Tab value="Next.js Pages Router">
189-
Next, create a new server route which will make the `CopilotRuntime` available at `/api/copilotkit` and ready to work with remote actions provided by our Python app.
190-
191-
Create a new file at `src/pages/api/copilotkit.ts` and add this code:
192-
193-
```ts filename="pages/api/copilotkit.ts" showLineNumbers
194-
import { NextApiRequest, NextApiResponse } from 'next';
195-
import {
196-
CopilotRuntime,
197-
copilotRuntimeNextJSPagesRouterEndpoint,
198-
} from '@copilotkit/runtime';
199-
import serviceAdapter from '@/lib/llmAdapter';
200-
201-
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
202-
203-
const runtime = new CopilotRuntime({
204-
remoteActions: [
205-
{
206-
url: `http://localhost:8000/copilotkit_remote`,
207-
}
208-
]
209-
});
210-
211-
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
212-
runtime,
213-
serviceAdapter,
214-
endpoint: '/api/copilotkit',
215-
});
216-
217-
return await handleRequest(req, res);
218-
};
219-
220-
export default handler;
221-
```
222-
</Tab>
223-
224-
{/* Node.js Express */}
225-
<Tab value="Node.js Express">
226-
Next, create a new route handler which will make the `CopilotRuntime` available at `/copilotkit` and ready to work with remote actions provided by our Python app:
227-
228-
```ts filename="server.ts" showLineNumbers
229-
import express from 'express';
230-
import {
231-
CopilotRuntime,
232-
copilotRuntimeNodeHttpEndpoint,
233-
} from '@copilotkit/runtime';
234-
import llmAdapter from '@/lib/llmAdapter';
235-
236-
const app = express();
237-
238-
app.use('/copilotkit', (req, res, next) => {
239-
240-
const runtime = new CopilotRuntime({
241-
remoteActions: [
242-
{
243-
url: `http://localhost:8000/copilotkit_remote`,
244-
}
245-
]
246-
});
247-
248-
const handler = copilotRuntimeNodeHttpEndpoint({
249-
endpoint: '/copilotkit',
250-
runtime,
251-
serviceAdapter: llmAdapter,
252-
});
253-
254-
return handler(req, res, next);
255-
});
256-
257-
app.listen(4000, () => {
258-
console.log('Listening at http://localhost:4000/copilotkit');
259-
});
260-
```
261-
</Tab>
262-
</Tabs>
26393

26494

265-
</Step>
95+
<Step>
26696

97+
## Agent-lock your Copilot to the `base_agent` agent.
26798

268-
<Step>
99+
CopilotKit supports router-mode as well as agent-lock mode. For more detail see [router-mode / agent-mode](/coagents/advanced/router-mode-agent-lock).
269100

270-
## Integrate agents into your CopilotKit frontend app
101+
For simplicity, wel'll use agent-lock mode in these tutorials, but in production use-cases you will likely want to use router-mode.
271102

272-
Once you've set up your agents, registered them with CopilotKit on the Python side, and mounted the CopilotRuntime within your app, all that's left is to hook this up to your frontend using the `<CopilotKit />` component.
103+
Lock the Copilot to the `basic_agent` setup earlier. This means every single interaction with the Copilot will be forwarded to the locked agent.
273104

274105
```tsx filename="src/page.tsx"
275106
// This UI will now invoke our AI agent, not just the LLM
@@ -283,13 +114,11 @@ Once you've set up your agents, registered them with CopilotKit on the Python si
283114
</CopilotKit>
284115
```
285116

286-
By passing in the `agent` prop, we're telling CopilotKit to go into "agent lock" mode, and only use the `basic_agent` we set up earlier. To customize this behavior, see our docs about [router mode](/coagents/advanced/router-mode-agent-lock).
287-
288-
Any of CopilotKit's pre-built components can be used to interact with CoAgents, and you can also use agent-specific hooks in your custom UI components to enable powerful new workflows.
289-
290117
</Step>
291118
</Steps>
292119

120+
🎉 Congrats! You've successfully integrated a LangGraph agent chatbot to your application! Give it a try now and see it in action.
121+
293122

294123

295124
<CoAgentsEnterpriseCTA />

0 commit comments

Comments
 (0)