2022-12-06 07:38:32 +00:00
|
|
|
import dotenv from 'dotenv-safe'
|
|
|
|
import { oraPromise } from 'ora'
|
|
|
|
|
2023-02-01 09:14:10 +00:00
|
|
|
import { ChatGPTAPI } 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() {
|
2023-02-01 09:14:10 +00:00
|
|
|
const api = new ChatGPTAPI({
|
|
|
|
apiKey: process.env.OPENAI_API_KEY,
|
|
|
|
debug: false
|
2022-12-12 11:28:53 +00:00
|
|
|
})
|
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
|
|
|
|
})
|
|
|
|
|
2023-02-01 09:14:10 +00:00
|
|
|
console.log('\n' + res.text + '\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,
|
2023-02-01 09:14:10 +00:00
|
|
|
parentMessageId: res.id
|
2022-12-17 04:48:42 +00:00
|
|
|
}),
|
|
|
|
{
|
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
|
|
|
)
|
2023-02-01 09:14:10 +00:00
|
|
|
console.log('\n' + res.text + '\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,
|
2023-02-01 09:14:10 +00:00
|
|
|
parentMessageId: res.id
|
2022-12-17 04:48:42 +00:00
|
|
|
}),
|
|
|
|
{
|
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
|
|
|
)
|
2023-02-01 09:14:10 +00:00
|
|
|
console.log('\n' + res.text + '\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,
|
2023-02-01 09:14:10 +00:00
|
|
|
parentMessageId: res.id
|
2022-12-17 04:48:42 +00:00
|
|
|
}),
|
|
|
|
{
|
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
|
|
|
)
|
2023-02-01 09:14:10 +00:00
|
|
|
console.log('\n' + res.text + '\n')
|
2022-12-06 07:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main().catch((err) => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|