Spaces:
Runtime error
Runtime error
feat: calls to OpenAI API are done in the frontend only
Browse files- src/components/GameCreator.tsx +11 -9
- src/pages/api/generate.ts +5 -1
- src/services/api/index.ts +5 -10
- src/services/api/openai.ts +0 -6
src/components/GameCreator.tsx
CHANGED
@@ -76,6 +76,8 @@ import { RunCircle } from "@mui/icons-material";
|
|
76 |
import Introduction from "@/components/Introduction";
|
77 |
import Instructions from "@/components/Instructions";
|
78 |
import Examples from "@/components/Examples";
|
|
|
|
|
79 |
const MonacoEditor = dynamic(import("@monaco-editor/react"), { ssr: false });
|
80 |
|
81 |
export interface ShareProps {
|
@@ -276,11 +278,15 @@ export default function GameCreator() {
|
|
276 |
const formObject = Object.fromEntries(formData);
|
277 |
try {
|
278 |
setLoading(true);
|
279 |
-
|
280 |
-
|
281 |
-
formObject
|
282 |
);
|
283 |
-
const answer =
|
|
|
|
|
|
|
|
|
284 |
setAnswers(previousAnswers => [answer, ...previousAnswers]);
|
285 |
setRunningId(answer.id);
|
286 |
setActiveId(answer.id);
|
@@ -296,11 +302,7 @@ export default function GameCreator() {
|
|
296 |
}}
|
297 |
>
|
298 |
<Stack sx={{ p: 1, pl: 0, gap: 2 }}>
|
299 |
-
<Secret
|
300 |
-
label="OpenAI API Key"
|
301 |
-
name="openAIAPIKey"
|
302 |
-
required={process.env.NODE_ENV === "production"}
|
303 |
-
/>
|
304 |
|
305 |
<Stack direction="row" spacing={1}>
|
306 |
<TextField
|
|
|
76 |
import Introduction from "@/components/Introduction";
|
77 |
import Instructions from "@/components/Instructions";
|
78 |
import Examples from "@/components/Examples";
|
79 |
+
import { toOpenAI } from "@/services/api";
|
80 |
+
import { createClient } from "@/services/api/openai";
|
81 |
const MonacoEditor = dynamic(import("@monaco-editor/react"), { ssr: false });
|
82 |
|
83 |
export interface ShareProps {
|
|
|
278 |
const formObject = Object.fromEntries(formData);
|
279 |
try {
|
280 |
setLoading(true);
|
281 |
+
|
282 |
+
const client = createClient(
|
283 |
+
formObject.openAIAPIKey as string
|
284 |
);
|
285 |
+
const answer = await toOpenAI({
|
286 |
+
...formObject,
|
287 |
+
client,
|
288 |
+
});
|
289 |
+
|
290 |
setAnswers(previousAnswers => [answer, ...previousAnswers]);
|
291 |
setRunningId(answer.id);
|
292 |
setActiveId(answer.id);
|
|
|
302 |
}}
|
303 |
>
|
304 |
<Stack sx={{ p: 1, pl: 0, gap: 2 }}>
|
305 |
+
<Secret label="OpenAI API Key" name="openAIAPIKey" />
|
|
|
|
|
|
|
|
|
306 |
|
307 |
<Stack direction="row" spacing={1}>
|
308 |
<TextField
|
src/pages/api/generate.ts
CHANGED
@@ -1,12 +1,16 @@
|
|
1 |
import { toOpenAI } from "@/services/api";
|
2 |
import { NextApiRequest, NextApiResponse } from "next";
|
3 |
import { AxiosError } from "axios";
|
|
|
|
|
4 |
|
5 |
export default async function handler(request: NextApiRequest, response: NextApiResponse) {
|
6 |
switch (request.method) {
|
7 |
case "POST":
|
8 |
try {
|
9 |
-
const
|
|
|
|
|
10 |
return response.status(200).json(answer);
|
11 |
} catch (error) {
|
12 |
return response.status((error as AxiosError).status ?? 500).json(error);
|
|
|
1 |
import { toOpenAI } from "@/services/api";
|
2 |
import { NextApiRequest, NextApiResponse } from "next";
|
3 |
import { AxiosError } from "axios";
|
4 |
+
import process from "node:process";
|
5 |
+
import { createClient } from "@/services/api/openai";
|
6 |
|
7 |
export default async function handler(request: NextApiRequest, response: NextApiResponse) {
|
8 |
switch (request.method) {
|
9 |
case "POST":
|
10 |
try {
|
11 |
+
const client = createClient(process.env.OPENAI_API_KEY as string);
|
12 |
+
|
13 |
+
const answer = await toOpenAI({ ...request.body, client });
|
14 |
return response.status(200).json(answer);
|
15 |
} catch (error) {
|
16 |
return response.status((error as AxiosError).status ?? 500).json(error);
|
src/services/api/index.ts
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import { ChatCompletionRequestMessage } from "openai";
|
2 |
import { nanoid } from "nanoid";
|
3 |
-
import { createClient, openai } from "@/services/api/openai";
|
4 |
import { extractCode, miniPrompt } from "@/utils/prompt";
|
5 |
import { systemMessage } from "@/constants";
|
6 |
|
@@ -11,16 +10,14 @@ export async function toOpenAI({
|
|
11 |
template = "",
|
12 |
model = "gpt-3.5-turbo",
|
13 |
maxTokens = "2048",
|
14 |
-
|
15 |
}) {
|
16 |
-
|
17 |
-
|
18 |
-
let client = openai;
|
19 |
-
|
20 |
-
if (openAIAPIKey !== "") {
|
21 |
-
client = createClient(openAIAPIKey);
|
22 |
}
|
23 |
|
|
|
|
|
24 |
const nextMessage: ChatCompletionRequestMessage = {
|
25 |
role: "user",
|
26 |
content: miniPrompt`
|
@@ -53,8 +50,6 @@ export async function toOpenAI({
|
|
53 |
const { message } = response.data.choices[0];
|
54 |
|
55 |
if (message) {
|
56 |
-
console.log("ORIGINAL OUTPUT");
|
57 |
-
console.log(message.content);
|
58 |
return {
|
59 |
...message,
|
60 |
content: extractCode(message.content).replace(
|
|
|
1 |
import { ChatCompletionRequestMessage } from "openai";
|
2 |
import { nanoid } from "nanoid";
|
|
|
3 |
import { extractCode, miniPrompt } from "@/utils/prompt";
|
4 |
import { systemMessage } from "@/constants";
|
5 |
|
|
|
10 |
template = "",
|
11 |
model = "gpt-3.5-turbo",
|
12 |
maxTokens = "2048",
|
13 |
+
client = null,
|
14 |
}) {
|
15 |
+
if (client === null) {
|
16 |
+
throw new Error("OpenAI client is not defined");
|
|
|
|
|
|
|
|
|
17 |
}
|
18 |
|
19 |
+
const prompt_ = prompt.trim();
|
20 |
+
|
21 |
const nextMessage: ChatCompletionRequestMessage = {
|
22 |
role: "user",
|
23 |
content: miniPrompt`
|
|
|
50 |
const { message } = response.data.choices[0];
|
51 |
|
52 |
if (message) {
|
|
|
|
|
53 |
return {
|
54 |
...message,
|
55 |
content: extractCode(message.content).replace(
|
src/services/api/openai.ts
CHANGED
@@ -1,10 +1,4 @@
|
|
1 |
import { Configuration, OpenAIApi } from "openai";
|
2 |
-
import process from "node:process";
|
3 |
-
|
4 |
-
export const configuration = new Configuration({
|
5 |
-
apiKey: process.env.OPENAI_API_KEY,
|
6 |
-
});
|
7 |
-
export const openai = new OpenAIApi(configuration);
|
8 |
|
9 |
export const createClient = (apiKey: string) => {
|
10 |
return new OpenAIApi(new Configuration({ apiKey }));
|
|
|
1 |
import { Configuration, OpenAIApi } from "openai";
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
export const createClient = (apiKey: string) => {
|
4 |
return new OpenAIApi(new Configuration({ apiKey }));
|