UNPKG

@genkit-ai/compat-oai

Version:

Genkit AI framework plugin for OpenAI APIs.

482 lines 13.4 kB
import { GenerationCommonConfigSchema, GenkitError, Message, modelRef, z } from "genkit"; import { model } from "genkit/plugin"; import { APIError } from "openai"; import { maybeCreateRequestScopedOpenAIClient, toModelName } from "./utils.mjs"; function parseRetryAfterMs(value) { if (!value || !value.trim()) return void 0; const seconds = Number(value); if (!isNaN(seconds) && seconds >= 0) return seconds * 1e3; const date = new Date(value); if (!isNaN(date.getTime())) return Math.max(0, date.getTime() - Date.now()); return void 0; } const VisualDetailLevelSchema = z.enum(["auto", "low", "high"]).optional(); const ChatCompletionCommonConfigSchema = GenerationCommonConfigSchema.extend({ temperature: z.number().min(0).max(2).optional(), frequencyPenalty: z.number().min(-2).max(2).optional(), logProbs: z.boolean().optional(), presencePenalty: z.number().min(-2).max(2).optional(), topLogProbs: z.number().int().min(0).max(20).optional() }); function toOpenAIRole(role) { switch (role) { case "user": return "user"; case "model": return "assistant"; case "system": return "system"; case "tool": return "tool"; default: throw new Error(`role ${role} doesn't map to an OpenAI role.`); } } function toOpenAITool(tool) { return { type: "function", function: { name: tool.name, parameters: tool.inputSchema !== null ? tool.inputSchema : void 0 } }; } function isImageContentType(contentType) { if (!contentType) return false; return contentType.startsWith("image/"); } function extractDataFromBase64Url(url) { const match = url.match(/^data:([^;]+);base64,(.+)$/); return match && { contentType: match[1], data: match[2] }; } const FILE_EXTENSIONS = { "application/pdf": "pdf", "application/msword": "doc", "application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx", "text/plain": "txt", "text/csv": "csv" }; function generateFilenameFromContentType(contentType) { const ext = FILE_EXTENSIONS[contentType] || ""; return ext ? `file.${ext}` : "file"; } function toOpenAITextAndMedia(part, visualDetailLevel) { if (part.text) { return { type: "text", text: part.text }; } else if (part.media) { let contentType = part.media.contentType; if (!contentType && part.media.url.startsWith("data:")) { const extracted = extractDataFromBase64Url(part.media.url); if (extracted) { contentType = extracted.contentType; } } if (!contentType || isImageContentType(contentType)) { return { type: "image_url", image_url: { url: part.media.url, detail: visualDetailLevel } }; } if (part.media.url.startsWith("data:")) { const extracted = extractDataFromBase64Url(part.media.url); if (!extracted) { throw Error( `Invalid data URL format for media: ${part.media.url.substring(0, 50)}...` ); } return { type: "file", file: { filename: generateFilenameFromContentType(extracted.contentType), file_data: part.media.url // Full data URL with prefix } }; } throw Error( `File URLs are not supported for chat completions. Only base64-encoded files and image URLs are supported. Content type: ${contentType}` ); } throw Error( `Unsupported genkit part fields encountered for current message role: ${JSON.stringify(part)}.` ); } function toOpenAIMessages(messages, visualDetailLevel = "auto") { const apiMessages = []; for (const message of messages) { const msg = new Message(message); const role = toOpenAIRole(message.role); switch (role) { case "user": const content = msg.content.map( (part) => toOpenAITextAndMedia(part, visualDetailLevel) ); const onlyTextContent = content.some((item) => item.type !== "text"); if (!onlyTextContent) { content.forEach((item) => { if (item.type === "text") { apiMessages.push({ role, content: item.text }); } }); } else { apiMessages.push({ role, content }); } break; case "system": apiMessages.push({ role, content: msg.text }); break; case "assistant": { const toolCalls = msg.content.filter( (part) => Boolean(part.toolRequest) ).map((part) => ({ id: part.toolRequest.ref ?? "", type: "function", function: { name: part.toolRequest.name, arguments: JSON.stringify(part.toolRequest.input) } })); if (toolCalls.length > 0) { apiMessages.push({ role, tool_calls: toolCalls }); } else { apiMessages.push({ role, content: msg.text }); } break; } case "tool": { const toolResponseParts = msg.toolResponseParts(); toolResponseParts.map((part) => { apiMessages.push({ role, tool_call_id: part.toolResponse.ref ?? "", content: typeof part.toolResponse.output === "string" ? part.toolResponse.output : JSON.stringify(part.toolResponse.output) }); }); break; } } } return apiMessages; } const finishReasonMap = { length: "length", stop: "stop", tool_calls: "stop", content_filter: "blocked" }; function fromOpenAIToolCall(toolCall, choice) { if (!toolCall.function) { throw Error( `Unexpected openAI chunk choice. tool_calls was provided but one or more tool_calls is missing.` ); } const f = toolCall.function; if (choice.finish_reason === "tool_calls") { return { toolRequest: { name: f.name, ref: toolCall.id, input: f.arguments ? JSON.parse(f.arguments) : f.arguments } }; } else { return { toolRequest: { name: f.name, ref: toolCall.id, input: "" } }; } } function fromOpenAIChoice(choice, jsonMode = false) { const toolRequestParts = choice.message.tool_calls?.map( (toolCall) => fromOpenAIToolCall(toolCall, choice) ); let content = []; if (toolRequestParts && toolRequestParts.length > 0) { content = toolRequestParts; } else { if ("reasoning_content" in choice.message && choice.message.reasoning_content) { content.push({ reasoning: choice.message.reasoning_content }); } if (choice.message.content) { content.push( jsonMode ? { data: JSON.parse(choice.message.content) } : { text: choice.message.content } ); } } return { finishReason: finishReasonMap[choice.finish_reason] || "other", message: { role: "model", content } }; } function fromOpenAIChunkChoice(choice, jsonMode = false) { const toolRequestParts = choice.delta.tool_calls?.map( (toolCall) => fromOpenAIToolCall(toolCall, choice) ); let content = []; if (toolRequestParts && toolRequestParts.length > 0) { content = toolRequestParts; } else { if ("reasoning_content" in choice.delta && choice.delta.reasoning_content) { content.push({ reasoning: choice.delta.reasoning_content }); } if (choice.delta.content) { content.push( jsonMode ? { data: JSON.parse(choice.delta.content) } : { text: choice.delta.content } ); } } return { finishReason: choice.finish_reason ? finishReasonMap[choice.finish_reason] || "other" : "unknown", message: { role: "model", content } }; } function toOpenAIRequestBody(modelName, request, requestBuilder) { const messages = toOpenAIMessages( request.messages, request.config?.visualDetailLevel ); const { temperature, maxOutputTokens, // unused topK, // unused topP: top_p, frequencyPenalty: frequency_penalty, logProbs: logprobs, presencePenalty: presence_penalty, topLogProbs: top_logprobs, stopSequences: stop, version: modelVersion, tools: toolsFromConfig, apiKey, ...restOfConfig } = request.config ?? {}; const tools = request.tools?.map(toOpenAITool) ?? []; if (toolsFromConfig) { tools.push(...toolsFromConfig); } let body = { model: modelVersion ?? modelName, messages, tools: tools.length > 0 ? tools : void 0, temperature, top_p, stop, frequency_penalty, presence_penalty, top_logprobs, logprobs }; if (requestBuilder) { requestBuilder(request, body); } else { body = { ...body, ...restOfConfig }; } const response_format = request.output?.format; if (response_format === "json") { if (request.output?.schema) { body.response_format = { type: "json_schema", json_schema: { name: "output", schema: request.output.schema } }; } else { body.response_format = { type: "json_object" }; } } else if (response_format === "text") { body.response_format = { type: "text" }; } for (const key in body) { if (!body[key] || Array.isArray(body[key]) && !body[key].length) delete body[key]; } return body; } function openAIModelRunner(name, defaultClient, requestBuilder, pluginOptions) { return async (request, options) => { const client = maybeCreateRequestScopedOpenAIClient( pluginOptions, request, defaultClient ); try { let response; const body = toOpenAIRequestBody(name, request, requestBuilder); if (options?.streamingRequested) { const stream = client.beta.chat.completions.stream( { ...body, stream: true, stream_options: { include_usage: true } }, { signal: options?.abortSignal } ); for await (const chunk of stream) { chunk.choices?.forEach((chunk2) => { const c = fromOpenAIChunkChoice(chunk2); options?.sendChunk({ index: chunk2.index, content: c.message?.content ?? [] }); }); } response = await stream.finalChatCompletion(); } else { response = await client.chat.completions.create(body, { signal: options?.abortSignal }); } const standardResponse = { usage: { inputTokens: response.usage?.prompt_tokens, outputTokens: response.usage?.completion_tokens, totalTokens: response.usage?.total_tokens }, raw: response }; if (response.choices.length === 0) { return standardResponse; } else { const choice = response.choices[0]; return { ...fromOpenAIChoice(choice, request.output?.format === "json"), ...standardResponse }; } } catch (e) { if (e instanceof APIError) { let status = "UNKNOWN"; switch (e.status) { case 429: status = "RESOURCE_EXHAUSTED"; break; case 401: status = "PERMISSION_DENIED"; break; case 403: status = "UNAUTHENTICATED"; break; case 400: status = "INVALID_ARGUMENT"; break; case 500: status = "INTERNAL"; break; case 503: status = "UNAVAILABLE"; break; } const retryAfterHeader = e.headers?.get?.("retry-after") ?? e.headers?.["retry-after"]; const retryAfterMs = retryAfterHeader ? parseRetryAfterMs(retryAfterHeader) : void 0; const responseMetadata = retryAfterMs !== void 0 ? { retryAfterMs } : void 0; throw new GenkitError({ status, message: e.message, responseMetadata }); } throw e; } }; } function defineCompatOpenAIModel(params) { const { name, client, pluginOptions, modelRef: modelRef2, requestBuilder } = params; const modelName = toModelName(name, pluginOptions?.name); const actionName = modelRef2?.name ?? `${pluginOptions?.name ?? "compat-oai"}/${modelName}`; return model( { name: actionName, ...modelRef2?.info, configSchema: modelRef2?.configSchema }, openAIModelRunner(modelName, client, requestBuilder, pluginOptions) ); } const GENERIC_MODEL_INFO = { supports: { multiturn: true, media: true, tools: true, toolChoice: true, systemRole: true } }; function compatOaiModelRef(params) { const { name, info = GENERIC_MODEL_INFO, configSchema, config = void 0, namespace } = params; return modelRef({ name, configSchema: configSchema || ChatCompletionCommonConfigSchema, info, config, namespace }); } export { ChatCompletionCommonConfigSchema, compatOaiModelRef, defineCompatOpenAIModel, fromOpenAIChoice, fromOpenAIChunkChoice, fromOpenAIToolCall, openAIModelRunner, toOpenAIMessages, toOpenAIRequestBody, toOpenAIRole, toOpenAITextAndMedia, toOpenAITool }; //# sourceMappingURL=model.mjs.map