{"version":3,"sources":["../../src/openai/whisper.ts"],"sourcesContent":["/**\n * Copyright 2024 The Fire Company\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 { ModelReference } from 'genkit';\nimport { modelRef, z } from 'genkit';\nimport type { ModelAction, ModelInfo } from 'genkit/model';\nimport { model } from 'genkit/plugin';\nimport OpenAI from 'openai';\nimport {\n  TranscriptionConfigSchema,\n  toSttRequest,\n  transcriptionToGenerateResponse,\n} from '../audio.mjs';\nimport type { PluginOptions } from '../index.mjs';\nimport {\n  toTranslationRequest,\n  translationToGenerateResponse,\n} from '../translate.mjs';\nimport { maybeCreateRequestScopedOpenAIClient, toModelName } from '../utils.mjs';\n\nexport const WHISPER_MODEL_INFO: ModelInfo = {\n  supports: {\n    media: true,\n    output: ['text', 'json'],\n    multiturn: false,\n    systemRole: false,\n    tools: false,\n  },\n};\n\n/**\n * Config schema for Whisper models. Extends the transcription config with\n * a `translate` flag that switches between transcription and translation APIs.\n */\nexport const WhisperConfigSchema = TranscriptionConfigSchema.extend({\n  /** When true, uses Translation API instead of Transcription. Default: false */\n  translate: z.boolean().optional().default(false),\n});\n\n/**\n * Method to define an OpenAI Whisper model that can perform both transcription and\n * translation based on the `translate` config flag.\n *\n * @param params.ai The Genkit AI instance.\n * @param params.name The name of the model.\n * @param params.client The OpenAI client instance.\n * @param params.modelRef Optional reference to the model's configuration and\n * custom options.\n *\n * @returns the created {@link ModelAction}\n */\nexport function defineOpenAIWhisperModel<\n  CustomOptions extends z.ZodTypeAny = z.ZodTypeAny,\n>(params: {\n  name: string;\n  client: OpenAI;\n  modelRef?: ModelReference<CustomOptions>;\n  pluginOptions?: PluginOptions;\n}): ModelAction {\n  const { name, client: defaultClient, pluginOptions, modelRef } = params;\n  const modelName = toModelName(name, pluginOptions?.name);\n  const actionName =\n    modelRef?.name ?? `${pluginOptions?.name ?? 'openai'}/${modelName}`;\n\n  return model(\n    {\n      name: actionName,\n      ...modelRef?.info,\n      configSchema: modelRef?.configSchema,\n    },\n    async (request, { abortSignal }) => {\n      const { translate, ...cleanConfig } = (request.config ?? {}) as Record<\n        string,\n        unknown\n      >;\n      const cleanRequest = { ...request, config: cleanConfig };\n      const client = maybeCreateRequestScopedOpenAIClient(\n        pluginOptions,\n        request,\n        defaultClient\n      );\n\n      if (translate === true) {\n        const params = toTranslationRequest(modelName, cleanRequest);\n        const result = await client.audio.translations.create(params, {\n          signal: abortSignal,\n        });\n        return translationToGenerateResponse(result);\n      } else {\n        const params = toSttRequest(modelName, cleanRequest);\n        // Explicitly setting stream to false ensures we use the non-streaming overload\n        const result = await client.audio.transcriptions.create(\n          {\n            ...params,\n            stream: false,\n          },\n          { signal: abortSignal }\n        );\n        return transcriptionToGenerateResponse(result);\n      }\n    }\n  );\n}\n\n/** OpenAI whisper ModelRef helper. */\nexport function openAIWhisperModelRef<\n  CustomOptions extends z.ZodTypeAny = z.ZodTypeAny,\n>(params: {\n  name: string;\n  info?: ModelInfo;\n  configSchema?: CustomOptions;\n  config?: any;\n}) {\n  const {\n    name,\n    info = WHISPER_MODEL_INFO,\n    configSchema,\n    config = undefined,\n  } = params;\n  return modelRef({\n    name,\n    configSchema: configSchema || (WhisperConfigSchema as any),\n    info,\n    config,\n    namespace: 'openai',\n  });\n}\n\nexport const SUPPORTED_WHISPER_MODELS = {\n  'whisper-1': openAIWhisperModelRef({\n    name: 'whisper-1',\n  }),\n};\n"],"mappings":"AAkBA,SAAS,UAAU,SAAS;AAE5B,SAAS,aAAa;AAEtB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EACE;AAAA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