neetnestor commited on
Commit
cc1f2fe
1 Parent(s): 34c1901

Hide schema if custom grammar is selected

Browse files
Files changed (3) hide show
  1. dist/index.js +0 -0
  2. index.html +2 -2
  3. src/index.js +16 -20
dist/index.js CHANGED
The diff for this file is too large to render. See raw diff
 
index.html CHANGED
@@ -58,7 +58,7 @@
58
  </label>
59
  </form>
60
  </div>
61
- <div class="card">
62
  <form>
63
  <label class="container"
64
  ><span><b>Schema</b></span>
@@ -70,7 +70,7 @@
70
  target="_blank"
71
  >TypeBox</a
72
  >
73
- for schema type syntax and documentation.</span
74
  >
75
  <div id="schema"></div>
76
  </label>
 
58
  </label>
59
  </form>
60
  </div>
61
+ <div id="schema-container" class="card">
62
  <form>
63
  <label class="container"
64
  ><span><b>Schema</b></span>
 
70
  target="_blank"
71
  >TypeBox</a
72
  >
73
+ for JSON schema type syntax and documentation.</span
74
  >
75
  <div id="schema"></div>
76
  </label>
src/index.js CHANGED
@@ -12,23 +12,12 @@ const { Type } = require("@sinclair/typebox");
12
 
13
  let engine = null;
14
  let useCustomGrammar = false;
15
- let customGrammar = String.raw`main ::= basic_array | basic_object
16
- basic_any ::= basic_number | basic_string | basic_boolean | basic_null | basic_array | basic_object
17
- basic_integer ::= ("0" | "-"? [1-9] [0-9]*) ".0"?
18
- basic_number ::= ("0" | "-"? [1-9] [0-9]*) ("." [0-9]+)? ([eE] [+-]? [0-9]+)?
19
- basic_string ::= (([\"] basic_string_1 [\"]))
20
- basic_string_1 ::= "" | [^"\\\x00-\x1F] basic_string_1 | "\\" escape basic_string_1
21
- escape ::= ["\\/bfnrt] | "u" [A-Fa-f0-9] [A-Fa-f0-9] [A-Fa-f0-9] [A-Fa-f0-9]
22
- basic_boolean ::= "true" | "false"
23
- basic_null ::= "null"
24
- basic_array ::= "[" ("" | ws basic_any (ws "," ws basic_any)*) ws "]"
25
- basic_object ::= "{" ("" | ws basic_string ws ":" ws basic_any ( ws "," ws basic_string ws ":" ws basic_any)*) ws "}"
26
- ws ::= [\n\t]*`;
27
 
28
  document.addEventListener("DOMContentLoaded", () => {
29
  // Ensure elements are loaded before using them
30
  const grammarSelection = document.getElementById("grammar-selection");
31
  const ebnfContainer = document.getElementById("ebnf-grammar-container");
 
32
  const modelSelection = document.getElementById("model-selection");
33
  const ebnfTextarea = document.getElementById("ebnf-grammar");
34
  const promptTextarea = document.getElementById("prompt");
@@ -36,22 +25,29 @@ document.addEventListener("DOMContentLoaded", () => {
36
  const statsParagraph = document.getElementById("stats");
37
 
38
  // Initialize the custom grammar textarea
39
- ebnfTextarea.value = customGrammar;
40
-
41
- // Update grammar value on change
42
- ebnfTextarea.onchange = (ev) => {
43
- customGrammar = ev.target.value;
44
- console.log("Grammar updated: ", customGrammar);
45
- };
 
 
 
 
 
46
 
47
  // Handle grammar selection changes
48
  grammarSelection.onchange = (ev) => {
49
  console.log("Grammar selection changed:", ev.target.value);
50
  if (ev.target.value === "json") {
51
  ebnfContainer.classList.add("hidden");
 
52
  useCustomGrammar = false;
53
  } else {
54
  ebnfContainer.classList.remove("hidden");
 
55
  useCustomGrammar = true;
56
  }
57
  };
@@ -156,7 +152,7 @@ Patronus is a string.
156
  messages: [{ role: "user", content: promptTextarea.value }],
157
  max_tokens: 128,
158
  response_format: useCustomGrammar
159
- ? { type: "grammar", grammar: customGrammar }
160
  : { type: "json_object", schema: schema },
161
  };
162
 
 
12
 
13
  let engine = null;
14
  let useCustomGrammar = false;
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  document.addEventListener("DOMContentLoaded", () => {
17
  // Ensure elements are loaded before using them
18
  const grammarSelection = document.getElementById("grammar-selection");
19
  const ebnfContainer = document.getElementById("ebnf-grammar-container");
20
+ const schemaContainer = document.getElementById("schema-container");
21
  const modelSelection = document.getElementById("model-selection");
22
  const ebnfTextarea = document.getElementById("ebnf-grammar");
23
  const promptTextarea = document.getElementById("prompt");
 
25
  const statsParagraph = document.getElementById("stats");
26
 
27
  // Initialize the custom grammar textarea
28
+ ebnfTextarea.value = String.raw`main ::= basic_array | basic_object
29
+ basic_any ::= basic_number | basic_string | basic_boolean | basic_null | basic_array | basic_object
30
+ basic_integer ::= ("0" | "-"? [1-9] [0-9]*) ".0"?
31
+ basic_number ::= ("0" | "-"? [1-9] [0-9]*) ("." [0-9]+)? ([eE] [+-]? [0-9]+)?
32
+ basic_string ::= (([\"] basic_string_1 [\"]))
33
+ basic_string_1 ::= "" | [^"\\\x00-\x1F] basic_string_1 | "\\" escape basic_string_1
34
+ escape ::= ["\\/bfnrt] | "u" [A-Fa-f0-9] [A-Fa-f0-9] [A-Fa-f0-9] [A-Fa-f0-9]
35
+ basic_boolean ::= "true" | "false"
36
+ basic_null ::= "null"
37
+ basic_array ::= "[" ("" | ws basic_any (ws "," ws basic_any)*) ws "]"
38
+ basic_object ::= "{" ("" | ws basic_string ws ":" ws basic_any ( ws "," ws basic_string ws ":" ws basic_any)*) ws "}"
39
+ ws ::= [\n\t]*`;
40
 
41
  // Handle grammar selection changes
42
  grammarSelection.onchange = (ev) => {
43
  console.log("Grammar selection changed:", ev.target.value);
44
  if (ev.target.value === "json") {
45
  ebnfContainer.classList.add("hidden");
46
+ schemaContainer.classList.remove("hidden");
47
  useCustomGrammar = false;
48
  } else {
49
  ebnfContainer.classList.remove("hidden");
50
+ schemaContainer.classList.add("hidden");
51
  useCustomGrammar = true;
52
  }
53
  };
 
152
  messages: [{ role: "user", content: promptTextarea.value }],
153
  max_tokens: 128,
154
  response_format: useCustomGrammar
155
+ ? { type: "grammar", grammar: ebnfTextarea.value }
156
  : { type: "json_object", schema: schema },
157
  };
158