genkitx-openai
Version:
Firebase Genkit AI framework plugin for OpenAI APIs.
1 lines • 6.2 kB
Source Map (JSON)
{"version":3,"sources":["../src/whisper.ts"],"sourcesContent":["/**\n * Copyright 2024 The Fire Company\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 { GenerateRequest, GenerateResponseData, Genkit } from 'genkit';\nimport { GenerationCommonConfigSchema, Message, z } from 'genkit';\nimport type { ModelAction } from 'genkit/model';\nimport { modelRef } from 'genkit/model';\nimport type OpenAI from 'openai';\nimport {\n type TranscriptionCreateParams,\n type Transcription,\n} from 'openai/resources/audio/index.mjs';\n\nexport const Whisper1ConfigSchema = GenerationCommonConfigSchema.extend({\n language: z.string().optional(),\n timestamp_granularities: z.array(z.enum(['word', 'segment'])).optional(),\n response_format: z\n .enum(['json', 'text', 'srt', 'verbose_json', 'vtt'])\n .optional(),\n});\n\nexport const whisper1 = modelRef({\n name: 'openai/whisper-1',\n info: {\n label: 'OpenAI - Whisper',\n supports: {\n media: true,\n output: ['text', 'json'],\n multiturn: false,\n systemRole: false,\n tools: false,\n },\n },\n configSchema: Whisper1ConfigSchema,\n});\n\nfunction toWhisper1Request(\n request: GenerateRequest<typeof Whisper1ConfigSchema>\n): TranscriptionCreateParams {\n const message = new Message(request.messages[0]);\n const media = message.media;\n if (!media?.url) {\n throw new Error('No media found in the request');\n }\n const mediaBuffer = Buffer.from(\n media.url.slice(media.url.indexOf(',') + 1),\n 'base64'\n );\n const mediaFile = new File([mediaBuffer], 'input', {\n type:\n media.contentType ??\n media.url.slice('data:'.length, media.url.indexOf(';')),\n });\n const options: TranscriptionCreateParams = {\n model: 'whisper-1',\n file: mediaFile,\n prompt: message.text,\n temperature: request.config?.temperature,\n language: request.config?.language,\n timestamp_granularities: request.config?.timestamp_granularities,\n };\n const outputFormat = request.output?.format as 'json' | 'text' | 'media';\n const customFormat = request.config?.response_format;\n if (outputFormat && customFormat) {\n if (\n outputFormat === 'json' &&\n customFormat !== 'json' &&\n customFormat !== 'verbose_json'\n ) {\n throw new Error(\n `Custom response format ${customFormat} is not compatible with output format ${outputFormat}`\n );\n }\n }\n if (outputFormat === 'media') {\n throw new Error(`Output format ${outputFormat} is not supported.`);\n }\n options.response_format = customFormat || outputFormat || 'text';\n for (const k in options) {\n if (options[k] === undefined) {\n delete options[k];\n }\n }\n return options;\n}\n\nfunction toGenerateResponse(\n result: Transcription | string\n): GenerateResponseData {\n return {\n candidates: [\n {\n index: 0,\n finishReason: 'stop',\n message: {\n role: 'model',\n content: [\n {\n text: typeof result === 'string' ? result : result.text,\n },\n ],\n },\n },\n ],\n };\n}\n\nexport function whisper1Model(\n ai: Genkit,\n client: OpenAI\n): ModelAction<typeof Whisper1ConfigSchema> {\n return ai.defineModel<typeof Whisper1ConfigSchema>(\n {\n name: whisper1.name,\n ...whisper1.info,\n configSchema: whisper1.configSchema,\n },\n async (request) => {\n const result = await client.audio.transcriptions.create(\n toWhisper1Request(request)\n );\n return toGenerateResponse(result);\n }\n );\n}\n"],"mappings":";;;;;AAgBA,SAAS,8BAA8B,SAAS,SAAS;AAEzD,SAAS,gBAAgB;AAOlB,MAAM,uBAAuB,6BAA6B,OAAO;AAAA,EACtE,UAAU,EAAE,OAAO,EAAE,SAAS;AAAA,EAC9B,yBAAyB,EAAE,MAAM,EAAE,KAAK,CAAC,QAAQ,SAAS,CAAC,CAAC,EAAE,SAAS;AAAA,EACvE,iBAAiB,EACd,KAAK,CAAC,QAAQ,QAAQ,OAAO,gBAAgB,KAAK,CAAC,EACnD,SAAS;AACd,CAAC;AAEM,MAAM,WAAW,SAAS;AAAA,EAC/B,MAAM;AAAA,EACN,MAAM;A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