UNPKG

@axflow/models

Version:

Zero-dependency, modular SDK for building robust natural language applications

133 lines (130 loc) 5.62 kB
type SharedRequestOptions = { apiKey?: string; apiUrl?: string; fetch?: typeof fetch; headers?: Record<string, string>; signal?: AbortSignal; }; declare namespace CohereGenerationTypes { type Request = { prompt: string; model?: string; num_generations?: number; max_tokens?: number; truncate?: string; temperature?: number; preset?: string; end_sequences?: string[]; stop_sequences?: string[]; k?: number; p?: number; frequency_penalty?: number; presence_penalty?: number; return_likelihoods?: string; logit_bias?: Record<string, any>; }; type RequestOptions = SharedRequestOptions; type Generation = { id: string; text: string; index?: number; likelihood?: number; token_likelihoods?: Array<{ token: string; likelihood: number; }>; }; type Response = { id: string; prompt?: string; generations: Generation[]; meta: { api_version: { version: string; is_deprecated?: boolean; is_experimental?: boolean; }; warnings?: string[]; }; }; type Chunk = { text?: string; is_finished: boolean; finished_reason?: 'COMPLETE' | 'MAX_TOKENS' | 'ERROR' | 'ERROR_TOXIC'; response?: { id: string; prompt?: string; generations: Generation[]; }; }; } /** * Run a generation against the Cohere API. * * @see https://docs.cohere.com/reference/generate * * @param request The request body sent to Cohere. See Cohere's documentation for /v1/generate for supported parameters. * @param options * @param options.apiKey Cohere API key. * @param options.apiUrl The url of the Cohere (or compatible) API. Defaults to https://api.cohere.ai/v1/generate. * @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 Cohere completion. See Cohere's documentation for /v1/generate. */ declare function run(request: CohereGenerationTypes.Request, options: CohereGenerationTypes.RequestOptions): Promise<CohereGenerationTypes.Response>; /** * Run a streaming generation against the Cohere API. The resulting stream is the raw unmodified bytes from the API. * * @see https://docs.cohere.com/reference/generate * * @param request The request body sent to Cohere. See Cohere's documentation for /v1/generate for supported parameters. * @param options * @param options.apiKey Cohere API key. * @param options.apiUrl The url of the Cohere (or compatible) API. Defaults to https://api.cohere.ai/v1/generate. * @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: CohereGenerationTypes.Request, options: CohereGenerationTypes.RequestOptions): Promise<ReadableStream<Uint8Array>>; /** * Run a streaming generation against the Cohere API. The resulting stream is the parsed stream data as JavaScript objects. * * @see https://docs.cohere.com/reference/generate * * @param request The request body sent to Cohere. See Cohere's documentation for /v1/generate for supported parameters. * @param options * @param options.apiKey Cohere API key. * @param options.apiUrl The url of the Cohere (or compatible) API. Defaults to https://api.cohere.ai/v1/generate. * @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: CohereGenerationTypes.Request, options: CohereGenerationTypes.RequestOptions): Promise<ReadableStream<CohereGenerationTypes.Chunk>>; /** * Run a streaming generation against the Cohere API. The resulting stream emits only the string tokens. * * @see https://docs.cohere.com/reference/generate * * @param request The request body sent to Cohere. See Cohere's documentation for /v1/generate for supported parameters. * @param options * @param options.apiKey Cohere API key. * @param options.apiUrl The url of the Cohere (or compatible) API. Defaults to https://api.cohere.ai/v1/generate. * @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: CohereGenerationTypes.Request, options: CohereGenerationTypes.RequestOptions): Promise<ReadableStream<string>>; /** * An object that encapsulates methods for calling the Cohere Generate API. */ declare class CohereGeneration { static run: typeof run; static stream: typeof stream; static streamBytes: typeof streamBytes; static streamTokens: typeof streamTokens; } export { CohereGeneration, CohereGenerationTypes };