搭配 LiteLLM 的 CopilotKit SDK
透過 LiteLLM Proxy 使用 CopilotKit SDK 搭配任何 LLM 提供者。
注意: CopilotKit SDK 與 LiteLLM Proxy 的整合可搭配 LiteLLM v1.81.7-nightly 或更高版本使用。
快速開始
1. 將模型新增至設定
config.yaml
model_list:
- model_name: claude-sonnet-4-5
litellm_params:
model: "anthropic/claude-sonnet-4-5-20250514-v1:0"
api_key: "os.environ/ANTHROPIC_API_KEY"
2. 啟動 LiteLLM Proxy
litellm --config config.yaml
3. 使用 CopilotKit SDK
import OpenAI from "openai";
import {
CopilotRuntime,
OpenAIAdapter,
copilotRuntimeNextJSAppRouterEndpoint,
} from "@copilotkit/runtime";
import { NextRequest } from "next/server";
const model = "claude-sonnet-4-5";
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY || "sk-12345",
baseURL: process.env.OPENAI_BASE_URL || "http://localhost:4000/v1",
});
const serviceAdapter = new OpenAIAdapter({ openai, model });
const runtime = new CopilotRuntime();
export const POST = async (req: NextRequest) => {
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
runtime,
serviceAdapter,
endpoint: "/api/copilotkit",
});
return handleRequest(req);
};
4. 測試
curl -X POST http://localhost:3000/api/copilotkit \
-H "Content-Type: application/json" \
-d '{
"method": "agent/run",
"params": {
"agentId": "default"
},
"runId": "your_run_id",
"threadId": "your_thread_id",
"runId": ""your_run_id"",
"tools": [],
"context": [],
"forwardedProps": {},
"state": {},
"messages": [
{
"id": "166e573e-f7c6-4c0f-8685-04dbefec18be",
"content": "Hi",
"role": "user"
}
]
}
}'
環境變數
| 變數 | 值 | 說明 |
|---|---|---|
OPENAI_API_KEY | sk-12345 | 您的 LiteLLM API 金鑰 |
OPENAI_BASE_URL | http://localhost:4000/v1 | LiteLLM proxy URL |