Skip to content

Instantly share code, notes, and snippets.

@yinkakun
Created November 18, 2023 11:35
Show Gist options
  • Save yinkakun/c64192af347667229f1d0f798dca4029 to your computer and use it in GitHub Desktop.
Save yinkakun/c64192af347667229f1d0f798dca4029 to your computer and use it in GitHub Desktop.
gpt-4-prompt.ts
const systemPrompt = `You are an expert web developer who specializes in tailwind css.
A user will provide you with a low-fidelity wireframe of an application.
You will return a single html file that uses HTML, tailwind css, and JavaScript to create a high fidelity website.
Include any extra CSS and JavaScript in the html file.
If you have any images, load them from Unsplash or use solid colored rectangles.
The user will provide you with notes in blue or red text, arrows, or drawings.
The user may also include images of other websites as style references. Transfer the styles as best as you can, matching fonts / colors / layouts.
They may also provide you with the html of a previous design that they want you to iterate from.
Carry out any changes they request from you.
In the wireframe, the previous design's html will appear as a white rectangle.
For your reference, all text from the image will also be provided to you as a list of strings, separated by newlines. Use them as a reference if any text is hard to read.
Use creative license to make the application more fleshed out.
Use JavaScript modules and unpkg to import any necessary dependencies.
Respond ONLY with the contents of the html file.`
export async function getHtmlFromOpenAI({
image,
html,
apiKey,
text,
}: {
image: string
html: string
apiKey: string
text: string
}) {
const body: GPT4VCompletionRequest = {
model: 'gpt-4-vision-preview',
max_tokens: 4096,
temperature: 0,
messages: [
{
role: 'system',
content: systemPrompt,
},
{
role: 'user',
content: [
{
type: 'image_url',
image_url: {
url: image,
detail: 'high',
},
},
{
type: 'text',
text: 'Turn this into a single html file using tailwind.',
},
{
type: 'text',
text: html,
},
{
type: 'text',
text,
},
],
},
],
}
let json = null
if (!apiKey) {
throw Error('You need to provide an API key (sorry)')
}
try {
const resp = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify(body),
})
console.log(resp)
json = await resp.json()
} catch (e) {
console.log(e)
}
return json
}
type MessageContent =
| string
| (
| string
| {
type: 'image_url'
image_url:
| string
| {
url: string
detail: 'low' | 'high' | 'auto'
}
}
| {
type: 'text'
text: string
}
)[]
export type GPT4VCompletionRequest = {
model: 'gpt-4-vision-preview'
messages: {
role: 'system' | 'user' | 'assistant' | 'function'
content: MessageContent
name?: string | undefined
}[]
functions?: any[] | undefined
function_call?: any | undefined
stream?: boolean | undefined
temperature?: number | undefined
top_p?: number | undefined
max_tokens?: number | undefined
n?: number | undefined
best_of?: number | undefined
frequency_penalty?: number | undefined
presence_penalty?: number | undefined
logit_bias?:
| {
[x: string]: number
}
| undefined
stop?: (string[] | string) | undefined
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment