Skip to content

Commit ac18bb6

Browse files
committed
prompt uses genText
1 parent b53989b commit ac18bb6

File tree

1 file changed

+46
-29
lines changed
  • typescript/packages/lookslike-high-level/src/recipes

1 file changed

+46
-29
lines changed

typescript/packages/lookslike-high-level/src/recipes/prompts.ts

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ import { html } from "@commontools/common-html";
22
import {
33
recipe,
44
lift,
5-
generateData,
5+
generateText,
66
handler,
77
NAME,
88
UI,
9-
str,
109
} from "@commontools/common-builder";
1110
import { launch } from '../data.js';
11+
import { z } from 'zod';
12+
import zodToJsonSchema from 'zod-to-json-schema';
13+
14+
const Prompt = z.object({
15+
prompt: z.string().describe('Image generation prompt'),
16+
});
17+
type Prompt = z.infer<typeof Prompt>;
18+
const jsonSchema = JSON.stringify(zodToJsonSchema(Prompt), null, 2);
1219

1320
const imageUrl = lift(
1421
({ title }) => `https://ct-img.m4ke.workers.dev/?prompt=${encodeURIComponent(title)}`
@@ -22,41 +29,51 @@ const updateTitle = handler<{ detail: { value: string } }, { title: string }>(
2229
({ detail }, state) => { (state.title = detail?.value ?? "untitled") }
2330
);
2431

25-
const maybeList = lift(({ result }) => result || []);
32+
const grabPrompts = lift<{ result: string }, Prompt[]>(({ result }) => {
33+
console.log("grabPrompts", result);
34+
if (!result) {
35+
return [];
36+
}
37+
const jsonMatch = result.match(/```json\n([\s\S]+?)```/);
38+
if (!jsonMatch) {
39+
console.error("No JSON found in text:", result);
40+
return [];
41+
}
42+
let rawData = JSON.parse(jsonMatch[1]);
43+
let parsedData = z.array(Prompt).safeParse(rawData);
44+
if (!parsedData.success) {
45+
console.error("Invalid JSON:", parsedData.error);
46+
return [];
47+
}
48+
return parsedData.data;
49+
});
50+
51+
const buildPrompt = lift<{ title: string }, { messages: string[], system: string, stop: string } | {}>(({ title }) => {
52+
if (!title) return;
2653

27-
// FIXME(ja): if type Prompt is just a string, the render map fails
28-
type Prompt = {
29-
prompt: string;
30-
}
54+
return {
55+
system: `Generate 10 image prompt variations when a user sends you a prompt.
56+
Some should change just the style, some should change the content,
57+
and some should change both. The last should be a completely different prompt.
58+
59+
<schema>${jsonSchema}</schema>`,
60+
messages: [`Generate image prompt variations for: ${title}`, '```json\n['],
61+
stop: '```'
62+
}
63+
});
3164

3265
const addToPrompt = handler<
33-
{ prompt: string },
34-
{ title: string }
66+
{ prompt: string },
67+
{ title: string }
3568
>((e, state) => {
36-
state.title += " " + e.prompt;
69+
state.title += " " + e.prompt;
3770
});
3871

3972
export const prompt = recipe<{ title: string }>("prompt", ({ title }) => {
4073
title.setDefault("abstract geometric art");
41-
const { result } = generateData<Prompt[]>({
42-
prompt: str`generate 10 image prompt variations for the current prompt: ${title}. Some should change just the style, some should change the content, and some should change both. The last should be a completely different prompt.`,
43-
result: [],
44-
schema: {
45-
type: "array",
46-
items: {
47-
type: "object",
48-
properties: {
49-
prompt: {
50-
type: "string",
51-
},
52-
},
53-
},
54-
},
55-
mode: "json",
56-
});
74+
const variations = grabPrompts(generateText(buildPrompt({ title })));
5775

5876
let src = imageUrl({ title });
59-
let variations = maybeList({result});
6077

6178
return {
6279
[NAME]: title,
@@ -67,10 +84,10 @@ export const prompt = recipe<{ title: string }>("prompt", ({ title }) => {
6784
oncommon-input=${updateTitle({ title })}
6885
></common-input>
6986
<img src=${src}} width="100%" />
70-
<ul>${variations.map(({ prompt }) => html`<li>${prompt} - <span onclick=${launcher({ title: prompt })}></span></li>`)}</ul>
87+
<ul>${variations.map(({ prompt }) => html`<li onclick=${launcher({ title: prompt })}>${prompt}</li>`)}</ul>
7188
</common-vstack>`,
7289
title,
73-
variations: result,
90+
variations,
7491
addToPrompt: addToPrompt({ title }),
7592
};
7693
});

0 commit comments

Comments
 (0)