* Sends a message to ChatGPT, waits for the response to resolve, and returns
* the response.
* If this is the first message in the conversation, the conversation ID and
* parent message ID will be automatically set.
* This allows you to send multiple messages to ChatGPT and receive responses,
* without having to manually pass the conversation ID and parent message ID
* for each message.
* If you want to manually pass the conversation ID and parent message ID,
* use `api.sendMessage` instead.
*
* @param message - The plaintext message to send.
* @param opts.onProgress - Optional listener which will be called every time the partial response is updated
* @param opts.onConversationResponse - Optional listener which will be called every time a conversation response is received
* @returns The plaintext response from ChatGPT.
*/
asyncsendMessage(
message:string,
opts:{
onProgress?:(partialResponse:string)=>void
onConversationResponse?:(
response:types.ConversationResponseEvent
)=>void
}={}
){
const{onProgress,onConversationResponse}=opts
if(!this.conversationId){
returnthis.api.sendMessage(message,{
onProgress,
onConversationResponse:(response)=>{
this.conversationId=response.conversation_id
this.parentMessageId=response.message.id
onConversationResponse?.(response)
}
})
}
returnthis.api.sendMessage(message,{
conversationId:this.conversationId,
parentMessageId:this.parentMessageId,
onProgress,
onConversationResponse:(response)=>{
this.conversationId=response.conversation_id
this.parentMessageId=response.message.id
onConversationResponse?.(response)
}
})
}
}
exportclassChatGPTAPI{
protected_sessionToken:string
protected_markdown:boolean
...
...
@@ -82,15 +155,25 @@ export class ChatGPTAPI {
* @param message - The plaintext message to send.
* @param opts.conversationId - Optional ID of the previous message in a conversation
* @param opts.onProgress - Optional listener which will be called every time the partial response is updated
* @param opts.onConversationResponse - Optional listener which will be called every time the partial response is updated with the full conversation response