aiwrapper
Version:
A Universal AI Wrapper for JavaScript & TypeScript
88 lines (87 loc) • 3.29 kB
TypeScript
import { LangOptions, LanguageProvider } from "../language-provider.ts";
import { LangMessages, LangMessage, LangTool } from "../messages.ts";
import { Model } from 'aimodels';
export type ReasoningEffort = "low" | "medium" | "high";
export type OpenAILikeConfig = {
apiKey?: string;
model: string;
systemPrompt: string;
maxTokens?: number;
maxCompletionTokens?: number;
baseURL: string;
headers?: Record<string, string>;
bodyProperties?: Record<string, unknown>;
reasoningEffort?: ReasoningEffort;
};
export type ReasoningTokenDetails = {
reasoningTokens?: number;
audioTokens?: number;
acceptedPredictionTokens?: number;
rejectedPredictionTokens?: number;
};
export type TokenUsageDetails = {
promptTokens: number;
completionTokens: number;
totalTokens: number;
promptTokensDetails?: {
cachedTokens?: number;
audioTokens?: number;
};
completionTokensDetails?: ReasoningTokenDetails;
};
type OpenAICompletionsTool = {
type: "function";
function: {
name: string;
description: string;
parameters: Record<string, any>;
};
};
export declare class OpenAIChatCompletionsLang extends LanguageProvider {
protected _config: OpenAILikeConfig;
protected modelInfo?: Model;
constructor(config: OpenAILikeConfig);
/** Decide how many tokens to request based on model info and optional limits */
private computeRequestMaxTokens;
/** Build OpenAI-like request body including tools and json schema toggles */
private buildRequestBody;
/** Build common request init for fetch */
private buildCommonRequest;
static custom(options: {
apiKey?: string;
model: string;
baseURL: string;
systemPrompt?: string;
maxTokens?: number;
maxCompletionTokens?: number;
headers?: Record<string, string>;
bodyProperties?: Record<string, unknown>;
reasoningEffort?: ReasoningEffort;
}): OpenAIChatCompletionsLang;
ask(prompt: string, options?: LangOptions): Promise<LangMessages>;
protected transformBody(body: Record<string, unknown>): Record<string, unknown>;
supportsReasoning(): boolean;
chat(messages: LangMessage[] | LangMessages, options?: LangOptions): Promise<LangMessages>;
protected formatTools(tools: LangTool[]): OpenAICompletionsTool[];
protected transformMessagesForProvider(messages: LangMessages): any[];
protected handleStreamData(data: any, result: LangMessages, onResult?: (result: LangMessage) => void, _toolArgBuffers?: Map<string, {
name: string;
buffer: string;
}>): void;
private mapMessageForProvider;
private buildContentParts;
private buildToolCalls;
private mapToolResultsMessage;
private mapImageItemToContentParts;
private shouldAppendVisionHint;
private messageHasImageItems;
private appendVisionHint;
private collectAssistantImages;
private ensureContentArray;
private payloadHasImageParts;
setReasoningEffort(effort: ReasoningEffort): OpenAIChatCompletionsLang;
getReasoningEffort(): ReasoningEffort | undefined;
setMaxCompletionTokens(maxTokens: number): OpenAIChatCompletionsLang;
getMaxCompletionTokens(): number | undefined;
}
export {};