Skip to content

Commit

Permalink
Merge pull request #56 from vlm-run/sh/add_zod
Browse files Browse the repository at this point in the history
fix: tests and upgrade version to 0.2.4
  • Loading branch information
shahrear33 authored Feb 17, 2025
2 parents 4aedce1 + 647fd4b commit a27ad26
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 129 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vlmrun",
"version": "0.2.3",
"version": "0.2.5",
"description": "The official TypeScript library for the VlmRun API",
"author": "VlmRun <[email protected]>",
"main": "dist/index.js",
Expand Down
2 changes: 2 additions & 0 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,5 @@ export interface VlmRunError extends Error {
code?: string;
cause?: Error;
}


Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { config } from "dotenv";

import { VlmRun } from "../../../src/index";
import { GenerationConfig } from "../../../src/index";
import { z } from "zod";

jest.setTimeout(60000);

describe("Integration: Predictions", () => {
describe("Integration: File Predictions", () => {
let client: VlmRun;

beforeAll(() => {
Expand All @@ -18,130 +17,6 @@ describe("Integration: Predictions", () => {
});
});

describe("ImagePredictions", () => {
it("should generate image predictions with default options", async () => {
const testImagePath = "tests/integration/assets/invoice.jpg";

const result = await client.image.generate({
images: [testImagePath],
model: "vlm-1",
domain: "document.invoice",
});

expect(result).toHaveProperty("id");
expect(result.status).toBe("completed");
expect(result.response.invoice_id).toContain("9999999");
expect(result.response.invoice_issue_date).toBe("2023-11-11");

expect(result.response.customer).toBe("Fred Davis");
expect(result.response.customer_email).toBe("[email protected]");
expect(result.response.customer_phone).toContain("4567");
expect(result.response.customer_billing_address.street).toContain(
"1335 Martin Luther King Jr Ave"
);
expect(result.response.customer_billing_address.city).toBe("Dunedin");
expect(result.response.customer_billing_address.state).toBe("FL");
expect(result.response.customer_billing_address.postal_code).toContain(
"34698"
);
expect(result.response.customer_shipping_address.street).toContain(
"249 Windward Passage"
);
expect(result.response.customer_shipping_address.city).toContain(
"Clearwater"
);
expect(result.response.customer_shipping_address.state).toContain("FL");
expect(result.response.customer_shipping_address.postal_code).toContain(
"33767"
);
expect(result.response.items.length).toBe(3);

expect(result.response.subtotal).toBe(400);
expect(result.response.total).toBe(400);
});

it("should generate image predictions from path with zod schema", async () => {
const testImagePath = "tests/integration/assets/invoice.jpg";

const schema = z.object({
invoice_id: z.string(),
invoice_issue_date: z.string(),
customer: z.string(),
customer_email: z.string(),
customer_phone: z.string(),
total: z.number(),
});

const result = await client.image.generate({
images: [testImagePath],
model: "vlm-1",
domain: "document.invoice",
config: {
responseModel: schema,
},
});

const response = result.response as z.infer<typeof schema>;
expect(response.invoice_id).toBe("9999999");
expect(response.total).toBe(400);
});

it("should generate image predictions from url with zod schema", async () => {
const imageUrl =
"https://storage.googleapis.com/vlm-data-public-prod/hub/examples/document.invoice/invoice_1.jpg";

const schema = z.object({
invoice_id: z.string(),
total: z.number(),
});

const result = await client.image.generate({
images: [imageUrl],
model: "vlm-1",
domain: "document.invoice",
config: {
responseModel: schema,
},
});

const response = result.response as z.infer<typeof schema>;
expect(response.invoice_id).toBe("9999999");
expect(response.total).toBe(400);
});

it("should generate image predictions with custom options", async () => {
const testImagePath = "tests/integration/assets/invoice.jpg";

const result = await client.image.generate({
images: [testImagePath],
model: "vlm-1",
domain: "document.invoice",
config: {
jsonSchema: {
type: "object",
properties: {
invoice_number: { type: "string" },
total_amount: { type: "number" },
},
},
},
});

expect(result).toHaveProperty("id");
expect(result.status).toBe("completed");
expect(result.response.invoice_number).toContain("9999999");
expect(result.response.total_amount).toBe(400);

expect(result.response).not.toHaveProperty("invoice_issue_date");
expect(result.response).not.toHaveProperty("customer");
expect(result.response).not.toHaveProperty("customer_email");
expect(result.response).not.toHaveProperty("customer_phone");
expect(result.response).not.toHaveProperty("customer_billing_address");
expect(result.response).not.toHaveProperty("customer_shipping_address");
expect(result.response).not.toHaveProperty("items");
});
});

describe("DocumentPredictions", () => {
const testFilePath = "tests/integration/assets/google_invoice.pdf";

Expand Down
143 changes: 143 additions & 0 deletions tests/integration/client/image-predictions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { config } from "dotenv";

import { VlmRun } from "../../../src/index";
import { z } from "zod";

jest.setTimeout(60000);

describe("Integration: Image Predictions", () => {
let client: VlmRun;

beforeAll(() => {
config({ path: ".env.test" });

client = new VlmRun({
apiKey: process.env.TEST_API_KEY as string,
baseURL: process.env.TEST_BASE_URL as string,
});
});

describe("ImagePredictions", () => {
it("should generate image predictions with default options", async () => {
const testImagePath = "tests/integration/assets/invoice.jpg";

const result = await client.image.generate({
images: [testImagePath],
model: "vlm-1",
domain: "document.invoice",
});

expect(result).toHaveProperty("id");
expect(result.status).toBe("completed");
expect(result.response.invoice_id).toContain("9999999");
expect(result.response.invoice_issue_date).toBe("2023-11-11");

expect(result.response.customer).toBe("Fred Davis");
expect(result.response.customer_email).toBe("[email protected]");
expect(result.response.customer_phone).toContain("4567");
expect(result.response.customer_billing_address.street).toContain(
"1335 Martin Luther King Jr Ave"
);
expect(result.response.customer_billing_address.city).toBe("Dunedin");
expect(result.response.customer_billing_address.state).toBe("FL");
expect(result.response.customer_billing_address.postal_code).toContain(
"34698"
);
expect(result.response.customer_shipping_address.street).toContain(
"249 Windward Passage"
);
expect(result.response.customer_shipping_address.city).toContain(
"Clearwater"
);
expect(result.response.customer_shipping_address.state).toContain("FL");
expect(result.response.customer_shipping_address.postal_code).toContain(
"33767"
);
expect(result.response.items.length).toBe(3);

expect(result.response.subtotal).toBe(400);
expect(result.response.total).toBe(400);
});

it("should generate image predictions from path with zod schema", async () => {
const testImagePath = "tests/integration/assets/invoice.jpg";

const schema = z.object({
invoice_id: z.string(),
invoice_issue_date: z.string(),
customer: z.string(),
customer_email: z.string(),
customer_phone: z.string(),
total: z.number(),
});

const result = await client.image.generate({
images: [testImagePath],
model: "vlm-1",
domain: "document.invoice",
config: {
responseModel: schema,
},
});

const response = result.response as z.infer<typeof schema>;
expect(response.invoice_id).toBe("9999999");
expect(response.total).toBe(400);
});

it("should generate image predictions from url with zod schema", async () => {
const imageUrl =
"https://storage.googleapis.com/vlm-data-public-prod/hub/examples/document.invoice/invoice_1.jpg";

const schema = z.object({
invoice_id: z.string(),
total: z.number(),
});

const result = await client.image.generate({
images: [imageUrl],
model: "vlm-1",
domain: "document.invoice",
config: {
responseModel: schema,
},
});

const response = result.response as z.infer<typeof schema>;
expect(response.invoice_id).toBe("9999999");
expect(response.total).toBe(400);
});

it("should generate image predictions with custom options", async () => {
const testImagePath = "tests/integration/assets/invoice.jpg";

const result = await client.image.generate({
images: [testImagePath],
model: "vlm-1",
domain: "document.invoice",
config: {
jsonSchema: {
type: "object",
properties: {
invoice_number: { type: "string" },
total_amount: { type: "number" },
},
},
},
});

expect(result).toHaveProperty("id");
expect(result.status).toBe("completed");
expect(result.response.invoice_number).toContain("9999999");
expect(result.response.total_amount).toBe(400);

expect(result.response).not.toHaveProperty("invoice_issue_date");
expect(result.response).not.toHaveProperty("customer");
expect(result.response).not.toHaveProperty("customer_email");
expect(result.response).not.toHaveProperty("customer_phone");
expect(result.response).not.toHaveProperty("customer_billing_address");
expect(result.response).not.toHaveProperty("customer_shipping_address");
expect(result.response).not.toHaveProperty("items");
});
});
});
4 changes: 2 additions & 2 deletions tests/unit/client/predictions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("Predictions", () => {
"image/generate",
undefined,
{
image: "base64-encoded-image",
images: ["base64-encoded-image"],
model: "model1",
domain: "domain1",
batch: false,
Expand Down Expand Up @@ -97,7 +97,7 @@ describe("Predictions", () => {
"image/generate",
undefined,
{
image: "base64-encoded-image",
images: ["base64-encoded-image"],
model: "model1",
domain: "domain1",
batch: true,
Expand Down

0 comments on commit a27ad26

Please sign in to comment.