homegraph 1.2.0 → 1.4.1
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/dist/bin/homegraph.js +3 -0
- package/dist/bin/homegraph.js.map +1 -1
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +85 -0
- package/dist/context/index.js.map +1 -1
- package/dist/db/index.d.ts +64 -5
- package/dist/db/index.d.ts.map +1 -1
- package/dist/db/index.js +178 -14
- package/dist/db/index.js.map +1 -1
- package/dist/db/migrations.d.ts +1 -1
- package/dist/db/migrations.d.ts.map +1 -1
- package/dist/db/migrations.js +19 -1
- package/dist/db/migrations.js.map +1 -1
- package/dist/db/queries.d.ts +117 -2
- package/dist/db/queries.d.ts.map +1 -1
- package/dist/db/queries.js +299 -6
- package/dist/db/queries.js.map +1 -1
- package/dist/db/schema.sql +32 -1
- package/dist/db/wal-valve.d.ts +106 -0
- package/dist/db/wal-valve.d.ts.map +1 -0
- package/dist/db/wal-valve.js +208 -0
- package/dist/db/wal-valve.js.map +1 -0
- package/dist/directory.d.ts +9 -5
- package/dist/directory.d.ts.map +1 -1
- package/dist/directory.js +210 -19
- package/dist/directory.js.map +1 -1
- package/dist/extraction/cfml-extractor.d.ts +107 -0
- package/dist/extraction/cfml-extractor.d.ts.map +1 -0
- package/dist/extraction/cfml-extractor.js +494 -0
- package/dist/extraction/cfml-extractor.js.map +1 -0
- package/dist/extraction/grammars.d.ts +25 -1
- package/dist/extraction/grammars.d.ts.map +1 -1
- package/dist/extraction/grammars.js +177 -24
- package/dist/extraction/grammars.js.map +1 -1
- package/dist/extraction/index.d.ts +43 -4
- package/dist/extraction/index.d.ts.map +1 -1
- package/dist/extraction/index.js +308 -5
- package/dist/extraction/index.js.map +1 -1
- package/dist/extraction/languages/c-cpp.d.ts +42 -0
- package/dist/extraction/languages/c-cpp.d.ts.map +1 -1
- package/dist/extraction/languages/c-cpp.js +369 -4
- package/dist/extraction/languages/c-cpp.js.map +1 -1
- package/dist/extraction/languages/cfquery.d.ts +12 -0
- package/dist/extraction/languages/cfquery.d.ts.map +1 -0
- package/dist/extraction/languages/cfquery.js +28 -0
- package/dist/extraction/languages/cfquery.js.map +1 -0
- package/dist/extraction/languages/cfscript.d.ts +3 -0
- package/dist/extraction/languages/cfscript.d.ts.map +1 -0
- package/dist/extraction/languages/cfscript.js +73 -0
- package/dist/extraction/languages/cfscript.js.map +1 -0
- package/dist/extraction/languages/cobol.d.ts +33 -0
- package/dist/extraction/languages/cobol.d.ts.map +1 -0
- package/dist/extraction/languages/cobol.js +499 -0
- package/dist/extraction/languages/cobol.js.map +1 -0
- package/dist/extraction/languages/erlang.d.ts +3 -0
- package/dist/extraction/languages/erlang.d.ts.map +1 -0
- package/dist/extraction/languages/erlang.js +350 -0
- package/dist/extraction/languages/erlang.js.map +1 -0
- package/dist/extraction/languages/index.d.ts.map +1 -1
- package/dist/extraction/languages/index.js +16 -0
- package/dist/extraction/languages/index.js.map +1 -1
- package/dist/extraction/languages/nix.d.ts +3 -0
- package/dist/extraction/languages/nix.d.ts.map +1 -0
- package/dist/extraction/languages/nix.js +294 -0
- package/dist/extraction/languages/nix.js.map +1 -0
- package/dist/extraction/languages/solidity.d.ts +3 -0
- package/dist/extraction/languages/solidity.d.ts.map +1 -0
- package/dist/extraction/languages/solidity.js +293 -0
- package/dist/extraction/languages/solidity.js.map +1 -0
- package/dist/extraction/languages/terraform.d.ts +3 -0
- package/dist/extraction/languages/terraform.d.ts.map +1 -0
- package/dist/extraction/languages/terraform.js +641 -0
- package/dist/extraction/languages/terraform.js.map +1 -0
- package/dist/extraction/languages/vbnet.d.ts +11 -0
- package/dist/extraction/languages/vbnet.d.ts.map +1 -0
- package/dist/extraction/languages/vbnet.js +141 -0
- package/dist/extraction/languages/vbnet.js.map +1 -0
- package/dist/extraction/mybatis-extractor.d.ts +30 -10
- package/dist/extraction/mybatis-extractor.d.ts.map +1 -1
- package/dist/extraction/mybatis-extractor.js +140 -38
- package/dist/extraction/mybatis-extractor.js.map +1 -1
- package/dist/extraction/parse-pool.d.ts +27 -1
- package/dist/extraction/parse-pool.d.ts.map +1 -1
- package/dist/extraction/parse-pool.js +75 -7
- package/dist/extraction/parse-pool.js.map +1 -1
- package/dist/extraction/parse-worker.js +9 -2
- package/dist/extraction/parse-worker.js.map +1 -1
- package/dist/extraction/tree-sitter-helpers.d.ts.map +1 -1
- package/dist/extraction/tree-sitter-helpers.js +1 -0
- package/dist/extraction/tree-sitter-helpers.js.map +1 -1
- package/dist/extraction/tree-sitter-types.d.ts +3 -1
- package/dist/extraction/tree-sitter-types.d.ts.map +1 -1
- package/dist/extraction/tree-sitter.d.ts +38 -0
- package/dist/extraction/tree-sitter.d.ts.map +1 -1
- package/dist/extraction/tree-sitter.js +754 -10
- package/dist/extraction/tree-sitter.js.map +1 -1
- package/dist/extraction/wasm/tree-sitter-arkts.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-cfml.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-cfquery.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-cfscript.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-cobol.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-erlang.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-nix.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-terraform.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-vbnet.wasm +0 -0
- package/dist/graph/traversal.d.ts.map +1 -1
- package/dist/graph/traversal.js +1 -1
- package/dist/graph/traversal.js.map +1 -1
- package/dist/index.d.ts +60 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +348 -5
- package/dist/index.js.map +1 -1
- package/dist/installer/index.d.ts +42 -0
- package/dist/installer/index.d.ts.map +1 -1
- package/dist/installer/index.js +97 -2
- package/dist/installer/index.js.map +1 -1
- package/dist/mcp/daemon.d.ts +25 -3
- package/dist/mcp/daemon.d.ts.map +1 -1
- package/dist/mcp/daemon.js +62 -8
- package/dist/mcp/daemon.js.map +1 -1
- package/dist/mcp/dynamic-boundaries.d.ts.map +1 -1
- package/dist/mcp/dynamic-boundaries.js +2 -1
- package/dist/mcp/dynamic-boundaries.js.map +1 -1
- package/dist/mcp/early-ppid.d.ts +26 -0
- package/dist/mcp/early-ppid.d.ts.map +1 -0
- package/dist/mcp/early-ppid.js +29 -0
- package/dist/mcp/early-ppid.js.map +1 -0
- package/dist/mcp/engine.d.ts +4 -5
- package/dist/mcp/engine.d.ts.map +1 -1
- package/dist/mcp/engine.js +27 -1
- package/dist/mcp/engine.js.map +1 -1
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +41 -6
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/liveness-watchdog.d.ts +18 -1
- package/dist/mcp/liveness-watchdog.d.ts.map +1 -1
- package/dist/mcp/liveness-watchdog.js +73 -13
- package/dist/mcp/liveness-watchdog.js.map +1 -1
- package/dist/mcp/memory-budget.d.ts +30 -0
- package/dist/mcp/memory-budget.d.ts.map +1 -0
- package/dist/mcp/memory-budget.js +123 -0
- package/dist/mcp/memory-budget.js.map +1 -0
- package/dist/mcp/proxy.d.ts.map +1 -1
- package/dist/mcp/proxy.js +88 -4
- package/dist/mcp/proxy.js.map +1 -1
- package/dist/mcp/query-cache.d.ts +35 -4
- package/dist/mcp/query-cache.d.ts.map +1 -1
- package/dist/mcp/query-cache.js +89 -19
- package/dist/mcp/query-cache.js.map +1 -1
- package/dist/mcp/query-pool.d.ts +46 -12
- package/dist/mcp/query-pool.d.ts.map +1 -1
- package/dist/mcp/query-pool.js +100 -23
- package/dist/mcp/query-pool.js.map +1 -1
- package/dist/mcp/server-instructions.d.ts +4 -27
- package/dist/mcp/server-instructions.d.ts.map +1 -1
- package/dist/mcp/server-instructions.js +39 -84
- package/dist/mcp/server-instructions.js.map +1 -1
- package/dist/mcp/session.d.ts +14 -0
- package/dist/mcp/session.d.ts.map +1 -1
- package/dist/mcp/session.js +28 -1
- package/dist/mcp/session.js.map +1 -1
- package/dist/mcp/startup-handshake.d.ts +44 -0
- package/dist/mcp/startup-handshake.d.ts.map +1 -0
- package/dist/mcp/startup-handshake.js +73 -0
- package/dist/mcp/startup-handshake.js.map +1 -0
- package/dist/mcp/tools.d.ts +104 -4
- package/dist/mcp/tools.d.ts.map +1 -1
- package/dist/mcp/tools.js +2390 -191
- package/dist/mcp/tools.js.map +1 -1
- package/dist/mcp/transport.d.ts.map +1 -1
- package/dist/mcp/transport.js +18 -0
- package/dist/mcp/transport.js.map +1 -1
- package/dist/project-config.d.ts +38 -0
- package/dist/project-config.d.ts.map +1 -1
- package/dist/project-config.js +101 -2
- package/dist/project-config.js.map +1 -1
- package/dist/resolution/c-fnptr-synthesizer.d.ts +2 -1
- package/dist/resolution/c-fnptr-synthesizer.d.ts.map +1 -1
- package/dist/resolution/c-fnptr-synthesizer.js +178 -146
- package/dist/resolution/c-fnptr-synthesizer.js.map +1 -1
- package/dist/resolution/callback-synthesizer.d.ts +9 -1
- package/dist/resolution/callback-synthesizer.d.ts.map +1 -1
- package/dist/resolution/callback-synthesizer.js +1164 -176
- package/dist/resolution/callback-synthesizer.js.map +1 -1
- package/dist/resolution/frameworks/cics.d.ts +20 -0
- package/dist/resolution/frameworks/cics.d.ts.map +1 -0
- package/dist/resolution/frameworks/cics.js +90 -0
- package/dist/resolution/frameworks/cics.js.map +1 -0
- package/dist/resolution/frameworks/index.d.ts.map +1 -1
- package/dist/resolution/frameworks/index.js +3 -0
- package/dist/resolution/frameworks/index.js.map +1 -1
- package/dist/resolution/frameworks/java.d.ts.map +1 -1
- package/dist/resolution/frameworks/java.js +14 -6
- package/dist/resolution/frameworks/java.js.map +1 -1
- package/dist/resolution/frameworks/terraform.d.ts +38 -0
- package/dist/resolution/frameworks/terraform.d.ts.map +1 -0
- package/dist/resolution/frameworks/terraform.js +277 -0
- package/dist/resolution/frameworks/terraform.js.map +1 -0
- package/dist/resolution/goframe-synthesizer.d.ts +2 -1
- package/dist/resolution/goframe-synthesizer.d.ts.map +1 -1
- package/dist/resolution/goframe-synthesizer.js +8 -3
- package/dist/resolution/goframe-synthesizer.js.map +1 -1
- package/dist/resolution/import-resolver.d.ts +7 -0
- package/dist/resolution/import-resolver.d.ts.map +1 -1
- package/dist/resolution/import-resolver.js +129 -4
- package/dist/resolution/import-resolver.js.map +1 -1
- package/dist/resolution/index.d.ts +60 -7
- package/dist/resolution/index.d.ts.map +1 -1
- package/dist/resolution/index.js +430 -80
- package/dist/resolution/index.js.map +1 -1
- package/dist/resolution/name-matcher.d.ts +0 -3
- package/dist/resolution/name-matcher.d.ts.map +1 -1
- package/dist/resolution/name-matcher.js +354 -33
- package/dist/resolution/name-matcher.js.map +1 -1
- package/dist/resolution/strip-comments.d.ts +1 -1
- package/dist/resolution/strip-comments.d.ts.map +1 -1
- package/dist/resolution/strip-comments.js +49 -0
- package/dist/resolution/strip-comments.js.map +1 -1
- package/dist/resolution/types.d.ts +29 -0
- package/dist/resolution/types.d.ts.map +1 -1
- package/dist/resolution/workspace-packages.d.ts +10 -0
- package/dist/resolution/workspace-packages.d.ts.map +1 -1
- package/dist/resolution/workspace-packages.js +142 -4
- package/dist/resolution/workspace-packages.js.map +1 -1
- package/dist/search/identifier-segments.d.ts +60 -0
- package/dist/search/identifier-segments.d.ts.map +1 -0
- package/dist/search/identifier-segments.js +176 -0
- package/dist/search/identifier-segments.js.map +1 -0
- package/dist/search/query-utils.d.ts +201 -0
- package/dist/search/query-utils.d.ts.map +1 -1
- package/dist/search/query-utils.js +919 -2
- package/dist/search/query-utils.js.map +1 -1
- package/dist/sync/git-hooks.d.ts.map +1 -1
- package/dist/sync/git-hooks.js +2 -0
- package/dist/sync/git-hooks.js.map +1 -1
- package/dist/sync/watcher.d.ts +10 -5
- package/dist/sync/watcher.d.ts.map +1 -1
- package/dist/sync/watcher.js +51 -14
- package/dist/sync/watcher.js.map +1 -1
- package/dist/sync/worktree.d.ts.map +1 -1
- package/dist/sync/worktree.js +5 -0
- package/dist/sync/worktree.js.map +1 -1
- package/dist/types.d.ts +19 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +10 -0
- package/dist/types.js.map +1 -1
- package/dist/upgrade/index.d.ts +32 -0
- package/dist/upgrade/index.d.ts.map +1 -1
- package/dist/upgrade/index.js +162 -14
- package/dist/upgrade/index.js.map +1 -1
- package/dist/upgrade/remove-binary.d.ts +87 -0
- package/dist/upgrade/remove-binary.d.ts.map +1 -0
- package/dist/upgrade/remove-binary.js +289 -0
- package/dist/upgrade/remove-binary.js.map +1 -0
- package/dist/upgrade/update-check.d.ts +92 -0
- package/dist/upgrade/update-check.d.ts.map +1 -0
- package/dist/upgrade/update-check.js +258 -0
- package/dist/upgrade/update-check.js.map +1 -0
- package/package.json +2 -2
- package/scripts/agent-eval/run-all.sh +6 -0
- package/scripts/build-bundle.sh +5 -0
- package/scripts/npm-shim.js +8 -1
- package/scripts/exp_boundary_eval/__pycache__/_utils.cpython-310.pyc +0 -0
- package/scripts/exp_boundary_eval/__pycache__/analyze.cpython-310.pyc +0 -0
- package/scripts/exp_boundary_eval/__pycache__/deveco_arm.cpython-310.pyc +0 -0
- package/scripts/exp_boundary_eval/__pycache__/run_one.cpython-310.pyc +0 -0
- package/scripts/exp_boundary_eval/__pycache__/run_session.cpython-310.pyc +0 -0
- package/scripts/exp_boundary_eval/__pycache__/setup.cpython-310.pyc +0 -0
- package/scripts/exp_boundary_eval/data/agents.json +0 -109
- package/scripts/exp_boundary_eval/data/experiments.json +0 -140
- package/scripts/qa_eval/README.md +0 -407
- package/scripts/qa_eval/_test_deveco_probe.py +0 -41
- package/scripts/qa_eval/agent_runner.py +0 -526
- package/scripts/qa_eval/data/.gitignore +0 -4
- package/scripts/qa_eval/data/test-set.jsonl +0 -2
- package/scripts/qa_eval/eval_metrics.py +0 -274
- package/scripts/qa_eval/external_agent.py +0 -976
- package/scripts/qa_eval/llm_config.py +0 -92
- package/scripts/qa_eval/memory_monitor.py +0 -132
- package/scripts/qa_eval/my_answer_accuracy.py +0 -187
- package/scripts/qa_eval/requirements.txt +0 -2
- package/scripts/qa_eval/run_pipeline.py +0 -804
- package/scripts/qa_eval/stats_efficiency.py +0 -279
- package/scripts/qa_eval/stats_scores.py +0 -207
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
3
|
-
Stage 2 — RAG Answer Accuracy Evaluation (LLM-as-Judge).
|
|
4
|
-
|
|
5
|
-
Reads agent-produced JSONL (query + output_answer + reference_answer),
|
|
6
|
-
strips tool-call blocks, scores 0–1 via MyAnswerAccuracy.
|
|
7
|
-
|
|
8
|
-
Usage:
|
|
9
|
-
export DASHSCOPE_API_KEY="sk-..."
|
|
10
|
-
python scripts/qa_eval/eval_metrics.py \\
|
|
11
|
-
-i scripts/qa_eval/log/result-with-builtin.jsonl \\
|
|
12
|
-
-o scripts/qa_eval/log/result-with-builtin-scored.jsonl \\
|
|
13
|
-
-w 2
|
|
14
|
-
"""
|
|
15
|
-
|
|
16
|
-
from __future__ import annotations
|
|
17
|
-
|
|
18
|
-
import argparse
|
|
19
|
-
import asyncio
|
|
20
|
-
import json
|
|
21
|
-
import logging
|
|
22
|
-
import os
|
|
23
|
-
import statistics
|
|
24
|
-
import sys
|
|
25
|
-
import threading
|
|
26
|
-
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
27
|
-
from pathlib import Path
|
|
28
|
-
|
|
29
|
-
from tqdm import tqdm
|
|
30
|
-
|
|
31
|
-
_SCRIPT_DIR = Path(__file__).resolve().parent
|
|
32
|
-
LOG_DIR = _SCRIPT_DIR / "log"
|
|
33
|
-
LOG_DIR.mkdir(parents=True, exist_ok=True)
|
|
34
|
-
|
|
35
|
-
if str(_SCRIPT_DIR) not in sys.path:
|
|
36
|
-
sys.path.insert(0, str(_SCRIPT_DIR))
|
|
37
|
-
|
|
38
|
-
from my_answer_accuracy import MyAnswerAccuracy # noqa: E402
|
|
39
|
-
from llm_config import PROVIDER_DASHSCOPE, PROVIDER_ZHIPU, provider_help, resolve_llm_config # noqa: E402
|
|
40
|
-
|
|
41
|
-
os.environ.setdefault("NO_PROXY", "localhost,127.0.0.1")
|
|
42
|
-
|
|
43
|
-
logging.basicConfig(
|
|
44
|
-
level=logging.INFO,
|
|
45
|
-
format="%(asctime)s - %(levelname)s - %(message)s",
|
|
46
|
-
handlers=[
|
|
47
|
-
logging.FileHandler(LOG_DIR / "eval_metrics.log"),
|
|
48
|
-
logging.StreamHandler(),
|
|
49
|
-
],
|
|
50
|
-
)
|
|
51
|
-
logger = logging.getLogger(__name__)
|
|
52
|
-
|
|
53
|
-
file_write_lock = threading.Lock()
|
|
54
|
-
|
|
55
|
-
DEFAULT_INPUT = LOG_DIR / "result-with-builtin.jsonl"
|
|
56
|
-
DEFAULT_OUTPUT = LOG_DIR / "result-with-builtin-scored.jsonl"
|
|
57
|
-
DEFAULT_MODEL = "qwen3-235b-a22b-instruct-2507"
|
|
58
|
-
DEFAULT_BASE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
|
59
|
-
DEFAULT_API_KEY_ENV = "DASHSCOPE_API_KEY"
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
class RAGEvaluator:
|
|
63
|
-
"""Answer accuracy judge with concurrent processing."""
|
|
64
|
-
|
|
65
|
-
def __init__(
|
|
66
|
-
self,
|
|
67
|
-
llm_model: str,
|
|
68
|
-
*,
|
|
69
|
-
api_key: str,
|
|
70
|
-
base_url: str,
|
|
71
|
-
extra_body: dict | None = None,
|
|
72
|
-
) -> None:
|
|
73
|
-
self.scorer = MyAnswerAccuracy.create(
|
|
74
|
-
api_key=api_key,
|
|
75
|
-
base_url=base_url,
|
|
76
|
-
model=llm_model,
|
|
77
|
-
extra_body=extra_body,
|
|
78
|
-
)
|
|
79
|
-
|
|
80
|
-
async def evaluate_answer_accuracy_single(self, sample_data: dict) -> dict:
|
|
81
|
-
return await self.scorer.evaluate_answer_accuracy_single(sample_data)
|
|
82
|
-
|
|
83
|
-
async def close(self) -> None:
|
|
84
|
-
await self.scorer.client.close()
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
async def evaluate_all_async(evaluator: RAGEvaluator, items: list[dict]) -> list[dict]:
|
|
88
|
-
processed: list[dict] = []
|
|
89
|
-
try:
|
|
90
|
-
for item in items:
|
|
91
|
-
processed.append(await evaluator.evaluate_answer_accuracy_single(item))
|
|
92
|
-
finally:
|
|
93
|
-
await evaluator.close()
|
|
94
|
-
return processed
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
def process_single_item_sync(
|
|
98
|
-
*,
|
|
99
|
-
llm_model: str,
|
|
100
|
-
api_key: str,
|
|
101
|
-
base_url: str,
|
|
102
|
-
extra_body: dict | None,
|
|
103
|
-
item: dict,
|
|
104
|
-
) -> dict:
|
|
105
|
-
loop = asyncio.new_event_loop()
|
|
106
|
-
asyncio.set_event_loop(loop)
|
|
107
|
-
evaluator = RAGEvaluator(
|
|
108
|
-
llm_model,
|
|
109
|
-
api_key=api_key,
|
|
110
|
-
base_url=base_url,
|
|
111
|
-
extra_body=extra_body,
|
|
112
|
-
)
|
|
113
|
-
try:
|
|
114
|
-
return loop.run_until_complete(evaluator.evaluate_answer_accuracy_single(item))
|
|
115
|
-
finally:
|
|
116
|
-
loop.run_until_complete(evaluator.close())
|
|
117
|
-
loop.close()
|
|
118
|
-
asyncio.set_event_loop(None)
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
def load_jsonl(path: Path) -> list[dict]:
|
|
122
|
-
items: list[dict] = []
|
|
123
|
-
with path.open("r", encoding="utf-8") as f:
|
|
124
|
-
for line_no, line in enumerate(f, 1):
|
|
125
|
-
line = line.strip()
|
|
126
|
-
if not line:
|
|
127
|
-
continue
|
|
128
|
-
try:
|
|
129
|
-
items.append(json.loads(line))
|
|
130
|
-
except json.JSONDecodeError as e:
|
|
131
|
-
logger.error("第 %s 行 JSON 解析失败: %s", line_no, e)
|
|
132
|
-
return items
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
def resolve_api_key(env_name: str) -> str | None:
|
|
136
|
-
key = os.environ.get(env_name, "").strip()
|
|
137
|
-
if key:
|
|
138
|
-
return key
|
|
139
|
-
fallback = os.environ.get("OPENAI_API_KEY", "").strip()
|
|
140
|
-
return fallback or None
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
def main() -> int:
|
|
144
|
-
parser = argparse.ArgumentParser(description="RAG Answer Accuracy Evaluation (LLM-as-Judge)")
|
|
145
|
-
parser.add_argument("--input", "-i", type=str, default=str(DEFAULT_INPUT), help="Agent 产出的 JSONL")
|
|
146
|
-
parser.add_argument("--output", "-o", type=str, default=str(DEFAULT_OUTPUT), help="打分后的 JSONL")
|
|
147
|
-
parser.add_argument("--workers", "-w", type=int, default=1, help="并发线程数")
|
|
148
|
-
parser.add_argument(
|
|
149
|
-
"--provider",
|
|
150
|
-
choices=[PROVIDER_DASHSCOPE, PROVIDER_ZHIPU],
|
|
151
|
-
default=None,
|
|
152
|
-
help=provider_help(),
|
|
153
|
-
)
|
|
154
|
-
parser.add_argument("--model", "-m", type=str, default=None, help="Judge 模型(默认随 provider)")
|
|
155
|
-
parser.add_argument("--base-url", type=str, default=None, help="OpenAI 兼容 API 端点(默认随 provider)")
|
|
156
|
-
args = parser.parse_args()
|
|
157
|
-
|
|
158
|
-
input_file = Path(args.input)
|
|
159
|
-
output_file = Path(args.output)
|
|
160
|
-
|
|
161
|
-
try:
|
|
162
|
-
llm = resolve_llm_config(provider=args.provider, model=args.model, base_url=args.base_url)
|
|
163
|
-
except RuntimeError as e:
|
|
164
|
-
print(f"错误: {e}", file=sys.stderr)
|
|
165
|
-
return 1
|
|
166
|
-
|
|
167
|
-
print(f"输入文件: {input_file}")
|
|
168
|
-
print(f"输出文件: {output_file}")
|
|
169
|
-
print(f"并发线程数: {args.workers}")
|
|
170
|
-
print(f"LLM provider: {llm.provider}")
|
|
171
|
-
print(f"评估模型: {llm.model}")
|
|
172
|
-
|
|
173
|
-
if not input_file.is_file():
|
|
174
|
-
print(f"错误: 输入文件 {input_file} 不存在")
|
|
175
|
-
return 1
|
|
176
|
-
|
|
177
|
-
api_key = llm.api_key
|
|
178
|
-
|
|
179
|
-
all_items = load_jsonl(input_file)
|
|
180
|
-
total_items = len(all_items)
|
|
181
|
-
print(f"读取到 {total_items} 个样本")
|
|
182
|
-
|
|
183
|
-
if total_items == 0:
|
|
184
|
-
print("没有找到有效的样本数据")
|
|
185
|
-
return 1
|
|
186
|
-
|
|
187
|
-
for i, item in enumerate(all_items, 1):
|
|
188
|
-
if not str(item.get("query", "")).strip():
|
|
189
|
-
print(f"错误: 第 {i} 条缺少 query")
|
|
190
|
-
return 1
|
|
191
|
-
if not str(item.get("reference_answer", "")).strip():
|
|
192
|
-
print(f"错误: 第 {i} 条缺少 reference_answer")
|
|
193
|
-
return 1
|
|
194
|
-
if not str(item.get("output_answer", "")).strip():
|
|
195
|
-
logger.warning("第 %s 条 (%s) output_answer 为空,Judge 可能得 0 分", i, item.get("id", "?"))
|
|
196
|
-
|
|
197
|
-
output_file.parent.mkdir(parents=True, exist_ok=True)
|
|
198
|
-
output_file.write_text("", encoding="utf-8")
|
|
199
|
-
|
|
200
|
-
evaluator = RAGEvaluator(
|
|
201
|
-
llm.model,
|
|
202
|
-
api_key=api_key,
|
|
203
|
-
base_url=llm.base_url,
|
|
204
|
-
extra_body=llm.extra_body,
|
|
205
|
-
)
|
|
206
|
-
print("初始化 Judge 完成")
|
|
207
|
-
|
|
208
|
-
processed_items: list[dict] = []
|
|
209
|
-
results_lock = threading.Lock()
|
|
210
|
-
|
|
211
|
-
if args.workers <= 1:
|
|
212
|
-
processed_items = asyncio.run(evaluate_all_async(evaluator, all_items))
|
|
213
|
-
with output_file.open("w", encoding="utf-8") as writer:
|
|
214
|
-
for row in processed_items:
|
|
215
|
-
writer.write(json.dumps(row, ensure_ascii=False) + "\n")
|
|
216
|
-
else:
|
|
217
|
-
with ThreadPoolExecutor(max_workers=max(1, args.workers)) as executor:
|
|
218
|
-
futures = {
|
|
219
|
-
executor.submit(
|
|
220
|
-
process_single_item_sync,
|
|
221
|
-
llm_model=llm.model,
|
|
222
|
-
api_key=api_key,
|
|
223
|
-
base_url=llm.base_url,
|
|
224
|
-
extra_body=llm.extra_body,
|
|
225
|
-
item=item,
|
|
226
|
-
): item
|
|
227
|
-
for item in all_items
|
|
228
|
-
}
|
|
229
|
-
for future in tqdm(as_completed(futures), total=len(all_items), desc="评估答案准确性"):
|
|
230
|
-
try:
|
|
231
|
-
processed_item = future.result()
|
|
232
|
-
with results_lock:
|
|
233
|
-
processed_items.append(processed_item)
|
|
234
|
-
if len(processed_items) % 5 == 0:
|
|
235
|
-
with file_write_lock:
|
|
236
|
-
with output_file.open("a", encoding="utf-8") as writer:
|
|
237
|
-
for row in processed_items[-5:]:
|
|
238
|
-
writer.write(json.dumps(row, ensure_ascii=False) + "\n")
|
|
239
|
-
except Exception as e:
|
|
240
|
-
logger.error("样本处理任务异常: %s", e)
|
|
241
|
-
|
|
242
|
-
remaining = len(processed_items) % 5
|
|
243
|
-
if remaining:
|
|
244
|
-
with file_write_lock:
|
|
245
|
-
with output_file.open("a", encoding="utf-8") as writer:
|
|
246
|
-
for row in processed_items[-remaining:]:
|
|
247
|
-
writer.write(json.dumps(row, ensure_ascii=False) + "\n")
|
|
248
|
-
|
|
249
|
-
if processed_items:
|
|
250
|
-
successful = [x for x in processed_items if x.get("evaluation_status") == "success"]
|
|
251
|
-
scores = [float(x.get("answer_accuracy_score", 0.0)) for x in processed_items]
|
|
252
|
-
print("\n评估完成!")
|
|
253
|
-
print(f"总样本数: {total_items}")
|
|
254
|
-
print(f"成功评估: {len(successful)}")
|
|
255
|
-
print(f"失败评估: {total_items - len(successful)}")
|
|
256
|
-
if successful:
|
|
257
|
-
ok_scores = [float(x["answer_accuracy_score"]) for x in successful]
|
|
258
|
-
print(f"平均答案准确性得分: {statistics.mean(ok_scores):.4f}")
|
|
259
|
-
print(f"最高得分: {max(ok_scores):.4f}")
|
|
260
|
-
print(f"最低得分: {min(ok_scores):.4f}")
|
|
261
|
-
print(f"\n结果已写入: {output_file}")
|
|
262
|
-
else:
|
|
263
|
-
print("没有成功处理任何样本")
|
|
264
|
-
return 1
|
|
265
|
-
|
|
266
|
-
return 0
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
if __name__ == "__main__":
|
|
270
|
-
try:
|
|
271
|
-
raise SystemExit(main())
|
|
272
|
-
except KeyboardInterrupt:
|
|
273
|
-
print("\n用户中断了程序执行")
|
|
274
|
-
raise SystemExit(130)
|