Skip to content

Commit 19d980f

Browse files
committed
fix: render n prompt metadata along with generateoptions
1 parent a0f812a commit 19d980f

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

js/ai/src/generate.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ export interface GenerateOptions<
170170
context?: ActionContext;
171171
/** Abort signal for the generate request. */
172172
abortSignal?: AbortSignal;
173+
/**
174+
* Additional metadata describing the GenerateOptions, used by tooling. If
175+
* this is an instance of a rendered dotprompt, will contain any prompt
176+
* metadata contained in the original frontmatter.
177+
**/
178+
metadata?: Record<string, any>;
173179
}
174180

175181
export async function toGenerateRequest(

js/ai/src/prompt.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@ function definePromptAsync<
330330
...resolvedOptions?.config,
331331
...renderOptions?.config,
332332
},
333+
metadata: resolvedOptions.metadata?.metadata
334+
? {
335+
prompt: resolvedOptions.metadata?.metadata,
336+
}
337+
: undefined,
333338
});
334339
// if config is empty and it was not explicitly passed in, we delete it, don't want {}
335340
if (Object.keys(opts.config).length === 0 && !renderOptions?.config) {
@@ -822,6 +827,18 @@ function loadPrompt(
822827
delete promptMetadata.input.schema.description;
823828
}
824829

830+
const metadata = {
831+
...promptMetadata.metadata,
832+
type: 'prompt',
833+
prompt: {
834+
...promptMetadata,
835+
template: parsedPrompt.template,
836+
},
837+
};
838+
if (promptMetadata.raw?.['metadata']) {
839+
metadata['metadata'] = { ...promptMetadata.raw?.['metadata'] };
840+
}
841+
825842
return {
826843
name: registryDefinitionKey(name, variant ?? undefined, ns),
827844
model: promptMetadata.model,
@@ -835,14 +852,7 @@ function loadPrompt(
835852
input: {
836853
jsonSchema: promptMetadata.input?.schema,
837854
},
838-
metadata: {
839-
...promptMetadata.metadata,
840-
type: 'prompt',
841-
prompt: {
842-
...promptMetadata,
843-
template: parsedPrompt.template,
844-
},
845-
},
855+
metadata,
846856
maxTurns: promptMetadata.raw?.['maxTurns'],
847857
toolChoice: promptMetadata.raw?.['toolChoice'],
848858
returnToolRequests: promptMetadata.raw?.['returnToolRequests'],

js/genkit/tests/prompts_test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,11 @@ describe('prompt', () => {
11451145
returnToolRequests: true,
11461146
toolChoice: 'required',
11471147
tools: ['toolA', 'toolB'],
1148+
metadata: {
1149+
prompt: {
1150+
foo: 'bar',
1151+
},
1152+
},
11481153
});
11491154
});
11501155

0 commit comments

Comments
 (0)