@genkit-ai/ai
Version:
Genkit AI framework generative AI APIs.
1 lines • 12.1 kB
Source Map (JSON)
{"version":3,"sources":["../src/genkit-ai.ts"],"sourcesContent":["/**\n * Copyright 2026 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n getContext,\n run,\n z,\n type ActionContext,\n type Operation,\n} from '@genkit-ai/core';\nimport { type Registry } from '@genkit-ai/core/registry';\nimport { cancelOperation } from './cancel-operation.js';\nimport { checkOperation } from './check-operation.js';\nimport { type DocumentData } from './document.js';\nimport {\n embed,\n embedMany,\n type EmbedderArgument,\n type EmbedderParams,\n type Embedding,\n type EmbeddingBatch,\n} from './embedder.js';\nimport {\n generate,\n generateStream,\n type GenerateOptions,\n type GenerateResponse,\n type GenerateStreamOptions,\n type GenerateStreamResponse,\n} from './generate.js';\nimport { GenerationCommonConfigSchema, type Part } from './model-types.js';\n\n/**\n * `GenkitAI` encapsulates Genkit's AI APIs.\n */\nexport class GenkitAI {\n readonly registry: Registry;\n\n constructor(registry: Registry) {\n this.registry = registry;\n }\n\n /**\n * Embeds the given `content` using the specified `embedder`.\n */\n embed<CustomOptions extends z.ZodTypeAny>(\n params: EmbedderParams<CustomOptions>\n ): Promise<Embedding[]> {\n return embed(this.registry, params);\n }\n\n /**\n * A veneer for interacting with embedder models in bulk.\n */\n embedMany<ConfigSchema extends z.ZodTypeAny = z.ZodTypeAny>(params: {\n embedder: EmbedderArgument<ConfigSchema>;\n content: string[] | DocumentData[];\n metadata?: Record<string, unknown>;\n options?: z.infer<ConfigSchema>;\n }): Promise<EmbeddingBatch> {\n return embedMany(this.registry, params);\n }\n\n /**\n * Make a generate call to the default model with a simple text prompt.\n *\n * ```ts\n * const ai = genkit({\n * plugins: [googleAI()],\n * model: googleAI.model('gemini-flash-latest'), // default model\n * })\n *\n * const { text } = await ai.generate('hi');\n * ```\n */\n generate<O extends z.ZodTypeAny = z.ZodTypeAny>(\n strPrompt: string\n ): Promise<GenerateResponse<z.infer<O>>>;\n\n /**\n * Make a generate call to the default model with a multipart request.\n *\n * ```ts\n * const ai = genkit({\n * plugins: [googleAI()],\n * model: googleAI.model('gemini-flash-latest'), // default model\n * })\n *\n * const { text } = await ai.generate([\n * { media: {url: 'http://....'} },\n * { text: 'describe this image' }\n * ]);\n * ```\n */\n generate<O extends z.ZodTypeAny = z.ZodTypeAny>(\n parts: Part[]\n ): Promise<GenerateResponse<z.infer<O>>>;\n\n /**\n * Generate calls a generative model based on the provided prompt and configuration. If\n * `messages` is provided, the generation will include a conversation history in its\n * request. If `tools` are provided, the generate method will automatically resolve\n * tool calls returned from the model unless `returnToolRequests` is set to `true`.\n *\n * See {@link GenerateOptions} for detailed information about available options.\n *\n * ```ts\n * const ai = genkit({\n * plugins: [googleAI()],\n * })\n *\n * const { text } = await ai.generate({\n * system: 'talk like a pirate',\n * prompt: [\n * { media: { url: 'http://....' } },\n * { text: 'describe this image' }\n * ],\n * messages: conversationHistory,\n * tools: [ userInfoLookup ],\n * model: googleAI.model('gemini-flash-latest'),\n * });\n * ```\n */\n generate<\n O extends z.ZodTypeAny = z.ZodTypeAny,\n CustomOptions extends z.ZodTypeAny = typeof GenerationCommonConfigSchema,\n >(\n opts:\n | GenerateOptions<O, CustomOptions>\n | PromiseLike<GenerateOptions<O, CustomOptions>>\n ): Promise<GenerateResponse<z.infer<O>>>;\n\n async generate<\n O extends z.ZodTypeAny = z.ZodTypeAny,\n CustomOptions extends z.ZodTypeAny = typeof GenerationCommonConfigSchema,\n >(\n options:\n | string\n | Part[]\n | GenerateOptions<O, CustomOptions>\n | PromiseLike<GenerateOptions<O, CustomOptions>>\n ): Promise<GenerateResponse<z.infer<O>>> {\n let resolvedOptions: GenerateOptions<O, CustomOptions>;\n if (options instanceof Promise) {\n resolvedOptions = await options;\n } else if (typeof options === 'string' || Array.isArray(options)) {\n resolvedOptions = {\n prompt: options,\n };\n } else {\n resolvedOptions = options as GenerateOptions<O, CustomOptions>;\n }\n return generate(this.registry, resolvedOptions);\n }\n\n /**\n * Make a streaming generate call to the default model with a simple text prompt.\n *\n * ```ts\n * const ai = genkit({\n * plugins: [googleAI()],\n * model: googleAI.model('gemini-flash-latest'), // default model\n * })\n *\n * const { response, stream } = ai.generateStream('hi');\n * for await (const chunk of stream) {\n * console.log(chunk.text);\n * }\n * console.log((await response).text);\n * ```\n */\n generateStream<O extends z.ZodTypeAny = z.ZodTypeAny>(\n strPrompt: string\n ): GenerateStreamResponse<z.infer<O>>;\n\n /**\n * Make a streaming generate call to the default model with a multipart request.\n *\n * ```ts\n * const ai = genkit({\n * plugins: [googleAI()],\n * model: googleAI.model('gemini-flash-latest'), // default model\n * })\n *\n * const { response, stream } = ai.generateStream([\n * { media: {url: 'http://....'} },\n * { text: 'describe this image' }\n * ]);\n * for await (const chunk of stream) {\n * console.log(chunk.text);\n * }\n * console.log((await response).text);\n * ```\n */\n generateStream<O extends z.ZodTypeAny = z.ZodTypeAny>(\n parts: Part[]\n ): GenerateStreamResponse<z.infer<O>>;\n\n /**\n * Streaming generate calls a generative model based on the provided prompt and configuration. If\n * `messages` is provided, the generation will include a conversation history in its\n * request. If `tools` are provided, the generate method will automatically resolve\n * tool calls returned from the model unless `returnToolRequests` is set to `true`.\n *\n * See {@link GenerateOptions} for detailed information about available options.\n *\n * ```ts\n * const ai = genkit({\n * plugins: [googleAI()],\n * })\n *\n * const { response, stream } = ai.generateStream({\n * system: 'talk like a pirate',\n * prompt: [\n * { media: { url: 'http://....' } },\n * { text: 'describe this image' }\n * ],\n * messages: conversationHistory,\n * tools: [ userInfoLookup ],\n * model: googleAI.model('gemini-flash-latest'),\n * });\n * for await (const chunk of stream) {\n * console.log(chunk.text);\n * }\n * console.log((await response).text);\n * ```\n */\n generateStream<\n O extends z.ZodTypeAny = z.ZodTypeAny,\n CustomOptions extends z.ZodTypeAny = typeof GenerationCommonConfigSchema,\n >(\n opts:\n | GenerateStreamOptions<O, CustomOptions>\n | PromiseLike<GenerateStreamOptions<O, CustomOptions>>\n ): GenerateStreamResponse<z.infer<O>>;\n\n generateStream<\n O extends z.ZodTypeAny = z.ZodTypeAny,\n CustomOptions extends z.ZodTypeAny = typeof GenerationCommonConfigSchema,\n >(\n options:\n | string\n | Part[]\n | GenerateStreamOptions<O, CustomOptions>\n | PromiseLike<GenerateStreamOptions<O, CustomOptions>>\n ): GenerateStreamResponse<z.infer<O>> {\n if (typeof options === 'string' || Array.isArray(options)) {\n options = { prompt: options };\n }\n return generateStream(this.registry, options);\n }\n\n /**\n * Checks the status of of a given operation. Returns a new operation which will contain the updated status.\n *\n * ```ts\n * let operation = await ai.generateOperation({\n * model: googleAI.model('veo-2.0-generate-001'),\n * prompt: 'A banana riding a bicycle.',\n * });\n *\n * while (!operation.done) {\n * operation = await ai.checkOperation(operation!);\n * await new Promise((resolve) => setTimeout(resolve, 5000));\n * }\n * ```\n *\n * @param operation\n * @returns\n */\n checkOperation<T>(operation: Operation<T>): Promise<Operation<T>> {\n return checkOperation(this.registry, operation);\n }\n\n /**\n * Cancels a given operation. Returns a new operation which will contain the updated status.\n *\n * @param operation\n * @returns\n */\n cancelOperation<T>(operation: Operation<T>): Promise<Operation<T>> {\n return cancelOperation(this.registry, operation);\n }\n\n /**\n * A flow step that executes the provided function. Each run step is recorded separately in the trace.\n *\n * ```ts\n * ai.defineFlow('hello', async() => {\n * await ai.run('step1', async () => {\n * // ... step 1\n * });\n * await ai.run('step2', async () => {\n * // ... step 2\n * });\n * return result;\n * })\n * ```\n */\n run<T>(name: string, func: () => Promise<T>): Promise<T>;\n\n /**\n * A flow step that executes the provided function. Each run step is recorded separately in the trace.\n *\n * ```ts\n * ai.defineFlow('hello', async(name) => {\n * const greeting = await ai.run('step1', name, async (input) => {\n * return `Hello, ${input}!`;\n * });\n * const result = await ai.run('step2', greeting, async (input) => {\n * // ... step 2\n * });\n * return result;\n * })\n */\n run<T>(\n name: string,\n input: any,\n func: (input?: any) => Promise<T>\n ): Promise<T>;\n\n run<T>(\n name: string,\n funcOrInput: () => Promise<T> | any,\n maybeFunc?: (input?: any) => Promise<T>\n ): Promise<T> {\n if (maybeFunc) {\n return run(name, funcOrInput, maybeFunc, this.registry);\n }\n return run(name, funcOrInput, this.registry);\n }\n\n /**\n * Returns current action (or flow) invocation context. Can be used to access things like auth\n * data set by HTTP server frameworks. If invoked outside of an action (e.g. flow or tool) will\n * return `undefined`.\n */\n currentContext(): ActionContext | undefined {\n return getContext();\n }\n}\n"],"mappings":"AAgBA;AAAA,EACE;AAAA,EACA;AAAA,OAIK;AAEP,SAAS,uBAAuB;AAChC,SAAS,sBAAsB;AAE/B;AAAA,EACE;AAAA,EACA;AAAA,OAKK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAKK;AAMA,MAAM,SAAS;AAAA,EACX;AAAA,EAET,YAAY,UAAoB;AAC9B,SAAK,WAAW;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA,EAKA,MACE,QACsB;AACtB,WAAO,MAAM,KAAK,UAAU,MAAM;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA,EAKA,UAA4D,QAKhC;AAC1B,WAAO,UAAU,KAAK,UAAU,MAAM;AAAA,EACxC;AAAA,EAuEA,MAAM,SAIJ,SAKuC;AACvC,QAAI;AACJ,QAAI,mBAAmB,SAAS;AAC9B,wBAAkB,MAAM;AAAA,IAC1B,WAAW,OAAO,YAAY,YAAY,MAAM,QAAQ,OAAO,GAAG;AAChE,wBAAkB;AAAA,QAChB,QAAQ;AAAA,MACV;AAAA,IACF,OAAO;AACL,wBAAkB;AAAA,IACpB;AACA,WAAO,SAAS,KAAK,UAAU,eAAe;AAAA,EAChD;AAAA,EAmFA,eAIE,SAKoC;AAC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