Skip to content

Instantly share code, notes, and snippets.

@temberature
Created February 2, 2023 05:48
Show Gist options
  • Save temberature/a70a098a7c8817ea9290b32628c05ab3 to your computer and use it in GitHub Desktop.
Save temberature/a70a098a7c8817ea9290b32628c05ab3 to your computer and use it in GitHub Desktop.
chatgpt 迭代总结
import { ChatGPTAPIBrowser } from 'chatgpt'
import fs from 'fs'
import { readdir } from 'node:fs/promises';
const directoryPath = "./text";
async function example() {
// use puppeteer to bypass cloudflare (headful because of captchas)
const api = new ChatGPTAPIBrowser({
email: process.env.OPENAI_EMAIL,
password: process.env.OPENAI_PASSWORD
})
await api.initSession()
let res;
const files = await readdir(directoryPath, { withFileTypes: true });
for (let index = 0; index < files.length; index++) {
const file = files[index].name;
if (!file.includes('.md')) {
return;
}
console.log(file);
const content = fs.readFileSync(directoryPath + '/' + file, 'utf8').replaceAll(/ {2,}/g, ' ').replaceAll('\n', '');
const size = 500;
for (let i = 0; i < content.length; i += size) {
try {
const data = '用中文总结一下"' + ((res || {}).response || '') + content.slice(i, i + size) + '"';
console.log(data);
if (!res) {
res = await api.sendMessage(data)
console.log(res.response)
} else {
res = await api.sendMessage(data, {
conversationId: res.conversationId,
parentMessageId: res.messageId
})
console.log(res.response)
}
} catch (err) {
console.error(err);
}
}
}
// send a message and wait for the response
// send a follow-up
// send another follow-up
// send a follow-up
}
example()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment