genkit
Version:
Genkit AI framework
1 lines • 8.38 kB
Source Map (JSON)
{"version":3,"sources":["../src/genkit-beta.ts"],"sourcesContent":["/**\n * Copyright 2025 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 defineInterrupt,\n ExecutablePrompt,\n InterruptConfig,\n isExecutablePrompt,\n ToolAction,\n} from '@genkit-ai/ai';\nimport { Chat, ChatOptions } from '@genkit-ai/ai/chat';\nimport { defineFormat } from '@genkit-ai/ai/formats';\nimport {\n getCurrentSession,\n Session,\n SessionData,\n SessionError,\n SessionOptions,\n} from '@genkit-ai/ai/session';\nimport { z } from '@genkit-ai/core';\nimport { v4 as uuidv4 } from 'uuid';\nimport { Formatter } from './formats';\nimport { Genkit, GenkitOptions } from './genkit';\n\nexport type { GenkitOptions as GenkitBetaOptions }; // in case they drift later\n\n/**\n * WARNING: these APIs are considered unstable and subject to frequent breaking changes that may not honor semver.\n *\n * Initializes Genkit BETA APIs with a set of options.\n *\n * This will create a new Genkit registry, register the provided plugins, stores, and other configuration. This\n * should be called before any flows are registered.\n *\n * @beta\n */\nexport function genkit(options: GenkitOptions): GenkitBeta {\n return new GenkitBeta(options);\n}\n\n/**\n * Genkit BETA APIs.\n *\n * @beta\n */\nexport class GenkitBeta extends Genkit {\n constructor(options?: GenkitOptions) {\n super(options);\n this.registry.apiStability = 'beta';\n }\n\n /**\n * Create a chat session with the provided options.\n *\n * ```ts\n * const chat = ai.chat({\n * system: 'talk like a pirate',\n * })\n * let response = await chat.send('tell me a joke')\n * response = await chat.send('another one')\n * ```\n *\n * @beta\n */\n chat<I>(options?: ChatOptions<I>): Chat;\n\n /**\n * Create a chat session with the provided preabmle.\n *\n * ```ts\n * const triageAgent = ai.definePrompt({\n * system: 'help the user triage a problem',\n * })\n * const chat = ai.chat(triageAgent)\n * const { text } = await chat.send('my phone feels hot');\n * ```\n *\n * @beta\n */\n chat<I>(preamble: ExecutablePrompt<I>, options?: ChatOptions<I>): Chat;\n\n /**\n * Create a chat session with the provided options.\n *\n * ```ts\n * const chat = ai.chat({\n * system: 'talk like a pirate',\n * })\n * let response = await chat.send('tell me a joke')\n * response = await chat.send('another one')\n * ```\n *\n * @beta\n */\n chat<I>(\n preambleOrOptions?: ChatOptions<I> | ExecutablePrompt<I>,\n maybeOptions?: ChatOptions<I>\n ): Chat {\n let options: ChatOptions<I> | undefined;\n let preamble: ExecutablePrompt<I> | undefined;\n if (maybeOptions) {\n options = maybeOptions;\n }\n if (preambleOrOptions) {\n if (isExecutablePrompt(preambleOrOptions)) {\n preamble = preambleOrOptions as ExecutablePrompt<I>;\n } else {\n options = preambleOrOptions as ChatOptions<I>;\n }\n }\n\n const session = this.createSession();\n if (preamble) {\n return session.chat(preamble, options);\n }\n return session.chat(options);\n }\n\n /**\n * Create a session for this environment.\n */\n createSession<S = any>(options?: SessionOptions<S>): Session<S> {\n const sessionId = options?.sessionId?.trim() || uuidv4();\n const sessionData: SessionData = {\n id: sessionId,\n state: options?.initialState,\n };\n return new Session(this.registry, {\n id: sessionId,\n sessionData,\n store: options?.store,\n });\n }\n\n /**\n * Loads a session from the store.\n *\n * @beta\n */\n async loadSession(\n sessionId: string,\n options: SessionOptions\n ): Promise<Session> {\n if (!options.store) {\n throw new Error('options.store is required');\n }\n const sessionData = await options.store.get(sessionId);\n\n return new Session(this.registry, {\n id: sessionId,\n sessionData,\n store: options.store,\n });\n }\n\n /**\n * Gets the current session from async local storage.\n *\n * @beta\n */\n currentSession<S = any>(): Session<S> {\n const currentSession = getCurrentSession(this.registry);\n if (!currentSession) {\n throw new SessionError('not running within a session');\n }\n return currentSession as Session;\n }\n\n /**\n * Defines and registers a custom model output formatter.\n *\n * Here's an example of a custom JSON output formatter:\n *\n * ```ts\n * import { extractJson } from 'genkit/extract';\n *\n * ai.defineFormat(\n * { name: 'customJson' },\n * (schema) => {\n * let instructions: string | undefined;\n * if (schema) {\n * instructions = `Output should be in JSON format and conform to the following schema:\n * \\`\\`\\`\n * ${JSON.stringify(schema)}\n * \\`\\`\\`\n * `;\n * }\n * return {\n * parseChunk: (chunk) => extractJson(chunk.accumulatedText),\n * parseMessage: (message) => extractJson(message.text),\n * instructions,\n * };\n * }\n * );\n *\n * const { output } = await ai.generate({\n * prompt: 'Invent a menu item for a pirate themed restaurant.',\n * output: { format: 'customJson', schema: MenuItemSchema },\n * });\n * ```\n *\n * @beta\n */\n defineFormat(\n options: {\n name: string;\n } & Formatter['config'],\n handler: Formatter['handler']\n ): { config: Formatter['config']; handler: Formatter['handler'] } {\n return defineFormat(this.registry, options, handler);\n }\n\n /**\n * Defines and registers an interrupt.\n *\n * Interrupts are special tools that halt model processing and return control back to the caller. Interrupts make it simpler to implement\n * \"human-in-the-loop\" and out-of-band processing patterns that require waiting on external actions to complete.\n *\n * @beta\n */\n defineInterrupt<I extends z.ZodTypeAny, O extends z.ZodTypeAny>(\n config: InterruptConfig<I, O>\n ): ToolAction<I, O> {\n return defineInterrupt(this.registry, config);\n }\n}\n"],"mappings":"AAgBA;AAAA,EACE;AAAA,EAGA;AAAA,OAEK;AAEP,SAAS,oBAAoB;AAC7B;AAAA,EACE;AAAA,EACA;AAAA,EAEA;AAAA,OAEK;AAEP,SAAS,MAAM,cAAc;AAE7B,SAAS,cAA6B;AAc/B,SAAS,OAAO,SAAoC;AACzD,SAAO,IAAI,WAAW,OAAO;AAC/B;AAOO,MAAM,mBAAmB,OAAO;AAAA,EACrC,YAAY,SAAyB;AACnC,UAAM,OAAO;AACb,SAAK,SAAS,eAAe;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAA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