-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathrequest.py
More file actions
94 lines (75 loc) · 3.07 KB
/
request.py
File metadata and controls
94 lines (75 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Text GAM — Request (Q&A) Example
使用 Workflow("text") 对已构建的 GAM 进行问答。
Usage:
python request.py [gam_path] [question]
Examples:
python request.py # 使用默认路径和问题
python request.py ./output/GAM # 指定 GAM 路径
python request.py ./output/GAM "这篇论文的主要贡献是什么?" # 指定路径和问题
"""
import sys
from pathlib import Path
from datetime import datetime
from gam import Workflow
def main():
# ----------------------------------------------------------------
# 1. 解析参数
# ----------------------------------------------------------------
default_gam = Path(__file__).parent / "output" / "GAM"
default_question = "总结一下这篇论文的要点。"
gam_path = Path(sys.argv[1]) if len(sys.argv) > 1 else default_gam
question = " ".join(sys.argv[2:]) if len(sys.argv) > 2 else default_question
if not gam_path.exists():
print(f"❌ GAM 不存在: {gam_path}")
print(f"\nUsage: python {Path(__file__).name} [gam_path] [question]")
print(f"💡 提示: 请先使用 add.py 构建 GAM")
return
# ----------------------------------------------------------------
# 2. 创建 Workflow (只需要这一步!)
# ----------------------------------------------------------------
wf = Workflow(
"text",
gam_dir=gam_path,
# LLM config — set via env vars GAM_MODEL, GAM_API_BASE, GAM_API_KEY
# or pass explicitly here:
# model="gpt-4o-mini",
# api_base="https://api.openai.com/v1",
# api_key="sk-xxx",
max_tokens=4096,
temperature=0.3,
max_iterations=20,
verbose=True,
)
print(f"📂 GAM 路径: {gam_path}")
print(f"🤖 模型: {wf.model}")
print(f"\n📂 GAM 结构:")
print(wf.get_tree_view(depth=3))
print(f"\n📋 问题: {question}\n")
# ----------------------------------------------------------------
# 3. 问答
# ----------------------------------------------------------------
start = datetime.now()
result = wf.request(question)
duration = (datetime.now() - start).total_seconds()
# ----------------------------------------------------------------
# 4. 显示结果
# ----------------------------------------------------------------
print("\n" + "=" * 80)
print("📝 答案:")
print("=" * 80)
print(result.answer or "(未生成答案)")
print("=" * 80)
print(f"\n📚 来源 ({len(result.sources)}):")
for i, src in enumerate(result.sources, 1):
print(f" {i}. {src}")
print(f"\n📖 读取的文件 ({len(result.files_read)}):")
for i, f in enumerate(result.files_read, 1):
print(f" {i}. {f}")
if hasattr(result, "confidence"):
print(f"\n✅ 置信度: {result.confidence:.2%}")
print(f"\n⏱️ 耗时: {duration:.2f} 秒")
if __name__ == "__main__":
main()