-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d01b73
commit 13f61db
Showing
8 changed files
with
91 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
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
63 changes: 63 additions & 0 deletions
63
packages/ethernaut-ai/src/internal/assistants/utils/files.js
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
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, | ||
} |
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
6 changes: 6 additions & 0 deletions
6
packages/ethernaut-ai/test/fixture-projects/basic-project/files/colors.md
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
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 |
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