Spaces:
Paused
Paused
matt HOFFNER
commited on
Commit
·
87495f8
1
Parent(s):
64b7c56
fix local and move to original
Browse files- components/Playground/index.tsx +6 -30
- package.json +1 -1
- pages/api/chat/index.ts +9 -1
components/Playground/index.tsx
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import React, {
|
2 |
import { Message, useChat } from "ai/react";
|
3 |
import { useAppDispatch, useAppSelector } from "../../store/hook";
|
4 |
import {
|
@@ -45,37 +45,10 @@ const Playground = () => {
|
|
45 |
const isValidCodeBlock = (markdownCode: string) => {
|
46 |
return markdownCode && markdownCode.length > 10 && markdownCode.includes('\n');
|
47 |
}
|
48 |
-
const [lastAppendedSystemMessage, setLastAppendedSystemMessage] = useState("");
|
49 |
-
|
50 |
-
const appendSystemMessage = async () => {
|
51 |
-
const systemMessageToAdd: Message = {
|
52 |
-
id: `${Date.now()}-system`,
|
53 |
-
createdAt: new Date(),
|
54 |
-
content: systemMessage,
|
55 |
-
role: "system"
|
56 |
-
};
|
57 |
-
await append(systemMessageToAdd);
|
58 |
-
setLastAppendedSystemMessage(systemMessage);
|
59 |
-
};
|
60 |
|
61 |
const modifiedHandleSubmit = async (e: FormEvent<HTMLFormElement>, chatRequestOptions?: ChatRequestOptions) => {
|
62 |
e.preventDefault();
|
63 |
-
|
64 |
-
// Step 1: Determine if the system message needs to be appended
|
65 |
-
const hasSystemMessage = messages.some(msg => msg.role === "system");
|
66 |
-
|
67 |
-
// Step 2: Append the system message if required
|
68 |
-
if (!hasSystemMessage) {
|
69 |
-
const systemMessageToAdd: Message = {
|
70 |
-
id: `${Date.now()}-system`,
|
71 |
-
createdAt: new Date(),
|
72 |
-
content: systemMessage,
|
73 |
-
role: "system"
|
74 |
-
};
|
75 |
-
await append(systemMessageToAdd);
|
76 |
-
}
|
77 |
-
|
78 |
-
// Step 3: Append the user message
|
79 |
await handleSubmit(e, chatRequestOptions);
|
80 |
};
|
81 |
|
@@ -93,8 +66,11 @@ const Playground = () => {
|
|
93 |
}, [markdownCode, prevMarkdownCode, dispatch]);
|
94 |
|
95 |
const { append, messages, input, setInput, handleSubmit, ...rest } = useChat({
|
|
|
|
|
|
|
96 |
onError: (error) => {
|
97 |
-
|
98 |
},
|
99 |
});
|
100 |
|
|
|
1 |
+
import React, { useRef, useEffect, useState, ChangeEvent, FormEvent } from "react";
|
2 |
import { Message, useChat } from "ai/react";
|
3 |
import { useAppDispatch, useAppSelector } from "../../store/hook";
|
4 |
import {
|
|
|
45 |
const isValidCodeBlock = (markdownCode: string) => {
|
46 |
return markdownCode && markdownCode.length > 10 && markdownCode.includes('\n');
|
47 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
const modifiedHandleSubmit = async (e: FormEvent<HTMLFormElement>, chatRequestOptions?: ChatRequestOptions) => {
|
50 |
e.preventDefault();
|
51 |
+
// Simply append the user message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
await handleSubmit(e, chatRequestOptions);
|
53 |
};
|
54 |
|
|
|
66 |
}, [markdownCode, prevMarkdownCode, dispatch]);
|
67 |
|
68 |
const { append, messages, input, setInput, handleSubmit, ...rest } = useChat({
|
69 |
+
body: {
|
70 |
+
systemMessage: systemMessage
|
71 |
+
},
|
72 |
onError: (error) => {
|
73 |
+
console.error(error);
|
74 |
},
|
75 |
});
|
76 |
|
package.json
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
"version": "0.1.0",
|
4 |
"private": true,
|
5 |
"scripts": {
|
6 |
-
"dev": "next dev
|
7 |
"build": "next build",
|
8 |
"start": "node .next/standalone/server.js",
|
9 |
"lint": "next lint",
|
|
|
3 |
"version": "0.1.0",
|
4 |
"private": true,
|
5 |
"scripts": {
|
6 |
+
"dev": "next dev",
|
7 |
"build": "next build",
|
8 |
"start": "node .next/standalone/server.js",
|
9 |
"lint": "next lint",
|
pages/api/chat/index.ts
CHANGED
@@ -9,7 +9,15 @@ const openai = new OpenAIApi(config);
|
|
9 |
export const runtime = "edge";
|
10 |
|
11 |
export default async function(req: Request) {
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
const response = await openai.createChatCompletion({
|
15 |
model: 'gpt-4',
|
|
|
9 |
export const runtime = "edge";
|
10 |
|
11 |
export default async function(req: Request) {
|
12 |
+
let { messages, systemMessage } = await req.json();
|
13 |
+
|
14 |
+
// Prepend the system message if it's not already there
|
15 |
+
if (messages.length === 0 || messages[0].role !== "system") {
|
16 |
+
messages = [{
|
17 |
+
role: "system",
|
18 |
+
content: systemMessage
|
19 |
+
}, ...messages];
|
20 |
+
}
|
21 |
|
22 |
const response = await openai.createChatCompletion({
|
23 |
model: 'gpt-4',
|