-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantic-provider-acceptance.mjs
More file actions
74 lines (67 loc) · 2.81 KB
/
Copy pathsemantic-provider-acceptance.mjs
File metadata and controls
74 lines (67 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import assert from "node:assert/strict";
import { startFakeOpenAiServer } from "../support/fake-openai-server.mjs";
import {
LocalOpenAiProvider,
SemanticProviderChain,
} from "../../dist/src/core/semantic-provider.js";
const primary = process.env.PROMPT_REFINER_PRIMARY_MODEL || "gemma3:12b";
const fallback = process.env.PROMPT_REFINER_FALLBACK_MODEL || "gemma3:1b";
const liveBaseUrl = process.env.PROMPT_REFINER_ACCEPTANCE_BASE_URL;
const requireLive = process.argv.includes("--require-live")
|| process.env.PROMPT_REFINER_ACCEPTANCE_REQUIRE_LIVE === "true";
const fake = await startFakeOpenAiServer({
unavailableModels: [primary],
responses: { [fallback]: "fallback accepted" },
});
try {
assert.ok(!requireLive || liveBaseUrl, [
"Required-live Gemma acceptance needs PROMPT_REFINER_ACCEPTANCE_BASE_URL.",
"Set it to the live OpenAI-compatible endpoint that serves the configured Gemma models.",
].join(" "));
if (liveBaseUrl) {
for (const model of [primary, fallback]) {
const liveProvider = new LocalOpenAiProvider({
baseUrl: liveBaseUrl,
models: [model],
timeoutMs: Number.parseInt(process.env.PROMPT_REFINER_ACCEPTANCE_TIMEOUT_MS || "120000", 10),
temperature: 0,
allowNonLoopback: process.env.PROMPT_REFINER_ACCEPTANCE_ALLOW_NON_LOOPBACK === "true",
});
const liveResult = await liveProvider.requestText({ taskName: "live acceptance", prompt: "Reply with accepted.", maxTokens: 16 });
assert.equal(liveResult?.model, model, `Live endpoint did not return a response from ${model}.`);
}
console.log(`Live semantic acceptance passed for ${primary} and ${fallback} at ${liveBaseUrl}.`);
}
const local = new LocalOpenAiProvider({
baseUrl: fake.baseUrl,
models: [primary, fallback],
timeoutMs: 2000,
temperature: 0,
allowNonLoopback: false,
});
const lastResort = {
name: "acceptance-fallback",
requestText: async () => ({
text: "provider fallback accepted",
provider: "acceptance-fallback",
model: "deterministic",
latencyMs: 0,
}),
};
const localResult = await local.requestText({ taskName: "acceptance", prompt: "hello", maxTokens: 16 });
assert.equal(localResult?.model, fallback);
assert.deepEqual(localResult?.fallbackFrom, [primary]);
const outageProvider = new LocalOpenAiProvider({
baseUrl: "http://127.0.0.1:1/v1",
models: [primary, fallback],
timeoutMs: 100,
temperature: 0,
allowNonLoopback: false,
});
const chainResult = await new SemanticProviderChain([outageProvider, lastResort])
.requestText({ taskName: "outage", prompt: "hello", maxTokens: 16 });
assert.equal(chainResult, "provider fallback accepted");
console.log(`Semantic acceptance passed: ${primary} -> ${fallback} and outage provider fallback.`);
} finally {
await fake.close();
}