typescript
// -------------------------
// 通用类型
// -------------------------
export interface Usage {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
}
export type FinishReason =
| "stop"
| "length"
| "function_call"
| "content_filter";
// -------------------------
// 1. Chat Completion
// -------------------------
export interface ChatCompletionMessage {
role: "system" | "user" | "assistant" | "function";
content?: string;
name?: string;
function_call?: Record<string, any>;
}
export interface ChatCompletionChoice {
index: number;
message: ChatCompletionMessage;
finish_reason?: FinishReason;
}
export interface ChatCompletionResponse {
id: string;
object: "chat.completion";
created: number;
model: string;
choices: ChatCompletionChoice[];
usage: Usage;
system_fingerprint?: string;
}
// -------------------------
// 2. Chat Streaming
// -------------------------
export interface ChatCompletionStreamChoice {
index: number;
delta: Record<string, any>;
finish_reason?: FinishReason;
}
export interface ChatCompletionStreamResponse {
id: string;
object: "chat.completion.chunk";
created: number;
model: string;
choices: ChatCompletionStreamChoice[];
system_fingerprint?: string;
}
// -------------------------
// 3. Embeddings
// -------------------------
export interface EmbeddingData {
object: "embedding";
index: number;
embedding: number[];
}
export interface EmbeddingResponse {
object: "list";
data: EmbeddingData[];
model: string;
usage: Record<string, any>;
}
// -------------------------
// 4. Text Completion(旧版)
// -------------------------
export interface CompletionChoice {
text: string;
index: number;
logprobs?: Record<string, any>;
finish_reason?: string;
}
export interface CompletionResponse {
id: string;
object: "text_completion";
created: number;
model: string;
choices: CompletionChoice[];
usage: Usage;
}
// -------------------------
// 5. 图像生成
// -------------------------
export interface ImageData {
url?: string;
b64_json?: string;
revised_prompt?: string;
}
export interface ImageResponse {
created: number;
data: ImageData[];
}
// -------------------------
// 6. 错误响应
// -------------------------
export interface ErrorDetail {
message: string;
type: string;
param?: string;
code?: string;
}
export interface ErrorResponse {
error: ErrorDetail;
}
// -------------------------
// 7. 模型列表
// -------------------------
export interface ModelData {
id: string;
object: "model";
created: number;
owned_by: string;
permission?: any[];
root?: string;
parent?: string;
}
export interface ModelListResponse {
object: "list";
data: ModelData[];
}
// -------------------------
// 8. 文件管理
// -------------------------
export interface FileResponse {
id: string;
object: "file";
bytes: number;
created_at: number;
filename: string;
purpose: string;
status?: "uploaded" | "processed" | "error";
status_details?: string;
}
// -------------------------
// 9. 微调作业
// -------------------------
export type FineTuneStatus =
| "validating_files"
| "queued"
| "running"
| "succeeded"
| "failed"
| "cancelled";
export interface FineTuningJobResponse {
id: string;
object: "fine_tuning.job";
created_at: number;
finished_at?: number;
model: string;
fine_tuned_model?: string;
organization_id: string;
status: FineTuneStatus;
hyperparameters: Record<string, any>;
training_file: string;
validation_file?: string;
result_files?: string[];
trained_tokens?: number;
error?: Record<string, any>;
}
// -------------------------
// 10. Assistants API
// -------------------------
export interface AssistantResponse {
id: string;
object: "assistant";
created_at: number;
name?: string;
description?: string;
model: string;
instructions?: string;
tools?: any[];
file_ids?: string[];
metadata?: Record<string, any>;
}
// -------------------------
// 11. Thread
// -------------------------
export interface ThreadResponse {
id: string;
object: "thread";
created_at: number;
metadata?: Record<string, any>;
}
// -------------------------
// 12. Message
// -------------------------
export interface MessageContent {
type: "text" | "image_file";
text?: Record<string, any>;
image_file?: Record<string, any>;
}
export interface MessageResponse {
id: string;
object: "thread.message";
created_at: number;
thread_id: string;
role: "user" | "assistant";
content: MessageContent[];
assistant_id?: string;
run_id?: string;
file_ids?: string[];
metadata?: Record<string, any>;
}
// -------------------------
// 13. Run
// -------------------------
export type RunStatus =
| "queued"
| "in_progress"
| "requires_action"
| "cancelling"
| "cancelled"
| "failed"
| "completed"
| "expired";
export interface RunResponse {
id: string;
object: "thread.run";
created_at: number;
thread_id: string;
assistant_id: string;
status: RunStatus;
required_action?: Record<string, any>;
last_error?: Record<string, any>;
expires_at?: number;
started_at?: number;
cancelled_at?: number;
failed_at?: number;
completed_at?: number;
model: string;
instructions?: string;
tools?: any[];
file_ids?: string[];
metadata?: Record<string, any>;
usage?: Usage;
}