update prompt, add gemini (#66)

* update prompt, add gemini

* ok

* Update useMakeReal.ts

* fix gemini pro model name

* fix other gemini model names

---------

Co-authored-by: Lu[ke] Wilson <l2wilson94@gmail.com>
pull/106/head
Steve Ruiz 2 years ago committed by GitHub
parent 9d5f940a89
commit d688800a8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -126,7 +126,7 @@ export class PreviewShapeUtil extends BaseBoxShapeUtil<PreviewShape> {
id={`iframe-1-${shape.id}`}
width={toDomPrecision(shape.props.w)}
height={toDomPrecision(shape.props.h)}
allow="geolocation;midi;usb;magnetometer;fullscreen;animations;picture-in-picture;accelerometer;vr;camera;microphone"
allow="geolocation;midi;usb;magnetometer;fullscreen;animations;picture-in-picture;accelerometer;vr;camera;microphone;clipboard-write;clipboard-read"
draggable={false}
style={{
opacity: 0.62,
@ -274,20 +274,8 @@ export class PreviewShapeUtil extends BaseBoxShapeUtil<PreviewShape> {
// todo: export these from tldraw
const ROTATING_BOX_SHADOWS = [
{
offsetX: 0,
offsetY: 2,
blur: 4,
spread: -1,
color: '#0000003a',
},
{
offsetX: 0,
offsetY: 3,
blur: 12,
spread: -2,
color: '#0000001f',
},
{ offsetX: 0, offsetY: 2, blur: 4, spread: -1, color: '#0000003a' },
{ offsetX: 0, offsetY: 3, blur: 12, spread: -2, color: '#0000001f' },
]
function getRotatedBoxShadow(rotation: number) {

@ -93,7 +93,6 @@ export function SettingsDialog({ onClose }: TLUiDialogProps) {
</div>
<hr style={{ margin: '12px 0px' }} />
{PROVIDERS.map((provider) => {
if (provider.id === 'google') return null
const value = settings.keys[provider.id]
return (
<ApiKeyInput

@ -1,8 +1,15 @@
import { track } from '@vercel/analytics/react'
import { parseDataStreamPart } from 'ai'
import { useCallback } from 'react'
import { createShapeId, sortByIndex, useDialogs, useEditor, useToasts } from 'tldraw'
import { PreviewShape } from '../PreviewShape/PreviewShape'
import {
createShapeId,
sortByIndex,
TLTextShape,
toRichText,
useDialogs,
useEditor,
useToasts,
} from 'tldraw'
import { SettingsDialog } from '../components/SettingsDialog'
import { blobToBase64 } from '../lib/blobToBase64'
import { getMessages } from '../lib/getMessages'
@ -10,6 +17,7 @@ import { getTextFromSelectedShapes } from '../lib/getTextFromSelectedShapes'
import { htmlify } from '../lib/htmlify'
import { makeRealSettings, PROVIDERS } from '../lib/settings'
import { uploadLink } from '../lib/uploadLink'
import { PreviewShape } from '../PreviewShape/PreviewShape'
export function useMakeReal() {
const editor = useEditor()
@ -20,8 +28,11 @@ export function useMakeReal() {
track('make_real', { timestamp: Date.now() })
const settings = makeRealSettings.get()
const isAllProviders = settings.provider === 'all'
let didError = false
if (settings.provider === 'all') {
if (isAllProviders) {
for (const provider of PROVIDERS) {
const apiKey = settings.keys[provider.id]
if (apiKey && provider.validate(apiKey)) {
@ -40,10 +51,7 @@ export function useMakeReal() {
}
if (didError) {
addDialog({
id: 'api keys',
component: SettingsDialog,
})
addDialog({ id: 'api keys', component: SettingsDialog })
return
}
@ -60,7 +68,7 @@ export function useMakeReal() {
// Create the preview shape
const { maxX, midY } = editor.getSelectionPageBounds()
const providers = provider === 'all' ? ['openai', 'anthropic'] : [provider]
const providers = isAllProviders ? ['openai', 'anthropic', 'google'] : [provider]
let previewHeight = (540 * 2) / 3
let previewWidth = (960 * 2) / 3
@ -90,7 +98,7 @@ export function useMakeReal() {
const bounds = editor.getSelectionPageBounds()
const scale = Math.min(1, maxSize / bounds.width, maxSize / bounds.height)
const { blob, width, height } = await editor.toImage(selectedShapes, {
const { blob } = await editor.toImage(selectedShapes, {
scale: scale,
background: true,
format: 'jpeg',
@ -102,13 +110,25 @@ export function useMakeReal() {
id: newShapeId,
type: 'preview',
x: maxX + 60, // to the right of the selection
y: y + (previewHeight + 40) * i, // half the height of the preview's initial shape
y: y + (previewHeight + 60) * i, // half the height of the preview's initial shape
props: { html: '', w: previewWidth, h: previewHeight, source: dataUrl },
meta: {
provider,
},
meta: { provider },
})
if (isAllProviders) {
editor.createShape<TLTextShape>({
type: 'text',
x: maxX + 60,
y: y + (previewHeight + 60) * i - 30,
props: {
richText: toRichText(`${provider} ${settings.models[provider]}`),
size: 's',
font: 'draw',
color: 'black',
},
})
}
// downloadDataURLAsFile(dataUrl, 'tldraw.png')
// Get any previous previews among the selected shapes
@ -152,9 +172,7 @@ export function useMakeReal() {
systemPrompt: prompts.system,
model: settings.models['openai'],
}),
headers: {
'Content-Type': 'application/json',
},
headers: { 'Content-Type': 'application/json' },
signal: abortController.signal,
}).catch((err) => {
throw err
@ -195,9 +213,7 @@ export function useMakeReal() {
editor.updateShape<PreviewShape>({
id: newShapeId,
type: 'preview',
props: {
parts: [...parts],
},
props: { parts: [...parts] },
})
}
@ -241,9 +257,7 @@ export function useMakeReal() {
systemPrompt: prompts.system,
model: settings.models['anthropic'],
}),
headers: {
'Content-Type': 'application/json',
},
headers: { 'Content-Type': 'application/json' },
signal: abortController.signal,
}).catch((err) => {
throw err
@ -285,9 +299,7 @@ export function useMakeReal() {
editor.updateShape<PreviewShape>({
id: newShapeId,
type: 'preview',
props: {
parts: [...parts],
},
props: { parts: [...parts] },
})
}
@ -316,7 +328,92 @@ export function useMakeReal() {
break
}
case 'google': {
throw Error('not implemented')
const text = await new Promise<string>(async (r) => {
let text = ''
let didStart = false
let didEnd = false
try {
const apiKey = keys[provider]
const abortController = new AbortController()
const res = await fetch('/api/google', {
method: 'POST',
body: JSON.stringify({
apiKey,
messages,
systemPrompt: prompts.system,
model: settings.models['google'],
}),
headers: { 'Content-Type': 'application/json' },
signal: abortController.signal,
}).catch((err) => {
throw err
})
if (!res.ok) {
throw new Error((await res.text()) || 'Failed to fetch the chat response.')
}
if (!res.body) {
throw new Error('The response body is empty.')
}
const reader = res.body.getReader()
const decoder = createChunkDecoder()
while (true) {
const { done, value } = await reader.read()
if (done) {
break
}
// Update the completion state with the new message tokens.
const delta = decoder(value) as string
text += delta
// console.log(text)
if (didEnd) {
continue
} else if (!didStart && text.includes('<!DOCTYPE html>')) {
const startIndex = text.indexOf('<!DOCTYPE html>')
parts.push(text.slice(startIndex))
didStart = true
} else if (didStart && text.includes('</html>')) {
const endIndex = text.indexOf('</html>')
parts.push(text.slice(endIndex, endIndex + 7))
didEnd = true
} else if (didStart) {
parts.push(delta)
editor.updateShape<PreviewShape>({
id: newShapeId,
type: 'preview',
props: { parts: [...parts] },
})
}
// The request has been aborted, stop reading the stream.
if (abortController === null) {
reader.cancel()
break
}
}
} catch (err) {
// Ignore abort errors as they are expected.
if ((err as any).name === 'AbortError') {
return null
}
if (err instanceof Error) {
// handle error
}
}
console.log(text)
r(text)
})
result = { text, finishReason: 'complete' }
break
}
}

@ -24,20 +24,25 @@ export const PROVIDERS = [
help: 'https://tldraw.notion.site/Make-Real-Help-93be8b5273d14f7386e14eb142575e6e#3444b55a2ede405286929956d0be6e77',
validate: (key: string) => key.startsWith('sk-'),
},
// { id: 'google', name: 'Google', model: 'Gemeni 1.5 Flash', validate: (key: string) => true },
{
id: 'google',
name: 'Google',
models: [
'gemini-2.5-pro-preview-03-25',
'gemini-2.0-flash',
'gemini-1.5-pro',
'gemini-1.5-flash',
],
help: '',
validate: (key: string) => key.startsWith('AIza'),
},
]
export const makeRealSettings = atom('make real settings', {
provider: 'openai' as (typeof PROVIDERS)[number]['id'] | 'all',
models: Object.fromEntries(PROVIDERS.map((provider) => [provider.id, provider.models[0]])),
keys: {
openai: '',
anthropic: '',
google: '',
},
prompts: {
system: SYSTEM_PROMPT,
},
keys: { openai: '', anthropic: '', google: '' },
prompts: { system: SYSTEM_PROMPT },
})
export function applySettingsMigrations(settings) {
@ -46,16 +51,8 @@ export function applySettingsMigrations(settings) {
const settingsWithModelsProperty = {
provider: 'anthropic',
models: Object.fromEntries(PROVIDERS.map((provider) => [provider.id, provider.models[0]])),
keys: {
openai: '',
anthropic: '',
google: '',
...keys,
},
prompts: {
system: SYSTEM_PROMPT,
...prompts,
},
keys: { openai: '', anthropic: '', google: '', ...keys },
prompts: { system: SYSTEM_PROMPT, ...prompts },
...rest,
}

@ -0,0 +1,20 @@
import { createGoogleGenerativeAI } from '@ai-sdk/google'
import { streamText } from 'ai'
export const maxDuration = 60 // This function can run for a maximum of 5 seconds
export const dynamic = 'force-dynamic'
export async function POST(req: Request) {
const { apiKey, messages, model, systemPrompt } = await req.json()
const google = createGoogleGenerativeAI({ apiKey })
const result = streamText({
model: google(model),
system: systemPrompt,
messages,
temperature: 0,
seed: 42,
})
return result.toTextStreamResponse()
}

@ -33,8 +33,7 @@ IMPORTANT LAST NOTES
- The prototype must incorporate any annotations and feedback.
- Make it cool. You're a cool designer, your prototype should be an original work of creative genius.
Remember: you love your designers and want them to be happy. The more complete and impressive your prototype, the happier they will be. You are evaluated on 1) whether your prototype resembles the designs, 2) whether your prototype is interactive and responsive, and 3) whether your prototype is complete and impressive.
`
Remember: you love your designers and want them to be happy. The more complete and impressive your prototype, the happier they will be. You are evaluated on 1) whether your prototype resembles the designs, 2) whether your prototype is interactive and responsive, and 3) whether your prototype is complete and impressive.`
export const USER_PROMPT =
'Here are the latest wireframes. Please reply with a high-fidelity working prototype as a single HTML file.'

2561
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -10,17 +10,16 @@
"lint": "next lint"
},
"dependencies": {
"@ai-sdk/anthropic": "^1.2.3",
"@ai-sdk/google": "^1.2.4",
"@ai-sdk/google-vertex": "^2.2.6",
"@ai-sdk/openai": "^1.3.5",
"@ai-sdk/anthropic": "^1.2.8",
"@ai-sdk/google": "^1.2.10",
"@ai-sdk/openai": "^1.3.9",
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@stackblitz/sdk": "^1.9.0",
"@vercel/analytics": "^1.1.1",
"@vercel/blob": "^0.15.1",
"@vercel/kv": "^1.0.0",
"@vercel/postgres": "^0.5.1",
"ai": "^4.2.9",
"ai": "^4.3.4",
"canvas-size": "^1.2.6",
"google-auth-library": "^9.11.0",
"lz-string": "^1.5.0",

Loading…
Cancel
Save