UNPKG

@genkit-ai/dotprompt

Version:

Genkit AI framework `.prompt` file format and management library.

137 lines (133 loc) 6.91 kB
import { GenerateOptions, PromptAction, GenerateResponse, GenerateStreamResponse } from '@genkit-ai/ai'; import { ModelArgument, MessageData } from '@genkit-ai/ai/model'; import { DocumentData } from '@genkit-ai/ai/retriever'; import { z } from '@genkit-ai/core'; import { Registry } from '@genkit-ai/core/registry'; import { PromptFrontmatter, PromptMetadata } from './metadata.js'; import '@genkit-ai/ai/tool'; import '@genkit-ai/core/schema'; /** * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ type PromptData = PromptFrontmatter & { template: string; }; type PromptGenerateOptions<V = unknown, CustomOptions extends z.ZodTypeAny = z.ZodTypeAny> = Omit<GenerateOptions<z.ZodTypeAny, CustomOptions>, 'prompt' | 'input' | 'model'> & { model?: ModelArgument<CustomOptions>; input?: V; }; interface RenderMetadata { docs?: DocumentData[]; messages?: MessageData[]; } declare class Dotprompt<I = unknown> implements PromptMetadata<z.ZodTypeAny> { private registry; name: string; description?: string; variant?: string; hash: string; template: string; model?: PromptMetadata['model']; metadata: PromptMetadata['metadata']; input?: PromptMetadata['input']; output?: PromptMetadata['output']; tools?: PromptMetadata['tools']; config?: PromptMetadata['config']; private _promptAction?; private _render; static parse(registry: Registry, name: string, source: string): Dotprompt<unknown>; static fromAction(registry: Registry, action: PromptAction): Dotprompt; constructor(registry: Registry, options: PromptMetadata, template: string, action?: PromptAction); /** * Renders all of the prompt's text parts into a raw string. * * @param input User input to the prompt template. * @param options Optional context and/or history for the prompt template. * @returns all of the text parts concatenated into a string. */ renderText(input: I, options?: RenderMetadata): string; /** * Renders the prompt template into an array of messages. * * @param input User input to the prompt template * @param options optional context and/or history for the prompt template. * @returns an array of messages representing an exchange between a user and a model. */ renderMessages(input?: I, options?: RenderMetadata): MessageData[]; toJSON(): PromptData; define(options?: { ns?: string; description?: string; }): void; get promptAction(): PromptAction | undefined; private _generateOptions; /** * 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<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny, O extends z.ZodTypeAny = z.ZodTypeAny>(opt: PromptGenerateOptions<I, CustomOptions>): GenerateOptions<CustomOptions, O>; renderInNewSpan<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny, O extends z.ZodTypeAny = z.ZodTypeAny>(opt: PromptGenerateOptions<I>): Promise<GenerateOptions<CustomOptions, O>>; /** * Generates a response by rendering the prompt template with given user input 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<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny, O extends z.ZodTypeAny = z.ZodTypeAny>(opt: PromptGenerateOptions<I, CustomOptions>): Promise<GenerateResponse<z.infer<O>>>; /** * Generates a streaming response by rendering the prompt template with given user input 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<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny>(opt: PromptGenerateOptions<I, CustomOptions>): Promise<GenerateStreamResponse>; } declare class DotpromptRef<Variables = unknown> { name: string; variant?: string; dir?: string; private _prompt?; constructor(name: string, options?: { variant?: string; dir?: string; }); /** Loads the prompt which is referenced. */ loadPrompt(registry: Registry): Promise<Dotprompt<Variables>>; /** * Generates a response by rendering the prompt template with given user input 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<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny, O extends z.ZodTypeAny = z.ZodTypeAny>(registry: Registry, opt: PromptGenerateOptions<Variables, CustomOptions>): Promise<GenerateResponse<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<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny, O extends z.ZodTypeAny = z.ZodTypeAny>(registry: Registry, opt: PromptGenerateOptions<Variables, CustomOptions>): Promise<GenerateOptions<z.ZodTypeAny, O>>; } /** * Define a dotprompt in code. This function is offered as an alternative to definitions in .prompt files. * * @param options the prompt definition, including its name, variant and model. Any options from .prompt file front matter are accepted. * @param template a string template, comparable to the main body of a prompt file. * @returns the newly defined prompt. */ declare function defineDotprompt<I extends z.ZodTypeAny = z.ZodTypeAny, CustomOptions extends z.ZodTypeAny = z.ZodTypeAny>(registry: Registry, options: PromptMetadata<I, CustomOptions>, template: string): Dotprompt<z.infer<I>>; export { Dotprompt, DotpromptRef, type PromptData, type PromptGenerateOptions, defineDotprompt };