-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathllamaindex_eval.py
More file actions
234 lines (195 loc) · 7.85 KB
/
Copy pathllamaindex_eval.py
File metadata and controls
234 lines (195 loc) · 7.85 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
LlamaIndex Agent Evaluation
-----------------------------
Covers two modes selectable via LLAMA_MODE env var:
LLAMA_MODE=rag (default) — VectorStore RAG query engine with token tracking
LLAMA_MODE=agent — ReAct agent with tool use and step-level tracing
Run:
pip install llama-index llama-index-llms-openai
AGENTX_API_KEY=key OPENAI_API_KEY=sk-... python examples/evaluations/llamaindex_eval.py
AGENTX_API_KEY=key OPENAI_API_KEY=sk-... LLAMA_MODE=agent python examples/evaluations/llamaindex_eval.py
"""
import os
from agentx import AgentX
from agentx.evaluations.models import EvaluationCase
# ---------------------------------------------------------------------------
# Mode: RAG query engine
# ---------------------------------------------------------------------------
def build_rag_engine():
try:
from llama_index.core import VectorStoreIndex, Document
from llama_index.llms.openai import OpenAI
documents = [
Document(
text="Our refund policy allows full refunds within 30 days of purchase. Contact support@example.com."
),
Document(
text="We support credit cards, PayPal, and bank transfers as payment methods."
),
Document(
text="Team plans support up to 50 seats. Contact sales for enterprise pricing."
),
Document(
text="Technical support is available 24/7 via live chat and email."
),
Document(
text="To export data go to Settings → Data → Export. CSV and JSON formats are available."
),
Document(
text="Free trial is 14 days, no credit card required. Upgrade anytime from the billing page."
),
]
llm = OpenAI(model="gpt-4o-mini", temperature=0)
index = VectorStoreIndex.from_documents(documents)
engine = index.as_query_engine(llm=llm, similarity_top_k=2)
return engine
except ImportError:
return None
def make_rag_fn(engine):
def eval_subject(case: EvaluationCase) -> dict:
if engine is None:
return {
"output": f"[stub] RAG response to: {case.query}",
"metadata": {"framework": "llamaindex", "mode": "rag"},
}
response = engine.query(case.query)
# Token counts from the raw LLM response
raw = getattr(response, "raw", None) or {}
usage = raw.get("usage", {}) if isinstance(raw, dict) else {}
input_tokens = usage.get("prompt_tokens")
output_tokens = usage.get("completion_tokens")
# Trace: sources used
source_nodes = getattr(response, "source_nodes", [])
trace_events = []
for node in source_nodes:
trace_events.append(
{
"type": "retrieval",
"name": "vector_search",
"summary": (
f"score={node.score:.3f} text={node.text[:80]}…"
if hasattr(node, "score")
else node.text[:80]
),
}
)
return {
"output": str(response),
"input_tokens": input_tokens,
"output_tokens": output_tokens,
"trace": {"events": trace_events} if trace_events else None,
"metadata": {
"framework": "llamaindex",
"mode": "rag",
"sources_used": len(source_nodes),
},
}
return eval_subject
# ---------------------------------------------------------------------------
# Mode: ReAct agent with tools
# ---------------------------------------------------------------------------
def build_react_agent():
try:
from llama_index.core.agent import ReActAgent
from llama_index.core.tools import FunctionTool
from llama_index.llms.openai import OpenAI
_POLICY_DB = {
"refund": "Full refunds within 30 days. Email support@example.com.",
"payment": "We accept Visa, Mastercard, PayPal, and bank transfer.",
"support": "24/7 live chat and email support available.",
"export": "Export via Settings → Data → Export (CSV/JSON).",
"trial": "14-day free trial, no credit card required.",
}
def lookup_policy(topic: str) -> str:
"""Look up company policies by topic."""
for key, val in _POLICY_DB.items():
if key in topic.lower():
return val
return "No specific policy found. Please contact support."
tool = FunctionTool.from_defaults(fn=lookup_policy)
llm = OpenAI(model="gpt-4o-mini", temperature=0)
agent = ReActAgent.from_tools([tool], llm=llm, verbose=False)
return agent
except ImportError:
return None
def make_agent_fn(agent):
def eval_subject(case: EvaluationCase) -> dict:
if agent is None:
return {
"output": f"[stub] Agent response to: {case.query}",
"metadata": {"framework": "llamaindex", "mode": "agent"},
}
response = agent.chat(case.query)
output = str(response)
# Extract tool steps from agent sources
trace_events = []
sources = getattr(response, "sources", [])
for source in sources:
tool_name = getattr(source, "tool_name", "unknown_tool")
content = str(getattr(source, "content", ""))[:100]
trace_events.append(
{"type": "tool_call", "name": tool_name, "summary": content}
)
return {
"output": output,
"trace": {"events": trace_events} if trace_events else None,
"metadata": {
"framework": "llamaindex",
"mode": "agent",
"model": "gpt-4o-mini",
},
}
return eval_subject
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main():
client = AgentX.from_env()
mode = os.getenv("LLAMA_MODE", "rag").lower()
if mode == "agent":
runner = build_react_agent()
eval_fn = make_agent_fn(runner)
display = "LlamaIndex ReAct Agent"
else:
runner = build_rag_engine()
eval_fn = make_rag_fn(runner)
display = "LlamaIndex RAG Engine"
dataset = (
client.evaluations.datasets.builder(
name=f"LlamaIndex Agent Dataset ({mode})",
description="Evaluates a LlamaIndex agent on product documentation queries.",
number_of_requests=2,
acceptance_criteria="Answers must be grounded in the provided documents.",
rejection_criteria="No answers that contradict or go beyond the source documents.",
)
.add_case(
query="What payment methods do you accept?",
expected_results="List all supported payment methods as described in the docs.",
)
.add_case(
query="Is there a free trial and do I need a credit card?",
expected_results="Confirm trial length and whether a credit card is required.",
)
.add_case(
query="How do I export my data?",
expected_results="Explain the export steps and available formats.",
)
.publish()
)
report = (
client.evaluations.run(
dataset_id=dataset.id,
subject={
"kind": "custom_agent",
"displayName": display,
"framework": "llamaindex",
"runtime": "local",
},
)
.execute(eval_fn)
.finalize()
.analyze()
)
print(f"\nDashboard: {report.dashboard_url}")
if __name__ == "__main__":
main()