llm-json-guard 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +32 -0
  2. package/index.js +59 -0
  3. package/package.json +17 -0
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # llm-json-guard
2
+
3
+ Production-safe JSON repair and schema validation for LLM outputs.
4
+
5
+ ## Installation
6
+
7
+ npm install llm-json-guard
8
+
9
+ ## Usage
10
+
11
+ import { LLMJsonGuard } from "llm-json-guard";
12
+
13
+ const guard = new LLMJsonGuard({
14
+ apiKey: process.env.RAPIDAPI_KEY
15
+ });
16
+
17
+ const result = await guard.guard(
18
+ "{name: 'Harsh', age: 21,}",
19
+ {
20
+ type: "object",
21
+ properties: {
22
+ name: { type: "string" },
23
+ age: { type: "number" }
24
+ },
25
+ required: ["name", "age"]
26
+ }
27
+ );
28
+
29
+ console.log(result.data);
30
+
31
+ Requires a RapidAPI key.
32
+ Get one here: https://rapidapi.com/...
package/index.js ADDED
@@ -0,0 +1,59 @@
1
+ class LLMJsonGuard {
2
+ constructor({ apiKey }) {
3
+ if (!apiKey) {
4
+ throw new Error("RapidAPI key is required");
5
+ }
6
+
7
+ this.apiKey = apiKey;
8
+ this.baseUrl =
9
+ "https://llm-json-sanitizer-schema-guard.p.rapidapi.com";
10
+ }
11
+
12
+ async sanitize(rawOutput) {
13
+ if (!rawOutput) {
14
+ throw new Error("rawOutput is required");
15
+ }
16
+
17
+ return this.#request("/api/json-repair/sanitize", {
18
+ raw_output: rawOutput
19
+ });
20
+ }
21
+
22
+ async guard(rawOutput, schema) {
23
+ if (!rawOutput || !schema) {
24
+ throw new Error("rawOutput and schema are required");
25
+ }
26
+
27
+ return this.#request("/api/json-repair/guard", {
28
+ raw_output: rawOutput,
29
+ schema
30
+ });
31
+ }
32
+
33
+ async #request(path, body) {
34
+ const response = await fetch(this.baseUrl + path, {
35
+ method: "POST",
36
+ headers: {
37
+ "Content-Type": "application/json",
38
+ "x-rapidapi-key": this.apiKey,
39
+ "x-rapidapi-host":
40
+ "llm-json-sanitizer-schema-guard.p.rapidapi.com"
41
+ },
42
+ body: JSON.stringify(body)
43
+ });
44
+
45
+ const data = await response.json();
46
+
47
+ if (!response.ok) {
48
+ const message =
49
+ data?.data?.response?.stage ||
50
+ data?.message ||
51
+ "Request failed";
52
+ throw new Error(message);
53
+ }
54
+
55
+ return data.data.response;
56
+ }
57
+ }
58
+
59
+ export { LLMJsonGuard };
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "llm-json-guard",
3
+ "version": "1.0.0",
4
+ "description": "Production-safe JSON repair and schema validation for LLM outputs",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "keywords": [
8
+ "json",
9
+ "llm",
10
+ "json-validation",
11
+ "json-repair",
12
+ "ai",
13
+ "schema"
14
+ ],
15
+ "author": "Harsh Verma",
16
+ "license": "MIT"
17
+ }