Skip to content

Commit

Permalink
fix conn error
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan370 committed Apr 25, 2024
1 parent 4bb76d5 commit 9b505cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
14 changes: 10 additions & 4 deletions app/api/chat/openai/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,32 @@ 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(
{
model: "gpt-3.5-turbo",
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;
Expand Down
23 changes: 12 additions & 11 deletions app/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
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",
content:
'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?",
},
Expand All @@ -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<HTMLInputElement>) => {
setMessage(event.target.value);
Expand Down Expand Up @@ -92,7 +93,7 @@ const Chat = () => {

</div>
</div>
<div className="p-4 max-w-md mx-auto bg-white shadow-md rounded-lg">
<div className="p-4 max-w-3xl mx-auto bg-white shadow-md rounded-lg">
<div className="mb-4 h-64 overflow-y-auto">
{conversations.map((text, index) => (
<div key={index} className={`message ${text.role}`}>
Expand Down

0 comments on commit 9b505cb

Please sign in to comment.