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 1 commit
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
34 changes: 6 additions & 28 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'> & {
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
model?: ModelArgument<CustomOptions>;
input?: I;
};

/**
Expand All @@ -91,7 +87,7 @@ export interface ExecutablePrompt<
*/
<Out extends O>(
input?: I,
opts?: Omit<PromptGenerateOptions<I, CustomOptions>, 'input'>
opts?: PromptGenerateOptions<I, CustomOptions>
): Promise<GenerateResponse<z.infer<Out>>>;

/**
Expand All @@ -102,27 +98,7 @@ export interface ExecutablePrompt<
*/
stream<Out extends O>(
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
input?: I,
opts?: Omit<PromptGenerateOptions<I, CustomOptions>, 'input'>
): 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>
opts?: PromptGenerateOptions<I, CustomOptions>
): Promise<GenerateStreamResponse<z.infer<Out>>>;

/**
Expand All @@ -132,7 +108,9 @@ export interface ExecutablePrompt<
* @returns a `GenerateOptions` object to be used with the `generate()` function from @genkit-ai/ai.
*/
render<Out extends O>(
opt: PromptGenerateOptions<I, CustomOptions>
opt: PromptGenerateOptions<I, CustomOptions> & {
input?: I;
}
): Promise<GenerateOptions<CustomOptions, Out>>;

/**
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
Loading