-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: RAG functionality #24
Merged
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
4713d02
feat: rag-engine impl. - with examples, no tests yet
amirai21 ceaa9e9
feat: rag-engine impl. - with examples, no tests yet
amirai21 657fa5e
feat: makeFormDataRequest - change type and disable lint
amirai21 d68e935
feat: remove log
amirai21 329eb3e
feat: add upload file object override
amirai21 6131ee8
feat: add upload file object override
amirai21 54a96b1
feat: FilePathOrFileObject
amirai21 48f2a8d
feat: support node path and file object\ and browser file object
amirai21 c055ca2
feat: support node path and file object\ and browser file object
amirai21 02b40df
feat: functioning browser upload
amirai21 b71e598
feat: reorganized
amirai21 a93d32c
feat: reorganized
amirai21 eac5e6e
feat: wip
amirai21 a628702
feat: wip
amirai21 6ff4df7
feat: basic unit tests for rag engine
amirai21 ab469be
feat: wip
amirai21 f438185
feat: wip
amirai21 382e69f
feat: wip
amirai21 7ca7640
feat: add file path check before opening + rag examples improved
amirai21 9705d0b
feat: fix tests
amirai21 c88bf02
feat: reorg imports
amirai21 e3b0810
feat: convert upload to non async
amirai21 29997e0
feat: node fetch casting
amirai21 90eb449
feat: disable examples for non node env
amirai21 8914f61
feat: disable examples for non node env
amirai21 585e278
feat: log
amirai21 18b46a0
feat: swap condition
amirai21 79e616b
test: Trying integration test
asafgardin 5888d67
fix: Added log
asafgardin e251c1b
fix: Run sync
asafgardin 9cccbc9
fix: Added logs
asafgardin 9de0844
fix: Added logs of env
asafgardin 4e7f794
fix: Added logs of env
asafgardin 8ff04de
fix: Added more logs
asafgardin e2129a6
fix: Checked env
asafgardin ac11d5c
ci: Added form-data to bundle
asafgardin 49d82c6
ci: Added form-data to bundle
asafgardin 1b5b314
fix: Node file checks
asafgardin e3b31ac
fix: Node file checks
asafgardin 670fc69
fix: check type
asafgardin 09be7a4
fix: Moved to factory
asafgardin d24dc65
fix: ignore ts
asafgardin af96613
fix: Import of runtime
asafgardin bf6ad2a
refactor: Renamed methods
asafgardin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.txt |
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 @@ | ||
The meerkat (Suricata suricatta) or suricate is a small mongoose found in southern Africa. It is characterised by a broad head, large eyes, a pointed snout, long legs, a thin tapering tail, and a brindled coat pattern. The head-and-body length is around 24–35 cm (9.4–13.8 in), and the weight is typically between 0.62 and 0.97 kg (1.4 and 2.1 lb). The coat is light grey to yellowish-brown with alternate, poorly-defined light and dark bands on the back. Meerkats have foreclaws adapted for digging and have the ability to thermoregulate to survive in their harsh, dry habitat. Three subspecies are recognised. |
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,139 @@ | ||
import { AI21, FileResponse, UploadFileResponse } from 'ai21'; | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
|
||
function sleep(ms) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
|
||
async function waitForFileProcessing( | ||
client: AI21, | ||
fileId: string, | ||
timeout: number = 30000, | ||
interval: number = 1000, | ||
) { | ||
const startTime = Date.now(); | ||
|
||
while (Date.now() - startTime < timeout) { | ||
const file: FileResponse = await client.files.get(fileId); | ||
if (file.status !== 'PROCESSING') { | ||
return file; | ||
} | ||
await sleep(interval); | ||
} | ||
|
||
throw new Error(`File processing timed out after ${timeout}ms`); | ||
} | ||
|
||
async function uploadGetUpdateDelete(fileInput, path) { | ||
const client = new AI21({ apiKey: process.env.AI21_API_KEY }); | ||
try { | ||
console.log(`Starting upload for file:`, typeof fileInput); | ||
const uploadFileResponse: UploadFileResponse = await client.files.create({ | ||
file: fileInput, | ||
path: path, | ||
}); | ||
console.log(`✓ Upload completed. File ID: ${uploadFileResponse.fileId}`); | ||
|
||
console.log('Waiting for file processing...'); | ||
let file: FileResponse = await waitForFileProcessing(client, uploadFileResponse.fileId); | ||
console.log(`✓ File processing completed with status: ${file.status}`); | ||
|
||
if (file.status === 'PROCESSED') { | ||
console.log('Starting file update...'); | ||
await client.files.update({ | ||
fileId: uploadFileResponse.fileId, | ||
labels: ['test99'], | ||
publicUrl: 'https://www.miri.com', | ||
}); | ||
file = await client.files.get(uploadFileResponse.fileId); | ||
console.log('✓ File update completed'); | ||
} else { | ||
console.log(`⚠ File processing failed with status ${file.status}`); | ||
return; // Exit early if processing failed | ||
} | ||
|
||
console.log('Starting file deletion...'); | ||
await client.files.delete(uploadFileResponse.fileId); | ||
console.log('✓ File deletion completed'); | ||
|
||
// Add buffer time between operations | ||
await sleep(2000); | ||
} catch (error) { | ||
console.error('❌ Error in uploadGetUpdateDelete:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
async function listFiles() { | ||
const client = new AI21({ apiKey: process.env.AI21_API_KEY }); | ||
const files = await client.files.list({ limit: 4 }); | ||
console.log(`Listed files: ${files}`); | ||
} | ||
|
||
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'; | ||
|
||
const createNodeFile = (content: Buffer, filename: string, type: string) => { | ||
if (process.platform === 'linux') { | ||
console.log('Running on Linux (GitHub Actions)'); | ||
// Special handling for Linux (GitHub Actions) | ||
return { | ||
name: filename, | ||
type: type, | ||
buffer: content, | ||
[Symbol.toStringTag]: 'File', | ||
}; | ||
} else { | ||
console.log('Running on other platforms'); | ||
// Regular handling for other platforms | ||
return new File([content], filename, { type }); | ||
} | ||
}; | ||
|
||
if (isBrowser) { | ||
console.log('Cannot run upload examples in Browser environment'); | ||
} else { | ||
/* Log environment details */ | ||
console.log('=== Environment Information ==='); | ||
console.log(`Node.js Version: ${process.version}`); | ||
console.log(`Platform: ${process.platform}`); | ||
console.log(`Architecture: ${process.arch}`); | ||
console.log(`Process ID: ${process.pid}`); | ||
console.log(`Current Working Directory: ${process.cwd()}`); | ||
console.log('===========================\n'); | ||
|
||
/* Run all operations sequentially */ | ||
(async () => { | ||
try { | ||
console.log('=== Starting first operation ==='); | ||
// First operation - upload file from path | ||
const filePath = path.resolve(process.cwd(), 'examples/studio/conversational-rag/files', 'meerkat.txt'); | ||
if (!fs.existsSync(filePath)) { | ||
throw new Error(`File not found: ${filePath}`); | ||
} else { | ||
console.log(`File found: ${filePath}`); | ||
} | ||
|
||
await uploadGetUpdateDelete(filePath, Date.now().toString()); | ||
console.log('=== First operation completed ===\n'); | ||
await sleep(2000); | ||
|
||
console.log('=== Starting second operation ==='); | ||
// Second operation - upload file from File instance | ||
const fileContent = Buffer.from( | ||
'Opossums are members of the marsupial order Didelphimorphia endemic to the Americas.', | ||
); | ||
const dummyFile = createNodeFile(fileContent, 'example.txt', 'text/plain'); | ||
await uploadGetUpdateDelete(dummyFile, Date.now().toString()); | ||
console.log('=== Second operation completed ===\n'); | ||
await sleep(2000); | ||
|
||
console.log('=== Starting file listing ==='); | ||
await listFiles(); | ||
console.log('=== File listing completed ==='); | ||
} catch (error) { | ||
console.error('❌ Main execution error:', error); | ||
process.exit(1); // Exit with error code if something fails | ||
} | ||
})(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { BrowserFilesHandler } from './files/BrowserFilesHandler'; | ||
import { BrowserFetch, Fetch, NodeFetch } from './fetch'; | ||
import { NodeFilesHandler } from './files/NodeFilesHandler'; | ||
import { BaseFilesHandler } from './files/BaseFilesHandler'; | ||
import { isBrowser, isWebWorker } from './runtime'; | ||
|
||
export function createFetchInstance(): Fetch { | ||
if (isBrowser || isWebWorker) { | ||
return new BrowserFetch(); | ||
} | ||
|
||
return new NodeFetch(); | ||
} | ||
|
||
export function createFilesHandlerInstance(): BaseFilesHandler { | ||
if (isBrowser || isWebWorker) { | ||
return new BrowserFilesHandler(); | ||
} | ||
|
||
return new NodeFilesHandler(); | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Josephasafg I don't like how this ended with, perhaps you'll have an idea for something better :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@amirai21 I think its pretty good and for the sake of moving forward, lets make a custom type called
type NodeHTTPBody = import('form-data') | string;
and one for the browser as well and call it a day for now. Maybe we'll improve it in the future, but for now I think its good enough. wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds great, exporting them to custom type.