chatgpt-api/demos/demo-conversation.ts

73 wiersze
1.3 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 } 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 api = new ChatGPTAPI({
apiKey: process.env.OPENAI_API_KEY,
debug: false
})
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.text + '\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.id
}),
{
2022-12-06 07:38:32 +00:00
text: prompt2
}
2022-12-06 07:38:32 +00:00
)
console.log('\n' + res.text + '\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.id
}),
{
2022-12-06 15:35:57 +00:00
text: prompt3
}
2022-12-06 15:35:57 +00:00
)
console.log('\n' + res.text + '\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.id
}),
{
2022-12-06 15:35:57 +00:00
text: prompt4
}
2022-12-06 07:38:32 +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)
})