diff --git a/app/api/chat/openai/route.ts b/app/api/chat/openai/route.ts index 33fb833..84098d7 100644 --- a/app/api/chat/openai/route.ts +++ b/app/api/chat/openai/route.ts @@ -1,6 +1,8 @@ import { getServerConfig } from "@/config/server"; import { ServerRuntime } from "next"; import OpenAI from "openai"; +import { OpenAIStream, StreamingTextResponse } from 'ai'; + export const runtime: ServerRuntime = "edge"; diff --git a/app/chat/page.tsx b/app/chat/page.tsx index ca09fda..665a22b 100644 --- a/app/chat/page.tsx +++ b/app/chat/page.tsx @@ -31,27 +31,49 @@ const Chat = () => { // Function to handle sending a message const sendMessage = async () => { const log = document.getElementById('chat-log'); - + if (message.trim()) { setConversations([...conversations, - { role: "user", content: message }]); - - // Call API route and add AI message to conversations - const response = await fetch("/api/chat/openai", { - method: "POST", - body: message, - }); - const data = await response.json(); - if (data) { + { role: "user", content: message } + ]); + + try { + // Call API route and add AI message to conversations + const response = await fetch("/api/chat/openai", { + method: "POST", + body: message, + }); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + const data = await response.json(); + console.log(data); + + // Add the assistant's response to the conversation setConversations([ ...conversations, - { role: "assistant", content: data}, + { role: "assistant", content: data }, ]); + } catch (error) { + console.error("Error during fetch:", error); + if (error instanceof TypeError && error.cause?.name === 'ConnectTimeoutError') { + console.error("Connection timeout error. Please check your network connection."); + // You can also update the UI to show a message to the user + setConversations([ + ...conversations, + { role: "assistant", content: "Connection timeout error. Please check your network connection." }, + ]); + } else { + setConversations([ + ...conversations, + { role: "assistant", content: "An error occurred while sending the message. Please check your OPENAI API KEY in the setting page." }, + ]); + } } - // Reset the message input setMessage(""); - }; - } + } + }; // Message input handler const handleMessageChange = (event: React.ChangeEvent) => { setMessage(event.target.value); diff --git a/config/server.ts b/config/server.ts index f92cf46..ffca306 100644 --- a/config/server.ts +++ b/config/server.ts @@ -16,6 +16,8 @@ declare global { NEXT_PUBLIC_AZURE_GPT_35_TURBO_ID: string; NEXT_PUBLIC_AZURE_GPT_45_VISION_ID: string; NEXT_PUBLIC_AZURE_GPT_45_TURBO_ID: string; + NEXT_PUBLIC_SUPABASE_URL: string; + NEXT_PUBLIC_SUPABASE_ANON_KEY: string; } } } @@ -40,6 +42,8 @@ declare global { NEXT_PUBLIC_AZURE_GPT_35_TURBO_ID, NEXT_PUBLIC_AZURE_GPT_45_VISION_ID, NEXT_PUBLIC_AZURE_GPT_45_TURBO_ID, + NEXT_PUBLIC_SUPABASE_URL, + NEXT_PUBLIC_SUPABASE_ANON_KEY, } = process.env; return { @@ -57,9 +61,10 @@ declare global { azureGpt35TurboId: NEXT_PUBLIC_AZURE_GPT_35_TURBO_ID, azureGpt45VisionId: NEXT_PUBLIC_AZURE_GPT_45_VISION_ID, azureGpt45TurboId: NEXT_PUBLIC_AZURE_GPT_45_TURBO_ID, + supabaseUrl: NEXT_PUBLIC_SUPABASE_URL, + supabaseAnonKey: NEXT_PUBLIC_SUPABASE_ANON_KEY, }; }; - // const express = require("express"); // const bodyParser = require("body-parser"); // const sqlite3 = require("sqlite3").verbose(); @@ -107,4 +112,4 @@ declare global { // app.listen(port, () => { // console.log(`Server is running on http://localhost:${port}`); // }); - \ No newline at end of file + diff --git a/lib/supabase.ts b/lib/supabase.ts index 74c1822..23207c7 100644 --- a/lib/supabase.ts +++ b/lib/supabase.ts @@ -1,72 +1,17 @@ -// import { createClient } from "@supabase/supabase-js"; +import { createClient } from "@supabase/supabase-js"; -// interface Client { -// url?: string; -// key?: string; -// } +interface Client { + url?: string; + key?: string; +} -// const client: Client = { -// url: process.env.NEXT_PUBLIC_SUPABASE_URL, -// key: process.env.SUPABASE_ANON_KEY -// }; +const client: Client = { + url: process.env.NEXT_PUBLIC_SUPABASE_URL, + key: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY +}; -// if (!client.url || !client.key) { -// throw new Error("Missing Supabase credentials"); -// } +if (!client.url || !client.key) { + throw new Error("Missing Supabase credentials"); +} -// export const supabaseClient = createClient(client.url!, client.key!); - -// import { NextPage } from "next"; -// import { useState } from "react"; - - -// const Embeddings: NextPage = () => { -// const [urls, setUrls] = useState([]); -// const [loading, setLoading] = useState(false); - -// const handleSubmit = async (e: React.FormEvent) => { -// e.preventDefault(); -// setLoading(true); - -// const response = await fetch("/api/generate-embeddings", { -// method: "POST", -// headers: { "Content-Type": "application/json" }, -// body: JSON.stringify({ urls }) -// }); - -// setLoading(false); - -// if (!response.ok) { -// // Handle error -// } -// }; - -// return ( -//
-//

-// Generate embeddings -//

-//

-// Paste a list of URLs below to geneate embeddings using the OpenAI API, and add the embeddings to the Supabase embeddings table. -//

-//
-//