A Django application that integrates CopilotKit with LangGraph agents for AI-powered assistance.
- π€ AI-powered chat agent using LangGraph
- π Web search capabilities with Tavily
- β° Current datetime tool
- π RESTful API endpoints
- π Streaming responses
- Python 3.12
- Virtual environment (recommended)
- API keys for the services you want to use
-
Clone and navigate to the project:
cd assistant -
Create your virtual environment:
# Windows py -m venv env # Windows (if you have more than one python version installed, create virtual environment using specific verion) py -3.12 -m venv env # macOS/Linux source env/bin/activate
-
Activate your virtual environment:
# Windows env\Scripts\activate # macOS/Linux source env/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
Create a .env file:
# Create .env file with your API keys DEEPSEEK_API_KEY=your_deepseek_api_key_here OPENAI_API_KEY=your_openai_api_key_here TAVILY_API_KEY=your_tavily_api_key_here DEBUG=True SECRET_KEY=your_django_secret_key -
Run database migrations (Optional):
python manage.py migrate
-
Start the server:
uvicorn assistant.asgi:application --reload
import { NextRequest } from "next/server";
import {
CopilotRuntime,
EmptyAdapter,
copilotRuntimeNextJSAppRouterEndpoint,
} from "@copilotkit/runtime";
const serviceAdapter = new EmptyAdapter();
const runtime = new CopilotRuntime({
remoteEndpoints: [
{
url: `${process.env.NEXT_PUBLIC_DJANGO_SERVER_URL || "http://localhost:8000"}/api/copilotkit`,
},
],
});
export async function POST(req: NextRequest) {
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
runtime,
serviceAdapter,
endpoint: "/api/copilotkit",
});
return handleRequest(req);
}GET/POST /api/copilotkit/info- Get agent informationPOST /api/copilotkit/agent/{name}- Execute an agentPOST /api/copilotkit/agent/{name}/state- Get agent statePOST /api/copilotkit/action/{name}- Execute an action
GET /api/health- Health checkPOST /api/echo- Echo message (for testing)
curl -X POST http://localhost:8000/api/copilotkit/infocurl http://localhost:8000/api/healthIf you're getting 500 errors, check:
- API Keys: Make sure your API keys are set in the
.envfile - Environment Variables: Verify the
.envfile is being loaded - Dependencies: Ensure all packages are installed correctly
- Missing API Keys: The application will show helpful error messages if API keys are missing
- Network Issues: Make sure you have internet access for API calls
- Port Conflicts: If port 8000 is busy, use a different port:
uvicorn assistant.asgi:application --host 0.0.0.0 --port 8001 --reload
assistant/
βββ app/
β βββ agent.py # LangGraph agent definition
β βββ chat.py # Chat node implementation
β βββ copilotkit_integration.py # Django-CopilotKit integration
β βββ model.py # LLM configuration
β βββ schema.py # Agent state schema
β βββ sdk.py # CopilotKit SDK setup (define your agent here)
β βββ views.py # Django views
βββ assistant/
β βββ settings.py # Django settings
β βββ urls.py # Main URL configuration
βββ manage.py # Django management script
βββ requirements.txt # Python dependencies
βββ setup_env.py # Environment setup script
To add new tools to the agent:
- Define the tool in
app/chat.py - Add it to the
toolslist in theget_toolsfunction - Update the system message if needed
To add new agents:
- Create a new graph in
app/agent.py - Add the agent to the SDK configuration in
app/sdk.py - Update the URL patterns if needed