matt HOFFNER commited on
Commit
a8f0877
1 Parent(s): 1de639c

configure system prompt and model select

Browse files
components/Playground/index.tsx CHANGED
@@ -26,6 +26,13 @@ const Playground = () => {
26
  const { logs, editorValue, isLogTabOpen } = useAppSelector(editor_state);
27
  const [markdownCode, setMarkdownCode] = useState('');
28
  const [prevMarkdownCode, setPrevMarkdownCode] = useState(markdownCode);
 
 
 
 
 
 
 
29
 
30
  const isValidCodeBlock = (markdownCode: string) => {
31
  return markdownCode && markdownCode.length > 10 && markdownCode.includes('\n');
@@ -44,12 +51,34 @@ const Playground = () => {
44
  };
45
  }, [markdownCode, prevMarkdownCode, dispatch]);
46
 
 
 
47
 
48
- const { messages, input, setInput, handleSubmit } = useChat({
 
49
  onError: (error) => {
50
- console.error(error);
51
  },
52
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  useEffect(() => {
55
  if (!esbuildStatus.isReady) {
@@ -97,12 +126,50 @@ const Playground = () => {
97
  return (
98
  <div style={{ background: theme.background }}>
99
  <div className="flex flex-col">
100
- <div className="px-4 pb-2 pt-3 shadow-lg sm:pb-3 sm:pt-4">
101
- <form
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  ref={formRef}
103
- onSubmit={handleSubmit}
104
  className="relative w-full"
105
  >
 
106
  <textarea ref={inputRef} onChange={(e) => setInput(e.target.value)}
107
  placeholder="Create a twitter clone"
108
  onKeyDown={(e) => {
 
26
  const { logs, editorValue, isLogTabOpen } = useAppSelector(editor_state);
27
  const [markdownCode, setMarkdownCode] = useState('');
28
  const [prevMarkdownCode, setPrevMarkdownCode] = useState(markdownCode);
29
+ const [isSystemInputVisible, setSystemInputVisible] = useState(true);
30
+ const [isModelInputVisible, setModelInputVisible] = useState(true);
31
+ const [selectedModel, setSelectedModel] = useState("GPT-4");
32
+
33
+ const [systemMessage, setSystemMessage] = useState(
34
+ "You are a helpful AI assistant. Assist the user in writing React code, ensuring that all logic is contained within a single app component file and that the output is rendered to the DOM's root element."
35
+ );
36
 
37
  const isValidCodeBlock = (markdownCode: string) => {
38
  return markdownCode && markdownCode.length > 10 && markdownCode.includes('\n');
 
51
  };
52
  }, [markdownCode, prevMarkdownCode, dispatch]);
53
 
54
+
55
+
56
 
57
+
58
+ const { setMessages, messages, input, setInput, handleSubmit, ...rest } = useChat({
59
  onError: (error) => {
60
+ console.error(error);
61
  },
62
+ });
63
+
64
+ const modifiedHandleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
65
+ // Stop the default form submission behavior
66
+ e.preventDefault();
67
+
68
+ // Prepend the system message if it's not already there
69
+ if (messages.length === 0 || messages[0].role !== "system") {
70
+ const newMessage = {
71
+ role: "system",
72
+ content: systemMessage
73
+ };
74
+ // @ts-ignore
75
+ setMessages(prevMessages => [newMessage, ...prevMessages]);
76
+ }
77
+
78
+ // Call the original handleSubmit from useChat
79
+ handleSubmit(e);
80
+ };
81
+
82
 
83
  useEffect(() => {
84
  if (!esbuildStatus.isReady) {
 
126
  return (
127
  <div style={{ background: theme.background }}>
128
  <div className="flex flex-col">
129
+ <div className="px-4 pb-2 pt-3 shadow-lg sm:pb-3 sm:pt-4">
130
+ <span className="text-lg font-semibold">System Prompt</span>
131
+ <button
132
+ onClick={() => setSystemInputVisible(!isSystemInputVisible)}
133
+ className="p-2 rounded-full hover:bg-gray-200 transition duration-300"
134
+ >
135
+ {isSystemInputVisible ? '−' : '+'}
136
+ </button>
137
+ <span className="text-lg font-semibold">Model</span>
138
+ <button
139
+ onClick={() => setModelInputVisible(!isModelInputVisible)}
140
+ className="p-2 rounded-full hover:bg-gray-200 transition duration-300"
141
+ >
142
+ {isModelInputVisible ? '−' : '+'}
143
+ </button>
144
+
145
+ {isSystemInputVisible && (
146
+ <textarea
147
+ className="textarea my-4 border p-2 rounded-md shadow-sm w-full resize-none"
148
+ value={systemMessage || ''}
149
+ onChange={e => setSystemMessage(e.target.value)}
150
+ placeholder="Enter custom system prompt here"
151
+ rows={2} // initial number of rows, can be adjusted
152
+ style={{ overflowWrap: 'break-word', whiteSpace: 'pre-wrap' }}
153
+ />
154
+ )}
155
+ {isModelInputVisible && (
156
+ <div className="my-4">
157
+ <select
158
+ value={selectedModel}
159
+ onChange={(e) => setSelectedModel(e.target.value)}
160
+ className="border p-2 rounded-md shadow-sm w-full bg-transparent text-gray-700"
161
+ >
162
+ <option value="GPT-4">GPT-4</option>
163
+ </select>
164
+ </div>
165
+ )}
166
+
167
+ <form
168
  ref={formRef}
169
+ onSubmit={modifiedHandleSubmit}
170
  className="relative w-full"
171
  >
172
+
173
  <textarea ref={inputRef} onChange={(e) => setInput(e.target.value)}
174
  placeholder="Create a twitter clone"
175
  onKeyDown={(e) => {
package.json CHANGED
@@ -5,7 +5,7 @@
5
  "scripts": {
6
  "dev": "next dev --turbo",
7
  "build": "next build",
8
- "start": "next start",
9
  "lint": "next lint",
10
  "codegen": "graphql-codegen --config codegen.yml"
11
  },
 
5
  "scripts": {
6
  "dev": "next dev --turbo",
7
  "build": "next build",
8
+ "start": "node .next/standalone/server.js",
9
  "lint": "next lint",
10
  "codegen": "graphql-codegen --config codegen.yml"
11
  },
pages/api/chat/index.ts CHANGED
@@ -9,15 +9,7 @@ const openai = new OpenAIApi(config);
9
  export const runtime = "edge";
10
 
11
  export default async function(req: Request) {
12
- let { messages } = 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: "You are a helpful AI assistant. Assist the user in writing React code, ensuring that all logic is contained within a single app component file and that the output is rendered to the DOM's root element."
19
- }, ...messages];
20
- }
21
 
22
  const response = await openai.createChatCompletion({
23
  model: 'gpt-4',
 
9
  export const runtime = "edge";
10
 
11
  export default async function(req: Request) {
12
+ const { messages } = await req.json();
 
 
 
 
 
 
 
 
13
 
14
  const response = await openai.createChatCompletion({
15
  model: 'gpt-4',