chatgpt-api/src/example.ts

58 wiersze
1.3 KiB
TypeScript
Czysty Zwykły widok Historia

2022-12-02 23:43:59 +00:00
import delay from 'delay'
import { oraPromise } from 'ora'
import { ChatGPTAPI } from './chatgpt-api'
/**
* Example CLI for testing functionality.
*/
async function main() {
const api = new ChatGPTAPI()
await api.init()
const isSignedIn = await api.getIsSignedIn()
if (!isSignedIn) {
// Wait until the user signs in via the chromium browser
await oraPromise(
new Promise<void>(async (resolve, reject) => {
2022-12-03 08:46:57 +00:00
do {
try {
await delay(1000)
const isSignedIn = await api.getIsSignedIn()
if (isSignedIn) {
return resolve()
}
} catch (err) {
return reject(err)
2022-12-02 23:43:59 +00:00
}
2022-12-03 08:46:57 +00:00
} while (true)
2022-12-02 23:43:59 +00:00
}),
2022-12-03 08:46:57 +00:00
'Please sign in to ChatGPT and dismiss the welcome modal'
2022-12-02 23:43:59 +00:00
)
}
const response = await api.sendMessage(
// 'Write a TypeScript function for conway sort.'
'Write a python version of bubble sort. Do not include example usage.'
)
2022-12-03 00:09:45 +00:00
2022-12-02 23:43:59 +00:00
// const prompts = await api.getPrompts()
// const messages = await api.getMessages()
// console.log('prompts', prompts)
// console.log('messages', messages)
2022-12-03 00:09:45 +00:00
// Wait forever; useful for debugging chromium sessions
2022-12-02 23:43:59 +00:00
// await new Promise(() => {})
await api.close()
2022-12-05 07:31:27 +00:00
return response
2022-12-02 23:43:59 +00:00
}
main().then((res) => {
console.log(res)
})