@genkit-ai/googleai
Version:
Genkit AI framework plugin for Google AI APIs, including Gemini APIs.
1 lines • 5.36 kB
Source Map (JSON)
{"version":3,"sources":["../../src/context-caching/index.ts"],"sourcesContent":["/**\n * Copyright 2024 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 type { CachedContent, StartChatParams } from '@google/generative-ai';\nimport {\n GoogleAICacheManager,\n type CachedContentCreateParams,\n} from '@google/generative-ai/server';\nimport { GenkitError, type GenerateRequest, type z } from 'genkit';\nimport { logger } from 'genkit/logging';\nimport type { CacheConfigDetails } from './types.js';\nimport {\n calculateTTL,\n generateCacheKey,\n getContentForCache,\n lookupContextCache,\n validateContextCacheRequest,\n} from './utils.js';\n\n/**\n * Handles context caching and transforms the chatRequest\n * @param apiKey\n * @param request\n * @param chatRequest\n * @param modelVersion\n * @returns\n */\nexport async function handleContextCache(\n apiKey: string,\n request: GenerateRequest<z.ZodTypeAny>,\n chatRequest: StartChatParams,\n modelVersion: string,\n cacheConfigDetails: CacheConfigDetails\n): Promise<{ cache: CachedContent; newChatRequest: StartChatParams }> {\n const cacheManager = new GoogleAICacheManager(apiKey);\n\n const { cachedContent, chatRequest: newChatRequest } = getContentForCache(\n request,\n chatRequest,\n modelVersion,\n cacheConfigDetails\n );\n cachedContent.model = modelVersion;\n const cacheKey = generateCacheKey(cachedContent);\n\n cachedContent.displayName = cacheKey;\n\n let cache = await lookupContextCache(cacheManager, cacheKey);\n logger.debug(`Cache hit: ${cache ? 'true' : 'false'}`);\n\n if (!cache) {\n try {\n logger.debug('No cache found, creating one.');\n const createParams: CachedContentCreateParams = {\n ...cachedContent,\n ttlSeconds: calculateTTL(cacheConfigDetails),\n };\n cache = await cacheManager.create(createParams);\n logger.debug(`Created new cache entry with key: ${cacheKey}`);\n } catch (cacheError) {\n logger.error(\n `Failed to create cache with key ${cacheKey}: ${cacheError}`\n );\n throw new GenkitError({\n status: 'INTERNAL',\n message: `Failed to create cache: ${cacheError}`,\n });\n }\n }\n\n if (!cache) {\n throw new GenkitError({\n status: 'INTERNAL',\n message: 'Failed to use context cache feature',\n });\n }\n\n return { cache, newChatRequest };\n}\n\n/**\n * Handles cache validation, creation, and usage, transforming the chatRequest if necessary.\n * @param apiKey The API key for accessing Google AI Gemini.\n * @param request The generate request passed to the model.\n * @param chatRequest The current chat request configuration.\n * @param modelVersion The version of the model being used.\n * @param cacheConfigDetails Configuration details for caching.\n * @returns A transformed chat request and cache data (if applicable).\n */\nexport async function handleCacheIfNeeded(\n apiKey: string,\n request: GenerateRequest<z.ZodTypeAny>,\n chatRequest: StartChatParams,\n modelVersion: string,\n cacheConfigDetails: CacheConfigDetails | null\n): Promise<{ chatRequest: StartChatParams; cache: CachedContent | null }> {\n // Skip caching if no configuration or if validation fails\n if (\n !cacheConfigDetails ||\n !validateContextCacheRequest(request, modelVersion)\n ) {\n return { chatRequest, cache: null };\n }\n\n const { cache, newChatRequest } = await handleContextCache(\n apiKey,\n request,\n chatRequest,\n modelVersion,\n cacheConfigDetails\n );\n return { chatRequest: newChatRequest, cache };\n}\n"],"mappings":"AAiBA;AAAA,EACE;AAAA,OAEK;AACP,SAAS,mBAAiD;AAC1D,SAAS,cAAc;AAEvB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAUP,eAAsB,mBACpB,QACA,SACA,aACA,cACA,oBACoE;AACpE,QAAM,eAAe,IAAI,qBAAqB,MAAM;AAEpD,QAAM,EAAE,eAAe,aAAa,eAAe,IAAI;AAAA,IACrD;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACA,gBAAc,QAAQ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