2022-12-06 07:38:32 +00:00
|
|
|
import dotenv from 'dotenv-safe'
|
|
|
|
import { oraPromise } from 'ora'
|
|
|
|
|
2023-01-02 21:27:08 +00:00
|
|
|
import { ChatGPTAPIBrowser } from '../src'
|
2022-12-06 07:38:32 +00:00
|
|
|
|
|
|
|
dotenv.config()
|
|
|
|
|
|
|
|
/**
|
2022-12-06 22:13:11 +00:00
|
|
|
* Demo CLI for testing conversation support.
|
2022-12-06 07:38:32 +00:00
|
|
|
*
|
|
|
|
* ```
|
2022-12-15 07:25:38 +00:00
|
|
|
* npx tsx demos/demo-conversation.ts
|
2022-12-06 07:38:32 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
async function main() {
|
2022-12-12 23:23:36 +00:00
|
|
|
const email = process.env.OPENAI_EMAIL
|
|
|
|
const password = process.env.OPENAI_PASSWORD
|
2022-12-12 11:28:53 +00:00
|
|
|
|
2023-01-02 21:27:08 +00:00
|
|
|
const api = new ChatGPTAPIBrowser({
|
2022-12-12 11:28:53 +00:00
|
|
|
email,
|
2023-01-02 21:27:08 +00:00
|
|
|
password,
|
|
|
|
debug: false,
|
|
|
|
minimize: true
|
2022-12-12 11:28:53 +00:00
|
|
|
})
|
2022-12-17 04:48:42 +00:00
|
|
|
await api.initSession()
|
2022-12-06 07:38:32 +00:00
|
|
|
|
2022-12-17 04:48:42 +00:00
|
|
|
const prompt = 'Write a poem about cats.'
|
2022-12-06 07:38:32 +00:00
|
|
|
|
2022-12-17 04:48:42 +00:00
|
|
|
let res = await oraPromise(api.sendMessage(prompt), {
|
2022-12-06 07:38:32 +00:00
|
|
|
text: prompt
|
|
|
|
})
|
|
|
|
|
2022-12-17 04:48:42 +00:00
|
|
|
console.log('\n' + res.response + '\n')
|
2022-12-06 07:38:32 +00:00
|
|
|
|
2022-12-17 04:48:42 +00:00
|
|
|
const prompt2 = 'Can you make it cuter and shorter?'
|
2022-12-06 07:38:32 +00:00
|
|
|
|
2022-12-17 04:48:42 +00:00
|
|
|
res = await oraPromise(
|
|
|
|
api.sendMessage(prompt2, {
|
|
|
|
conversationId: res.conversationId,
|
|
|
|
parentMessageId: res.messageId
|
|
|
|
}),
|
|
|
|
{
|
2022-12-06 07:38:32 +00:00
|
|
|
text: prompt2
|
2022-12-17 04:48:42 +00:00
|
|
|
}
|
2022-12-06 07:38:32 +00:00
|
|
|
)
|
2022-12-17 04:48:42 +00:00
|
|
|
console.log('\n' + res.response + '\n')
|
2022-12-06 07:38:32 +00:00
|
|
|
|
2022-12-17 04:48:42 +00:00
|
|
|
const prompt3 = 'Now write it in French.'
|
2022-12-06 15:35:57 +00:00
|
|
|
|
2022-12-17 04:48:42 +00:00
|
|
|
res = await oraPromise(
|
|
|
|
api.sendMessage(prompt3, {
|
|
|
|
conversationId: res.conversationId,
|
|
|
|
parentMessageId: res.messageId
|
|
|
|
}),
|
|
|
|
{
|
2022-12-06 15:35:57 +00:00
|
|
|
text: prompt3
|
2022-12-17 04:48:42 +00:00
|
|
|
}
|
2022-12-06 15:35:57 +00:00
|
|
|
)
|
2022-12-17 04:48:42 +00:00
|
|
|
console.log('\n' + res.response + '\n')
|
2022-12-06 15:35:57 +00:00
|
|
|
|
2022-12-17 04:48:42 +00:00
|
|
|
const prompt4 = 'What were we talking about again?'
|
2022-12-06 15:35:57 +00:00
|
|
|
|
2022-12-17 04:48:42 +00:00
|
|
|
res = await oraPromise(
|
|
|
|
api.sendMessage(prompt4, {
|
|
|
|
conversationId: res.conversationId,
|
|
|
|
parentMessageId: res.messageId
|
|
|
|
}),
|
|
|
|
{
|
2022-12-06 15:35:57 +00:00
|
|
|
text: prompt4
|
2022-12-17 04:48:42 +00:00
|
|
|
}
|
2022-12-06 07:38:32 +00:00
|
|
|
)
|
2022-12-17 04:48:42 +00:00
|
|
|
console.log('\n' + res.response + '\n')
|
|
|
|
|
2023-01-02 23:28:35 +00:00
|
|
|
// close the browser at the end
|
2022-12-17 04:48:42 +00:00
|
|
|
await api.closeSession()
|
2022-12-06 07:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main().catch((err) => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|