Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: removed generate/generateStream from executable prompts #1239

Merged
merged 10 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions js/ai/src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,8 @@ export function isPrompt(arg: any): boolean {
export type PromptGenerateOptions<
I = undefined,
CustomOptions extends z.ZodTypeAny = z.ZodTypeAny,
> = Omit<
GenerateOptions<z.ZodTypeAny, CustomOptions>,
'prompt' | 'input' | 'model'
> & {
> = Omit<GenerateOptions<z.ZodTypeAny, CustomOptions>, 'prompt' | 'model'> & {
model?: ModelArgument<CustomOptions>;
input?: I;
};

/**
Expand All @@ -89,51 +85,33 @@ export interface ExecutablePrompt<
* @param opt Options for the prompt template, including user input variables and custom model configuration options.
* @returns the model response as a promise of `GenerateStreamResponse`.
*/
<Out extends O>(
(
input?: I,
opts?: PromptGenerateOptions<I, CustomOptions>
): Promise<GenerateResponse<z.infer<Out>>>;
): Promise<GenerateResponse<z.infer<O>>>;

/**
* Generates a response by rendering the prompt template with given user input and then calling the model.
* @param input Prompt inputs.
* @param opt Options for the prompt template, including user input variables and custom model configuration options.
* @returns the model response as a promise of `GenerateStreamResponse`.
*/
stream<Out extends O>(
stream(
input?: I,
opts?: PromptGenerateOptions<I, CustomOptions>
): Promise<GenerateStreamResponse<z.infer<Out>>>;

/**
* Generates a response by rendering the prompt template with given user input and additional generate options and then calling the model.
*
* @param opt Options for the prompt template, including user input variables and custom model configuration options.
* @returns the model response as a promise of `GenerateResponse`.
*/
generate<Out extends O>(
opt: PromptGenerateOptions<I, CustomOptions>
): Promise<GenerateResponse<z.infer<Out>>>;

/**
* Generates a streaming response by rendering the prompt template with given user input and additional generate options and then calling the model.
*
* @param opt Options for the prompt template, including user input variables and custom model configuration options.
* @returns the model response as a promise of `GenerateStreamResponse`.
*/
generateStream<Out extends O>(
opt: PromptGenerateOptions<I, CustomOptions>
): Promise<GenerateStreamResponse<z.infer<Out>>>;
): Promise<GenerateStreamResponse<z.infer<O>>>;

/**
* Renders the prompt template based on user input.
*
* @param opt Options for the prompt template, including user input variables and custom model configuration options.
* @returns a `GenerateOptions` object to be used with the `generate()` function from @genkit-ai/ai.
*/
render<Out extends O>(
opt: PromptGenerateOptions<I, CustomOptions>
): Promise<GenerateOptions<CustomOptions, Out>>;
render(
opt: PromptGenerateOptions<I, CustomOptions> & {
input?: I;
}
): Promise<GenerateOptions<CustomOptions, O>>;

/**
* Returns the prompt usable as a tool.
Expand Down
24 changes: 4 additions & 20 deletions js/genkit/src/genkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,28 +460,12 @@ export class Genkit {
});
return this.generateStream(renderedOpts);
};
(executablePrompt as ExecutablePrompt<I, O, CustomOptions>).generate =
async (
opt: PromptGenerateOptions<I, CustomOptions>
): Promise<GenerateResponse<O>> => {
const renderedOpts = await (
executablePrompt as ExecutablePrompt<I, O, CustomOptions>
).render(opt);
return this.generate(renderedOpts);
};
(executablePrompt as ExecutablePrompt<I, O, CustomOptions>).generateStream =
async (
opt: PromptGenerateOptions<I, CustomOptions>
): Promise<GenerateStreamResponse<O>> => {
const renderedOpts = await (
executablePrompt as ExecutablePrompt<I, O, CustomOptions>
).render(opt);
return this.generateStream(renderedOpts);
};
(executablePrompt as ExecutablePrompt<I, O, CustomOptions>).render = async <
Out extends O,
>(
opt: PromptGenerateOptions<I, CustomOptions>
opt: PromptGenerateOptions<I, CustomOptions> & {
input?: I;
}
): Promise<GenerateOptions<CustomOptions, Out>> => {
let model: ModelAction | undefined;
options = await options;
Expand Down Expand Up @@ -510,7 +494,7 @@ export class Genkit {
},
model,
} as GenerateOptions<CustomOptions, Out>;
delete (resultOptions as PromptGenerateOptions<I, CustomOptions>).input;
delete (resultOptions as any).input;
return resultOptions;
};
(executablePrompt as ExecutablePrompt<I, O, CustomOptions>).asTool =
Expand Down
123 changes: 0 additions & 123 deletions js/genkit/tests/prompts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,6 @@ describe('definePrompt - dotprompt', () => {
);
});

it('calls dotprompt with .generate', async () => {
const hi = ai.definePrompt(
{
name: 'hi',
input: {
schema: z.object({
name: z.string(),
}),
},
config: {
temperature: 11,
},
},
'hi {{ name }}'
);

const response = await hi.generate({
input: { name: 'Genkit' },
config: { version: 'abc' },
});
assert.strictEqual(
response.text,
'Echo: hi Genkit; config: {"version":"abc","temperature":11}'
);
});

it('calls dotprompt with default model via retrieved prompt', async () => {
ai.definePrompt(
{
Expand Down Expand Up @@ -215,39 +189,6 @@ describe('definePrompt - dotprompt', () => {
assert.deepStrictEqual(chunks, ['3', '2', '1']);
});

it('streams dotprompt .generateStream', async () => {
const hi = ai.definePrompt(
{
name: 'hi',
input: {
schema: z.object({
name: z.string(),
}),
},
config: {
temperature: 11,
},
},
'hi {{ name }}'
);

const { response, stream } = await hi.generateStream({
input: { name: 'Genkit' },
config: { version: 'abc' },
});
const chunks: string[] = [];
for await (const chunk of stream) {
chunks.push(chunk.text);
}
const responseText = (await response).text;

assert.strictEqual(
responseText,
'Echo: hi Genkit; config: {"version":"abc","temperature":11}'
);
assert.deepStrictEqual(chunks, ['3', '2', '1']);
});

it('calls dotprompt with default model via retrieved prompt', async () => {
ai.definePrompt(
{
Expand Down Expand Up @@ -621,70 +562,6 @@ describe('definePrompt', () => {
'Echo: hi Genkit; config: {"version":"abc","temperature":11}'
);
});

it('works with .generate', async () => {
const hi = ai.definePrompt(
{
name: 'hi',
model: 'echoModel',
input: {
schema: z.object({
name: z.string(),
}),
},
},
async (input) => {
return {
messages: [
{ role: 'user', content: [{ text: `hi ${input.name}` }] },
],
};
}
);

const response = await hi.generate({ input: { name: 'Genkit' } });
assert.strictEqual(response.text, 'Echo: hi Genkit; config: {}');
});

it('streams dotprompt with .generateStream', async () => {
const hi = ai.definePrompt(
{
name: 'hi',
input: {
schema: z.object({
name: z.string(),
}),
},
config: {
temperature: 11,
},
},
async (input) => {
return {
messages: [
{ role: 'user', content: [{ text: `hi ${input.name}` }] },
],
};
}
);

const { response, stream } = await hi.generateStream({
model: 'echoModel',
input: { name: 'Genkit' },
config: { version: 'abc' },
});
const chunks: string[] = [];
for await (const chunk of stream) {
chunks.push(chunk.text);
}
const responseText = (await response).text;

assert.strictEqual(
responseText,
'Echo: hi Genkit; config: {"version":"abc","temperature":11}'
);
assert.deepStrictEqual(chunks, ['3', '2', '1']);
});
});

describe('render', () => {
Expand Down
10 changes: 6 additions & 4 deletions js/testapps/flow-simple-ai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,12 @@ export const dotpromptContext = ai.defineFlow(
},
];

const result = await ai.prompt('dotpromptContext').generate({
input: { question: question },
docs,
});
const result = await ai.prompt('dotpromptContext')(
{ question: question },
{
docs,
}
);
return result.output as any;
}
);
Expand Down
10 changes: 3 additions & 7 deletions js/testapps/menu/src/02/flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ export const s02_menuQuestionFlow = ai.defineFlow(
outputSchema: AnswerOutputSchema,
},
async (input) => {
return s02_dataMenuPrompt
.generate({
input: { question: input.question },
})
.then((response) => {
return { answer: response.text };
});
return s02_dataMenuPrompt({ question: input.question }).then((response) => {
return { answer: response.text };
});
}
);
8 changes: 3 additions & 5 deletions js/testapps/menu/src/04/flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,9 @@ export const s04_ragMenuQuestionFlow = ai.defineFlow(
);

// Generate the response
const response = await s04_ragDataMenuPrompt.generate({
input: {
menuData: menuData,
question: input.question,
},
const response = await s04_ragDataMenuPrompt({
menuData: menuData,
question: input.question,
});
return { answer: response.text };
}
Expand Down
14 changes: 5 additions & 9 deletions js/testapps/menu/src/05/flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ export const s05_readMenuFlow = ai.defineFlow(
},
async (unused) => {
const imageDataUrl = await inlineDataUrl('menu.jpeg', 'image/jpeg');
const response = await s05_readMenuPrompt.generate({
input: {
imageUrl: imageDataUrl,
},
const response = await s05_readMenuPrompt({
imageUrl: imageDataUrl,
});
return { menuText: response.text };
}
Expand All @@ -57,11 +55,9 @@ export const s05_textMenuQuestionFlow = ai.defineFlow(
outputSchema: AnswerOutputSchema,
},
async (input) => {
const response = await s05_textMenuPrompt.generate({
input: {
menuText: input.menuText,
question: input.question,
},
const response = await s05_textMenuPrompt({
menuText: input.menuText,
question: input.question,
});
return { answer: response.text };
}
Expand Down
13 changes: 6 additions & 7 deletions js/testapps/prompt-file/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ ai.defineFlow(
outputSchema: RecipeSchema,
},
async (input) =>
(await ai.prompt('recipe').generate<typeof RecipeSchema>({ input: input }))
.output!
(await ai.prompt('recipe')<typeof RecipeSchema>(input)).output!
);

ai.defineFlow(
Expand All @@ -73,8 +72,7 @@ ai.defineFlow(
outputSchema: z.any(),
},
async (input) =>
(await ai.prompt('recipe', { variant: 'robot' }).generate({ input: input }))
.output
(await ai.prompt('recipe', { variant: 'robot' })(input)).output
);

// A variation that supports streaming, optionally
Expand All @@ -92,15 +90,16 @@ ai.defineStreamingFlow(
async ({ subject, personality }, streamingCallback) => {
const storyPrompt = ai.prompt('story');
if (streamingCallback) {
const { response, stream } = await storyPrompt.generateStream({
input: { subject, personality },
const { response, stream } = await storyPrompt.stream({
subject,
personality,
});
for await (const chunk of stream) {
streamingCallback(chunk.content[0]?.text!);
}
return (await response).text;
} else {
const response = await storyPrompt.generate({ input: { subject } });
const response = await storyPrompt({ subject });
return response.text;
}
}
Expand Down
16 changes: 8 additions & 8 deletions js/testapps/rag/src/pdf_rag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ export const pdfQA = ai.defineFlow(
options: { k: 3 },
});

return augmentedPrompt
.generate({
input: {
question: query,
context: docs.map((d) => d.text),
},
return augmentedPrompt(
{
question: query,
context: docs.map((d) => d.text),
},
{
streamingCallback,
})
.then((r) => r.text);
}
).then((r) => r.text);
}
);

Expand Down
Loading
Loading