-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_quickstart.py
More file actions
132 lines (111 loc) · 4.82 KB
/
test_quickstart.py
File metadata and controls
132 lines (111 loc) · 4.82 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
"""QUICKSTARTドキュメントの動作確認テスト"""
import os
import sys
# API Keyを設定(環境変数から取得、なければデフォルト値)
if "GENFLUX_API_KEY" not in os.environ:
os.environ["GENFLUX_API_KEY"] = "dev_test_key_12345"
def main():
"""メイン処理."""
try:
from genflux import GenFlux
print("="*70)
print(" QUICKSTART動作確認テスト")
print("="*70)
# クライアント初期化
print("\n1. クライアント初期化...")
client = GenFlux(base_url="http://localhost:9000/api/v1/external")
print(" ✅ クライアント初期化成功")
# Config一覧取得
print("\n2. Config一覧取得...")
try:
configs = client.configs.list()
print(f" ✅ Config一覧取得成功: {len(configs.configs)}件")
if configs.configs:
print(f" 最初のConfig: {configs.configs[0].name}")
config_id = str(configs.configs[0].id)
else:
print(" ⚠️ Configが存在しないため、作成します...")
from genflux.models.config import ConfigCreate
config = client.configs.create(
ConfigCreate( # type: ignore[call-arg]
name="Test RAG API",
api_endpoint="https://api.example.com/chat",
auth_type="bearer_token",
auth_token="test_token",
request_format={
"method": "POST",
"body_template": {"query": "{{prompt}}"}
},
response_format={"response_path": "answer"}
)
)
config_id = str(config.id)
print(f" ✅ Config作成成功: {config.name}")
except Exception as e:
print(f" ❌ Config操作失敗: {e}")
return 1
# 評価実行(デフォルトConfigを使用する場合)
print("\n3. 評価実行(デフォルトConfig使用)...")
try:
evaluator = client.evaluation(config_id) # まずは明示的にconfig_idを指定
result = evaluator.faithfulness(
question="What is Python?",
answer="Python is a programming language.",
contexts=["Python is a high-level programming language."],
on_progress=lambda x: None # プログレスバー非表示
)
print(" ✅ 評価成功!")
print(f" Score: {result.score}")
print(f" Reason: {result.reason}")
print(f" Engine: {result.engine}")
# スコア判定
if result.score >= 0.7:
print(" 判定: ✅ 高品質")
else:
print(" 判定: ⚠️ 要改善")
except Exception as e:
print(f" ❌ 評価失敗: {e}")
import traceback
traceback.print_exc()
return 1
# 複数メトリック評価
print("\n4. 複数メトリック評価...")
metrics_to_test = [
("answer_relevancy", "Answer Relevancy"),
("contextual_relevancy", "Contextual Relevancy"),
]
for metric_key, metric_name in metrics_to_test:
try:
method = getattr(evaluator, metric_key)
result = method(
question="What is Python?",
answer="Python is a programming language.",
contexts=["Python is a high-level programming language."],
)
print(f" ✅ {metric_name}: {result.score:.2f}")
except Exception as e:
print(f" ⚠️ {metric_name}: スキップ ({e})")
# 成功
print("\n" + "="*70)
print("🎉 全てのテストが成功しました!")
print("="*70)
print("\nドキュメントの手順は正しく動作します。")
print("次のステップ:")
print(" - docs/QUICKSTART.md で簡単な使い方を学ぶ")
print(" - docs/WORKFLOW.md で本格的な使い方を学ぶ")
print(" - docs/API_REFERENCE.md で機能全体像を確認する")
return 0
except ImportError as e:
print(f"❌ SDKのインポートに失敗しました: {e}")
print("\n以下のコマンドでSDKをインストールしてください:")
print(" cd genflux-python-sdk")
print(" pip install -e .")
return 1
except Exception as e:
print(f"❌ 予期しないエラー: {e}")
import traceback
traceback.print_exc()
return 1
if __name__ == "__main__":
sys.exit(main())