Skip to content

Update route.ts #1787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 58 additions & 30 deletions app/api/chat/custom/route.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,94 @@
import { Database } from "@/supabase/types"
import { ChatSettings } from "@/types"
import { createClient } from "@supabase/supabase-js"
import { OpenAIStream, StreamingTextResponse } from "ai"
import { ServerRuntime } from "next"
import OpenAI from "openai"
import { ChatCompletionCreateParamsBase } from "openai/resources/chat/completions.mjs"
import { Database } from "@/supabase/types";
import { ChatSettings } from "@/types";
import { createClient } from "@supabase/supabase-js";
import { OpenAIStream, StreamingTextResponse } from "ai";
import { ServerRuntime } from "next";
import OpenAI from "openai";
import { ChatCompletionCreateParamsBase } from "openai/resources/chat/completions.mjs";

export const runtime: ServerRuntime = "edge"
export const runtime: ServerRuntime = "edge";

export async function POST(request: Request) {
const json = await request.json()
const { chatSettings, messages, customModelId } = json as {
chatSettings: ChatSettings
messages: any[]
customModelId: string
}
// Helper function to log messages
const log = (message: string, data?: any) => {
console.log(`[LOG] ${message}`, data || '');
};

export async function POST(request: Request) {
try {
// Parse the JSON body
const json = await request.json();
const { chatSettings, messages, customModelId } = json as {
chatSettings: ChatSettings;
messages: any[];
customModelId: string;
};

// Validate the parsed data
if (!chatSettings || !messages || !customModelId) {
throw new Error("Invalid request payload");
}

log("Parsed request payload", { chatSettings, messages, customModelId });

// Initialize Supabase client
const supabaseAdmin = createClient<Database>(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!
)
);

log("Initialized Supabase client");

// Fetch the custom model data
const { data: customModel, error } = await supabaseAdmin
.from("models")
.select("*")
.eq("id", customModelId)
.single()
.single();

if (!customModel) {
throw new Error(error.message)
if (error || !customModel) {
throw new Error(error?.message || "Custom model not found");
}

log("Fetched custom model", customModel);

// Initialize OpenAI client
const custom = new OpenAI({
apiKey: customModel.api_key || "",
baseURL: customModel.base_url
})
});

log("Initialized OpenAI client");

// Create chat completion with streaming response
const response = await custom.chat.completions.create({
model: chatSettings.model as ChatCompletionCreateParamsBase["model"],
messages: messages as ChatCompletionCreateParamsBase["messages"],
temperature: chatSettings.temperature,
stream: true
})
});

log("Created chat completion");

const stream = OpenAIStream(response)
const stream = OpenAIStream(response);

log("Generated streaming response");

return new StreamingTextResponse(stream);

return new StreamingTextResponse(stream)
} catch (error: any) {
let errorMessage = error.message || "An unexpected error occurred"
const errorCode = error.status || 500
let errorMessage = error.message || "An unexpected error occurred";
const errorCode = error.status || 500;

log("Error occurred", { errorMessage, errorCode });

if (errorMessage.toLowerCase().includes("api key not found")) {
errorMessage =
"Custom API Key not found. Please set it in your profile settings."
errorMessage = "Custom API Key not found. Please set it in your profile settings.";
} else if (errorMessage.toLowerCase().includes("incorrect api key")) {
errorMessage =
"Custom API Key is incorrect. Please fix it in your profile settings."
errorMessage = "Custom API Key is incorrect. Please fix it in your profile settings.";
}

return new Response(JSON.stringify({ message: errorMessage }), {
status: errorCode
})
});
}
}