Last active
July 4, 2023 20:10
-
-
Save louis030195/f98373cc710cf4d4aa1d104add9ade15 to your computer and use it in GitHub Desktop.
shitty attempt to build a mineflayer ai assistant to create bots :D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { exec } = require('child_process'); | |
const recursive = require("recursive-readdir"); | |
const { Configuration, OpenAIApi } = require("openai"); | |
const readline = require('readline'); | |
const fs = require('fs'); | |
const path = require('path'); | |
async function listModels() { | |
const response = await fetch("https://api.airtable.com/v0/appwJMZ6IAUnKpSwV/all", { | |
headers: { | |
Authorization: "Bearer patBrBkdsFw0ArVlF.89a5669f5fd05d20e1d0f77216d072d929b13a215c0471b9a1a2d764537cbe8d" | |
} | |
}); | |
const data = await response.json(); | |
return { | |
data: data.records | |
.filter((record) => record.fields["url"] !== undefined) | |
.map((record) => ({ | |
id: record.id, | |
object: "model", | |
owned_by: record.fields["contact"] || "anonymous", | |
permission: ["read"], | |
createdTime: record.createdTime, | |
...record.fields | |
})) | |
} | |
} | |
const cloneRepo = () => { | |
return new Promise((resolve, reject) => { | |
const repoPath = path.resolve(__dirname, 'mineflayer'); | |
if (fs.existsSync(repoPath)) { | |
console.log('Directory already exists'); | |
resolve('Directory already exists'); | |
} else { | |
exec('git clone https://github.com/PrismarineJS/mineflayer', (error, stdout, stderr) => { | |
if (error) { | |
console.error(`exec error: ${error}`); | |
reject(error); | |
} | |
console.log(stdout ? stdout : stderr); | |
resolve(stdout ? stdout : stderr); | |
}); | |
} | |
}); | |
} | |
const readFiles = () => { | |
return new Promise((resolve, reject) => { | |
recursive("mineflayer", (err, files) => { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(files); | |
} | |
}); | |
}); | |
} | |
const buildContext = async (botType) => { | |
const files = await readFiles(); | |
let responseObj = {}; | |
for (let file of files) { | |
if (!file.includes(".js") && !file.includes(".md")) continue | |
const content = fs.readFileSync(file, 'utf8'); | |
console.log(`Reading file: ${file}`); | |
const contentLines = content.split('\n'); | |
const chunks = []; | |
while (contentLines.length > 0) { | |
chunks.push(contentLines.splice(0, 50).join('\n')) | |
} | |
for (const chunk of chunks) { | |
const aiResponse = await askQuestionToAI( | |
`Given this file chunk: ${chunk}, can it be useful to solve the user question: ${botType}? You answer the minimalistic piece of useful commented code or say "no"` | |
); | |
console.log(`AI Response: ${aiResponse}`); | |
if (aiResponse.toLowerCase().includes('no')) { | |
if (!responseObj[file]) { | |
responseObj[file] = []; | |
} | |
responseObj[file].push(aiResponse); | |
} | |
} | |
} | |
return responseObj | |
} | |
let modelUrl | |
let model | |
const askQuestionToAI = (prompt) => { | |
const configuration = new Configuration({ | |
basePath: modelUrl, | |
apiKey: 'EMPTY', | |
}); | |
const openai = new OpenAIApi(configuration); | |
console.log(`Asking AI: ${prompt}`); | |
return openai.createCompletion({ | |
model: model, | |
prompt: prompt, | |
max_tokens: 100, | |
stream: false, | |
}).then((response) => response.data.choices[0].text) | |
} | |
(async () => { | |
const listModelsResponse = await listModels(); | |
modelUrl = listModelsResponse.data[0].url; | |
model = listModelsResponse.data[0].model; | |
console.log(`Selected model: ${model}`); | |
await cloneRepo(); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
rl.question('What kind of bot do you want to create? ', async (botType) => { | |
console.log(`User input: ${botType}`); | |
const responses = await buildContext(botType); | |
console.log(responses); | |
rl.close(); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment