-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
224 lines (176 loc) · 7.25 KB
/
example_usage.py
File metadata and controls
224 lines (176 loc) · 7.25 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
"""
Example usage of the monolithic and ensemble agents.
This script demonstrates how to use both agents with your own documents and tasks.
Note: Defaults to local Ollama. Set LLM_PROVIDER=gemini to use Gemini.
"""
import os
import json
from pathlib import Path
from typing import List, Dict, Any
from dotenv import load_dotenv
from PyPDF2 import PdfReader
import mlflow
from monolithic import MonolithicAgent
from ensemble import EnsembleAgent
# Load environment variables
load_dotenv()
def load_source_documents(doc_dir: str, pattern: str = "doc*.pdf") -> List[str]:
"""Load source documents (PDF or text) from the specified directory.
Args:
doc_dir: Directory containing source documents
pattern: Glob pattern for filtering files (default: "doc*.pdf" for examples)
"""
documents = []
doc_path = Path(doc_dir)
# Load PDF files matching the pattern
for filepath in sorted(doc_path.glob(pattern)):
if filepath.suffix.lower() == '.pdf':
reader = PdfReader(filepath)
text = ""
for page in reader.pages:
text += page.extract_text() + "\n"
documents.append(text.strip())
elif filepath.suffix.lower() == '.txt':
with open(filepath, "r", encoding="utf-8") as f:
documents.append(f.read())
return documents
def load_tasks(task_file: str) -> List[Dict[str, Any]]:
"""Load synthesis tasks from JSON file."""
with open(task_file, "r", encoding="utf-8") as f:
return json.load(f)
def example_monolithic(documents: List[str], task: str):
"""Example usage of the monolithic agent.
Args:
documents: List of source documents to synthesize
task: Task description for synthesis
"""
print("="*60)
print("MONOLITHIC AGENT EXAMPLE")
print("="*60)
# Initialize agent
agent = MonolithicAgent()
# Run synthesis
print(f"\nSynthesizing {len(documents)} documents...")
print(f"Task: {task[:80]}..." if len(task) > 80 else f"Task: {task}")
with mlflow.start_run(run_name="example_monolithic", tags={"run_type": "example_usage", "agent": "monolithic"}):
mlflow.log_param("agent_type", "monolithic")
mlflow.log_param("task_excerpt", task[:120])
mlflow.log_param("num_documents", len(documents))
result = agent.synthesize(documents, task)
for key, value in result.get("metrics", {}).items():
mlflow.log_metric(key, value)
mlflow.log_text(result.get("output", ""), "example_monolithic_output.txt")
# Display results
print("\n" + "-"*60)
print("OUTPUT:")
print("-"*60)
print(result["output"])
print("\n" + "-"*60)
print("METRICS:")
print("-"*60)
for key, value in result["metrics"].items():
print(f" {key}: {value}")
def example_ensemble(documents: List[str], task: str):
"""Example usage of the ensemble agent.
Args:
documents: List of source documents to synthesize
task: Task description for synthesis
"""
print("\n" + "="*60)
print("ENSEMBLE AGENT EXAMPLE")
print("="*60)
# Initialize agent
agent = EnsembleAgent()
# Run synthesis
print(f"\nSynthesizing {len(documents)} documents with ensemble (3 agents)...")
print(f"Task: {task[:80]}..." if len(task) > 80 else f"Task: {task}")
with mlflow.start_run(run_name="example_ensemble", tags={"run_type": "example_usage", "agent": "ensemble"}):
mlflow.log_param("agent_type", "ensemble")
mlflow.log_param("task_excerpt", task[:120])
mlflow.log_param("num_documents", len(documents))
result = agent.synthesize(documents, task)
# Metrics
for key, value in result.get("metrics", {}).items():
mlflow.log_metric(key, value)
# Intermediate artifacts
intermediate = result.get("intermediate_outputs", {})
if intermediate.get("archived_info"):
mlflow.log_text(str(intermediate["archived_info"]), "example_ensemble_archivist.txt")
if intermediate.get("draft"):
mlflow.log_text(str(intermediate["draft"]), "example_ensemble_draft.txt")
mlflow.log_text(result.get("output", ""), "example_ensemble_final.txt")
# Display results
print("\n" + "-"*60)
print("ARCHIVIST OUTPUT:")
print("-"*60)
print(result["intermediate_outputs"]["archived_info"][:300] + "...")
print("\n" + "-"*60)
print("DRAFTER OUTPUT:")
print("-"*60)
print(result["intermediate_outputs"]["draft"][:300] + "...")
print("\n" + "-"*60)
print("CRITIC (FINAL) OUTPUT:")
print("-"*60)
print(result["output"])
print("\n" + "-"*60)
print("METRICS:")
print("-"*60)
for key, value in result["metrics"].items():
print(f" {key}: {value}")
def check_api_key():
"""Validate required configuration for the selected provider."""
provider = os.getenv("LLM_PROVIDER", "ollama").strip().lower()
if provider != "gemini":
return True
api_key = os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")
if not api_key or api_key == "your_api_key_here":
print("⚠️ WARNING: Gemini API key not configured!")
print("Please set GEMINI_API_KEY (or GOOGLE_API_KEY) in your .env file")
return False
return True
def main():
"""Run example demonstrations."""
print("="*60)
print("Agent Systems Evaluation - Usage Examples")
print("="*60)
print()
if not check_api_key():
print("\nSkipping examples (missing required configuration)")
return
# Configuration for test/example runs
doc_dir = "data/source_documents"
task_file = "data/tasks/example_tasks.json"
doc_pattern = "doc*.pdf" # Use doc*.pdf for examples/testing
try:
# Load documents and tasks
print("\nLoading example documents and tasks...")
documents = load_source_documents(doc_dir, pattern=doc_pattern)
tasks = load_tasks(task_file)
print(f"Loaded {len(documents)} example documents")
print(f"Loaded {len(tasks)} example tasks")
# Configure MLflow for example runs
mlflow.set_tracking_uri("file:./mlruns")
mlflow.set_experiment("examples_demo")
# Run examples with first task
if tasks:
first_task = tasks[0]
task_description = first_task["task_description"]
# Run monolithic example
example_monolithic(documents, task_description)
# Run ensemble example
example_ensemble(documents, task_description)
else:
print("\n⚠️ No tasks found in example_tasks.json")
return
print("\n" + "="*60)
print("Examples completed successfully!")
print("="*60)
except Exception as e:
print(f"\n❌ Error running examples: {e}")
print("\nMake sure you have:")
print(" 1. Valid configuration in .env (or using Ollama locally)")
print(" 2. Installed all requirements: pip install -r requirements.txt")
print(" 3. Example documents (doc*.pdf) in data/source_documents/")
print(" 4. Example tasks in data/tasks/example_tasks.json")
if __name__ == "__main__":
main()