请用typescript 给出OpenAI的响应格式
- User
请用typescript 给出OpenAI的响应格式
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
请用typescript 给出OpenAI的响应格式
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
以下是使用 TypeScript 定义的 OpenAI API 响应格式:
// Chat Completion 相关类型
interface ChatCompletionMessage {
role: "system" | "user" | "assistant" | "function";
content?: string | null;
name?: string;
function_call?: {
name: string;
arguments: string;
};
}
interface ChatCompletionChoice {
index: number;
message: ChatCompletionMessage;
finish_reason?: "stop" | "length" | "function_call" | "content_filter" | null;
}
interface Usage {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
}
interface ChatCompletionResponse {
id: string;
object: "chat.completion";
created: number;
model: string;
choices: ChatCompletionChoice[];
usage: Usage;
system_fingerprint?: string | null;
}
// Streaming 响应
interface ChatCompletionStreamChoice {
index: number;
delta: {
role?: "system" | "user" | "assistant" | "function";
content?: string;
function_call?: {
name?: string;
arguments?: string;
};
};
finish_reason?: "stop" | "length" | "function_call" | "content_filter" | null;
}
interface ChatCompletionStreamResponse {
id: string;
object: "chat.completion.chunk";
created: number;
model: string;
choices: ChatCompletionStreamChoice[];
system_fingerprint?: string | null;
}
// Embeddings 响应
interface EmbeddingData {
object: "embedding";
index: number;
embedding: number[];
}
interface EmbeddingResponse {
object: "list";
data: EmbeddingData[];
model: string;
usage: {
prompt_tokens: number;
total_tokens: number;
};
}
// Completions 响应(旧版 API)
interface CompletionChoice {
text: string;
index: number;
logprobs?: {
tokens: string[];
token_logprobs: number[];
top_logprobs: Record<string, number>[];
text_offset: number[];
} | null;
finish_reason?: string | null;
}
interface CompletionResponse {
id: string;
object: "text_completion";
created: number;
model: string;
choices: CompletionChoice[];
usage: Usage;
}
// 图像生成响应
interface ImageData {
url?: string;
b64_json?: string;
revised_prompt?: string;
}
interface ImageResponse {
created: number;
data: ImageData[];
}
// 错误响应
interface ErrorDetail {
message: string;
type: string;
param?: string | null;
code?: string | null;
}
interface ErrorResponse {
error: ErrorDetail;
}
// 模型列表响应
interface ModelData {
id: string;
object: "model";
created: number;
owned_by: string;
permission?: Array<{
id: string;
object: string;
created: number;
allow_create_engine: boolean;
allow_sampling: boolean;
allow_logprobs: boolean;
allow_search_indices: boolean;
allow_view: boolean;
allow_fine_tuning: boolean;
organization: string;
group?: string | null;
is_blocking: boolean;
}>;
root?: string | null;
parent?: string | null;
}
interface ModelListResponse {
object: "list";
data: ModelData[];
}
// 文件相关响应
interface FileResponse {
id: string;
object: "file";
bytes: number;
created_at: number;
filename: string;
purpose: string;
status?: "uploaded" | "processed" | "error";
status_details?: string | null;
}
// Fine-tuning 相关
interface FineTuningJobResponse {
id: string;
object: "fine_tuning.job";
created_at: number;
finished_at?: number | null;
model: string;
fine_tuned_model?: string | null;
organization_id: string;
status: "validating_files" | "queued" | "running" | "succeeded" | "failed" | "cancelled";
hyperparameters: {
n_epochs: number | "auto";
batch_size?: number | "auto";
learning_rate_multiplier?: number | "auto";
};
training_file: string;
validation_file?: string | null;
result_files?: string[];
trained_tokens?: number | null;
error?: {
code: string;
message: string;
param?: string | null;
} | null;
}
// Assistant API 响应
interface AssistantTool {
type: "code_interpreter" | "retrieval" | "function";
function?: {
name: string;
description?: string;
parameters: Record<string, any>;
};
}
interface AssistantResponse {
id: string;
object: "assistant";
created_at: number;
name?: string | null;
description?: string | null;
model: string;
instructions?: string | null;
tools?: AssistantTool[];
file_ids?: string[];
metadata?: Record<string, any>;
}
// Thread 响应
interface ThreadResponse {
id: string;
object: "thread";
created_at: number;
metadata?: Record<string, any>;
}
// Message 响应
interface TextContent {
type: "text";
text: {
value: string;
annotations: Array<{
type: "file_citation" | "file_path";
text: string;
start_index: number;
end_index: number;
file_citation?: {
file_id: string;
quote: string;
};
file_path?: {
file_id: string;
};
}>;
};
}
interface ImageFileContent {
type: "image_file";
image_file: {
file_id: string;
};
}
type MessageContent = TextContent | ImageFileContent;
interface MessageResponse {
id: string;
object: "thread.message";
created_at: number;
thread_id: string;
role: "user" | "assistant";
content: MessageContent[];
assistant_id?: string | null;
run_id?: string | null;
file_ids?: string[];
metadata?: Record<string, any>;
}
// Run 响应
interface RequiredAction {
type: "submit_tool_outputs";
submit_tool_outputs: {
tool_calls: Array<{
id: string;
type: "function";
function: {
name: string;
arguments: string;
};
}>;
};
}
interface RunResponse {
id: string;
object: "thread.run";
created_at: number;
thread_id: string;
assistant_id: string;
status: "queued" | "in_progress" | "requires_action" | "cancelling" | "cancelled" | "failed" | "completed" | "expired";
required_action?: RequiredAction | null;
last_error?: {
code: "server_error" | "rate_limit_exceeded";
message: string;
} | null;
expires_at?: number | null;
started_at?: number | null;
cancelled_at?: number | null;
failed_at?: number | null;
completed_at?: number | null;
model: string;
instructions?: string | null;
tools?: AssistantTool[];
file_ids?: string[];
metadata?: Record<string, any>;
usage?: Usage | null;
}
// 音频转录响应
interface TranscriptionResponse {
text: string;
language?: string;
duration?: number;
segments?: Array<{
id: number;
seek: number;
start: number;
end: number;
text: string;
tokens: number[];
temperature: number;
avg_logprob: number;
compression_ratio: number;
no_speech_prob: number;
}>;
}
// 音频翻译响应
interface TranslationResponse {
text: string;
}
// Moderation 响应
interface ModerationResponse {
id: string;
model: string;
results: Array<{
flagged: boolean;
categories: {
sexual: boolean;
hate: boolean;
harassment: boolean;
"self-harm": boolean;
"sexual/minors": boolean;
"hate/threatening": boolean;
"violence/graphic": boolean;
"self-harm/intent": boolean;
"self-harm/instructions": boolean;
"harassment/threatening": boolean;
violence: boolean;
};
category_scores: {
sexual: number;
hate: number;
harassment: number;
"self-harm": number;
"sexual/minors": number;
"hate/threatening": number;
"violence/graphic": number;
"self-harm/intent": number;
"self-harm/instructions": number;
"harassment/threatening": number;
violence: number;
};
}>;
}
// 导出所有类型
export type {
ChatCompletionMessage,
ChatCompletionChoice,
ChatCompletionResponse,
ChatCompletionStreamChoice,
ChatCompletionStreamResponse,
EmbeddingResponse,
CompletionResponse,
ImageResponse,
ErrorResponse,
ModelListResponse,
FileResponse,
FineTuningJobResponse,
AssistantResponse,
ThreadResponse,
MessageResponse,
RunResponse,
TranscriptionResponse,
TranslationResponse,
ModerationResponse,
Usage,
AssistantTool,
MessageContent,
RequiredAction
};
这些 TypeScript 类型定义涵盖了 OpenAI API 的主要响应格式,包括:
使用这些类型定义可以在 TypeScript 项目中获得完整的类型检查和智能提示,确保与 OpenAI API 的交互更加安全和可靠。