LaiTool/src/define/response/openAIResponse.ts

96 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-07-13 15:44:13 +08:00
type OpenAISuccessResponse = {
id: string
object: string
created: number
model: string
choices: [
{
index: number
message: {
role: string
content: string
}
finish_reason: string
}
]
usage: {
prompt_tokens: number
completion_tokens: number
total_tokens: number
}
}
type RixApiErrorResponse = {
error: {
message: string // 错误信息
type: string
param: string
code: string
}
}
type KimiErrorResponse = {
error: {
message: string
type: string
}
}
type DoubaoErrorResponse = {
error: {
code: string
message: string
param: string
type: string
}
}
/**
* OpenAI系列返回的成功response
* @param response OpenAI返回的response
* @returns
*/
export function GetOpenAISuccessResponse(response: string | OpenAISuccessResponse): string {
if (typeof response === 'string') {
response = JSON.parse(response) as OpenAISuccessResponse
}
// 开始处理response
return response.choices[0].message.content
}
/**
* RixApi系列返回的错误response
* @param response RixApi返回的response
* @returns
*/
export function GetRixApiErrorResponse(response: string | RixApiErrorResponse): string {
if (typeof response === 'string') {
response = JSON.parse(response) as RixApiErrorResponse
}
return response.error.message
}
/**
* kimi的错误信息返回
* @param response
* @returns
*/
export function GetKimiErrorResponse(response: string | KimiErrorResponse): string {
if (typeof response === 'string') {
response = JSON.parse(response) as KimiErrorResponse
}
return response.error.message
}
/**
*
* @param response
* @returns
*/
export function GetDoubaoErrorResponse(response: string | DoubaoErrorResponse): string {
if (typeof response === 'string') {
response = JSON.parse(response) as DoubaoErrorResponse
}
return response.error.message
}