optional params for Conversation

pull/26/head
simon3000 2022-12-06 14:46:55 +01:00
rodzic 9e891f529a
commit df94c61313
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 31FCC4A4E83CDA61
1 zmienionych plików z 24 dodań i 15 usunięć

Wyświetl plik

@ -16,16 +16,21 @@ const USER_AGENT =
* manually pass the conversation ID and parent message ID for each message. * manually pass the conversation ID and parent message ID for each message.
*/ */
class Conversation { class Conversation {
protected _api: ChatGPTAPI api: ChatGPTAPI
protected _conversationId: string = undefined conversationId: string = undefined
protected _parentMessageId: string = undefined parentMessageId: string = undefined
/** /**
* Creates a new conversation wrapper around the ChatGPT API. * Creates a new conversation wrapper around the ChatGPT API.
* @param api - The ChatGPT API instance to use. * @param api - The ChatGPT API instance to use.
*/ */
constructor(api: ChatGPTAPI) { constructor(
this._api = api api: ChatGPTAPI,
opts: { conversationId?: string; parentMessageId?: string } = {}
) {
this.api = api
this.conversationId = opts.conversationId
this.parentMessageId = opts.parentMessageId
} }
/** /**
@ -54,23 +59,23 @@ class Conversation {
} = {} } = {}
) { ) {
const { onProgress, onConversationResponse } = opts const { onProgress, onConversationResponse } = opts
if (!this._conversationId) { if (!this.conversationId) {
return this._api.sendMessage(message, { return this.api.sendMessage(message, {
onProgress, onProgress,
onConversationResponse: (response) => { onConversationResponse: (response) => {
this._conversationId = response.conversation_id this.conversationId = response.conversation_id
this._parentMessageId = response.message.id this.parentMessageId = response.message.id
onConversationResponse?.(response) onConversationResponse?.(response)
} }
}) })
} }
return this._api.sendMessage(message, { return this.api.sendMessage(message, {
conversationId: this._conversationId, conversationId: this.conversationId,
parentMessageId: this._parentMessageId, parentMessageId: this.parentMessageId,
onProgress, onProgress,
onConversationResponse: (response) => { onConversationResponse: (response) => {
this._parentMessageId = response.message.id this.parentMessageId = response.message.id
onConversationResponse?.(response) onConversationResponse?.(response)
} }
}) })
@ -285,9 +290,13 @@ export class ChatGPTAPI {
/** /**
* Get a new Conversation instance, which can be used to send multiple messages as part of a single conversation. * Get a new Conversation instance, which can be used to send multiple messages as part of a single conversation.
* *
* @param opts.conversationId - Optional Data of the previous message in a conversation
* @param opts.parentMessageId - Optional Data of the previous message in a conversation
* @returns a new Conversation instance * @returns a new Conversation instance
*/ */
getConversation() { getConversation(
return new Conversation(this) opts: { conversationId?: string; parentMessageId?: string } = {}
) {
return new Conversation(this, opts)
} }
} }