Skip to content

Commit

Permalink
fix: commonjs issue and function parameter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
shahrear33 committed Feb 4, 2025
1 parent e64f3e4 commit f7f9c8e
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 131 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ const client = new VlmRun({

// Process an image
async function processImage() {
const response = await client.image.generate(
["path/to/invoice.jpg"],
"vlm-1",
"document.invoice",
const response = await client.imagePredictions.generate(
{
images: ["path/to/invoice.jpg"],
model: "vlm-1",
domain: "document.invoice",
jsonSchema: {
type: "object",
properties: {
Expand All @@ -66,7 +66,7 @@ async function processImage() {
### Image Utilities

```typescript
import { encodeImage, isImage } from "vlmrun/utils/image";
import { encodeImage, isImage } from "vlmrun";

// Convert image to base64
const base64Image = encodeImage("path/to/image.jpg");
Expand All @@ -87,8 +87,9 @@ src/
│ ├── feedback.ts # Feedback operations
│ └── types.ts # Type definitions
├── utils/ # Utility functions
│ └── image.ts # Image processing utilities
└── index.ts # Main entry point
│ ├── image.ts # Image processing utilities
│ └── index.ts # Utility functions
└── index.ts # Main entry point
```

## 🛠️ Examples
Expand Down
20 changes: 10 additions & 10 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export default {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/tests/**/*.test.ts'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
transform: {
'^.+\\.tsx?$': ['ts-jest', {
useESM: true,
}]
},
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/tests/**/*.test.ts'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
transform: {
'^.+\\.tsx?$': ['ts-jest', {
useESM: true,
}]
},
extensionsToTreatAsEsm: [".ts"],
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "vlmrun",
"version": "0.1.8",
"version": "0.1.9",
"description": "The official TypeScript library for the VlmRun API",
"author": "VlmRun <[email protected]>",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"type": "module",
"type": "commonjs",
"repository": "github:vlm-run/vlmrun-node-sdk",
"license": "Apache-2.0",
"keywords": [],
Expand Down
21 changes: 6 additions & 15 deletions src/client/feedback.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Client, APIRequestor } from "./base_requestor";
import { FeedbackSubmitResponse } from "./types";
import { FeedbackSubmitResponse, FeedbackSubmitParams } from "./types";

export class Feedback {
private client: Client;
Expand All @@ -13,25 +13,16 @@ export class Feedback {
});
}

async submit(
id: string,
options: {
label?: Record<string, any>;
notes?: string;
flag?: boolean;
} = {}
): Promise<FeedbackSubmitResponse> {
const { label, notes, flag } = options;

async submit(params: FeedbackSubmitParams): Promise<FeedbackSubmitResponse> {
const [response] = await this.requestor.request<FeedbackSubmitResponse>(
"POST",
"feedback/submit",
undefined,
{
request_id: id,
response: label,
notes,
flag,
request_id: params.id,
response: params.label,
notes: params.notes,
flag: params.flag,
}
);
return response;
Expand Down
22 changes: 9 additions & 13 deletions src/client/files.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createHash } from 'crypto';
import { readFile } from 'fs/promises';
import { Client, APIRequestor } from './base_requestor';
import { FileResponse } from './types';
import { FileResponse, ListParams, FileUploadParams } from './types';

export class Files {
private client: Client;
Expand All @@ -12,11 +12,11 @@ export class Files {
this.requestor = new APIRequestor(client);
}

async list(skip: number = 0, limit: number = 10): Promise<FileResponse[]> {
async list(params: ListParams = {}): Promise<FileResponse[]> {
const [response] = await this.requestor.request<FileResponse[]>(
'GET',
'files',
{ skip, limit }
{ skip: params.skip, limit: params.limit }
);
return response;
}
Expand All @@ -40,29 +40,25 @@ export class Files {
}
}

async upload(
filePath: string,
purpose: string,
checkDuplicate: boolean = true
): Promise<FileResponse> {
if (checkDuplicate) {
const existingFile = await this.checkFileExists(filePath);
async upload(params: FileUploadParams): Promise<FileResponse> {
if (params.checkDuplicate !== false) {
const existingFile = await this.checkFileExists(params.filePath);
if (existingFile) {
return existingFile;
}
}

const fileBuffer = await readFile(filePath);
const fileBuffer = await readFile(params.filePath);
const formData = new FormData();
formData.append('file', new Blob([fileBuffer]));
formData.append('purpose', purpose);
formData.append('purpose', params.purpose);

const [response] = await this.requestor.request<FileResponse>(
'POST',
'files',
undefined,
undefined,
{ file: new Blob([fileBuffer]), purpose }
{ file: new Blob([fileBuffer]), purpose: params.purpose }
);
return response;
}
Expand Down
52 changes: 21 additions & 31 deletions src/client/predictions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Client, APIRequestor } from './base_requestor';
import { PredictionResponse, DetailLevel } from './types';
import {
PredictionResponse,
DetailLevel,
ListParams,
ImagePredictionParams,
FilePredictionParams
} from './types';
import { processImage } from '../utils/image';

export class Predictions {
Expand All @@ -11,44 +17,36 @@ export class Predictions {
this.requestor = new APIRequestor(client);
}

async list(skip: number = 0, limit: number = 10): Promise<PredictionResponse[]> {
async list(params: ListParams = {}): Promise<PredictionResponse[]> {
const [response] = await this.requestor.request<PredictionResponse[]>(
'GET',
'predictions',
{ skip, limit }
{ skip: params.skip, limit: params.limit }
);
return response;
}

async get(id: string): Promise<PredictionResponse> {
async get(params: { id: string }): Promise<PredictionResponse> {
const [response] = await this.requestor.request<PredictionResponse>(
'GET',
`predictions/${id}`
`predictions/${params.id}`
);
return response;
}
}

export class ImagePredictions extends Predictions {
async generate(
images: string[],
model: string,
domain: string,
options: {
jsonSchema?: Record<string, any>;
detail?: DetailLevel;
batch?: boolean;
metadata?: Record<string, any>;
callbackUrl?: string;
} = {}
): Promise<PredictionResponse> {
async generate(params: ImagePredictionParams): Promise<PredictionResponse> {
const {
images,
model,
domain,
jsonSchema,
detail = 'auto',
batch = false,
metadata = {},
callbackUrl,
} = options;
} = params;

const encodedImages = images.map(image => processImage(image));

Expand Down Expand Up @@ -79,25 +77,17 @@ export class FilePredictions extends Predictions {
this.route = route;
}

async generate(
fileIds: string[],
model: string,
domain: string,
options: {
jsonSchema?: Record<string, any>;
detail?: DetailLevel;
batch?: boolean;
metadata?: Record<string, any>;
callbackUrl?: string;
} = {}
): Promise<PredictionResponse> {
async generate(params: FilePredictionParams): Promise<PredictionResponse> {
const {
fileIds,
model,
domain,
jsonSchema,
detail = 'auto',
batch = false,
metadata = {},
callbackUrl,
} = options;
} = params;

const [response] = await this.requestor.request<PredictionResponse>(
'POST',
Expand Down
49 changes: 39 additions & 10 deletions src/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
export type JobStatus = 'pending' | 'running' | 'completed' | 'failed';
export type JobStatus = string;

export type FilePurpose =
| 'fine-tune'
| 'assistants'
| 'assistants_output'
| 'batch'
| 'batch_output'
| 'vision'
| 'datasets';
export type FilePurpose = string;

export type DetailLevel = 'auto' | 'lo' | 'hi';
export type DetailLevel = string;

export interface FileResponse {
id: string;
Expand Down Expand Up @@ -48,6 +41,42 @@ export interface FeedbackSubmitResponse {
response: any;
}

export interface ListParams {
skip?: number;
limit?: number;
}

export interface FileUploadParams {
filePath: string;
purpose: string;
checkDuplicate?: boolean;
}

export interface FeedbackSubmitParams {
id: string;
label?: Record<string, any>;
notes?: string;
flag?: boolean;
}

export interface PredictionGenerateParams {
model: string;
domain: string;
jsonSchema?: Record<string, any>;
detail?: DetailLevel;
batch?: boolean;
metadata?: Record<string, any>;
callbackUrl?: string;
}

export interface ImagePredictionParams extends PredictionGenerateParams {
images: string[];
}

export interface FilePredictionParams extends PredictionGenerateParams {
fileIds: string[];
}

export class APIError extends Error {
constructor(
message: string,
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export * from "./client/files";
export * from "./client/predictions";
export * from "./client/feedback";

export * from "./utils";

export interface VlmRunConfig {
apiKey: string;
baseURL?: string;
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './image';
Loading

0 comments on commit f7f9c8e

Please sign in to comment.