types.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. import * as Keyv from 'keyv'
  2. export type Role = 'user' | 'assistant' | 'system'
  3. export type FetchFn = typeof fetch
  4. export type ChatGPTAPIOptions = {
  5. apiKey: string
  6. apiEndpoint: string
  7. apiDeployment: string
  8. apiVersion: string
  9. apiOrg?: string
  10. /** @defaultValue `false` **/
  11. debug?: boolean
  12. completionParams?: Partial<Omit<openai.CreateChatCompletionRequest, 'messages' | 'n' | 'stream'>>
  13. systemMessage?: string
  14. /** @defaultValue `4096` **/
  15. maxModelTokens?: number
  16. /** @defaultValue `1000` **/
  17. maxResponseTokens?: number
  18. messageStore?: Keyv
  19. getMessageById?: GetMessageByIdFunction
  20. upsertMessage?: UpsertMessageFunction
  21. fetch?: FetchFn
  22. }
  23. export type SendMessageOptions = {
  24. /** The name of a user in a multi-user chat. */
  25. name?: string
  26. parentMessageId?: string
  27. conversationId?: string
  28. messageId?: string
  29. stream?: boolean
  30. systemMessage?: string
  31. timeoutMs?: number
  32. onProgress?: (partialResponse: ChatMessage) => void
  33. abortSignal?: AbortSignal
  34. completionParams?: Partial<Omit<openai.CreateChatCompletionRequest, 'messages' | 'n' | 'stream'>>
  35. }
  36. export type MessageActionType = 'next' | 'variant'
  37. export type SendMessageBrowserOptions = {
  38. conversationId?: string
  39. parentMessageId?: string
  40. messageId?: string
  41. action?: MessageActionType
  42. timeoutMs?: number
  43. onProgress?: (partialResponse: ChatMessage) => void
  44. abortSignal?: AbortSignal
  45. }
  46. export interface ChatMessage {
  47. id: string
  48. text: string
  49. role: Role
  50. name?: string
  51. delta?: string
  52. detail?: openai.CreateChatCompletionResponse | CreateChatCompletionStreamResponse
  53. // relevant for both ChatGPTAPI and ChatGPTUnofficialProxyAPI
  54. parentMessageId?: string
  55. // only relevant for ChatGPTUnofficialProxyAPI (optional for ChatGPTAPI)
  56. conversationId?: string
  57. }
  58. export class ChatGPTError extends Error {
  59. statusCode?: number
  60. statusText?: string
  61. isFinal?: boolean
  62. accountId?: string
  63. cause?: any
  64. constructor(message: string, options?: any) {
  65. super(message)
  66. this.cause = options?.cause
  67. }
  68. }
  69. /** Returns a chat message from a store by it's ID (or null if not found). */
  70. export type GetMessageByIdFunction = (id: string) => Promise<ChatMessage>
  71. /** Upserts a chat message to a store. */
  72. export type UpsertMessageFunction = (message: ChatMessage) => Promise<void>
  73. export interface CreateChatCompletionStreamResponse extends openai.CreateChatCompletionDeltaResponse {
  74. usage: CreateCompletionStreamResponseUsage
  75. }
  76. export interface CreateCompletionStreamResponseUsage extends openai.CreateCompletionResponseUsage {
  77. estimated: true
  78. }
  79. /**
  80. * https://chat.openapi.com/backend-api/conversation
  81. */
  82. export type ConversationJSONBody = {
  83. /**
  84. * The action to take
  85. */
  86. action: string
  87. /**
  88. * The ID of the conversation
  89. */
  90. conversation_id?: string
  91. /**
  92. * Prompts to provide
  93. */
  94. messages: Prompt[]
  95. /**
  96. * The model to use
  97. */
  98. model: string
  99. /**
  100. * The parent message ID
  101. */
  102. parent_message_id: string
  103. }
  104. export type Prompt = {
  105. /**
  106. * The content of the prompt
  107. */
  108. content: PromptContent
  109. /**
  110. * The ID of the prompt
  111. */
  112. id: string
  113. /**
  114. * The role played in the prompt
  115. */
  116. role: Role
  117. }
  118. export type ContentType = 'text'
  119. export type PromptContent = {
  120. /**
  121. * The content type of the prompt
  122. */
  123. content_type: ContentType
  124. /**
  125. * The parts to the prompt
  126. */
  127. parts: string[]
  128. }
  129. export type ConversationResponseEvent = {
  130. message?: Message
  131. conversation_id?: string
  132. error?: string | null
  133. }
  134. export type Message = {
  135. id: string
  136. content: MessageContent
  137. role: Role
  138. user: string | null
  139. create_time: string | null
  140. update_time: string | null
  141. end_turn: null
  142. weight: number
  143. recipient: string
  144. metadata: MessageMetadata
  145. }
  146. export type MessageContent = {
  147. content_type: string
  148. parts: string[]
  149. }
  150. export type MessageMetadata = any
  151. export namespace openai {
  152. export interface CreateChatCompletionDeltaResponse {
  153. id: string
  154. object: 'chat.completion.chunk'
  155. created: number
  156. model: string
  157. choices: [
  158. {
  159. delta: {
  160. role: Role
  161. content?: string
  162. }
  163. index: number
  164. finish_reason: string | null
  165. }
  166. ]
  167. }
  168. /**
  169. *
  170. * @export
  171. * @interface ChatCompletionRequestMessage
  172. */
  173. export interface ChatCompletionRequestMessage {
  174. /**
  175. * The role of the author of this message.
  176. * @type {string}
  177. * @memberof ChatCompletionRequestMessage
  178. */
  179. role: ChatCompletionRequestMessageRoleEnum
  180. /**
  181. * The contents of the message
  182. * @type {string}
  183. * @memberof ChatCompletionRequestMessage
  184. */
  185. content: string
  186. /**
  187. * The name of the user in a multi-user chat
  188. * @type {string}
  189. * @memberof ChatCompletionRequestMessage
  190. */
  191. name?: string
  192. }
  193. export declare const ChatCompletionRequestMessageRoleEnum: {
  194. readonly System: 'system'
  195. readonly User: 'user'
  196. readonly Assistant: 'assistant'
  197. }
  198. export declare type ChatCompletionRequestMessageRoleEnum =
  199. (typeof ChatCompletionRequestMessageRoleEnum)[keyof typeof ChatCompletionRequestMessageRoleEnum]
  200. /**
  201. *
  202. * @export
  203. * @interface ChatCompletionResponseMessage
  204. */
  205. export interface ChatCompletionResponseMessage {
  206. /**
  207. * The role of the author of this message.
  208. * @type {string}
  209. * @memberof ChatCompletionResponseMessage
  210. */
  211. role: ChatCompletionResponseMessageRoleEnum
  212. /**
  213. * The contents of the message
  214. * @type {string}
  215. * @memberof ChatCompletionResponseMessage
  216. */
  217. content: string
  218. }
  219. export declare const ChatCompletionResponseMessageRoleEnum: {
  220. readonly System: 'system'
  221. readonly User: 'user'
  222. readonly Assistant: 'assistant'
  223. }
  224. export declare type ChatCompletionResponseMessageRoleEnum =
  225. (typeof ChatCompletionResponseMessageRoleEnum)[keyof typeof ChatCompletionResponseMessageRoleEnum]
  226. /**
  227. *
  228. * @export
  229. * @interface CreateChatCompletionRequest
  230. */
  231. export interface CreateChatCompletionRequest {
  232. /**
  233. * ID of the model to use. Currently, only `gpt-3.5-turbo` and `gpt-3.5-turbo-0301` are supported.
  234. * @type {string}
  235. * @memberof CreateChatCompletionRequest
  236. */
  237. model: string
  238. /**
  239. * The messages to generate chat completions for, in the [chat format](/docs/guides/chat/introduction).
  240. * @type {Array<ChatCompletionRequestMessage>}
  241. * @memberof CreateChatCompletionRequest
  242. */
  243. messages: Array<ChatCompletionRequestMessage>
  244. /**
  245. * What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or `top_p` but not both.
  246. * @type {number}
  247. * @memberof CreateChatCompletionRequest
  248. */
  249. temperature?: number | null
  250. /**
  251. * An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or `temperature` but not both.
  252. * @type {number}
  253. * @memberof CreateChatCompletionRequest
  254. */
  255. top_p?: number | null
  256. /**
  257. * How many chat completion choices to generate for each input message.
  258. * @type {number}
  259. * @memberof CreateChatCompletionRequest
  260. */
  261. n?: number | null
  262. /**
  263. * If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) as they become available, with the stream terminated by a `data: [DONE]` message.
  264. * @type {boolean}
  265. * @memberof CreateChatCompletionRequest
  266. */
  267. stream?: boolean | null
  268. /**
  269. *
  270. * @type {CreateChatCompletionRequestStop}
  271. * @memberof CreateChatCompletionRequest
  272. */
  273. stop?: CreateChatCompletionRequestStop
  274. /**
  275. * The maximum number of tokens allowed for the generated answer. By default, the number of tokens the model can return will be (4096 - prompt tokens).
  276. * @type {number}
  277. * @memberof CreateChatCompletionRequest
  278. */
  279. max_tokens?: number
  280. /**
  281. * Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model\'s likelihood to talk about new topics. [See more information about frequency and presence penalties.](/docs/api-reference/parameter-details)
  282. * @type {number}
  283. * @memberof CreateChatCompletionRequest
  284. */
  285. presence_penalty?: number | null
  286. /**
  287. * Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model\'s likelihood to repeat the same line verbatim. [See more information about frequency and presence penalties.](/docs/api-reference/parameter-details)
  288. * @type {number}
  289. * @memberof CreateChatCompletionRequest
  290. */
  291. frequency_penalty?: number | null
  292. /**
  293. * Modify the likelihood of specified tokens appearing in the completion. Accepts a json object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
  294. * @type {object}
  295. * @memberof CreateChatCompletionRequest
  296. */
  297. logit_bias?: object | null
  298. /**
  299. * A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](/docs/guides/safety-best-practices/end-user-ids).
  300. * @type {string}
  301. * @memberof CreateChatCompletionRequest
  302. */
  303. user?: string
  304. }
  305. /**
  306. * @type CreateChatCompletionRequestStop
  307. * Up to 4 sequences where the API will stop generating further tokens.
  308. * @export
  309. */
  310. export declare type CreateChatCompletionRequestStop = Array<string> | string
  311. /**
  312. *
  313. * @export
  314. * @interface CreateChatCompletionResponse
  315. */
  316. export interface CreateChatCompletionResponse {
  317. /**
  318. *
  319. * @type {string}
  320. * @memberof CreateChatCompletionResponse
  321. */
  322. id: string
  323. /**
  324. *
  325. * @type {string}
  326. * @memberof CreateChatCompletionResponse
  327. */
  328. object: string
  329. /**
  330. *
  331. * @type {number}
  332. * @memberof CreateChatCompletionResponse
  333. */
  334. created: number
  335. /**
  336. *
  337. * @type {string}
  338. * @memberof CreateChatCompletionResponse
  339. */
  340. model: string
  341. /**
  342. *
  343. * @type {Array<CreateChatCompletionResponseChoicesInner>}
  344. * @memberof CreateChatCompletionResponse
  345. */
  346. choices: Array<CreateChatCompletionResponseChoicesInner>
  347. /**
  348. *
  349. * @type {CreateCompletionResponseUsage}
  350. * @memberof CreateChatCompletionResponse
  351. */
  352. usage?: CreateCompletionResponseUsage
  353. }
  354. /**
  355. *
  356. * @export
  357. * @interface CreateChatCompletionResponseChoicesInner
  358. */
  359. export interface CreateChatCompletionResponseChoicesInner {
  360. /**
  361. *
  362. * @type {number}
  363. * @memberof CreateChatCompletionResponseChoicesInner
  364. */
  365. index?: number
  366. /**
  367. *
  368. * @type {ChatCompletionResponseMessage}
  369. * @memberof CreateChatCompletionResponseChoicesInner
  370. */
  371. message?: ChatCompletionResponseMessage
  372. /**
  373. *
  374. * @type {string}
  375. * @memberof CreateChatCompletionResponseChoicesInner
  376. */
  377. finish_reason?: string
  378. }
  379. /**
  380. *
  381. * @export
  382. * @interface CreateCompletionResponseUsage
  383. */
  384. export interface CreateCompletionResponseUsage {
  385. /**
  386. *
  387. * @type {number}
  388. * @memberof CreateCompletionResponseUsage
  389. */
  390. prompt_tokens: number
  391. /**
  392. *
  393. * @type {number}
  394. * @memberof CreateCompletionResponseUsage
  395. */
  396. completion_tokens: number
  397. /**
  398. *
  399. * @type {number}
  400. * @memberof CreateCompletionResponseUsage
  401. */
  402. total_tokens: number
  403. }
  404. }