Skip to content

Commit 5f171b8

Browse files
committed
improved code generation for gpt-4-turbo
1 parent 3be5f7d commit 5f171b8

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

src/engine/prompts/script.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,27 @@ ${instructions.script.map((instruction) => `- ${instruction}`).join('\n')}
1313
Code formatting rules:
1414
- we use 1 space for indentation
1515
- code is compressed, all JS variables have maximum 3 characters
16+
- we must store everything in appData, so please begin your code with \`var app = window.appData || {};\`
1617
- this is not a tutorial or a demo, but the final project
18+
19+
Here is the current page structure:
1720
\`\`\`html
1821
${
1922
// to save space, we only give essential info to the model
2023
DOMPurify.sanitize(html, {
2124
ALLOWED_TAGS: ['div', 'button', 'canvas'],
2225
ALLOWED_ATTR: ['classname', 'id', 'width', 'height'],
2326
}).replace(/(^[ \t]*\n)/gm, '') // remove all empty lines, too
24-
}<script>var app={};`
27+
}
28+
<script>
29+
window.appData = {};
30+
</script>
31+
\`\`\`
32+
33+
Your turn! (remember we want the FINAL version)
34+
35+
# Output
36+
\`\`\`javascript
37+
`
2538

2639
export const scriptPrompt = genericScript('appData')

src/engine/prompts/tasks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Brief: A startup website called Aisy about a new cool SaaS product for creators
2525
Spec: {
2626
"summary": "Website of startup called Aisy, selling an AI tool for making movies",
2727
"layout": "A classic SaaS product launch page, with a header section, a main content section, and a footer",
28-
"art direction": "The design is fresh and sleek, with a sans serif font abd original colors on the theme of AI and movies (purple, black, grey, red)",
28+
"art direction": "The design is fresh and sleek, with a sans serif font and original colors on the theme of AI and movies (purple, black, grey, red)",
2929
"text content": "Page contains marketing content to explain why people should use Aisy to generate movies. Text is concise and to the point, with punch lines for titles. It should make us wish we purchased the product.",
3030
"interactivity": "no javascript needed",
3131
"js modules": "no library available",

src/providers/openai/index.ts

+35-14
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,19 @@ export const imagineHTML = async (
6060
return mocks.html
6161
}
6262

63-
const raw = await imagineString(prompt, presets.html, model, apiKey)
63+
let raw = await imagineString(prompt, presets.html, model, apiKey)
64+
65+
raw = raw
66+
.replaceAll("```html\n", "")
67+
.replaceAll("```html", "")
6468

65-
console.log('imagineHTML> raw:', raw)
69+
// we remove everything after the last ```
70+
raw = raw.split('```')[0].trim()
71+
72+
// const toStrip = `<div ${raw}`
73+
const toStrip = `${raw}`
6674

67-
const toStrip = `<div ${raw}`
75+
console.log('imagineHTML> raw:', raw)
6876

6977
// we don't care about hallucinated image src
7078
const toPurify = toStrip.replace(/src="[^"]+/g, 'src="')
@@ -122,23 +130,32 @@ Object.values(libraries).map(({ prod }) => prod).join('\n')
122130
window.appData = {};
123131
${output}`.trim()
124132
*/
125-
let script = `<script>
126-
window.appData = {};
127-
var app = window.appData;
128-
${output}`.trim()
129-
// for some reason the LLM sometimes generates JS code with ‘ instead of '
130-
script = script.replace('‘', "'")
131133

132-
// for some reason the LLM sometimes believe it is in a Markdown file
133-
// so we remove this extra garbage
134-
script = script.split('```')[0].trim()
134+
let script = `${output}`.trim()
135+
136+
// we remove all the output markdown
137+
script = script
138+
.replaceAll('```html\n', '')
139+
.replaceAll('```html', '')
140+
.replaceAll('```javascript\n', '')
141+
.replaceAll('```javascript\n', '')
142+
.replaceAll('```javascript', '')
135143

144+
// for some reason the LLM sometimes generates JS code with ‘ instead of '
145+
.replaceAll('‘', "'")
146+
147+
// we remove everything after the last ```
148+
script = (script.split('```').shift() || "").trim()
149+
150+
if (!script.startsWith('<script>')) {
151+
script = `<script>${script}`
152+
}
136153
// it is imperative that we ignore everything that might have been added after the main script
137154
script = (script.split('</script>').shift() || "").trim()
138155

139156
// todo: should be done in a better way
140157
if (!script.endsWith('</script>')) {
141-
script += '</script>'
158+
script = `${script}</script>`
142159
}
143160

144161
return script
@@ -176,14 +193,18 @@ export const imagineJSON = async <T>(
176193
.replaceAll("[```json", "")
177194
.replaceAll("{{```json", "")
178195
.replaceAll("{```json", "")
196+
.replaceAll("```json\n", "")
197+
.replaceAll("```json", "")
179198
.replaceAll("[[```", "")
180199
.replaceAll("[```", "")
181200
.replaceAll("{{```", "")
182201
.replaceAll("{```", "")
183202
.replaceAll("{json", "")
184203
.replaceAll("[json", "")
185-
.replaceAll("```", "")
186204

205+
// we remove everything after the last ```
206+
raw = raw.split('```')[0].trim()
207+
187208
// try to fix the LLM adding commas at the end of each line
188209
const regex = /\,(?!\s*?[\{\[\"\'\w])/g
189210
const input = raw.replace(regex, '')

0 commit comments

Comments
 (0)