chatgpt-api/demos/demo-conversation.ts

82 wiersze
1.6 KiB
TypeScript
Czysty Zwykły widok Historia

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()
/**
* 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() {
const email = process.env.OPENAI_EMAIL
const password = process.env.OPENAI_PASSWORD
2023-01-02 21:27:08 +00:00
const api = new ChatGPTAPIBrowser({
email,
2023-01-02 21:27:08 +00:00
password,
debug: false,
minimize: true
})
await api.initSession()
2022-12-06 07:38:32 +00:00
const prompt = 'Write a poem about cats.'
2022-12-06 07:38:32 +00:00
let res = await oraPromise(api.sendMessage(prompt), {
2022-12-06 07:38:32 +00:00
text: prompt
})
console.log('\n' + res.response + '\n')
2022-12-06 07:38:32 +00:00
const prompt2 = 'Can you make it cuter and shorter?'
2022-12-06 07:38:32 +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-06 07:38:32 +00:00
)
console.log('\n' + res.response + '\n')
2022-12-06 07:38:32 +00:00
const prompt3 = 'Now write it in French.'
2022-12-06 15:35:57 +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-06 15:35:57 +00:00
)
console.log('\n' + res.response + '\n')
2022-12-06 15:35:57 +00:00
const prompt4 = 'What were we talking about again?'
2022-12-06 15:35:57 +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-06 07:38:32 +00:00
)
console.log('\n' + res.response + '\n')
// close the browser at the end
await api.closeSession()
2022-12-06 07:38:32 +00:00
}
main().catch((err) => {
console.error(err)
process.exit(1)
})