Skip to content

Commit f438185

Browse files
amirai21asafgardin
authored andcommitted
feat: wip
1 parent ab469be commit f438185

File tree

8 files changed

+43
-48
lines changed

8 files changed

+43
-48
lines changed

examples/studio/conversational-rag/rag-engine.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ function sleep(ms) {
55
return new Promise((resolve) => setTimeout(resolve, ms));
66
}
77

8-
async function uploadGetUpdateDelete(fileInput, label) {
8+
async function uploadGetUpdateDelete(fileInput, path) {
99
const client = new AI21({ apiKey: process.env.AI21_API_KEY });
1010
try {
11-
const uploadFileResponse: UploadFileResponse = await client.ragEngine.create(fileInput, {
12-
path: label,
13-
});
11+
const uploadFileResponse: UploadFileResponse = await client.ragEngine.create(
12+
{
13+
file: fileInput,
14+
path: path,
15+
});
1416
console.log(uploadFileResponse);
1517
let file: FileResponse = await client.ragEngine.get(uploadFileResponse.fileId);
1618
console.log(file);
1719
await sleep(1000); // Give it a sec to start process before updating
1820
console.log('Now updating the file labels and publicUrl...');
19-
await client.ragEngine.update(uploadFileResponse.fileId, {
21+
await client.ragEngine.update({
22+
fileId: uploadFileResponse.fileId,
2023
labels: ['test99'],
2124
publicUrl: 'https://www.miri.com',
2225
});

src/APIClient.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,16 @@ const validatePositiveInteger = (name: string, n: unknown): number => {
2626
return n;
2727
};
2828

29-
30-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
31-
const appendBodyToFormData = (formData: UnifiedFormData, body: Record<string, any>): void => {
32-
for (const [key, value] of Object.entries(body)) {
33-
if (Array.isArray(value)) {
34-
value.forEach((item) => formData.append(key, item));
35-
} else {
36-
formData.append(key, value);
37-
}
29+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30+
const appendBodyToFormData = (formData: UnifiedFormData, body: Record<string, any>): void => {
31+
for (const [key, value] of Object.entries(body)) {
32+
if (Array.isArray(value)) {
33+
value.forEach((item) => formData.append(key, item));
34+
} else {
35+
formData.append(key, value);
3836
}
39-
};
40-
37+
}
38+
};
4139

4240
export abstract class APIClient {
4341
protected baseURL: string;
@@ -136,7 +134,11 @@ export abstract class APIClient {
136134
return url;
137135
}
138136

139-
private prepareAndExecuteRequest<Req, Rsp>(method: HTTPMethod, path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
137+
private prepareAndExecuteRequest<Req, Rsp>(
138+
method: HTTPMethod,
139+
path: string,
140+
opts?: RequestOptions<Req>,
141+
): Promise<Rsp> {
140142
const options = {
141143
method,
142144
path,
@@ -148,9 +150,7 @@ export abstract class APIClient {
148150
options.headers = { ...options.headers, 'Content-Type': 'application/json' };
149151
}
150152

151-
return this.performRequest(options).then(
152-
(response) => this.fetch.handleResponse<Rsp>(response) as Rsp,
153-
);
153+
return this.performRequest(options).then((response) => this.fetch.handleResponse<Rsp>(response) as Rsp);
154154
}
155155

156156
private async performRequest(options: FinalRequestOptions): Promise<APIResponseProps> {

src/resources/rag/ragEngine.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,20 @@ import {
55
UploadFileRequest,
66
ListFilesFilters,
77
UpdateFileRequest,
8-
FilePathOrFileObject,
98
} from '../../types/rag';
109
import { FileResponse } from 'types/rag/FileResponse';
1110

1211
const RAG_ENGINE_PATH = '/library/files';
1312

1413
export class RAGEngine extends APIResource {
15-
async create(
16-
file: string,
17-
body: UploadFileRequest,
18-
options?: Models.RequestOptions,
19-
): Promise<UploadFileResponse>;
20-
21-
async create(
22-
file: File,
23-
body: UploadFileRequest,
24-
options?: Models.RequestOptions,
25-
): Promise<UploadFileResponse>;
2614

2715
async create(
28-
file: FilePathOrFileObject,
2916
body: UploadFileRequest,
3017
options?: Models.RequestOptions,
3118
): Promise<UploadFileResponse> {
19+
const {file, ...bodyWithoutFile} = body
3220
return this.client.upload<Models.UnifiedFormData, UploadFileResponse>(RAG_ENGINE_PATH, file, {
33-
body: body,
21+
body: bodyWithoutFile,
3422
...options,
3523
} as Models.RequestOptions<Models.UnifiedFormData>) as Promise<UploadFileResponse>;
3624
}
@@ -49,15 +37,15 @@ export class RAGEngine extends APIResource {
4937
) as Promise<null>;
5038
}
5139

52-
list(body: ListFilesFilters | null, options?: Models.RequestOptions): Promise<FileResponse[]> {
40+
list(body?: ListFilesFilters, options?: Models.RequestOptions): Promise<FileResponse[]> {
5341
return this.client.get<ListFilesFilters | null, FileResponse[]>(RAG_ENGINE_PATH, {
5442
query: body,
5543
...options,
5644
} as Models.RequestOptions<ListFilesFilters | null>) as Promise<FileResponse[]>;
5745
}
5846

59-
update(fileId: string, body: UpdateFileRequest, options?: Models.RequestOptions): Promise<null> {
60-
return this.client.put<UpdateFileRequest, null>(`${RAG_ENGINE_PATH}/${fileId}`, {
47+
update(body: UpdateFileRequest, options?: Models.RequestOptions): Promise<null> {
48+
return this.client.put<UpdateFileRequest, null>(`${RAG_ENGINE_PATH}/${body.fileId}`, {
6149
body,
6250
...options,
6351
} as Models.RequestOptions<UpdateFileRequest>) as Promise<null>;

src/types/API.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,3 @@ export type CrossPlatformReadableStream = ReadableStream<Uint8Array> | import('s
3333
export type UnifiedFormData = FormData | import('form-data');
3434

3535
export type FormDataRequest = { formData: UnifiedFormData; headers: Headers };
36-

src/types/rag/UpdateFileRequest.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface UpdateFileRequest {
2+
fileId: string;
3+
labels?: string[] | null;
4+
publicUrl?: string | null;
5+
}

src/types/rag/UploadFileRequest.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export interface UpdateFileRequest {
2-
labels?: string[] | null;
3-
publicUrl?: string | null;
4-
}
1+
import { FilePathOrFileObject } from "./FilePathOrFileObject";
52

6-
export interface UploadFileRequest extends UpdateFileRequest {
3+
export interface UploadFileRequest {
4+
file: FilePathOrFileObject
75
path?: string | null;
6+
labels?: string[] | null;
7+
publicUrl?: string | null;
88
}

src/types/rag/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ export { type UploadFileResponse } from './UploadFileResponse';
1414

1515
export { type ListFilesFilters } from './ListFilesFilters';
1616

17-
export { type UpdateFileRequest } from './UploadFileRequest';
17+
export { type UpdateFileRequest } from './UpdateFileRequest';
1818

1919
export { type FilePathOrFileObject } from './FilePathOrFileObject';

tests/unittests/resources/rag-engine.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ describe('RAGEngine', () => {
3131

3232
it('should upload a file and return the fileId', async () => {
3333
const fileInput = 'path/to/file.txt';
34-
const body = { path: 'label' };
34+
const body = { file: fileInput, path: 'label' };
3535
const expectedResponse = { fileId: '12345' };
3636

3737
mockClient.upload.mockResolvedValue(expectedResponse);
3838

39-
const response = await ragEngine.create(fileInput, body, options);
39+
const response = await ragEngine.create(body);
4040

4141
expect(mockClient.upload).toHaveBeenCalledWith(
4242
'/library/files',
@@ -77,11 +77,11 @@ describe('RAGEngine', () => {
7777

7878
it('should update a file by ID and return null', async () => {
7979
const fileId = '12345';
80-
const body = { labels: ['test'], publicUrl: 'https://example.com' };
80+
const body = { fileId, labels: ['test'], publicUrl: 'https://example.com' };
8181

8282
mockClient.put.mockResolvedValue(null);
8383

84-
const response = await ragEngine.update(fileId, body, options);
84+
const response = await ragEngine.update(body);
8585

8686
expect(mockClient.put).toHaveBeenCalledWith(
8787
`/library/files/${fileId}`,

0 commit comments

Comments
 (0)