Skip to content

Commit

Permalink
feat: redesign main sendMessage, initSession, closeSession API
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Dec 17, 2022
1 parent 24f51ac commit 1af5db2
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 224 deletions.
57 changes: 40 additions & 17 deletions demos/demo-conversation-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,63 @@ async function main() {
const email = process.env.OPENAI_EMAIL
const password = process.env.OPENAI_PASSWORD

const api = new ChatGPTAPIBrowser({ email, password, debug: true })
await api.init()
const api = new ChatGPTAPIBrowser({
email,
password,
debug: false,
minimize: true
})
await api.initSession()

const prompt = 'What is OpenAI?'
const prompt = 'Write a poem about cats.'

const response = await oraPromise(api.sendMessage(prompt), {
let res = await oraPromise(api.sendMessage(prompt), {
text: prompt
})

console.log(response)
console.log('\n' + res.response + '\n')

const prompt2 = 'Did they made OpenGPT?'
const prompt2 = 'Can you make it cuter and shorter?'

console.log(
await oraPromise(api.sendMessage(prompt2), {
res = await oraPromise(
api.sendMessage(prompt2, {
conversationId: res.conversationId,
parentMessageId: res.messageId
}),
{
text: prompt2
})
}
)
console.log('\n' + res.response + '\n')

const prompt3 = 'Who founded this institute?'
const prompt3 = 'Now write it in French.'

console.log(
await oraPromise(api.sendMessage(prompt3), {
res = await oraPromise(
api.sendMessage(prompt3, {
conversationId: res.conversationId,
parentMessageId: res.messageId
}),
{
text: prompt3
})
}
)
console.log('\n' + res.response + '\n')

const prompt4 = 'Who is that?'
const prompt4 = 'What were we talking about again?'

console.log(
await oraPromise(api.sendMessage(prompt4), {
res = await oraPromise(
api.sendMessage(prompt4, {
conversationId: res.conversationId,
parentMessageId: res.messageId
}),
{
text: prompt4
})
}
)
console.log('\n' + res.response + '\n')

// close the browser at the end
await api.closeSession()
}

main().catch((err) => {
Expand Down
51 changes: 33 additions & 18 deletions demos/demo-conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,56 @@ async function main() {
})

const api = new ChatGPTAPI({ ...authInfo })
await api.ensureAuth()
await api.initSession()

const conversation = api.getConversation()
const prompt = 'Write a poem about cats.'

const prompt = 'What is OpenAI?'

const response = await oraPromise(conversation.sendMessage(prompt), {
let res = await oraPromise(api.sendMessage(prompt), {
text: prompt
})

console.log(response)
console.log('\n' + res.response + '\n')

const prompt2 = 'Did they made OpenGPT?'
const prompt2 = 'Can you make it cuter and shorter?'

console.log(
await oraPromise(conversation.sendMessage(prompt2), {
res = await oraPromise(
api.sendMessage(prompt2, {
conversationId: res.conversationId,
parentMessageId: res.messageId
}),
{
text: prompt2
})
}
)
console.log('\n' + res.response + '\n')

const prompt3 = 'Who founded this institute?'
const prompt3 = 'Now write it in French.'

console.log(
await oraPromise(conversation.sendMessage(prompt3), {
res = await oraPromise(
api.sendMessage(prompt3, {
conversationId: res.conversationId,
parentMessageId: res.messageId
}),
{
text: prompt3
})
}
)
console.log('\n' + res.response + '\n')

const prompt4 = 'Who is that?'
const prompt4 = 'What were we talking about again?'

console.log(
await oraPromise(conversation.sendMessage(prompt4), {
res = await oraPromise(
api.sendMessage(prompt4, {
conversationId: res.conversationId,
parentMessageId: res.messageId
}),
{
text: prompt4
})
}
)
console.log('\n' + res.response + '\n')

await api.closeSession()
}

main().catch((err) => {
Expand Down
21 changes: 9 additions & 12 deletions demos/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,21 @@ async function main() {
debug: false,
minimize: true
})
await api.init()
await api.initSession()

const prompt =
'Write a python version of bubble sort. Do not include example usage.'

const response = await oraPromise(api.sendMessage(prompt), {
const res = await oraPromise(api.sendMessage(prompt), {
text: prompt
})
console.log(res.response)

await api.close()
return response
// close the browser at the end
await api.closeSession()
}

main()
.then((res) => {
console.log(res)
})
.catch((err) => {
console.error(err)
process.exit(1)
})
main().catch((err) => {
console.error(err)
process.exit(1)
})
66 changes: 66 additions & 0 deletions src/abstract-chatgpt-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import * as types from './types'

export abstract class AChatGPTAPI {
/**
* Performs any async initialization work required to ensure that this API is
* properly authenticated.
*
* @throws An error if the session failed to initialize properly.
*/
abstract initSession(): Promise<void>

/**
* Sends a message to ChatGPT, waits for the response to resolve, and returns
* the response.
*
* If you want to receive a stream of partial responses, use `opts.onProgress`.
*
* @param message - The prompt message to send
* @param opts.conversationId - Optional ID of a conversation to continue
* @param opts.parentMessageId - Optional ID of the previous message in the conversation
* @param opts.messageId - Optional ID of the message to send (defaults to a random UUID)
* @param opts.action - Optional ChatGPT `action` (either `next` or `variant`)
* @param opts.timeoutMs - Optional timeout in milliseconds (defaults to no timeout)
* @param opts.onProgress - Optional callback which will be invoked every time the partial response is updated
* @param opts.abortSignal - Optional callback used to abort the underlying `fetch` call using an [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)
*
* @returns The response from ChatGPT, including `conversationId`, `messageId`, and
* the `response` text.
*/
abstract sendMessage(
message: string,
opts?: types.SendMessageOptions
): Promise<types.ChatResponse>

/**
* @returns `true` if the client is authenticated with a valid session or `false`
* otherwise.
*/
abstract getIsAuthenticated(): Promise<boolean>

/**
* Refreshes the current ChatGPT session.
*
* @returns Access credentials for the new session.
* @throws An error if it fails.
*/
abstract refreshSession(): Promise<any>

/**
* Closes the current ChatGPT session and starts a new one.
*
* @returns Access credentials for the new session.
* @throws An error if it fails.
*/
async resetSession(): Promise<any> {
await this.closeSession()
return this.initSession()
}

/**
* Closes the active session.
*
* @throws An error if it fails.
*/
abstract closeSession(): Promise<void>
}
Loading

0 comments on commit 1af5db2

Please sign in to comment.