Skip to content

Commit

Permalink
fix: move copilotkit initialization to the POST function (#2639)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiryous authored Nov 26, 2024
1 parent b4d05d9 commit cdf0df1
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions keep-ui/app/api/copilotkit/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ import {
import OpenAI, { OpenAIError } from "openai";
import { NextRequest } from "next/server";

function initializeCopilotRuntime() {
try {
const openai = new OpenAI({
organization: process.env.OPEN_AI_ORGANIZATION_ID,
apiKey: process.env.OPEN_AI_API_KEY,
});
const serviceAdapter = new OpenAIAdapter({ openai });
const runtime = new CopilotRuntime();
return { runtime, serviceAdapter };
} catch (error) {
if (error instanceof OpenAIError) {
console.log("Error connecting to OpenAI", error);
} else {
console.error("Error initializing Copilot Runtime", error);
export const POST = async (req: NextRequest) => {
function initializeCopilotRuntime() {
try {
const openai = new OpenAI({
organization: process.env.OPEN_AI_ORGANIZATION_ID,
apiKey: process.env.OPEN_AI_API_KEY,
});
const serviceAdapter = new OpenAIAdapter({ openai });
const runtime = new CopilotRuntime();
return { runtime, serviceAdapter };
} catch (error) {
if (error instanceof OpenAIError) {
console.log("Error connecting to OpenAI", error);
} else {
console.error("Error initializing Copilot Runtime", error);
}
return null;
}
return null;
}
}

const runtimeOptions = initializeCopilotRuntime();
const runtimeOptions = initializeCopilotRuntime();

export const POST = async (req: NextRequest) => {
if (!runtimeOptions) {
return new Response("Error initializing Copilot Runtime", { status: 500 });
}
Expand Down

0 comments on commit cdf0df1

Please sign in to comment.