chatgpt-api/demos/demo-conversation.ts

81 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'
import { ChatGPTAPI, getOpenAIAuth } 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
const authInfo = await getOpenAIAuth({
email,
password
})
const api = new ChatGPTAPI({ ...authInfo })
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')
await api.closeSession()
2022-12-06 07:38:32 +00:00
}
main().catch((err) => {
console.error(err)
process.exit(1)
})