neetnestor commited on
Commit
f18fa7e
1 Parent(s): 48dfd32

Use ace editor for grammar input

Browse files
Files changed (3) hide show
  1. dist/index.js +0 -0
  2. index.html +2 -7
  3. src/index.js +37 -33
dist/index.js CHANGED
The diff for this file is too large to render. See raw diff
 
index.html CHANGED
@@ -47,7 +47,7 @@
47
  <div id="ebnf-grammar-container" class="card hidden">
48
  <form>
49
  <label for="ebnf-grammar" class="container"
50
- ><span><b>Custom EBNF Grammar</b></span>
51
  <span>
52
  The custom grammar is described in the
53
  <a
@@ -58,12 +58,7 @@
58
  >. Below is an example of JSON grammar in EBNF. Please follow
59
  this example when writing new grammars.
60
  </span>
61
- <textarea
62
- id="ebnf-grammar"
63
- dir="ltr"
64
- placeholder="Type your custom EBNF grammar..."
65
- rows="1"
66
- ></textarea>
67
  </label>
68
  </form>
69
  </div>
 
47
  <div id="ebnf-grammar-container" class="card hidden">
48
  <form>
49
  <label for="ebnf-grammar" class="container"
50
+ ><span><b>Custom Grammar</b></span>
51
  <span>
52
  The custom grammar is described in the
53
  <a
 
58
  >. Below is an example of JSON grammar in EBNF. Please follow
59
  this example when writing new grammars.
60
  </span>
61
+ <div id="ebnf-grammar"></div>
 
 
 
 
 
62
  </label>
63
  </form>
64
  </div>
src/index.js CHANGED
@@ -24,20 +24,6 @@ document.addEventListener("DOMContentLoaded", () => {
24
  const outputDiv = document.getElementById("output");
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);
@@ -79,15 +65,14 @@ ws ::= [\n\t]*`;
79
  engine = null; // Reset the engine when the model changes
80
  };
81
 
82
- // JSON editor setup with Ace
83
- const editor = ace.edit("schema", {
84
  mode: "ace/mode/javascript",
85
  theme: "ace/theme/github",
86
  wrap: true,
87
  });
88
-
89
- editor.setTheme("ace/theme/github");
90
- editor.setValue(`Type.Object({
91
  "name": Type.String(),
92
  "house": Type.Enum({
93
  "Gryffindor": "Gryffindor",
@@ -115,6 +100,24 @@ ws ::= [\n\t]*`;
115
  "patronus": Type.String(),
116
  })`);
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  // Set initial prompt
119
  promptTextarea.value = `Hermione Granger is a character in Harry Potter. Please fill in the following information about this character in JSON format.
120
  Name is a string of character name.
@@ -127,16 +130,6 @@ Patronus is a string.
127
  `;
128
  // Generate button click handler
129
  document.getElementById("generate").onclick = async () => {
130
- const schemaInput = editor.getValue();
131
- let T;
132
- try {
133
- T = eval(schemaInput);
134
- } catch (e) {
135
- console.error("Invalid schema", e);
136
- return;
137
- }
138
- const schema = JSON.stringify(T);
139
-
140
  if (!engine) {
141
  engine = await CreateMLCEngine(selectedModel, {
142
  initProgressCallback: (progress) => {
@@ -145,15 +138,26 @@ Patronus is a string.
145
  },
146
  });
147
  }
148
-
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  const request = {
150
  stream: true,
151
  stream_options: { include_usage: true },
152
  messages: [{ role: "user", content: promptTextarea.value }],
153
  max_tokens: 512,
154
- response_format: useCustomGrammar
155
- ? { type: "grammar", grammar: ebnfTextarea.value }
156
- : { type: "json_object", schema: schema },
157
  };
158
 
159
  let curMessage = "";
 
24
  const outputDiv = document.getElementById("output");
25
  const statsParagraph = document.getElementById("stats");
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  // Handle grammar selection changes
28
  grammarSelection.onchange = (ev) => {
29
  console.log("Grammar selection changed:", ev.target.value);
 
65
  engine = null; // Reset the engine when the model changes
66
  };
67
 
68
+ // Editors setup with Ace
69
+ const jsonSchemaEditor = ace.edit("schema", {
70
  mode: "ace/mode/javascript",
71
  theme: "ace/theme/github",
72
  wrap: true,
73
  });
74
+ jsonSchemaEditor.setTheme("ace/theme/github");
75
+ jsonSchemaEditor.setValue(`Type.Object({
 
76
  "name": Type.String(),
77
  "house": Type.Enum({
78
  "Gryffindor": "Gryffindor",
 
100
  "patronus": Type.String(),
101
  })`);
102
 
103
+ const grammarEditor = ace.edit("ebnf-grammar", {
104
+ theme: "ace/theme/github",
105
+ wrap: true,
106
+ });
107
+ grammarEditor.setTheme("ace/theme/github");
108
+ grammarEditor.setValue(String.raw`main ::= basic_array | basic_object
109
+ basic_any ::= basic_number | basic_string | basic_boolean | basic_null | basic_array | basic_object
110
+ basic_integer ::= ("0" | "-"? [1-9] [0-9]*) ".0"?
111
+ basic_number ::= ("0" | "-"? [1-9] [0-9]*) ("." [0-9]+)? ([eE] [+-]? [0-9]+)?
112
+ basic_string ::= (([\"] basic_string_1 [\"]))
113
+ basic_string_1 ::= "" | [^"\\\x00-\x1F] basic_string_1 | "\\" escape basic_string_1
114
+ escape ::= ["\\/bfnrt] | "u" [A-Fa-f0-9] [A-Fa-f0-9] [A-Fa-f0-9] [A-Fa-f0-9]
115
+ basic_boolean ::= "true" | "false"
116
+ basic_null ::= "null"
117
+ basic_array ::= "[" ("" | ws basic_any (ws "," ws basic_any)*) ws "]"
118
+ basic_object ::= "{" ("" | ws basic_string ws ":" ws basic_any ( ws "," ws basic_string ws ":" ws basic_any)*) ws "}"
119
+ ws ::= [\n\t]*`);
120
+
121
  // Set initial prompt
122
  promptTextarea.value = `Hermione Granger is a character in Harry Potter. Please fill in the following information about this character in JSON format.
123
  Name is a string of character name.
 
130
  `;
131
  // Generate button click handler
132
  document.getElementById("generate").onclick = async () => {
 
 
 
 
 
 
 
 
 
 
133
  if (!engine) {
134
  engine = await CreateMLCEngine(selectedModel, {
135
  initProgressCallback: (progress) => {
 
138
  },
139
  });
140
  }
141
+ let response_format = { type: "grammar", grammar: grammarEditor.getValue() };
142
+ if (!useCustomGrammar) {
143
+ const schemaInput = jsonSchemaEditor.getValue();
144
+ let T;
145
+ try {
146
+ T = eval(schemaInput);
147
+ } catch (e) {
148
+ console.error("Invalid schema", e);
149
+ return;
150
+ }
151
+ const schema = JSON.stringify(T);
152
+ response_format = { type: "json_object", schema }
153
+ }
154
+ console.log(response_format);
155
  const request = {
156
  stream: true,
157
  stream_options: { include_usage: true },
158
  messages: [{ role: "user", content: promptTextarea.value }],
159
  max_tokens: 512,
160
+ response_format,
 
 
161
  };
162
 
163
  let curMessage = "";