2023-03-02 02:49:20 +00:00
|
|
|
import type * as openai from 'openai'
|
|
|
|
|
|
|
|
export { openai }
|
|
|
|
|
|
|
|
export type Role = 'user' | 'assistant' | 'system'
|
2022-12-05 05:13:36 +00:00
|
|
|
|
2023-02-14 06:30:06 +00:00
|
|
|
export type FetchFn = typeof fetch
|
|
|
|
|
2022-12-07 00:19:30 +00:00
|
|
|
export type SendMessageOptions = {
|
2023-03-02 02:49:20 +00:00
|
|
|
/** The name of a user in a multi-user chat. */
|
|
|
|
name?: string
|
2022-12-07 00:19:30 +00:00
|
|
|
parentMessageId?: string
|
2022-12-12 16:37:44 +00:00
|
|
|
messageId?: string
|
2023-02-01 09:14:10 +00:00
|
|
|
stream?: boolean
|
2023-03-02 02:49:20 +00:00
|
|
|
systemMessage?: string
|
2022-12-07 04:07:14 +00:00
|
|
|
timeoutMs?: number
|
2023-02-01 09:14:10 +00:00
|
|
|
onProgress?: (partialResponse: ChatMessage) => void
|
2022-12-07 00:19:30 +00:00
|
|
|
abortSignal?: AbortSignal
|
|
|
|
}
|
|
|
|
|
2023-02-19 07:56:19 +00:00
|
|
|
export type MessageActionType = 'next' | 'variant'
|
|
|
|
|
|
|
|
export type SendMessageBrowserOptions = {
|
|
|
|
conversationId?: string
|
|
|
|
parentMessageId?: string
|
|
|
|
messageId?: string
|
|
|
|
action?: MessageActionType
|
|
|
|
timeoutMs?: number
|
|
|
|
onProgress?: (partialResponse: ChatMessage) => void
|
|
|
|
abortSignal?: AbortSignal
|
|
|
|
}
|
|
|
|
|
2023-02-01 09:14:10 +00:00
|
|
|
export interface ChatMessage {
|
|
|
|
id: string
|
|
|
|
text: string
|
|
|
|
role: Role
|
2023-03-02 02:49:20 +00:00
|
|
|
name?: string
|
|
|
|
delta?: string
|
|
|
|
detail?: any
|
|
|
|
|
|
|
|
// relevant for both ChatGPTAPI and ChatGPTUnofficialProxyAPI
|
2023-02-01 09:14:10 +00:00
|
|
|
parentMessageId?: string
|
2023-03-02 02:49:20 +00:00
|
|
|
// only relevant for ChatGPTUnofficialProxyAPI
|
2022-12-16 06:28:30 +00:00
|
|
|
conversationId?: string
|
|
|
|
}
|
|
|
|
|
2023-02-01 09:14:10 +00:00
|
|
|
export class ChatGPTError extends Error {
|
|
|
|
statusCode?: number
|
|
|
|
statusText?: string
|
2023-02-19 07:56:19 +00:00
|
|
|
isFinal?: boolean
|
|
|
|
accountId?: string
|
2022-12-16 06:28:30 +00:00
|
|
|
}
|
2023-02-01 10:48:36 +00:00
|
|
|
|
|
|
|
/** Returns a chat message from a store by it's ID (or null if not found). */
|
|
|
|
export type GetMessageByIdFunction = (id: string) => Promise<ChatMessage>
|
|
|
|
|
|
|
|
/** Upserts a chat message to a store. */
|
|
|
|
export type UpsertMessageFunction = (message: ChatMessage) => Promise<void>
|
|
|
|
|
2023-02-19 07:56:19 +00:00
|
|
|
/**
|
|
|
|
* https://chat.openapi.com/backend-api/conversation
|
|
|
|
*/
|
|
|
|
export type ConversationJSONBody = {
|
|
|
|
/**
|
|
|
|
* The action to take
|
|
|
|
*/
|
|
|
|
action: string
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ID of the conversation
|
|
|
|
*/
|
|
|
|
conversation_id?: string
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prompts to provide
|
|
|
|
*/
|
|
|
|
messages: Prompt[]
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The model to use
|
|
|
|
*/
|
|
|
|
model: string
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The parent message ID
|
|
|
|
*/
|
|
|
|
parent_message_id: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Prompt = {
|
|
|
|
/**
|
|
|
|
* The content of the prompt
|
|
|
|
*/
|
|
|
|
content: PromptContent
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ID of the prompt
|
|
|
|
*/
|
|
|
|
id: string
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The role played in the prompt
|
|
|
|
*/
|
|
|
|
role: Role
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ContentType = 'text'
|
|
|
|
|
|
|
|
export type PromptContent = {
|
|
|
|
/**
|
|
|
|
* The content type of the prompt
|
|
|
|
*/
|
|
|
|
content_type: ContentType
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The parts to the prompt
|
|
|
|
*/
|
|
|
|
parts: string[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ConversationResponseEvent = {
|
|
|
|
message?: Message
|
|
|
|
conversation_id?: string
|
|
|
|
error?: string | null
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Message = {
|
|
|
|
id: string
|
|
|
|
content: MessageContent
|
|
|
|
role: Role
|
|
|
|
user: string | null
|
|
|
|
create_time: string | null
|
|
|
|
update_time: string | null
|
|
|
|
end_turn: null
|
|
|
|
weight: number
|
|
|
|
recipient: string
|
|
|
|
metadata: MessageMetadata
|
|
|
|
}
|
|
|
|
|
|
|
|
export type MessageContent = {
|
|
|
|
content_type: string
|
|
|
|
parts: string[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export type MessageMetadata = any
|
|
|
|
|
2023-03-02 02:49:20 +00:00
|
|
|
export interface CreateChatCompletionDeltaResponse {
|
|
|
|
id: string
|
|
|
|
object: 'chat.completion.chunk'
|
|
|
|
created: number
|
|
|
|
model: string
|
|
|
|
choices: [
|
|
|
|
{
|
|
|
|
delta: {
|
|
|
|
role: Role
|
|
|
|
content?: string
|
|
|
|
}
|
|
|
|
index: number
|
|
|
|
finish_reason: string | null
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|