Skip to content

Commit e0c9de1

Browse files
authored
pulled feature flagging out of env file so we can use it on the client (tensorzero#2994)
* pulled feature flagging out of env file so we can use it on the client * updated references to TENSORZERO_FORCE_CACHE_ON * pulled feature flagging out of env file so we can use it on the client * updated references to TENSORZERO_FORCE_CACHE_ON
1 parent 4c113bd commit e0c9de1

7 files changed

Lines changed: 57 additions & 28 deletions

File tree

.github/workflows/ui-tests-e2e-model-inference-cache.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jobs:
8686
echo "FIREWORKS_BASE_URL=http://mock-inference-provider:3030/fireworks/" >> fixtures/.env
8787
echo "OPENAI_BASE_URL=http://mock-inference-provider:3030/openai/" >> fixtures/.env
8888
echo "FIREWORKS_ACCOUNT_ID=fake_fireworks_account" >> fixtures/.env
89-
echo "TENSORZERO_FORCE_CACHE_ON=1" >> fixtures/.env
89+
echo "VITE_TENSORZERO_FORCE_CACHE_ON=1" >> fixtures/.env
9090
9191
- name: Regenerate model inference cache
9292
working-directory: ui

ui/app/routes/api/tensorzero/inference.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { JSONParseError } from "~/utils/common";
33
import type { Route } from "./+types/inference";
44
import { getNativeTensorZeroClient } from "~/utils/tensorzero/native_client.server";
55
import type { ClientInferenceParams } from "tensorzero-node";
6-
import { getExtraInferenceOptions } from "~/utils/env.server";
6+
import { getExtraInferenceOptions } from "~/utils/feature_flags";
77

88
export async function action({ request }: Route.ActionArgs): Promise<Response> {
99
const formData = await request.formData();

ui/app/routes/playground/route.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import DatapointPlaygroundOutput from "./DatapointPlaygroundOutput";
3434
import { safeParseInt } from "~/utils/common";
3535
import { getNativeTensorZeroClient } from "~/utils/tensorzero/native_client.server";
3636
import type { InferenceResponse } from "tensorzero-node";
37-
import { getExtraInferenceOptions } from "~/utils/env.server";
37+
import { getExtraInferenceOptions } from "~/utils/feature_flags";
3838

3939
const DEFAULT_LIMIT = 5;
4040

ui/app/utils/env.server.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ interface Env {
2020
OPENAI_BASE_URL: string | null;
2121
FIREWORKS_BASE_URL: string | null;
2222
FIREWORKS_ACCOUNT_ID: string | null;
23-
/// When set, sets `cache_options.enabled = "on"` on all inference calls
24-
/// Normally, we leave this unset, which uses the TensorZero default of 'write_only'
25-
/// This is used by e2e tests to allow us to populate the model inference cache
26-
/// from regen-fixtures without trampling existing entries, and then to use the cached
27-
/// entries from the normal ui e2e tests
28-
TENSORZERO_FORCE_CACHE_ON: boolean;
2923
}
3024

3125
let _env: Env;
@@ -66,28 +60,11 @@ export function getEnv(): Env {
6660
TENSORZERO_EVALUATIONS_PATH:
6761
process.env.TENSORZERO_EVALUATIONS_PATH || "evaluations",
6862
FIREWORKS_ACCOUNT_ID: process.env.FIREWORKS_ACCOUNT_ID || null,
69-
TENSORZERO_FORCE_CACHE_ON: process.env.TENSORZERO_FORCE_CACHE_ON === "1",
7063
};
7164

7265
return _env;
7366
}
7467

75-
/// Returns an object containing extra parameters that should be passed to
76-
/// inference calls on our TensorZero client
77-
export function getExtraInferenceOptions(): object {
78-
if (getEnv().TENSORZERO_FORCE_CACHE_ON) {
79-
return {
80-
// We need to force dryrun off, as it prevents us from writing to the cache
81-
// (which we need in order to populate our model inference cache)
82-
dryrun: false,
83-
cache_options: {
84-
enabled: "on",
85-
},
86-
};
87-
}
88-
return {};
89-
}
90-
9168
function getClickhouseUrl() {
9269
const url = process.env.TENSORZERO_CLICKHOUSE_URL;
9370
if (url) {

ui/app/utils/feature_flags.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { canUseDOM } from "./common";
2+
3+
interface FeatureFlags {
4+
/// When set, sets `cache_options.enabled = "on"` on all inference calls
5+
/// Normally, we leave this unset, which uses the TensorZero default of 'write_only'
6+
/// This is used by e2e tests to allow us to populate the model inference cache
7+
/// from regen-fixtures without trampling existing entries, and then to use the cached
8+
/// entries from the normal ui e2e tests
9+
FORCE_CACHE_ON: boolean;
10+
}
11+
12+
/**
13+
* Get feature flags for the application.
14+
* This can be accessed from the client.
15+
* @returns FeatureFlags
16+
*/
17+
export function getFeatureFlags(): FeatureFlags {
18+
const envValue = canUseDOM
19+
? import.meta.env.VITE_TENSORZERO_FORCE_CACHE_ON
20+
: process.env.VITE_TENSORZERO_FORCE_CACHE_ON;
21+
const FORCE_CACHE_ON = envValue === "1";
22+
return {
23+
FORCE_CACHE_ON,
24+
};
25+
}
26+
27+
interface ExtraInferenceOptions {
28+
cache_options?: {
29+
enabled: "on" | "off" | "write_only";
30+
max_age_s: number | null;
31+
};
32+
dryrun?: boolean;
33+
}
34+
35+
/**
36+
* Returns an object containing extra parameters that should be passed to
37+
* inference calls on our TensorZero client
38+
*/
39+
export function getExtraInferenceOptions(): ExtraInferenceOptions {
40+
if (getFeatureFlags().FORCE_CACHE_ON) {
41+
return {
42+
// We need to force dryrun off, as it prevents us from writing to the
43+
// cache (which we need in order to populate our model inference cache)
44+
cache_options: {
45+
enabled: "on",
46+
max_age_s: null,
47+
},
48+
dryrun: false,
49+
};
50+
}
51+
return {};
52+
}

ui/fixtures/docker-compose.ui.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ services:
1111
- OPENAI_BASE_URL
1212
- FIREWORKS_BASE_URL
1313
- FIREWORKS_ACCOUNT_ID
14-
- TENSORZERO_FORCE_CACHE_ON
14+
- VITE_TENSORZERO_FORCE_CACHE_ON
1515
volumes:
1616
- ./config:/app/config:ro
1717
env_file:

ui/fixtures/regenerate-model-inference-cache.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cd "$(dirname "$0")"/../
66

77
docker compose -f ./fixtures/docker-compose.e2e.yml -f ./fixtures/docker-compose.ui.yml down
88
docker compose -f ./fixtures/docker-compose.e2e.yml -f ./fixtures/docker-compose.ui.yml rm -f
9-
OPENAI_BASE_URL=http://mock-inference-provider:3030/openai/ FIREWORKS_BASE_URL=http://mock-inference-provider:3030/fireworks/ FIREWORKS_ACCOUNT_ID=fake_fireworks_account TENSORZERO_SKIP_LARGE_FIXTURES=1 TENSORZERO_FORCE_CACHE_ON=1 docker compose -f ./fixtures/docker-compose.e2e.yml -f ./fixtures/docker-compose.ui.yml up --build --force-recreate -d
9+
OPENAI_BASE_URL=http://mock-inference-provider:3030/openai/ FIREWORKS_BASE_URL=http://mock-inference-provider:3030/fireworks/ FIREWORKS_ACCOUNT_ID=fake_fireworks_account TENSORZERO_SKIP_LARGE_FIXTURES=1 VITE_TENSORZERO_FORCE_CACHE_ON=1 docker compose -f ./fixtures/docker-compose.e2e.yml -f ./fixtures/docker-compose.ui.yml up --build --force-recreate -d
1010
docker compose -f ./fixtures/docker-compose.e2e.yml -f ./fixtures/docker-compose.ui.yml wait fixtures
1111
# Wipe the ModelInferenceCache table to ensure that we regenerate everything
1212
docker run --add-host=host.docker.internal:host-gateway clickhouse/clickhouse-server clickhouse-client --host host.docker.internal --user chuser --password chpassword --database tensorzero_ui_fixtures 'TRUNCATE TABLE ModelInferenceCache SYNC'

0 commit comments

Comments
 (0)