Spaces:
Sleeping
Sleeping
File size: 10,564 Bytes
b840b29 | 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | #!/usr/bin/env python3
"""
Run script for Agentic Defensor.
This script provides multiple ways to interact with the Agentic Defensor system:
1. API mode: Run the FastAPI server to handle queries over HTTP
2. CLI mode: Run a single query from the command line
3. Agent mode: Use the multi-agent system to process a query
4. Interactive mode: Start an interactive session to ask multiple questions
"""
import os
import sys
import json
import argparse
import uvicorn
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def run_api(port):
"""Run the API server."""
print(f"Starting Agentic Defensor API server on port {port}...")
print(f"The API will be available at http://localhost:{port}")
print("Press Ctrl+C to stop the server")
uvicorn.run("src.api.app:app", host="0.0.0.0", port=port, reload=True)
def run_cli(query, top_k, model, output, verbose):
"""Run a query using the standard legal agent."""
from src.main import process_query, save_result
from src.utils.config import CHAT_MODEL
# Ensure model is not None
if model is None:
model = CHAT_MODEL
print(f"Processing query: {query}")
print(f"Using model: {model}")
result = process_query(query, top_k, model)
# Print the answer
print("\n--- Answer ---")
print(result["answer"])
# Print additional information if verbose
if verbose:
print("\n--- Query Information ---")
print(f"Model used: {result['model_used']}")
print(f"Retrieved chunks: {len(result['retrieved_chunks'])}")
# Save the result if output path is provided
if output:
save_result(result, output)
print(f"Results saved to {output}")
def run_agentic(query, top_k, model, output, verbose, debug):
"""Run a query using the multi-agent system."""
from src.agents.agent_director import AgentDirector
from src.utils.config import CHAT_MODEL
# Ensure model is not None
if model is None:
model = CHAT_MODEL
# Initialize the agent director
print("Initializing agent director...")
print(f"Using model: {model}")
if debug:
print("Debug mode enabled: Agent reasoning will be shown")
director = AgentDirector(top_k=top_k, model=model, debug=debug)
# Process the query
print(f"\nProcessing query: {query}")
result = director.process_query(query)
# Display the result
print("\n" + "="*80)
print("QUERY:")
print(query)
print("\nANSWER:")
print(result["answer"])
print("="*80)
# Display processing steps
if verbose:
print("\nPROCESSING STEPS:")
if "query_analysis" in result:
print("1. Query Analysis: Completed")
structured_analysis = result["query_analysis"].get("structured_analysis", "")
if structured_analysis:
print(f" - Extracted structured information from the query")
print(f"2. Retrieved {result.get('num_chunks_retrieved', 0)} document chunks")
if "context_aggregation" in result:
agg = result["context_aggregation"]
print("3. Context Aggregation:")
print(f" - Processed {agg.get('num_raw_content_items', 0)} content items")
print(f" - Organized context: {agg.get('has_organized_content', False)}")
print(f"4. Answer Generation: Completed")
# Save results if requested
if output:
print(f"\nSaving results to {output}...")
with open(output, "w", encoding="utf-8") as f:
json.dump(result, f, ensure_ascii=False, indent=2)
print(f"Results saved successfully.")
def run_interactive(model, top_k, agent_mode, verbose, debug):
"""Run an interactive session with the user."""
from src.agents.agent_director import AgentDirector
from src.agents.legal_agent import LegalAgent
from src.utils.config import CHAT_MODEL
# Ensure model is not None
if model is None:
model = CHAT_MODEL
print("=== Agentic Defensor Interactive Mode ===")
print("Type 'exit', 'quit', or 'q' to end the session.")
print("Type 'help' or '?' for assistance.")
print()
if agent_mode:
print("Using multi-agent system for processing queries.")
if debug:
print("Debug mode enabled: Agent reasoning will be shown")
agent = AgentDirector(top_k=top_k, model=model, debug=debug)
else:
print("Using standard legal agent for processing queries.")
agent = LegalAgent(model=model)
print(f"Using model: {model}")
print(f"Retrieving {top_k} chunks per query")
history = []
while True:
# Get the query from the user
try:
query = input("\nYour query: ").strip()
except (KeyboardInterrupt, EOFError):
print("\nExiting interactive mode.")
break
# Check for exit commands
if query.lower() in ['exit', 'quit', 'q']:
print("Exiting interactive mode.")
break
# Check for help command
if query.lower() in ['help', '?']:
print("\nAgentic Defensor Help:")
print("- Type your legal query and press Enter to get an answer.")
print("- Type 'exit', 'quit', or 'q' to end the session.")
print("- Type 'history' to see your previous queries.")
print("- Type 'save FILENAME' to save the session history to a file.")
continue
# Check for history command
if query.lower() == 'history':
if not history:
print("No history available.")
else:
print("\nQuery History:")
for i, item in enumerate(history, start=1):
print(f"{i}. {item['query']}")
continue
# Check for save command
if query.lower().startswith('save '):
filename = query[5:].strip()
if not filename:
print("Please provide a filename: save FILENAME")
continue
if not history:
print("No history to save.")
continue
try:
with open(filename, 'w', encoding='utf-8') as f:
json.dump(history, f, ensure_ascii=False, indent=2)
print(f"History saved to {filename}")
except Exception as e:
print(f"Error saving history: {e}")
continue
# Skip empty queries
if not query:
continue
# Process the query
print("Processing query...")
try:
if agent_mode:
result = agent.process_query(query)
else:
result = agent.answer_query(query, top_k)
# Store in history
history.append({
'query': query,
'answer': result.get('answer', 'No answer available.')
})
# Display the answer
print("\n--- Answer ---")
print(result.get('answer', 'No answer available.'))
# Print additional information if verbose
if verbose:
if agent_mode and 'num_chunks_retrieved' in result:
print(f"\nRetrieved {result['num_chunks_retrieved']} document chunks")
elif not agent_mode and 'retrieved_chunks' in result:
print(f"\nRetrieved {len(result['retrieved_chunks'])} document chunks")
print(f"Used model: {result.get('model_used', model)}")
except Exception as e:
print(f"Error processing query: {e}")
def main():
"""Main function to parse arguments and run the appropriate mode."""
# Create the top-level parser
parser = argparse.ArgumentParser(description="Agentic Defensor: Legal RAG System")
parser.add_argument('--model', type=str, default=None, help='OpenAI model to use')
parser.add_argument('--verbose', action='store_true', help='Print verbose output')
parser.add_argument('--debug', action='store_true', help='Show agent reasoning steps')
# Create subparsers for different modes
subparsers = parser.add_subparsers(dest='mode', help='Operating mode')
# API mode
api_parser = subparsers.add_parser('api', help='Run the API server')
api_parser.add_argument('--port', type=int, default=8000, help='Port to run the API server on')
# CLI mode
cli_parser = subparsers.add_parser('cli', help='Run a query from the command line')
cli_parser.add_argument('query', type=str, help='The legal query to process')
cli_parser.add_argument('--top-k', type=int, default=200, help='Number of chunks to retrieve')
cli_parser.add_argument('--output', type=str, default=None, help='Output file path for saving the response')
# Agent mode
agent_parser = subparsers.add_parser('agent', help='Run a query using the multi-agent system')
agent_parser.add_argument('query', type=str, help='The legal query to process')
agent_parser.add_argument('--top-k', type=int, default=50, help='Number of chunks to retrieve')
agent_parser.add_argument('--output', type=str, default=None, help='Output file path for saving the response')
# Interactive mode
interactive_parser = subparsers.add_parser('interactive', help='Start an interactive session')
interactive_parser.add_argument('--top-k', type=int, default=50, help='Number of chunks to retrieve')
interactive_parser.add_argument('--agent', action='store_true', help='Use multi-agent system')
# Parse the arguments
args = parser.parse_args()
# Run the appropriate mode
if args.mode == 'api':
run_api(args.port)
elif args.mode == 'cli':
run_cli(args.query, args.top_k, args.model, args.output, args.verbose)
elif args.mode == 'agent':
run_agentic(args.query, args.top_k, args.model, args.output, args.verbose, args.debug)
elif args.mode == 'interactive':
run_interactive(args.model, args.top_k, args.agent, args.verbose, args.debug)
else:
# Default to interactive mode if no mode specified
print("No mode specified, starting interactive mode...\n")
run_interactive(args.model, 50, False, args.verbose, args.debug)
if __name__ == "__main__":
main() |