-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
openai.client.ts
166 lines (139 loc) · 3.69 KB
/
openai.client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import OpenAI from "openai";
import {
ChatCompletionContentPart,
ChatCompletionMessageParam,
} from "openai/resources";
import { systemPrompt } from "./openai.prompts";
type UITransformerConfig = {
apiKey: string;
base64Image: string;
imageTexts?: string | null;
maxTokens: number;
onChunk?: (chunk: string) => Promise<void>;
stack?: string;
customInstructions?: string | null;
filePath: string;
fileContent?: string | null;
};
const START_QUOTE_REGEX = /```[^\n]*\n/;
const PARTIAL_END_QUOTE = /\n`{0,3}$/;
const FULL_END_QUOTE_REGEX = /\n```/;
const REMOVE_PREFIX_REGEX = /.*```[^\n]*\n/s;
const REMOVE_SUFFIX_REGEX = /\n```.*/s;
export const uiToComponent = async ({
base64Image,
imageTexts,
apiKey,
stack,
onChunk,
maxTokens,
customInstructions = "",
filePath,
fileContent,
}: UITransformerConfig) => {
const client = new OpenAI({ apiKey });
const messages: ChatCompletionMessageParam[] = [];
// SYSTEM PROMPT
messages.push({
role: "system",
content: customInstructions
? systemPrompt + "\n\n" + customInstructions
: systemPrompt,
});
// USER PROMPT
const userMessageParts: ChatCompletionContentPart[] = [];
userMessageParts.push({
type: "image_url",
image_url: {
url: base64Image,
detail: "high",
},
});
userMessageParts.push({
type: "text",
text: "Turn these wireframes into code",
});
if (imageTexts && imageTexts.length > 0)
userMessageParts.push({
type: "text",
text: "The wireframes text is: \n" + imageTexts,
});
else
userMessageParts.push({
type: "text",
text: "The text could be extracted from the wireframes.",
});
if (stack && stack.length > 0)
userMessageParts.push({
type: "text",
text: "The project's stack is: " + stack,
});
userMessageParts.push({
type: "text",
text: "The file path is: " + filePath,
});
if (fileContent && fileContent.length > 0) {
userMessageParts.push({
type: "text",
text: "The current file content is: \n```\n" + fileContent + "\n```",
});
} else {
userMessageParts.push({
type: "text",
text: "The file is currently empty.",
});
}
messages.push({
role: "user",
content: userMessageParts,
});
const response = await client.chat.completions.create({
model: "gpt-4-vision-preview",
stream: true,
max_tokens: maxTokens,
messages: messages,
});
// #region BUFFER
let buffer = "";
let output = "";
let hasCodeStarted = false;
for await (const chunk of response) {
if (chunk.choices.length === 0 || !chunk.choices[0].delta.content) continue;
const textChunk = chunk.choices[0].delta.content;
buffer += textChunk;
// We want to strip everything but the code
if (!hasCodeStarted) {
hasCodeStarted = START_QUOTE_REGEX.test(buffer);
if (!hasCodeStarted) continue;
buffer = buffer.replace(REMOVE_PREFIX_REGEX, "");
}
// Stop at full end quote
if (FULL_END_QUOTE_REGEX.test(buffer)) {
const code = buffer.replace(REMOVE_SUFFIX_REGEX, "");
if (code !== "") {
output += code;
if (onChunk) await onChunk(code);
}
break;
}
// Buffer if not a full end quote, but a partial one
if (PARTIAL_END_QUOTE.test(buffer)) {
const parts = buffer.split("\n");
buffer = "\n" + parts.pop() || "";
const code = parts.join("\n");
if (code !== "") {
output += code;
if (onChunk) await onChunk(code);
}
continue;
}
// Skip empty buffer
if (buffer === "") continue;
// Dump buffer
output += buffer;
if (onChunk) await onChunk(buffer);
buffer = "";
}
// #endregion
return output;
};