import type { WithAbortSignal } from "#Source/abort/index.ts" import type * as Aio from "#Source/aio/index.ts" import type { LoggerFriendly, LoggerFriendlyOptions } from "#Source/log/index.ts" import { Logger } from "#Source/log/index.ts" import type { Either } from "#Source/result/index.ts" import type { Tube } from "#Source/tube/index.ts" import type { StringWithAutoCompleteOptions } from "#Source/type/index.ts" export interface ChatCompletionContentPartText { /** * @description The text content. */ text: string /** * @description The type of the content part. */ type: "text" } export interface ChatCompletionContentPartRefusal { /** * @description The refusal message generated by the model. */ refusal: string /** * @description The type of the content part. */ type: "refusal" } export interface ChatCompletionContentPartImage { imageUrl: { /** * @description Either a URL of the image or the base64 encoded image data. */ url: string /** * @description Specifies the detail level of the image. Learn more in the [Vision * guide](https://platform.openai.com/docs/guides/vision/low-or-high-fidelity-image-understanding). */ detail?: "auto" | "low" | "high" } /** * @description The type of the content part. */ type: "image_url" } export interface ChatCompletionSystemMessage { /** * @description The contents of the system message. */ content: string | ChatCompletionContentPartText[] /** * @description The role of the messages author, in this case `system`. */ role: "system" } export interface ChatCompletionUserMessage { /** * @description The contents of the user message. */ content: string | Array /** * @description The role of the messages author, in this case `user`. */ role: "user" } export interface ChatCompletionAssistantMessage { /** * @description The contents of the assistant message. Required unless `tool_calls` or * `function_call` is specified. */ content: string | Array /** * @description The role of the messages author, in this case `assistant`. */ role: "assistant" } export type ChatCompletionMessage = | ChatCompletionSystemMessage | ChatCompletionUserMessage | ChatCompletionAssistantMessage const CHAT_COMPLETION_MODEL_LIST = [ "o1-preview", "o1-preview-2024-09-12", "o1-mini", "o1-mini-2024-09-12", "gpt-4o", "gpt-4o-2024-08-06", "gpt-4o-2024-05-13", "gpt-4o-realtime-preview-2024-10-01", "chatgpt-4o-latest", "gpt-4o-mini", "gpt-4o-mini-2024-07-18", "gpt-4-turbo", "gpt-4-turbo-2024-04-09", "gpt-4-0125-preview", "gpt-4-turbo-preview", "gpt-4-1106-preview", "gpt-4-vision-preview", "gpt-4", "gpt-4-0314", "gpt-4-0613", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-0613", "gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-3.5-turbo-0301", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-1106", "gpt-3.5-turbo-0125", "gpt-3.5-turbo-16k-0613", "yi-lightning", "hunyuan-standard", "grok-3", "gemini-2.0-flash-lite-preview-02-05", "deepseek-r1", "deepseek-v3", ] as const export type ChatCompletionModel = (typeof CHAT_COMPLETION_MODEL_LIST)[number] export interface ResponseFormatText { /** * @description The type of response format being defined: `text` */ type: "text" } export interface ResponseFormatJSONObject { /** * @description The type of response format being defined: `json_object` */ type: "json_object" } export type ResponseFormat = ResponseFormatText | ResponseFormatJSONObject export interface ChatCompletionOptions extends WithAbortSignal { messages: ChatCompletionMessage[] model: ChatCompletionModel frequencyPenalty?: number | null logitBias?: Record | null logprobs?: boolean | null maxCompletionTokens?: number | null maxTokens?: number | null presencePenalty?: number | null responseFormat?: ResponseFormat temperature?: number | null topLogprobs?: number | null topP?: number | null /** * @description The timeout for the first chunk of data to be received, in milliseconds. */ timeoutFirstChunk?: number | undefined /** * @description The timeout for each chunk of data to be received, in milliseconds. */ timeoutPerChunk?: number | undefined /** * @description The maximum number of tries for the request. */ maxTries?: number | undefined } export interface ChatCompletionLeft { code: StringWithAutoCompleteOptions } export interface Completion { content: Aio.TextContent token: Aio.NumberContent } export interface ChatCompletionRight { completionTube: Tube } export type ChatCompletionResult = Either export interface BaseChatCompletionOptions extends LoggerFriendlyOptions {} export abstract class BaseChatCompletion implements LoggerFriendly { readonly logger: Logger private readonly supportedModelList: ChatCompletionModel[] constructor(options: BaseChatCompletionOptions) { this.logger = Logger.fromOptions(options).setDefaultName("BaseChatCompletion") this.supportedModelList = [...CHAT_COMPLETION_MODEL_LIST] } isSupportModel(model: ChatCompletionModel): boolean { return this.supportedModelList.includes(model) } abstract chatCompletion(options: ChatCompletionOptions): Promise }