Skip to content

Commit

Permalink
Prepare files upload
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 committed Oct 23, 2024
1 parent 9d01b73 commit 13f61db
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 9 deletions.
4 changes: 4 additions & 0 deletions packages/ethernaut-ai/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ extendConfig((config, userConfig) => {
localConfig.ai?.interpreter?.additionalInstructions.concat() ||
userConfig.ethernaut?.ai?.interpreter?.additionalInstructions?.concat() ||
[],
files:
localConfig.ai?.interpreter?.files.concat() ||
userConfig.ethernaut?.ai?.interpreter?.files?.concat() ||
[],
},
explainer: {
additionalInstructions:
Expand Down
10 changes: 7 additions & 3 deletions packages/ethernaut-ai/src/internal/assistants/Assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ const storage = require('../storage')
const openai = require('../openai')
const debug = require('ethernaut-common/src/ui/debug')
const output = require('ethernaut-common/src/ui/output')
const { uploadFiles } = require('./utils/files')
const EventEmitter = require('events')
const EthernautCliError = require('ethernaut-common/src/error/error')

class Assistant extends EventEmitter {
constructor(name, config) {
super()

async initialize(hre, name, config) {
if (hre.config.ethernaut.ai.model !== config.model) {
config.model = hre.config.ethernaut.ai.model
}
Expand Down Expand Up @@ -185,6 +184,11 @@ class Assistant extends EventEmitter {
text,
)
}

async processFiles(files) {
await uploadFiles(openai(), files)
process.exit(0)
}
}

module.exports = Assistant
4 changes: 2 additions & 2 deletions packages/ethernaut-ai/src/internal/assistants/Explainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Assistant = require('./Assistant')
const Thread = require('../threads/Thread')

class Explainer extends Assistant {
constructor(hre) {
async initialize(hre) {
const config = require('./configs/explainer.json')

const docs = buildDocs(hre)
Expand All @@ -12,7 +12,7 @@ class Explainer extends Assistant {
docs.join('\n'),
)

super('explainer', config)
await super.initialize(hre, 'explainer', config)

this.injectAdditionalInstructions(
hre.config.ethernaut.ai.explainer.additionalInstructions,
Expand Down
6 changes: 4 additions & 2 deletions packages/ethernaut-ai/src/internal/assistants/Interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ const Action = require('../Action')
const debug = require('ethernaut-common/src/ui/debug')

class Interpreter extends Assistant {
constructor(hre) {
async initialize(hre) {
const config = require('./configs/interpreter.json')
config.tools = buildToolsSpec(hre)

super('interpreter', config)
await super.initialize(hre, 'interpreter', config)

this.injectAdditionalInstructions(
hre.config.ethernaut.ai.interpreter.additionalInstructions,
)

await this.processFiles(hre.config.ethernaut.ai.interpreter.files)

this.on('tool_calls_required', this.processToolCalls)

this.hre = hre
Expand Down
63 changes: 63 additions & 0 deletions packages/ethernaut-ai/src/internal/assistants/utils/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const fs = require('fs')
const path = require('path')
const debug = require('ethernaut-common/src/ui/debug')

async function uploadFiles(openai, files) {
// await _deleteAllFiles(openai)

const uploadedFiles = await listFiles(openai)
debug.log(`Uploaded files: ${JSON.stringify(uploadedFiles, null, 2)}`, 'ai')

for (const file of files) {
const filename = path.basename(file)

if (isUploaded(uploadedFiles, filename)) {
debug.log(`File ${file} already uploaded`, 'ai')
continue
}

await _uploadFile(openai, file)
}
}

function isUploaded(uploadedFiles, filename) {
debug.log(`Checking if file ${filename} is uploaded`, 'ai')

const uploaded = uploadedFiles.find((f) => f.filename === filename)

if (!uploaded) return false

if (uploaded.status !== 'processed') return false

return true
}

async function listFiles(openai) {
const response = await openai.files.list()
return response.data
}

async function _uploadFile(openai, file) {
debug.log(`Uploading file ${file}`, 'ai')

const fileStream = fs.createReadStream(file)
const response = await openai.files.create({
file: fileStream,
purpose: 'assistants',
})
debug.log(`Uploaded file ${JSON.stringify(response, null, 2)}`, 'ai')

return response.id
}

async function _deleteAllFiles(openai) {
const uploadedFiles = await listFiles(openai)
for (const file of uploadedFiles) {
await openai.files.del(file.id)
}
}

module.exports = {
uploadFiles,
readFiles: listFiles,
}
6 changes: 4 additions & 2 deletions packages/ethernaut-ai/src/tasks/interpret.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ require('../scopes/ai')
const buildingAssistantLIstener = () =>
spinner.progress('Building assistant...', 'ai')

_interpreter = new Interpreter(hre)
_interpreter = new Interpreter()
await _interpreter.initialize(hre)
_interpreter.on('status_update', statusUpdateListener)
_interpreter.on('building_assistant', buildingAssistantLIstener)
_interpreter.on('actions_required', processActions)

_explainer = new Explainer(hre)
_explainer = new Explainer()
await _explainer.initialize(hre)
_explainer.on('status_update', statusUpdateListener)
_explainer.on('building_assistant', buildingAssistantLIstener)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# A guide of how to associate colors with smells

- The color red smells like the sea
- The color blue smells like a strawberry
- The color green smells like lemon
- The color yellow smells like teen spirit
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
ai: {
interpreter: {
additionalInstructions: [''],
files: ['files/colors.md'],
},
explainer: {
additionalInstructions: [''],
Expand Down

0 comments on commit 13f61db

Please sign in to comment.