Skip to content

Commit

Permalink
⬆️ upgrade vite, vitest, @vitest/browser (#365)
Browse files Browse the repository at this point in the history
Fixes #267 

inference package used to substitute "process.env" with `process.env`
contents in `vitest.config.ts` (using `define`), this is broken in more
recent version of vitest:
vitest-dev/vitest#3900.

So the solution is to use
[`import.meta.env`](https://vitejs.dev/guide/env-and-mode.html#env-variables).
But since it passes through only vars with "VITE_" prefix, `envPrefix`
is needed include vars starting with `HF_` and `VCR_` .

Additionally, moved `expect.closeTo()` to a separate file, it can be
removed with update to vitest 1.0.
  • Loading branch information
vvmnnnkv authored Nov 28, 2023
1 parent 30a3f6b commit d30133b
Show file tree
Hide file tree
Showing 28 changed files with 815 additions and 814 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"@vitest/browser": "^0.29.7",
"@vitest/browser": "^0.34.6",
"eslint": "^8.35.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^4.2.1",
Expand All @@ -20,9 +20,9 @@
"semver": "^7.5.0",
"ts-node": "^10.9.1",
"tsup": "^6.7.0",
"typescript": "^5.0.2",
"vite": "4.1.4",
"vitest": "^0.29.4",
"typescript": "^5.0.4",
"vite": "^5.0.2",
"vitest": "^0.34.6",
"webdriverio": "^8.6.7"
}
}
5 changes: 2 additions & 3 deletions packages/agents/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"prepublishOnly": "pnpm run build",
"build": "tsup",
"test": "vitest run",
"test:browser": "vitest run --browser.name=chrome --browser.headless --config ./vitest-browser.config.ts",
"test:browser": "vitest run --browser.name=chrome --browser.headless --config vitest-browser.config.mts",
"check": "tsc"
},
"files": [
Expand All @@ -54,8 +54,7 @@
"license": "MIT",
"devDependencies": {
"@types/node": "^18.13.0",
"type-fest": "^3.9.0",
"typescript": "^5.0.4"
"type-fest": "^3.9.0"
},
"dependencies": {
"@huggingface/inference": "^2.6.1"
Expand Down
13 changes: 4 additions & 9 deletions packages/agents/pnpm-lock.yaml

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

22 changes: 13 additions & 9 deletions packages/agents/test/HfAgent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { describe, expect, it } from "vitest";
import { HfAgent, defaultTools, LLMFromHub, LLMFromEndpoint } from "../src";
import type { Data } from "../src/types";
import type { HfInference } from "@huggingface/inference";

if (!process.env.HF_ACCESS_TOKEN) {
const env = import.meta.env;
if (!env.HF_ACCESS_TOKEN) {
console.warn("Set HF_ACCESS_TOKEN in the env to run the tests for better rate limits");
}

describe("HfAgent", () => {
it("You can create an agent from the hub", async () => {
const llm = LLMFromHub(process.env.HF_ACCESS_TOKEN, "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5");
const agent = new HfAgent(process.env.HF_ACCESS_TOKEN, llm);
const llm = LLMFromHub(env.HF_ACCESS_TOKEN, "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5");
const agent = new HfAgent(env.HF_ACCESS_TOKEN, llm);
expect(agent).toBeDefined();
});

it("You can create an agent from an endpoint", async () => {
const llm = LLMFromEndpoint(process.env.HF_ACCESS_TOKEN ?? "", "endpoint");
const agent = new HfAgent(process.env.HF_ACCESS_TOKEN, llm);
const llm = LLMFromEndpoint(env.HF_ACCESS_TOKEN ?? "", "endpoint");
const agent = new HfAgent(env.HF_ACCESS_TOKEN, llm);
expect(agent).toBeDefined();
});

Expand All @@ -29,7 +32,8 @@ describe("HfAgent", () => {
tools: ["uppercase"],
},
],
call: async (input) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
call: async (input: Promise<Data>, inference: HfInference): Promise<Data> => {
const data = await input;
if (typeof data !== "string") {
throw new Error("Input must be a string");
Expand All @@ -38,7 +42,7 @@ describe("HfAgent", () => {
},
};

const agent = new HfAgent(process.env.HF_ACCESS_TOKEN, undefined, [uppercaseTool, ...defaultTools]);
const agent = new HfAgent(env.HF_ACCESS_TOKEN, undefined, [uppercaseTool, ...defaultTools]);
const code = `
async function generate() {
const output = uppercase("hello friends");
Expand All @@ -57,7 +61,7 @@ async function generate() {
message(output);
}`;

const agent = new HfAgent(process.env.HF_ACCESS_TOKEN);
const agent = new HfAgent(env.HF_ACCESS_TOKEN);

await agent.evaluateCode(code).then((output) => {
expect(output.length).toBeGreaterThan(0);
Expand All @@ -71,7 +75,7 @@ async function generate() {
toolThatDoesntExist(aaa);
}`;

const hf = new HfAgent(process.env.HF_ACCESS_TOKEN);
const hf = new HfAgent(env.HF_ACCESS_TOKEN);

await hf.evaluateCode(code).then((output) => {
expect(output.length).toBeGreaterThan(0);
Expand Down
1 change: 1 addition & 0 deletions packages/agents/test/vitest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
4 changes: 2 additions & 2 deletions packages/agents/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"lib": ["ES2022", "DOM"],
"module": "CommonJS",
"module": "ESNext",
"moduleResolution": "node",
"target": "ES2022",
"forceConsistentCasingInFileNames": true,
Expand All @@ -14,6 +14,6 @@
"outDir": "./dist",
"declaration": true
},
"include": ["src", "index.ts", "../shared/src"],
"include": ["src", "test", "index.ts", "../shared/src"],
"exclude": ["dist"]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { configDefaults, defineConfig } from "vitest/config";

export default defineConfig({
envPrefix: ["HF_"],
test: {
exclude: [...configDefaults.exclude],
},
Expand Down
3 changes: 1 addition & 2 deletions packages/doc-internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"devDependencies": {
"@types/node": "^18.14.5",
"typedoc": "^0.23.26",
"typedoc-plugin-markdown": "^3.14.0",
"typescript": "^4.9.5"
"typedoc-plugin-markdown": "^3.14.0"
},
"dependencies": {
"glob": "^9.2.1",
Expand Down
3 changes: 0 additions & 3 deletions packages/doc-internal/pnpm-lock.yaml

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

5 changes: 2 additions & 3 deletions packages/hub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"prepublishOnly": "pnpm run build",
"build": "tsup",
"test": "vitest run",
"test:browser": "vitest run --browser.name=chrome --browser.headless --config ./vitest-browser.config.ts",
"test:browser": "vitest run --browser.name=chrome --browser.headless --config vitest-browser.config.mts",
"check": "tsc"
},
"files": [
Expand All @@ -56,8 +56,7 @@
"license": "MIT",
"devDependencies": {
"@types/node": "^18.13.0",
"type-fest": "^3.9.0",
"typescript": "^5.0.4"
"type-fest": "^3.9.0"
},
"dependencies": {
"hash-wasm": "^4.9.0"
Expand Down
13 changes: 4 additions & 9 deletions packages/hub/pnpm-lock.yaml

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

File renamed without changes.
7 changes: 3 additions & 4 deletions packages/inference/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@
"format:check": "prettier --check .",
"prepare": "pnpm run build",
"prepublishOnly": "pnpm run build",
"test": "vitest run --config vitest.config.ts",
"test:browser": "vitest run --browser.name=chrome --browser.headless --config vitest.config.ts",
"test": "vitest run --config vitest.config.mts",
"test:browser": "vitest run --browser.name=chrome --browser.headless --config vitest.config.mts",
"check": "tsc"
},
"devDependencies": {
"@types/node": "18.13.0",
"typescript": "^5.0.4"
"@types/node": "18.13.0"
},
"resolutions": {}
}
13 changes: 4 additions & 9 deletions packages/inference/pnpm-lock.yaml

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

31 changes: 3 additions & 28 deletions packages/inference/test/HfInference.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import "./vcr";
import { readTestFile } from "./test-files";

const TIMEOUT = 60000 * 3;
const env = import.meta.env;

if (!process.env.HF_ACCESS_TOKEN) {
if (!env.HF_ACCESS_TOKEN) {
console.warn("Set HF_ACCESS_TOKEN in the env to run the tests for better rate limits");
}

describe.concurrent(
"HfInference",
() => {
// Individual tests can be ran without providing an api key, however running all tests without an api key will result in rate limiting error.
const hf = new HfInference(process.env.HF_ACCESS_TOKEN);
const hf = new HfInference(env.HF_ACCESS_TOKEN);

it("throws error if model does not exist", () => {
expect(
Expand Down Expand Up @@ -309,32 +310,6 @@ describe.concurrent(
]);
});
it("zeroShotClassification", async () => {
expect.extend({
closeTo(received, expected, precision) {
const { isNot } = this;
let pass = false;
let expectedDiff = 0;
let receivedDiff = 0;

if (received === Infinity && expected === Infinity) {
pass = true;
} else if (received === -Infinity && expected === -Infinity) {
pass = true;
} else {
expectedDiff = 10 ** -precision / 2;
receivedDiff = Math.abs(expected - received);
pass = receivedDiff < expectedDiff;
}

return {
pass,
message: () =>
isNot
? `expected ${received} to not be close to ${expected}, received difference is ${receivedDiff}, but expected ${expectedDiff}`
: `expected ${received} to be close to ${expected}, received difference is ${receivedDiff}, but expected ${expectedDiff}`,
};
},
});
expect(
await hf.zeroShotClassification({
model: "facebook/bart-large-mnli",
Expand Down
31 changes: 31 additions & 0 deletions packages/inference/test/expect-closeto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// https://github.com/vitest-dev/vitest/pull/4260
// this file can be removed after vitest update to 1.0

import { expect } from "vitest";

expect.extend({
closeTo(received: number, expected: number, precision: number) {
const { isNot } = this;
let pass = false;
let expectedDiff = 0;
let receivedDiff = 0;

if (received === Infinity && expected === Infinity) {
pass = true;
} else if (received === -Infinity && expected === -Infinity) {
pass = true;
} else {
expectedDiff = 10 ** -precision / 2;
receivedDiff = Math.abs(expected - received);
pass = receivedDiff < expectedDiff;
}

return {
pass,
message: () =>
isNot
? `expected ${received} to not be close to ${expected}, received difference is ${receivedDiff}, but expected ${expectedDiff}`
: `expected ${received} to be close to ${expected}, received difference is ${receivedDiff}, but expected ${expectedDiff}`,
};
},
});
9 changes: 0 additions & 9 deletions packages/inference/test/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
declare global {
namespace Vi {
interface Assertion {
closeTo(expected: number, precision: number): Assertion;
}
interface AsymmetricMatchersContaining {
closeTo(expected: number, precision: number): Assertion;
}
}

const __TEST_FILES__: Record<string, string>;
}

Expand Down
9 changes: 5 additions & 4 deletions packages/inference/test/vcr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ let VCR_MODE: MODE;
*
* If VCR_MODE is not set and a token is present then disable it.
*/
if (process.env.VCR_MODE) {
if ((process.env.VCR_MODE === MODE.RECORD || process.env.VCR_MODE === MODE.CACHE) && isFrontend) {
const env = import.meta.env;
if (env.VCR_MODE) {
if ((env.VCR_MODE === MODE.RECORD || env.VCR_MODE === MODE.CACHE) && isFrontend) {
throw new Error("VCR_MODE=record is not supported in the browser");
}

VCR_MODE = process.env.VCR_MODE as MODE;
VCR_MODE = env.VCR_MODE as MODE;
} else {
VCR_MODE = process.env.HF_ACCESS_TOKEN ? MODE.DISABLED : MODE.PLAYBACK;
VCR_MODE = env.HF_ACCESS_TOKEN ? MODE.DISABLED : MODE.PLAYBACK;
}

const originalFetch = globalThis.fetch;
Expand Down
Loading

0 comments on commit d30133b

Please sign in to comment.