Spaces:
Running
Running
Update app.js
Browse files
app.js
CHANGED
|
@@ -15,6 +15,7 @@
|
|
| 15 |
* 4. FOCUS TRACKER: guides AI generation away from overrepresented domains.
|
| 16 |
* 5. DOWNLOAD endpoint returns dated filename.
|
| 17 |
* 6. RPS throttle on AI calls, SAMPLES_PER_CALL configurable.
|
|
|
|
| 18 |
*
|
| 19 |
* Endpoints:
|
| 20 |
* GET /status
|
|
@@ -37,12 +38,10 @@ import fs from "fs";
|
|
| 37 |
import path from "path";
|
| 38 |
import {
|
| 39 |
BedrockRuntimeClient,
|
| 40 |
-
|
| 41 |
-
ConverseCommand
|
| 42 |
} from "@aws-sdk/client-bedrock-runtime";
|
| 43 |
import { NodeHttpHandler } from "@smithy/node-http-handler";
|
| 44 |
|
| 45 |
-
|
| 46 |
// ββ CONFIG ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 47 |
const PORT = parseInt(process.env.PORT || "7860");
|
| 48 |
const AWS_REGION = process.env.AWS_REGION || "us-east-1";
|
|
@@ -53,7 +52,7 @@ const DEFAULT_RPS = parseFloat(process.env.DEFAULT_RPS || "3");
|
|
| 53 |
const MAX_RPS = parseFloat(process.env.MAX_RPS || "8");
|
| 54 |
const SAMPLES_PER_CALL = parseInt(process.env.SAMPLES_PER_CALL || "20");
|
| 55 |
const SCALE_K = 30.0;
|
| 56 |
-
const LATENT_D_MAX = 8;
|
| 57 |
|
| 58 |
// ββ LOGGING βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
const ts = () => new Date().toISOString().slice(11, 23);
|
|
@@ -73,17 +72,11 @@ const safeNorm = (b) => {
|
|
| 73 |
const randchoice = (arr) => arr[Math.floor(Math.random() * arr.length)];
|
| 74 |
|
| 75 |
// ββ NORMALIZER ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 76 |
-
/**
|
| 77 |
-
* Core pipeline fix: map raw c_values to [0,1] using declared bounds.
|
| 78 |
-
* Parses vars_text like "VARS c0 IN [lo hi] c1 IN [lo hi] ..."
|
| 79 |
-
* If bounds can't be parsed, clamps raw values to [0,1].
|
| 80 |
-
*/
|
| 81 |
function normalizeSample(s) {
|
| 82 |
const parsed = parseVarsBounds(s.vars_text, s.n_vars);
|
| 83 |
|
| 84 |
if (!Array.isArray(s.c_values) || s.c_values.length < s.n_vars) return null;
|
| 85 |
|
| 86 |
-
// Tolerate if AI appended extra zeros by slicing to exact n_vars
|
| 87 |
const raw = s.c_values.slice(0, s.n_vars);
|
| 88 |
|
| 89 |
const normalized = raw.map((v, i) => {
|
|
@@ -95,7 +88,6 @@ function normalizeSample(s) {
|
|
| 95 |
|
| 96 |
if (normalized.includes(null)) return null;
|
| 97 |
|
| 98 |
-
// Pad mask to the global LATENT_D_MAX requirement
|
| 99 |
const mask =[...Array(s.n_vars).fill(1), ...Array(Math.max(0, LATENT_D_MAX - s.n_vars)).fill(0)];
|
| 100 |
|
| 101 |
return {
|
|
@@ -108,14 +100,12 @@ function normalizeSample(s) {
|
|
| 108 |
}
|
| 109 |
|
| 110 |
function parseVarsBounds(varsText, nVars) {
|
| 111 |
-
// Returns [[lo,hi], ...] for each variable. [FIX]: Now tolerates commas
|
| 112 |
const bounds = [];
|
| 113 |
const re = /IN\s*[\[({]?\s*(-?[\d.e+\-]+)[\s,]+(-?[\d.e+\-]+)\s*[\])}]?/gi;
|
| 114 |
let m;
|
| 115 |
while ((m = re.exec(varsText)) !== null) {
|
| 116 |
bounds.push([parseFloat(m[1]), parseFloat(m[2])]);
|
| 117 |
}
|
| 118 |
-
// Pad with[0,1] if fewer bounds than vars
|
| 119 |
while (bounds.length < nVars) bounds.push([0, 1]);
|
| 120 |
return bounds;
|
| 121 |
}
|
|
@@ -151,8 +141,6 @@ function validateSample(s) {
|
|
| 151 |
|
| 152 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 153 |
// DEEP SYNTHETIC GENERATORS
|
| 154 |
-
// Each generator builds a 2-3 scope AXL program, not just a label.
|
| 155 |
-
// c_values are in raw domain units β normalizeSample() handles [0,1] mapping.
|
| 156 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 157 |
|
| 158 |
// ββ PHYSICS βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -242,7 +230,7 @@ function synthPhysicsSamples(n) {
|
|
| 242 |
` END TRANSFORM`,
|
| 243 |
``,
|
| 244 |
` OBSERVE result EQUALS ${bRaw.toFixed(6)}`,
|
| 245 |
-
` UNKNOWN ${cRaw.map((_,j)=>`c${j}: REAL BOUND
|
| 246 |
``,
|
| 247 |
` COLLAPSE ${t.name.toLowerCase().replace(/ /g,"_")}`,
|
| 248 |
` GIVEN {result EQUALS ${bRaw.toFixed(4)}}`,
|
|
@@ -292,7 +280,7 @@ function synthFinanceSamples(n) {
|
|
| 292 |
} else if (kind === "loan") {
|
| 293 |
const P = rnd(50000,500000), r = rnd(0.002,0.02), np = randint(60,360);
|
| 294 |
bRaw = P*r*(1+r)**np / ((1+r)**np - 1 + 1e-9);
|
| 295 |
-
cRaw =
|
| 296 |
bounds = [[50000,500000],[0.002,0.02],[60,360]];
|
| 297 |
formula = "P*r*(1+r)^n / ((1+r)^n - 1)";
|
| 298 |
subdomain = "loan_payment";
|
|
@@ -331,7 +319,7 @@ function synthFinanceSamples(n) {
|
|
| 331 |
const varsBounds = bounds.map(([lo,hi],j)=>`c${j} IN [${lo} ${hi}]`).join(" ");
|
| 332 |
|
| 333 |
const axl = [
|
| 334 |
-
`SCOPE financial_primitives
|
| 335 |
` ASSUME capital EXISTS`,
|
| 336 |
` ASSUME time EXISTS`,
|
| 337 |
` CONSERVES total_capital APPROXIMATELY ACROSS ${subdomain}`,
|
|
@@ -341,14 +329,14 @@ function synthFinanceSamples(n) {
|
|
| 341 |
` ASSUME financial_primitives SCOPE HOLDS`,
|
| 342 |
``,
|
| 343 |
` ENTITY financial_system`,
|
| 344 |
-
...bounds.map(([lo,hi],j) => ` FIELD c${j}: REAL BOUND
|
| 345 |
` FIELD output: REAL`,
|
| 346 |
` DERIVES output FROM {${cRaw.map((_,j)=>`c${j}`).join(", ")}} BY ${subdomain}_formula`,
|
| 347 |
` TENDS output ${ kind==="portfolio_variance"?"MIN":"MAX" }`,
|
| 348 |
` END ENTITY`,
|
| 349 |
``,
|
| 350 |
` TRANSFORM ${subdomain}_formula`,
|
| 351 |
-
` INPUT {${bounds.map(([lo,hi],j)=>`c${j}: REAL BOUND
|
| 352 |
` OUTPUT {result: REAL}`,
|
| 353 |
` PRODUCES result EQUALS ${formula}`,
|
| 354 |
` END TRANSFORM`,
|
|
@@ -386,7 +374,7 @@ function synthFinanceSamples(n) {
|
|
| 386 |
return samples;
|
| 387 |
}
|
| 388 |
|
| 389 |
-
// ββ CRYPTOGRAPHY
|
| 390 |
function synthXorSamples(n) {
|
| 391 |
const samples =[];
|
| 392 |
for (let i = 0; i < n; i++) {
|
|
@@ -433,11 +421,11 @@ function synthXorSamples(n) {
|
|
| 433 |
` FIELD raw: INTEGER BOUND [0, 255] EQUALS ${plain}`,
|
| 434 |
` END ENTITY`,
|
| 435 |
` ENTITY cipher_byte`,
|
| 436 |
-
` FIELD raw: INTEGER BOUND
|
| 437 |
` DERIVES raw FROM {key_byte.raw, plain_byte.raw} BY xor_byte_transform`,
|
| 438 |
` END ENTITY`,
|
| 439 |
` TRANSFORM xor_byte_transform`,
|
| 440 |
-
` INPUT {key: INTEGER BOUND[0,255], plain: INTEGER BOUND
|
| 441 |
` OUTPUT {cipher: INTEGER BOUND [0,255]}`,
|
| 442 |
` CONSERVES nothing`,
|
| 443 |
` CHANGES cipher`,
|
|
@@ -497,7 +485,7 @@ function synthCaesarSamples(n) {
|
|
| 497 |
` ASSUME modular_arithmetic HOLDS`,
|
| 498 |
` ENTITY alphabet`,
|
| 499 |
` FIELD size: INTEGER EQUALS 26`,
|
| 500 |
-
` BOUND any_char WITHIN[0, 25]`,
|
| 501 |
` END ENTITY`,
|
| 502 |
` TRANSFORM modular_add`,
|
| 503 |
` INPUT {a: INTEGER, b: INTEGER, mod: INTEGER}`,
|
|
@@ -527,12 +515,12 @@ function synthCaesarSamples(n) {
|
|
| 527 |
`SCOPE caesar_message [level: 2]`,
|
| 528 |
` ASSUME caesar_char SCOPE HOLDS`,
|
| 529 |
` ENTITY message`,
|
| 530 |
-
` FIELD plaintext: VECTOR OF INTEGER EQUALS[${plains.join(", ")}]`,
|
| 531 |
` FIELD ciphertext: VECTOR OF INTEGER`,
|
| 532 |
` FIELD length: INTEGER EQUALS ${msgLen}`,
|
| 533 |
` DERIVES ciphertext FROM {plaintext, shift_key.raw} BY caesar_transform APPLIED_TO_EACH`,
|
| 534 |
` END ENTITY`,
|
| 535 |
-
` OBSERVE message.ciphertext EQUALS
|
| 536 |
` OBSERVE SUM(message.ciphertext) EQUALS ${bRaw}`,
|
| 537 |
` UNKNOWN shift_key.normalised: REAL BOUND [0, 1]`,
|
| 538 |
` COLLAPSE caesar_message`,
|
|
@@ -546,7 +534,7 @@ function synthCaesarSamples(n) {
|
|
| 546 |
samples.push({
|
| 547 |
id: randomUUID(), domain: "cryptography", subdomain: "caesar",
|
| 548 |
sense: "EQUAL", n_vars: 1,
|
| 549 |
-
vars_text: "VARS c0 IN[0 25]",
|
| 550 |
expr_text: `EXPR CAESAR plains=[${plains}] ciphers=[${ciphers}] shift=UNKNOWN`,
|
| 551 |
sense_text: "SENSE EQUAL",
|
| 552 |
b_raw: fmt(bRaw), b_norm: fmt(safeNorm(bRaw), 8),
|
|
@@ -663,7 +651,7 @@ function synthAffineSamples(n) {
|
|
| 663 |
const verify = ((aInv*(cipher-b))%26+26)%26;
|
| 664 |
if (verify !== p) continue;
|
| 665 |
|
| 666 |
-
const axl =[
|
| 667 |
`SCOPE modular_arithmetic [level: 0]`,
|
| 668 |
` ASSUME integer EXISTS`,
|
| 669 |
` TRANSFORM modular_multiply_add`,
|
|
@@ -694,20 +682,22 @@ function synthAffineSamples(n) {
|
|
| 694 |
` END ENTITY`,
|
| 695 |
` ENTITY affine_transform_result`,
|
| 696 |
` FIELD plaintext: INTEGER BOUND[0, 25] EQUALS ${p}`,
|
| 697 |
-
` FIELD ciphertext: INTEGER BOUND
|
| 698 |
` DERIVES ciphertext FROM {affine_key.a, plaintext, affine_key.b} BY modular_multiply_add`,
|
|
|
|
| 699 |
` END ENTITY`,
|
| 700 |
` ENTITY affine_inverse`,
|
| 701 |
` FIELD a_inv: INTEGER EQUALS ${aInv}`,
|
| 702 |
` FIELD recovery: INTEGER`,
|
| 703 |
` DERIVES recovery FROM {a_inv, ciphertext, b} BY modular_multiply_add`,
|
|
|
|
| 704 |
` END ENTITY`,
|
| 705 |
` CONTRADICT invertible WITH non_coprime_a`,
|
| 706 |
` AT SCOPE affine_cipher`,
|
| 707 |
` BOUNDARY_TYPE INFORMATION`,
|
| 708 |
` END CONTRADICT`,
|
| 709 |
` OBSERVE affine_transform_result.ciphertext EQUALS ${cipher}`,
|
| 710 |
-
` UNKNOWN affine_key.a_norm: REAL BOUND
|
| 711 |
` UNKNOWN affine_key.b_norm: REAL BOUND [0, 1]`,
|
| 712 |
` COLLAPSE affine_cipher`,
|
| 713 |
` GIVEN {affine_transform_result.plaintext, affine_transform_result.ciphertext}`,
|
|
@@ -720,7 +710,7 @@ function synthAffineSamples(n) {
|
|
| 720 |
samples.push({
|
| 721 |
id: randomUUID(), domain: "cryptography", subdomain: "affine",
|
| 722 |
sense: "EQUAL", n_vars: 2,
|
| 723 |
-
vars_text: "VARS c0 IN
|
| 724 |
expr_text: `EXPR AFFINE a=${a}(idx=${aIdx}) b=${b} plain=${p} cipher=${cipher}`,
|
| 725 |
sense_text: "SENSE EQUAL",
|
| 726 |
b_raw: fmt(cipher), b_norm: fmt(safeNorm(cipher), 8),
|
|
@@ -737,7 +727,7 @@ function synthAffineSamples(n) {
|
|
| 737 |
return samples;
|
| 738 |
}
|
| 739 |
|
| 740 |
-
// ββ OPTIMIZATION (deep AXL
|
| 741 |
function synthOptimizationSamples(n) {
|
| 742 |
const samples =[];
|
| 743 |
for (let i = 0; i < n; i++) {
|
|
@@ -817,13 +807,13 @@ function synthOptimizationSamples(n) {
|
|
| 817 |
|
| 818 |
// ββ SYNTHETIC GENERATOR MAP βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 819 |
const SYNTHETIC_GENERATORS = {
|
| 820 |
-
physics:
|
| 821 |
-
finance:
|
| 822 |
cryptography_xor: synthXorSamples,
|
| 823 |
cryptography_caesar: synthCaesarSamples,
|
| 824 |
cryptography_vigenere:synthVigenereSamples,
|
| 825 |
cryptography_affine: synthAffineSamples,
|
| 826 |
-
optimization:
|
| 827 |
};
|
| 828 |
|
| 829 |
// ββ FOCUS TRACKER βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -854,7 +844,14 @@ function saveDataset() {
|
|
| 854 |
}
|
| 855 |
|
| 856 |
// ββ BEDROCK CLIENT ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 857 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 858 |
|
| 859 |
const AXL_SPEC = `
|
| 860 |
# AXL β Abstraction Exchange Language
|
|
@@ -998,7 +995,7 @@ The system will work backwards from this to find what
|
|
| 998 |
configuration of unknowns is consistent with it.
|
| 999 |
|
| 1000 |
OBSERVE <field | entity | state> EQUALS <value>
|
| 1001 |
-
OBSERVE <field> WITHIN[<min>, <max>]
|
| 1002 |
OBSERVE <pattern> IN <output>
|
| 1003 |
|
| 1004 |
---
|
|
@@ -1009,7 +1006,7 @@ The system navigates the abstraction domain to find
|
|
| 1009 |
values of UNKNOWNs that satisfy all constraints
|
| 1010 |
given the OBSERVATIONs.
|
| 1011 |
|
| 1012 |
-
UNKNOWN <name>: <type> BOUND
|
| 1013 |
UNKNOWN <name>: DISCRETE WITHIN <set>
|
| 1014 |
|
| 1015 |
---
|
|
@@ -1260,7 +1257,7 @@ SCOPE nucleus[level: 2]
|
|
| 1260 |
END SCOPE
|
| 1261 |
|
| 1262 |
|
| 1263 |
-
SCOPE atom
|
| 1264 |
|
| 1265 |
ASSUME nucleus SCOPE HOLDS
|
| 1266 |
|
|
@@ -1298,7 +1295,7 @@ END SCOPE
|
|
| 1298 |
OBSERVE spectral_emission EQUALS {656nm, 486nm, 434nm, 410nm}
|
| 1299 |
|
| 1300 |
UNKNOWN element: SYMBOLIC WITHIN periodic_table
|
| 1301 |
-
UNKNOWN atomic_number: INTEGER BOUND
|
| 1302 |
UNKNOWN electron_configuration: VECTOR
|
| 1303 |
|
| 1304 |
COLLAPSE atom
|
|
@@ -1384,7 +1381,7 @@ SCOPE block_header[level: 2]
|
|
| 1384 |
ASSUME sha256_internals SCOPE HOLDS
|
| 1385 |
|
| 1386 |
ENTITY block_header
|
| 1387 |
-
FIELD version: INTEGER BOUND
|
| 1388 |
FIELD prev_hash: bit_array BOUND length EQUALS 256 # GIVEN, fixed
|
| 1389 |
FIELD merkle_root: bit_array BOUND length EQUALS 256 # GIVEN, fixed
|
| 1390 |
FIELD timestamp: INTEGER # GIVEN, approximately fixed
|
|
@@ -1414,7 +1411,7 @@ SCOPE mining [level: 3]
|
|
| 1414 |
OBJECTIVE valid_block
|
| 1415 |
OPTIMIZE hash MIN # minimize hash value numerically
|
| 1416 |
SUBJECT TO hash BELOW target # hard bound β must be satisfied
|
| 1417 |
-
SUBJECT TO nonce WITHIN
|
| 1418 |
END OBJECTIVE
|
| 1419 |
|
| 1420 |
# ββ HERE IS THE INTELLIGENCE LAYER ββ
|
|
@@ -1488,7 +1485,7 @@ axl:
|
|
| 1488 |
# with error correction and state machines
|
| 1489 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1490 |
|
| 1491 |
-
SCOPE molecular
|
| 1492 |
|
| 1493 |
ASSUME molecule EXISTS
|
| 1494 |
ASSUME information CAN BE ENCODED IN molecule
|
|
@@ -1523,7 +1520,7 @@ SCOPE molecular [level: 0]
|
|
| 1523 |
END SCOPE
|
| 1524 |
|
| 1525 |
|
| 1526 |
-
SCOPE genome
|
| 1527 |
|
| 1528 |
ASSUME molecular SCOPE HOLDS
|
| 1529 |
|
|
@@ -1772,7 +1769,7 @@ JSON SCHEMA (output array of these):
|
|
| 1772 |
"subdomain": "<specific topic>",
|
| 1773 |
"sense": "<EQUAL|MINIMIZE|MAXIMIZE|SELECT|TEND>",
|
| 1774 |
"n_vars": <integer>,
|
| 1775 |
-
"vars_text": "VARS c0 IN[lo hi] c1 IN
|
| 1776 |
"expr_text": "EXPR <description>",
|
| 1777 |
"sense_text": "SENSE <EQUAL|MINIMIZE|MAXIMIZE|SELECT|TEND>",
|
| 1778 |
"b_raw": <float>,
|
|
@@ -1796,54 +1793,24 @@ OUTPUT THE JSON ARRAY NOW:`;
|
|
| 1796 |
}
|
| 1797 |
|
| 1798 |
async function callBedrock(prompt) {
|
| 1799 |
-
|
| 1800 |
-
|
| 1801 |
-
|
| 1802 |
-
|
| 1803 |
-
|
| 1804 |
-
|
| 1805 |
-
|
| 1806 |
-
|
| 1807 |
-
modelId: BEDROCK_MODEL,
|
| 1808 |
-
system:[{ text: prompt }],
|
| 1809 |
-
messages: [{ role: "user", content: {text: "OUTPUT THE JSON ARRAY NOW"} }],
|
| 1810 |
-
|
| 1811 |
-
// Ensure maxTokens is large enough for reasoning + response
|
| 1812 |
-
inferenceConfig: {
|
| 1813 |
-
maxTokens: 4000,
|
| 1814 |
-
temperature: 1
|
| 1815 |
-
},
|
| 1816 |
-
|
| 1817 |
-
additionalModelRequestFields: (
|
| 1818 |
-
/* if (model.includes("haiku")) {
|
| 1819 |
-
return {
|
| 1820 |
-
reasoning_config: {
|
| 1821 |
-
type: "enabled",
|
| 1822 |
-
budget_tokens: 2048
|
| 1823 |
-
}
|
| 1824 |
-
};
|
| 1825 |
-
} */
|
| 1826 |
-
|
| 1827 |
-
// thinking: { type: "adaptive" },
|
| 1828 |
-
{output_config: { effort: "high" }}
|
| 1829 |
-
)
|
| 1830 |
-
|
| 1831 |
-
|
| 1832 |
-
|
| 1833 |
-
});
|
| 1834 |
-
|
| 1835 |
-
const response = await bedrockClient.send(command);
|
| 1836 |
|
| 1837 |
-
|
| 1838 |
-
|
| 1839 |
-
const
|
| 1840 |
-
const res = response;
|
| 1841 |
-
const raw = JSON.parse(Buffer.from(res.body).toString("utf8"));
|
| 1842 |
-
const text = raw.content[0].text;
|
| 1843 |
log(`Bedrock call ${((Date.now()-t0)/1000).toFixed(1)}s β ${text.length} chars`);
|
| 1844 |
-
|
| 1845 |
-
|
| 1846 |
-
|
|
|
|
|
|
|
| 1847 |
}
|
| 1848 |
|
| 1849 |
function parseAIResponse(text) {
|
|
@@ -1885,7 +1852,7 @@ app.use(express.json());
|
|
| 1885 |
app.get("/status", (_, res) => res.json({
|
| 1886 |
dataset_size: dataset.length,
|
| 1887 |
dataset_path: DATASET_PATH,
|
| 1888 |
-
bedrock_model:
|
| 1889 |
focus_tracker: tracker.stats(),
|
| 1890 |
server_time: new Date().toISOString(),
|
| 1891 |
}));
|
|
@@ -1995,7 +1962,7 @@ app.get("/dataset/sample", (req, res) => {
|
|
| 1995 |
loadDataset();
|
| 1996 |
app.listen(PORT, '0.0.0.0', () => {
|
| 1997 |
log(`AXL Dataset Server listening on :${PORT}`, "OK");
|
| 1998 |
-
log(`Model: ${
|
| 1999 |
log(`Dataset: ${DATASET_PATH}`);
|
| 2000 |
log(`RPS: ${DEFAULT_RPS} default / ${MAX_RPS} max`);
|
| 2001 |
});
|
|
|
|
| 15 |
* 4. FOCUS TRACKER: guides AI generation away from overrepresented domains.
|
| 16 |
* 5. DOWNLOAD endpoint returns dated filename.
|
| 17 |
* 6. RPS throttle on AI calls, SAMPLES_PER_CALL configurable.
|
| 18 |
+
* 7. AWS BEDROCK updated to ConverseCommand with NodeHttpHandler.
|
| 19 |
*
|
| 20 |
* Endpoints:
|
| 21 |
* GET /status
|
|
|
|
| 38 |
import path from "path";
|
| 39 |
import {
|
| 40 |
BedrockRuntimeClient,
|
| 41 |
+
ConverseCommand
|
|
|
|
| 42 |
} from "@aws-sdk/client-bedrock-runtime";
|
| 43 |
import { NodeHttpHandler } from "@smithy/node-http-handler";
|
| 44 |
|
|
|
|
| 45 |
// ββ CONFIG ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 46 |
const PORT = parseInt(process.env.PORT || "7860");
|
| 47 |
const AWS_REGION = process.env.AWS_REGION || "us-east-1";
|
|
|
|
| 52 |
const MAX_RPS = parseFloat(process.env.MAX_RPS || "8");
|
| 53 |
const SAMPLES_PER_CALL = parseInt(process.env.SAMPLES_PER_CALL || "20");
|
| 54 |
const SCALE_K = 30.0;
|
| 55 |
+
const LATENT_D_MAX = 8;
|
| 56 |
|
| 57 |
// ββ LOGGING βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 58 |
const ts = () => new Date().toISOString().slice(11, 23);
|
|
|
|
| 72 |
const randchoice = (arr) => arr[Math.floor(Math.random() * arr.length)];
|
| 73 |
|
| 74 |
// ββ NORMALIZER ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
function normalizeSample(s) {
|
| 76 |
const parsed = parseVarsBounds(s.vars_text, s.n_vars);
|
| 77 |
|
| 78 |
if (!Array.isArray(s.c_values) || s.c_values.length < s.n_vars) return null;
|
| 79 |
|
|
|
|
| 80 |
const raw = s.c_values.slice(0, s.n_vars);
|
| 81 |
|
| 82 |
const normalized = raw.map((v, i) => {
|
|
|
|
| 88 |
|
| 89 |
if (normalized.includes(null)) return null;
|
| 90 |
|
|
|
|
| 91 |
const mask =[...Array(s.n_vars).fill(1), ...Array(Math.max(0, LATENT_D_MAX - s.n_vars)).fill(0)];
|
| 92 |
|
| 93 |
return {
|
|
|
|
| 100 |
}
|
| 101 |
|
| 102 |
function parseVarsBounds(varsText, nVars) {
|
|
|
|
| 103 |
const bounds = [];
|
| 104 |
const re = /IN\s*[\[({]?\s*(-?[\d.e+\-]+)[\s,]+(-?[\d.e+\-]+)\s*[\])}]?/gi;
|
| 105 |
let m;
|
| 106 |
while ((m = re.exec(varsText)) !== null) {
|
| 107 |
bounds.push([parseFloat(m[1]), parseFloat(m[2])]);
|
| 108 |
}
|
|
|
|
| 109 |
while (bounds.length < nVars) bounds.push([0, 1]);
|
| 110 |
return bounds;
|
| 111 |
}
|
|
|
|
| 141 |
|
| 142 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 143 |
// DEEP SYNTHETIC GENERATORS
|
|
|
|
|
|
|
| 144 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 145 |
|
| 146 |
// ββ PHYSICS βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 230 |
` END TRANSFORM`,
|
| 231 |
``,
|
| 232 |
` OBSERVE result EQUALS ${bRaw.toFixed(6)}`,
|
| 233 |
+
` UNKNOWN ${cRaw.map((_,j)=>`c${j}: REAL BOUND[${t.bounds[j][0]}, ${t.bounds[j][1]}]`).join("\n UNKNOWN ")}`,
|
| 234 |
``,
|
| 235 |
` COLLAPSE ${t.name.toLowerCase().replace(/ /g,"_")}`,
|
| 236 |
` GIVEN {result EQUALS ${bRaw.toFixed(4)}}`,
|
|
|
|
| 280 |
} else if (kind === "loan") {
|
| 281 |
const P = rnd(50000,500000), r = rnd(0.002,0.02), np = randint(60,360);
|
| 282 |
bRaw = P*r*(1+r)**np / ((1+r)**np - 1 + 1e-9);
|
| 283 |
+
cRaw =[P, r, np];
|
| 284 |
bounds = [[50000,500000],[0.002,0.02],[60,360]];
|
| 285 |
formula = "P*r*(1+r)^n / ((1+r)^n - 1)";
|
| 286 |
subdomain = "loan_payment";
|
|
|
|
| 319 |
const varsBounds = bounds.map(([lo,hi],j)=>`c${j} IN [${lo} ${hi}]`).join(" ");
|
| 320 |
|
| 321 |
const axl = [
|
| 322 |
+
`SCOPE financial_primitives[level: 0]`,
|
| 323 |
` ASSUME capital EXISTS`,
|
| 324 |
` ASSUME time EXISTS`,
|
| 325 |
` CONSERVES total_capital APPROXIMATELY ACROSS ${subdomain}`,
|
|
|
|
| 329 |
` ASSUME financial_primitives SCOPE HOLDS`,
|
| 330 |
``,
|
| 331 |
` ENTITY financial_system`,
|
| 332 |
+
...bounds.map(([lo,hi],j) => ` FIELD c${j}: REAL BOUND[${lo}, ${hi}]`),
|
| 333 |
` FIELD output: REAL`,
|
| 334 |
` DERIVES output FROM {${cRaw.map((_,j)=>`c${j}`).join(", ")}} BY ${subdomain}_formula`,
|
| 335 |
` TENDS output ${ kind==="portfolio_variance"?"MIN":"MAX" }`,
|
| 336 |
` END ENTITY`,
|
| 337 |
``,
|
| 338 |
` TRANSFORM ${subdomain}_formula`,
|
| 339 |
+
` INPUT {${bounds.map(([lo,hi],j)=>`c${j}: REAL BOUND[${lo},${hi}]`).join(", ")}}`,
|
| 340 |
` OUTPUT {result: REAL}`,
|
| 341 |
` PRODUCES result EQUALS ${formula}`,
|
| 342 |
` END TRANSFORM`,
|
|
|
|
| 374 |
return samples;
|
| 375 |
}
|
| 376 |
|
| 377 |
+
// ββ CRYPTOGRAPHY βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 378 |
function synthXorSamples(n) {
|
| 379 |
const samples =[];
|
| 380 |
for (let i = 0; i < n; i++) {
|
|
|
|
| 421 |
` FIELD raw: INTEGER BOUND [0, 255] EQUALS ${plain}`,
|
| 422 |
` END ENTITY`,
|
| 423 |
` ENTITY cipher_byte`,
|
| 424 |
+
` FIELD raw: INTEGER BOUND[0, 255]`,
|
| 425 |
` DERIVES raw FROM {key_byte.raw, plain_byte.raw} BY xor_byte_transform`,
|
| 426 |
` END ENTITY`,
|
| 427 |
` TRANSFORM xor_byte_transform`,
|
| 428 |
+
` INPUT {key: INTEGER BOUND [0,255], plain: INTEGER BOUND[0,255]}`,
|
| 429 |
` OUTPUT {cipher: INTEGER BOUND [0,255]}`,
|
| 430 |
` CONSERVES nothing`,
|
| 431 |
` CHANGES cipher`,
|
|
|
|
| 485 |
` ASSUME modular_arithmetic HOLDS`,
|
| 486 |
` ENTITY alphabet`,
|
| 487 |
` FIELD size: INTEGER EQUALS 26`,
|
| 488 |
+
` BOUND any_char WITHIN [0, 25]`,
|
| 489 |
` END ENTITY`,
|
| 490 |
` TRANSFORM modular_add`,
|
| 491 |
` INPUT {a: INTEGER, b: INTEGER, mod: INTEGER}`,
|
|
|
|
| 515 |
`SCOPE caesar_message [level: 2]`,
|
| 516 |
` ASSUME caesar_char SCOPE HOLDS`,
|
| 517 |
` ENTITY message`,
|
| 518 |
+
` FIELD plaintext: VECTOR OF INTEGER EQUALS [${plains.join(", ")}]`,
|
| 519 |
` FIELD ciphertext: VECTOR OF INTEGER`,
|
| 520 |
` FIELD length: INTEGER EQUALS ${msgLen}`,
|
| 521 |
` DERIVES ciphertext FROM {plaintext, shift_key.raw} BY caesar_transform APPLIED_TO_EACH`,
|
| 522 |
` END ENTITY`,
|
| 523 |
+
` OBSERVE message.ciphertext EQUALS[${ciphers.join(", ")}]`,
|
| 524 |
` OBSERVE SUM(message.ciphertext) EQUALS ${bRaw}`,
|
| 525 |
` UNKNOWN shift_key.normalised: REAL BOUND [0, 1]`,
|
| 526 |
` COLLAPSE caesar_message`,
|
|
|
|
| 534 |
samples.push({
|
| 535 |
id: randomUUID(), domain: "cryptography", subdomain: "caesar",
|
| 536 |
sense: "EQUAL", n_vars: 1,
|
| 537 |
+
vars_text: "VARS c0 IN [0 25]",
|
| 538 |
expr_text: `EXPR CAESAR plains=[${plains}] ciphers=[${ciphers}] shift=UNKNOWN`,
|
| 539 |
sense_text: "SENSE EQUAL",
|
| 540 |
b_raw: fmt(bRaw), b_norm: fmt(safeNorm(bRaw), 8),
|
|
|
|
| 651 |
const verify = ((aInv*(cipher-b))%26+26)%26;
|
| 652 |
if (verify !== p) continue;
|
| 653 |
|
| 654 |
+
const axl = [
|
| 655 |
`SCOPE modular_arithmetic [level: 0]`,
|
| 656 |
` ASSUME integer EXISTS`,
|
| 657 |
` TRANSFORM modular_multiply_add`,
|
|
|
|
| 682 |
` END ENTITY`,
|
| 683 |
` ENTITY affine_transform_result`,
|
| 684 |
` FIELD plaintext: INTEGER BOUND[0, 25] EQUALS ${p}`,
|
| 685 |
+
` FIELD ciphertext: INTEGER BOUND[0, 25]`,
|
| 686 |
` DERIVES ciphertext FROM {affine_key.a, plaintext, affine_key.b} BY modular_multiply_add`,
|
| 687 |
+
` # f(${p}) = (${a} * ${p} + ${b}) mod 26 = ${cipher}`,
|
| 688 |
` END ENTITY`,
|
| 689 |
` ENTITY affine_inverse`,
|
| 690 |
` FIELD a_inv: INTEGER EQUALS ${aInv}`,
|
| 691 |
` FIELD recovery: INTEGER`,
|
| 692 |
` DERIVES recovery FROM {a_inv, ciphertext, b} BY modular_multiply_add`,
|
| 693 |
+
` # a_inv*(cipher-b) mod 26 = ${p} β`,
|
| 694 |
` END ENTITY`,
|
| 695 |
` CONTRADICT invertible WITH non_coprime_a`,
|
| 696 |
` AT SCOPE affine_cipher`,
|
| 697 |
` BOUNDARY_TYPE INFORMATION`,
|
| 698 |
` END CONTRADICT`,
|
| 699 |
` OBSERVE affine_transform_result.ciphertext EQUALS ${cipher}`,
|
| 700 |
+
` UNKNOWN affine_key.a_norm: REAL BOUND[0, 1]`,
|
| 701 |
` UNKNOWN affine_key.b_norm: REAL BOUND [0, 1]`,
|
| 702 |
` COLLAPSE affine_cipher`,
|
| 703 |
` GIVEN {affine_transform_result.plaintext, affine_transform_result.ciphertext}`,
|
|
|
|
| 710 |
samples.push({
|
| 711 |
id: randomUUID(), domain: "cryptography", subdomain: "affine",
|
| 712 |
sense: "EQUAL", n_vars: 2,
|
| 713 |
+
vars_text: "VARS c0 IN[0 11] c1 IN [0 25]",
|
| 714 |
expr_text: `EXPR AFFINE a=${a}(idx=${aIdx}) b=${b} plain=${p} cipher=${cipher}`,
|
| 715 |
sense_text: "SENSE EQUAL",
|
| 716 |
b_raw: fmt(cipher), b_norm: fmt(safeNorm(cipher), 8),
|
|
|
|
| 727 |
return samples;
|
| 728 |
}
|
| 729 |
|
| 730 |
+
// ββ OPTIMIZATION (deep AXL) βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 731 |
function synthOptimizationSamples(n) {
|
| 732 |
const samples =[];
|
| 733 |
for (let i = 0; i < n; i++) {
|
|
|
|
| 807 |
|
| 808 |
// ββ SYNTHETIC GENERATOR MAP βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 809 |
const SYNTHETIC_GENERATORS = {
|
| 810 |
+
physics: synthPhysicsSamples,
|
| 811 |
+
finance: synthFinanceSamples,
|
| 812 |
cryptography_xor: synthXorSamples,
|
| 813 |
cryptography_caesar: synthCaesarSamples,
|
| 814 |
cryptography_vigenere:synthVigenereSamples,
|
| 815 |
cryptography_affine: synthAffineSamples,
|
| 816 |
+
optimization: synthOptimizationSamples,
|
| 817 |
};
|
| 818 |
|
| 819 |
// ββ FOCUS TRACKER βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 844 |
}
|
| 845 |
|
| 846 |
// ββ BEDROCK CLIENT ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 847 |
+
|
| 848 |
+
// Initialize Bedrock Client with NodeHttpHandler explicitly
|
| 849 |
+
const bedrockClient = new BedrockRuntimeClient({
|
| 850 |
+
region: AWS_REGION,
|
| 851 |
+
requestHandler: new NodeHttpHandler({
|
| 852 |
+
http2Handler: undefined,
|
| 853 |
+
})
|
| 854 |
+
});
|
| 855 |
|
| 856 |
const AXL_SPEC = `
|
| 857 |
# AXL β Abstraction Exchange Language
|
|
|
|
| 995 |
configuration of unknowns is consistent with it.
|
| 996 |
|
| 997 |
OBSERVE <field | entity | state> EQUALS <value>
|
| 998 |
+
OBSERVE <field> WITHIN [<min>, <max>]
|
| 999 |
OBSERVE <pattern> IN <output>
|
| 1000 |
|
| 1001 |
---
|
|
|
|
| 1006 |
values of UNKNOWNs that satisfy all constraints
|
| 1007 |
given the OBSERVATIONs.
|
| 1008 |
|
| 1009 |
+
UNKNOWN <name>: <type> BOUND[<min>, <max>]
|
| 1010 |
UNKNOWN <name>: DISCRETE WITHIN <set>
|
| 1011 |
|
| 1012 |
---
|
|
|
|
| 1257 |
END SCOPE
|
| 1258 |
|
| 1259 |
|
| 1260 |
+
SCOPE atom[level: 3]
|
| 1261 |
|
| 1262 |
ASSUME nucleus SCOPE HOLDS
|
| 1263 |
|
|
|
|
| 1295 |
OBSERVE spectral_emission EQUALS {656nm, 486nm, 434nm, 410nm}
|
| 1296 |
|
| 1297 |
UNKNOWN element: SYMBOLIC WITHIN periodic_table
|
| 1298 |
+
UNKNOWN atomic_number: INTEGER BOUND[1, 118]
|
| 1299 |
UNKNOWN electron_configuration: VECTOR
|
| 1300 |
|
| 1301 |
COLLAPSE atom
|
|
|
|
| 1381 |
ASSUME sha256_internals SCOPE HOLDS
|
| 1382 |
|
| 1383 |
ENTITY block_header
|
| 1384 |
+
FIELD version: INTEGER BOUND[1, 4]
|
| 1385 |
FIELD prev_hash: bit_array BOUND length EQUALS 256 # GIVEN, fixed
|
| 1386 |
FIELD merkle_root: bit_array BOUND length EQUALS 256 # GIVEN, fixed
|
| 1387 |
FIELD timestamp: INTEGER # GIVEN, approximately fixed
|
|
|
|
| 1411 |
OBJECTIVE valid_block
|
| 1412 |
OPTIMIZE hash MIN # minimize hash value numerically
|
| 1413 |
SUBJECT TO hash BELOW target # hard bound β must be satisfied
|
| 1414 |
+
SUBJECT TO nonce WITHIN[0, 4294967295]
|
| 1415 |
END OBJECTIVE
|
| 1416 |
|
| 1417 |
# ββ HERE IS THE INTELLIGENCE LAYER ββ
|
|
|
|
| 1485 |
# with error correction and state machines
|
| 1486 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1487 |
|
| 1488 |
+
SCOPE molecular[level: 0]
|
| 1489 |
|
| 1490 |
ASSUME molecule EXISTS
|
| 1491 |
ASSUME information CAN BE ENCODED IN molecule
|
|
|
|
| 1520 |
END SCOPE
|
| 1521 |
|
| 1522 |
|
| 1523 |
+
SCOPE genome[level: 1]
|
| 1524 |
|
| 1525 |
ASSUME molecular SCOPE HOLDS
|
| 1526 |
|
|
|
|
| 1769 |
"subdomain": "<specific topic>",
|
| 1770 |
"sense": "<EQUAL|MINIMIZE|MAXIMIZE|SELECT|TEND>",
|
| 1771 |
"n_vars": <integer>,
|
| 1772 |
+
"vars_text": "VARS c0 IN [lo hi] c1 IN[lo hi] ...",
|
| 1773 |
"expr_text": "EXPR <description>",
|
| 1774 |
"sense_text": "SENSE <EQUAL|MINIMIZE|MAXIMIZE|SELECT|TEND>",
|
| 1775 |
"b_raw": <float>,
|
|
|
|
| 1793 |
}
|
| 1794 |
|
| 1795 |
async function callBedrock(prompt) {
|
| 1796 |
+
const command = new ConverseCommand({
|
| 1797 |
+
modelId: BEDROCK_MODEL_ID,
|
| 1798 |
+
messages: [{ role: "user", content:[{ text: prompt }] }],
|
| 1799 |
+
inferenceConfig: {
|
| 1800 |
+
maxTokens: 10000,
|
| 1801 |
+
temperature: 0.9
|
| 1802 |
+
}
|
| 1803 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1804 |
|
| 1805 |
+
const t0 = Date.now();
|
| 1806 |
+
const res = await bedrockClient.send(command);
|
| 1807 |
+
const text = res.output.message.content.find(b => b.text)?.text || "";
|
|
|
|
|
|
|
|
|
|
| 1808 |
log(`Bedrock call ${((Date.now()-t0)/1000).toFixed(1)}s β ${text.length} chars`);
|
| 1809 |
+
|
| 1810 |
+
const tokenUsage = res.usage ? (res.usage.inputTokens + res.usage.outputTokens) : 0;
|
| 1811 |
+
console.log("Token Usage: " + tokenUsage);
|
| 1812 |
+
|
| 1813 |
+
return text;
|
| 1814 |
}
|
| 1815 |
|
| 1816 |
function parseAIResponse(text) {
|
|
|
|
| 1852 |
app.get("/status", (_, res) => res.json({
|
| 1853 |
dataset_size: dataset.length,
|
| 1854 |
dataset_path: DATASET_PATH,
|
| 1855 |
+
bedrock_model: BEDROCK_MODEL_ID,
|
| 1856 |
focus_tracker: tracker.stats(),
|
| 1857 |
server_time: new Date().toISOString(),
|
| 1858 |
}));
|
|
|
|
| 1962 |
loadDataset();
|
| 1963 |
app.listen(PORT, '0.0.0.0', () => {
|
| 1964 |
log(`AXL Dataset Server listening on :${PORT}`, "OK");
|
| 1965 |
+
log(`Model: ${BEDROCK_MODEL_ID}`);
|
| 1966 |
log(`Dataset: ${DATASET_PATH}`);
|
| 1967 |
log(`RPS: ${DEFAULT_RPS} default / ${MAX_RPS} max`);
|
| 1968 |
});
|