Skip to content

Commit

Permalink
fix: fixed dotprompt history (#1301)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelgj authored Nov 14, 2024
1 parent 0c35004 commit 4e53051
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
23 changes: 19 additions & 4 deletions js/genkit/src/genkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ import {
defineDotprompt,
defineHelper,
definePartial,
Dotprompt,
PromptMetadata as DotpromptPromptMetadata,
loadPromptFolder,
prompt,
Expand Down Expand Up @@ -398,7 +399,8 @@ export class Genkit {
);
return this.wrapPromptActionInExecutablePrompt(
dotprompt.promptAction! as PromptAction<I>,
options
options,
dotprompt
);
} else {
const p = definePrompt(
Expand Down Expand Up @@ -434,7 +436,8 @@ export class Genkit {
promptAction: PromptAction<I> | Promise<PromptAction<I>>,
options:
| Partial<PromptMetadata<I, CustomOptions>>
| Promise<Partial<PromptMetadata<I, CustomOptions>>>
| Promise<Partial<PromptMetadata<I, CustomOptions>>>,
dotprompt?: Dotprompt<z.infer<I>>
): ExecutablePrompt<I, O, CustomOptions> {
const executablePrompt = async (
input?: z.infer<I>,
Expand Down Expand Up @@ -476,7 +479,11 @@ export class Genkit {
}
}
const p = await promptAction;
const promptResult = await p(opt.input);
// If it's a dotprompt template, we invoke dotprompt template directly
// because it can take in more PromptGenerateOptions (not just inputs).
const promptResult = await (dotprompt
? dotprompt.render(opt)
: p(opt.input));
const resultOptions = {
messages: promptResult.messages,
docs: promptResult.docs,
Expand All @@ -485,7 +492,12 @@ export class Genkit {
promptResult.output?.format || promptResult.output?.schema
? {
format: promptResult.output?.format,
jsonSchema: promptResult.output?.schema,
jsonSchema: dotprompt
? (promptResult as GenerateOptions).output?.jsonSchema
: promptResult.output.schema,
contentType: promptResult.output?.contentType,
instructions: promptResult.output?.instructions,
schema: promptResult.output?.schema,
}
: options.output,
config: {
Expand All @@ -496,6 +508,9 @@ export class Genkit {
model,
} as GenerateOptions<O, CustomOptions>;
delete (resultOptions as any).input;
if ((promptResult as GenerateOptions).prompt) {
resultOptions.prompt = (promptResult as GenerateOptions).prompt;
}
return resultOptions;
};
(executablePrompt as ExecutablePrompt<I, O, CustomOptions>).asTool =
Expand Down
59 changes: 52 additions & 7 deletions js/genkit/tests/prompts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ describe('definePrompt - dotprompt', () => {
}),
},
output: {
format: 'json',
schema: Foo,
},
},
Expand Down Expand Up @@ -235,6 +236,54 @@ describe('definePrompt - dotprompt', () => {
assert.strictEqual(response.text, 'Echo: hi Genkit; config: {}');
});

it('calls dotprompt with history', async () => {
const hi = ai.definePrompt(
{
name: 'hi',
model: 'echoModel',
input: {
schema: z.object({
name: z.string(),
}),
},
},
'{{ history}} hi {{ name }}'
);

const response = await hi(
{ name: 'Genkit' },
{
messages: [
{ role: 'user', content: [{ text: 'hi' }] },
{ role: 'model', content: [{ text: 'bye' }] },
],
}
);
assert.deepStrictEqual(response.messages, [
{
role: 'user',
content: [{ text: 'hi' }],
metadata: { purpose: 'history' },
},
{
role: 'model',
content: [{ text: 'bye' }],
metadata: { purpose: 'history' },
},
{
role: 'model',
content: [{ text: ' hi Genkit' }],
},
{
role: 'model',
content: [
{ text: 'Echo: hi,bye, hi Genkit' },
{ text: '; config: {}' },
],
},
]);
});

it('calls dotprompt with default model with config', async () => {
const hi = ai.definePrompt(
{
Expand Down Expand Up @@ -306,16 +355,12 @@ describe('definePrompt - dotprompt', () => {
assert.deepStrictEqual(response, {
config: {},
docs: undefined,
messages: [
prompt: [
{
content: [
{
text: 'hi Genkit',
},
],
role: 'user',
text: 'hi Genkit',
},
],
messages: [],
output: undefined,
tools: [],
});
Expand Down

0 comments on commit 4e53051

Please sign in to comment.