@artmate/chat
Version:
**开箱即用的AI组件库(基于 Vue3 + ElementPlus)**
89 lines (88 loc) • 3.22 kB
TypeScript
import { AnyObject } from '../_util/type';
import { ArtStreamOptions, SSEOutput } from '../artStream';
import { ArtFetchOptions } from './artFetch';
export interface ArtRequestBaseOptions {
/**
* @description Base URL, e.g., 'https://api.example.com/v1/chat'
*/
baseURL: string;
/**
* @description Model name, e.g., 'gpt-3.5-turbo'
*/
model?: string;
/**
* @warning 🔥🔥 Its dangerously!
*
* Enabling the dangerouslyApiKey option can be dangerous because it exposes
* your secret API credentials in the client-side code. Web browsers are inherently
* less secure than server environments, any user with access to the browser can
* potentially inspect, extract, and misuse these credentials. This could lead to
* unauthorized access using your credentials and potentially compromise sensitive
* data or functionality.
*/
dangerouslyApiKey?: string;
}
interface ArtRequestCustomOptions {
/**
* @description Custom fetch
*/
fetch?: ArtFetchOptions['fetch'];
}
export type ArtRequestOptions = ArtRequestBaseOptions & ArtRequestCustomOptions;
type ArtRequestMessageContent = AnyObject | string;
interface ArtRequestMessage extends AnyObject {
role?: string;
content?: ArtRequestMessageContent;
}
/**
* Compatible with the parameters of OpenAI's chat.completions.create,
* with plans to support more parameters and adapters in the future
*/
export interface ArtRequestParams {
/**
* @description Model name, e.g., 'gpt-3.5-turbo'
* @default ArtRequestOptions.model
*/
model?: string;
/**
* @description Indicates whether to use streaming for the response
*/
stream?: boolean;
/**
* @description The messages to be sent to the model
*/
messages?: ArtRequestMessage[];
}
export interface ArtRequestCallbacks<Output> {
/**
* @description Callback when the request is successful
*/
onSuccess: (chunks: Output[]) => void;
/**
* @description Callback when the request fails
*/
onError: (error: Error) => void;
/**
* @description Callback when the request is updated
*/
onUpdate: (chunk: Output) => void;
/**
* @description Callback monitoring and control the stream
*/
onStream?: (abortController: AbortController) => void;
}
export type ArtRequestFunction<Input = AnyObject, Output = SSEOutput> = (params: ArtRequestParams & Input, callbacks: ArtRequestCallbacks<Output>, transformStream?: ArtStreamOptions<Output>['transformStream']) => Promise<void>;
declare class ArtRequestClass {
readonly baseURL: string;
readonly model: string;
private defaultHeaders;
private customOptions;
private constructor();
static init(options: ArtRequestOptions): ArtRequestClass;
create: <Input = AnyObject, Output = SSEOutput>(params: ArtRequestParams & Input, callbacks?: ArtRequestCallbacks<Output>, transformStream?: ArtStreamOptions<Output>["transformStream"]) => Promise<void>;
private customResponseHandler;
private sseResponseHandler;
private jsonResponseHandler;
}
declare const ArtRequest: typeof ArtRequestClass.init;
export default ArtRequest;