ragas-js 0.1.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.
- package/LICENSE +21 -0
- package/README.md +66 -0
- package/dist/index.cjs +93 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +66 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 pankajpatel19
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# ragas-js
|
|
2
|
+
|
|
3
|
+
LLM output evaluation metrics for Node.js/TypeScript — a JS-native alternative to [RAGAS](https://github.com/explodinggradients/ragas), for teams building RAG/LLM apps in Node instead of Python.
|
|
4
|
+
|
|
5
|
+
## Why
|
|
6
|
+
|
|
7
|
+
RAGAS is a popular framework for evaluating LLM/RAG outputs (hallucination checks, relevance scoring, etc.), but it's Python-only. Most Node/React/MERN teams building RAG apps have no equivalent way to evaluate their LLM outputs without spinning up a separate Python service.
|
|
8
|
+
|
|
9
|
+
`ragas-js` brings the same idea — using an LLM as a judge to score another LLM's output — natively to Node/TypeScript.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
\`\`\`bash
|
|
14
|
+
npm install ragas-js
|
|
15
|
+
\`\`\`
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
\`\`\`ts
|
|
20
|
+
import { faithfulness } from "ragas-js";
|
|
21
|
+
|
|
22
|
+
const result = await faithfulness(
|
|
23
|
+
{
|
|
24
|
+
question: "What is the capital of France?",
|
|
25
|
+
answer: "Paris is the capital of France.",
|
|
26
|
+
context: ["Paris is the capital and most populous city of France."],
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
provider: "openai",
|
|
30
|
+
apiKey: process.env.OPENAI_API_KEY!,
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
console.log(result);
|
|
35
|
+
// { score: 1, statements: [{ statement: "...", supported: true }] }
|
|
36
|
+
\`\`\`
|
|
37
|
+
|
|
38
|
+
## What it does
|
|
39
|
+
|
|
40
|
+
`faithfulness` measures how much of an answer is actually supported by the given context. It works in two LLM calls:
|
|
41
|
+
|
|
42
|
+
1. Breaks the answer down into atomic factual statements
|
|
43
|
+
2. Checks each statement against the context and marks it supported or not
|
|
44
|
+
|
|
45
|
+
The score is the fraction of statements that were supported (0 to 1).
|
|
46
|
+
|
|
47
|
+
## Current scope (v0.1)
|
|
48
|
+
|
|
49
|
+
This is an early release — scope is intentionally narrow:
|
|
50
|
+
|
|
51
|
+
- ✅ One metric: `faithfulness`
|
|
52
|
+
- ✅ One LLM provider: OpenAI
|
|
53
|
+
- ❌ No other metrics yet (answerRelevancy, contextPrecision, contextRecall)
|
|
54
|
+
- ❌ No other LLM providers yet (Anthropic, Gemini)
|
|
55
|
+
- ❌ No CLI, no dashboard, no persistence
|
|
56
|
+
|
|
57
|
+
## Roadmap
|
|
58
|
+
|
|
59
|
+
- [ ] `answerRelevancy` metric
|
|
60
|
+
- [ ] `contextPrecision` / `contextRecall`
|
|
61
|
+
- [ ] Support for Anthropic and Gemini
|
|
62
|
+
- [ ] CLI for running evals against a JSON/CSV dataset
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
|
|
66
|
+
MIT — see [LICENSE](./LICENSE).
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
faithfulness: () => faithfulness
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/metrics/faithfulness.ts
|
|
28
|
+
var import_zod = require("zod");
|
|
29
|
+
|
|
30
|
+
// src/llmClient.ts
|
|
31
|
+
var import_client = require("openai/client");
|
|
32
|
+
async function LLMCall(config, prompt) {
|
|
33
|
+
const client = new import_client.OpenAI({
|
|
34
|
+
apiKey: config.apiKey
|
|
35
|
+
});
|
|
36
|
+
const res = await client.chat.completions.create({
|
|
37
|
+
messages: [
|
|
38
|
+
{
|
|
39
|
+
role: "user",
|
|
40
|
+
content: prompt
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
model: config.model,
|
|
44
|
+
temperature: config.temperature,
|
|
45
|
+
max_tokens: config.maxTokens
|
|
46
|
+
});
|
|
47
|
+
return res.choices[0]?.message?.content?.toString() ?? Promise.reject("No response from LLM");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/metrics/faithfulness.ts
|
|
51
|
+
var statementSchema = import_zod.z.object({
|
|
52
|
+
statements: import_zod.z.array(import_zod.z.string())
|
|
53
|
+
});
|
|
54
|
+
var verdictSchema = import_zod.z.object({
|
|
55
|
+
verdicts: import_zod.z.array(
|
|
56
|
+
import_zod.z.object({
|
|
57
|
+
statement: import_zod.z.string(),
|
|
58
|
+
supported: import_zod.z.boolean()
|
|
59
|
+
})
|
|
60
|
+
)
|
|
61
|
+
});
|
|
62
|
+
async function faithfulness(input, llm) {
|
|
63
|
+
const extractionPrompt = `Given a question and an answer, break the answer into short, atomic factual statements. Return ONLY valid JSON: {"statements": ["...", "..."]}.
|
|
64
|
+
|
|
65
|
+
Question: ${input.question}
|
|
66
|
+
Answer: ${input.answer}`;
|
|
67
|
+
const rawStatements = await LLMCall(llm, extractionPrompt);
|
|
68
|
+
const parsed = statementSchema.safeParse(
|
|
69
|
+
JSON.parse(cleanJSON(rawStatements))
|
|
70
|
+
);
|
|
71
|
+
const verdictPrompt = `Given a context and a list of statements, judge for each statement whether it is directly supported by the context. Return ONLY valid JSON: {"verdicts": [{"statement": "...", "supported": true}]}.
|
|
72
|
+
|
|
73
|
+
Context:
|
|
74
|
+
${input.context.join("\n")}
|
|
75
|
+
|
|
76
|
+
Statements:
|
|
77
|
+
${parsed.success ? parsed.data.statements.map((s, i) => `${i + 1}. ${s}`).join("\n") : ""}`;
|
|
78
|
+
const verdict = await LLMCall(llm, verdictPrompt);
|
|
79
|
+
const parsedVerdict = verdictSchema.safeParse(JSON.parse(cleanJSON(verdict)));
|
|
80
|
+
const supportedCount = parsedVerdict.success ? parsedVerdict.data.verdicts.filter((v) => v.supported).length : 0;
|
|
81
|
+
const score = parsedVerdict.success && parsedVerdict.data.verdicts.length > 0 ? supportedCount / parsedVerdict.data.verdicts.length : 0;
|
|
82
|
+
return {
|
|
83
|
+
score,
|
|
84
|
+
statements: parsedVerdict.success ? parsedVerdict.data.verdicts : []
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function cleanJSON(raw) {
|
|
88
|
+
return raw.replace(/```json|```/g, "").trim();
|
|
89
|
+
}
|
|
90
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
+
0 && (module.exports = {
|
|
92
|
+
faithfulness
|
|
93
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
interface LLMconfig {
|
|
2
|
+
provider: "openai";
|
|
3
|
+
model: string;
|
|
4
|
+
apiKey: string;
|
|
5
|
+
temperature: number;
|
|
6
|
+
maxTokens: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare function faithfulness(input: {
|
|
10
|
+
question: string;
|
|
11
|
+
answer: string;
|
|
12
|
+
context: string[];
|
|
13
|
+
}, llm: LLMconfig): Promise<{
|
|
14
|
+
score: number;
|
|
15
|
+
statements: {
|
|
16
|
+
statement: string;
|
|
17
|
+
supported: boolean;
|
|
18
|
+
}[];
|
|
19
|
+
}>;
|
|
20
|
+
|
|
21
|
+
export { type LLMconfig, faithfulness };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
interface LLMconfig {
|
|
2
|
+
provider: "openai";
|
|
3
|
+
model: string;
|
|
4
|
+
apiKey: string;
|
|
5
|
+
temperature: number;
|
|
6
|
+
maxTokens: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare function faithfulness(input: {
|
|
10
|
+
question: string;
|
|
11
|
+
answer: string;
|
|
12
|
+
context: string[];
|
|
13
|
+
}, llm: LLMconfig): Promise<{
|
|
14
|
+
score: number;
|
|
15
|
+
statements: {
|
|
16
|
+
statement: string;
|
|
17
|
+
supported: boolean;
|
|
18
|
+
}[];
|
|
19
|
+
}>;
|
|
20
|
+
|
|
21
|
+
export { type LLMconfig, faithfulness };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// src/metrics/faithfulness.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
// src/llmClient.ts
|
|
5
|
+
import { OpenAI } from "openai/client";
|
|
6
|
+
async function LLMCall(config, prompt) {
|
|
7
|
+
const client = new OpenAI({
|
|
8
|
+
apiKey: config.apiKey
|
|
9
|
+
});
|
|
10
|
+
const res = await client.chat.completions.create({
|
|
11
|
+
messages: [
|
|
12
|
+
{
|
|
13
|
+
role: "user",
|
|
14
|
+
content: prompt
|
|
15
|
+
}
|
|
16
|
+
],
|
|
17
|
+
model: config.model,
|
|
18
|
+
temperature: config.temperature,
|
|
19
|
+
max_tokens: config.maxTokens
|
|
20
|
+
});
|
|
21
|
+
return res.choices[0]?.message?.content?.toString() ?? Promise.reject("No response from LLM");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// src/metrics/faithfulness.ts
|
|
25
|
+
var statementSchema = z.object({
|
|
26
|
+
statements: z.array(z.string())
|
|
27
|
+
});
|
|
28
|
+
var verdictSchema = z.object({
|
|
29
|
+
verdicts: z.array(
|
|
30
|
+
z.object({
|
|
31
|
+
statement: z.string(),
|
|
32
|
+
supported: z.boolean()
|
|
33
|
+
})
|
|
34
|
+
)
|
|
35
|
+
});
|
|
36
|
+
async function faithfulness(input, llm) {
|
|
37
|
+
const extractionPrompt = `Given a question and an answer, break the answer into short, atomic factual statements. Return ONLY valid JSON: {"statements": ["...", "..."]}.
|
|
38
|
+
|
|
39
|
+
Question: ${input.question}
|
|
40
|
+
Answer: ${input.answer}`;
|
|
41
|
+
const rawStatements = await LLMCall(llm, extractionPrompt);
|
|
42
|
+
const parsed = statementSchema.safeParse(
|
|
43
|
+
JSON.parse(cleanJSON(rawStatements))
|
|
44
|
+
);
|
|
45
|
+
const verdictPrompt = `Given a context and a list of statements, judge for each statement whether it is directly supported by the context. Return ONLY valid JSON: {"verdicts": [{"statement": "...", "supported": true}]}.
|
|
46
|
+
|
|
47
|
+
Context:
|
|
48
|
+
${input.context.join("\n")}
|
|
49
|
+
|
|
50
|
+
Statements:
|
|
51
|
+
${parsed.success ? parsed.data.statements.map((s, i) => `${i + 1}. ${s}`).join("\n") : ""}`;
|
|
52
|
+
const verdict = await LLMCall(llm, verdictPrompt);
|
|
53
|
+
const parsedVerdict = verdictSchema.safeParse(JSON.parse(cleanJSON(verdict)));
|
|
54
|
+
const supportedCount = parsedVerdict.success ? parsedVerdict.data.verdicts.filter((v) => v.supported).length : 0;
|
|
55
|
+
const score = parsedVerdict.success && parsedVerdict.data.verdicts.length > 0 ? supportedCount / parsedVerdict.data.verdicts.length : 0;
|
|
56
|
+
return {
|
|
57
|
+
score,
|
|
58
|
+
statements: parsedVerdict.success ? parsedVerdict.data.verdicts : []
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function cleanJSON(raw) {
|
|
62
|
+
return raw.replace(/```json|```/g, "").trim();
|
|
63
|
+
}
|
|
64
|
+
export {
|
|
65
|
+
faithfulness
|
|
66
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ragas-js",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "LLM output evaluation metrics for Node.js/TypeScript",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
17
|
+
"test": "vitest run"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/pankajpatel19/ragas-js.git"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"LLM",
|
|
25
|
+
"evaluation",
|
|
26
|
+
"metrics",
|
|
27
|
+
"Node.js",
|
|
28
|
+
"TypeScript",
|
|
29
|
+
"ragas",
|
|
30
|
+
"AI"
|
|
31
|
+
],
|
|
32
|
+
"author": "Pankaj Patel <pankajpatelinter@gmail.com>",
|
|
33
|
+
"license": "ISC",
|
|
34
|
+
"type": "module",
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=18"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist"
|
|
40
|
+
],
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/pankajpatel19/ragas-js/issues"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/pankajpatel19/ragas-js#readme",
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^22.20.2",
|
|
47
|
+
"tsup": "^8.5.1",
|
|
48
|
+
"typescript": "5.9.3",
|
|
49
|
+
"vitest": "^5.0.0"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"openai": "^7.15.0",
|
|
53
|
+
"user": "^0.0.0",
|
|
54
|
+
"zod": "^4.6.4"
|
|
55
|
+
}
|
|
56
|
+
}
|