@axflow/models
Version:
Zero-dependency, modular SDK for building robust natural language applications
130 lines (127 loc) • 5.75 kB
text/typescript
type SharedRequestOptions = {
apiKey?: string;
apiUrl?: string;
fetch?: typeof fetch;
headers?: Record<string, string>;
signal?: AbortSignal;
};
declare namespace OpenAICompletionTypes {
type RequestOptions = SharedRequestOptions;
type Request = {
model: string;
prompt: string | Array<string> | Array<number> | Array<Array<number>>;
suffix?: string | null;
max_tokens?: number | null;
temperature?: number | null;
top_p?: number | null;
n?: number | null;
logprobs?: number | null;
echo?: boolean | null;
stop?: string | null | Array<string>;
presence_penalty?: number | null;
frequency_penalty?: number | null;
best_of?: number | null;
logit_bias?: Record<string, number> | null;
user?: string;
};
type Response = {
id: string;
object: string;
created: number;
model: string;
choices: Choice[];
usage?: {
completion_tokens: number;
prompt_tokens: number;
total_tokens: number;
};
};
type Choice = {
text: string;
index: number;
logprobs: {
text_offset?: Array<number>;
token_logprobs?: Array<number>;
tokens?: Array<string>;
top_logprobs?: Array<Record<string, number>>;
} | null;
finish_reason: 'stop' | 'length';
};
type Chunk = {
id: string;
object: string;
created: number;
model: string;
choices: Choice[];
};
}
/**
* Run a completion against the OpenAI API.
*
* @see https://platform.openai.com/docs/api-reference/completions
*
* @param request The request body sent to OpenAI. See OpenAI's documentation for /v1/completions for supported parameters.
* @param options
* @param options.apiKey OpenAI API key.
* @param options.apiUrl The url of the OpenAI (or compatible) API. Defaults to https://api.openai.com/v1/completions.
* @param options.fetch A custom implementation of fetch. Defaults to globalThis.fetch.
* @param options.headers Optionally add additional HTTP headers to the request.
* @param options.signal An AbortSignal that can be used to abort the fetch request.
* @returns OpenAI completion. See OpenAI's documentation for /v1/completions.
*/
declare function run(request: OpenAICompletionTypes.Request, options: OpenAICompletionTypes.RequestOptions): Promise<OpenAICompletionTypes.Response>;
/**
* Run a streaming completion against the OpenAI API. The resulting stream is the raw unmodified bytes from the API.
*
* @see https://platform.openai.com/docs/api-reference/completions
*
* @param request The request body sent to OpenAI. See OpenAI's documentation for /v1/completions for supported parameters.
* @param options
* @param options.apiKey OpenAI API key.
* @param options.apiUrl The url of the OpenAI (or compatible) API. Defaults to https://api.openai.com/v1/completions.
* @param options.fetch A custom implementation of fetch. Defaults to globalThis.fetch.
* @param options.headers Optionally add additional HTTP headers to the request.
* @param options.signal An AbortSignal that can be used to abort the fetch request.
* @returns A stream of bytes directly from the API.
*/
declare function streamBytes(request: OpenAICompletionTypes.Request, options: OpenAICompletionTypes.RequestOptions): Promise<ReadableStream<Uint8Array>>;
/**
* Run a streaming completion against the OpenAI API. The resulting stream is the parsed stream data as JavaScript objects.
*
* @see https://platform.openai.com/docs/api-reference/completions
*
* @param request The request body sent to OpenAI. See OpenAI's documentation for /v1/completions for supported parameters.
* @param options
* @param options.apiKey OpenAI API key.
* @param options.apiUrl The url of the OpenAI (or compatible) API. Defaults to https://api.openai.com/v1/completions.
* @param options.fetch A custom implementation of fetch. Defaults to globalThis.fetch.
* @param options.headers Optionally add additional HTTP headers to the request.
* @param options.signal An AbortSignal that can be used to abort the fetch request.
* @returns A stream of objects representing each chunk from the API.
*/
declare function stream(request: OpenAICompletionTypes.Request, options: OpenAICompletionTypes.RequestOptions): Promise<ReadableStream<OpenAICompletionTypes.Chunk>>;
/**
* Run a streaming completion against the OpenAI API. The resulting stream emits only the string tokens.
*
* @see https://platform.openai.com/docs/api-reference/completions
*
* @param request The request body sent to OpenAI. See OpenAI's documentation for /v1/completions for supported parameters.
* @param options
* @param options.apiKey OpenAI API key.
* @param options.apiUrl The url of the OpenAI (or compatible) API. Defaults to https://api.openai.com/v1/completions.
* @param options.fetch A custom implementation of fetch. Defaults to globalThis.fetch.
* @param options.headers Optionally add additional HTTP headers to the request.
* @param options.signal An AbortSignal that can be used to abort the fetch request.
* @returns A stream of tokens from the API.
*/
declare function streamTokens(request: OpenAICompletionTypes.Request, options: OpenAICompletionTypes.RequestOptions): Promise<ReadableStream<string>>;
/**
* An object that encapsulates methods for calling the OpenAI Completion API.
*/
declare class OpenAICompletion {
static run: typeof run;
static stream: typeof stream;
static streamBytes: typeof streamBytes;
static streamTokens: typeof streamTokens;
}
export { OpenAICompletion, OpenAICompletionTypes };