From 9b505cb410783b720926a6d0502b426b352a83d9 Mon Sep 17 00:00:00 2001 From: Stan Ke <156306548@qq.com> Date: Thu, 25 Apr 2024 22:25:58 +0800 Subject: [PATCH] fix conn error --- app/api/chat/openai/route.ts | 14 ++++++++++---- app/chat/page.tsx | 23 ++++++++++++----------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/app/api/chat/openai/route.ts b/app/api/chat/openai/route.ts index 91c5210..3c11be6 100644 --- a/app/api/chat/openai/route.ts +++ b/app/api/chat/openai/route.ts @@ -9,10 +9,11 @@ export async function POST(request: Request) { const config = await getServerConfig(); const openai = new OpenAI({ - apiKey: config.openaiApiKey || "", + apiKey: config.openaiApiKey, baseURL: config.openaiBaseUrl || config.openaiProxyUrl, }); console.log(openai.baseURL); + console.log(openai.apiKey); const response = await openai.chat.completions.create( { @@ -20,15 +21,20 @@ export async function POST(request: Request) { messages: [{ role: "user", content: "Say this is a test" }], stream: true, }, - { headers: { Authorization: `Bearer ${openai.apiKey}`, - Accept: "*/*" } } + { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${openai.apiKey}`, + }, + } ); + for await (const chunk of response) { console.log(chunk.choices[0].delta); process.stdout.write(chunk.choices[0]?.delta?.content || ""); } - return; + return response; } catch (error: any) { const errorMessage = error.error?.message || "An unexpected error occurred"; const errorCode = error.status || 500; diff --git a/app/chat/page.tsx b/app/chat/page.tsx index 9d1dd59..645b049 100644 --- a/app/chat/page.tsx +++ b/app/chat/page.tsx @@ -3,8 +3,9 @@ import { useState } from "react"; import Siderbar from "../components/Siderbar"; import { OpenAIChatMessage } from "@/lib/ModelSetting"; +import OpenAI from "openai"; -const initialAgents = [ +const initialAgents:OpenAIChatMessage[] = [ { role: "system", name: "GPT Prompt builder", @@ -12,7 +13,8 @@ const initialAgents = [ 'Read all of the instructions below and once you understand them say "Shall we begin:" I want you to become my Prompt Creator. Your goal is to help me craft the best possible prompt for my needs. The prompt will be used by you, ChatGPT. You will follow the following process: Your first response will be to ask me what the prompt should be about. I will provide my answer, but we will need to improve it through continual iterations by going through the next steps. Based on my input, you will generate 3 sections. Revised Prompt (provide your rewritten prompt. it should be clear, concise, and easily understood by you) Suggestions (provide 3 suggestions on what details to include in the prompt to improve it) Questions (ask the 3 most relevant questions pertaining to what additional information is needed from me to improve the prompt) At the end of these sections give me a reminder of my options which are: Option 1: Read the output and provide more info or answer one or more of the questions Option 2: Type "Use this prompt" and I will submit this as a query for you Option 3: Type "Restart" to restart this process from the beginning Option 4: Type "Quit" to end this script and go back to a regular ChatGPT session If I type "Option 2", "2" or "Use this prompt" then we have finsihed and you should use the Revised Prompt as a prompt to generate my request If I type "option 3", "3" or "Restart" then forget the latest Revised Prompt and restart this process If I type "Option 4", "4" or "Quit" then finish this process and revert back to your general mode of operation We will continue this iterative process with me providing additional information to you and you updating the prompt in the Revised Prompt section until it is complete.', }, { - role: "user", + role: "system", + name:"books", content: "I really enjoyed reading To Kill a Mockingbird, could you recommend me a book that is similar and tell me why?", }, @@ -22,35 +24,34 @@ const initialAgents = [ const Chat = () => { const [message, setMessage] = useState("Hi"); const [selectedAgent, setSelectedAgent] = useState(initialAgents[0]); - const [conversations, setConversations] = useState(initialAgents[0]); + const [conversations, setConversations] = useState(initialAgents); // Function to handle sending a message const sendMessage = async () => { // Add user message to conversations if (message.trim()) { - setConversations([...conversations, { role: "user", content: message }]); + setConversations([...conversations, + { role: "user", content: message }]); // Call API route and add AI message to conversations //const response = await fetch('/api/chat/openai', { const response = await fetch("http://localhost:3000/api/chat/openai", { method: "POST", - headers: {}, body: JSON.stringify({ message }), }); - const data = await response.json(); - if (data.message) { + if (data) { setConversations([ ...conversations, - { role: "assistant", content: data.message }, + { role: "assistant", content: "4" }, ]); } // Reset the message input setMessage(""); - } - }; + }; + } // Message input handler const handleMessageChange = (event: React.ChangeEvent) => { setMessage(event.target.value); @@ -92,7 +93,7 @@ const Chat = () => { -
+
{conversations.map((text, index) => (