Spaces:
Starting
Starting
File size: 8,946 Bytes
751d628 c6951f4 751d628 c6951f4 751d628 |
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 |
import asyncio
import os
import logging
import tempfile
from pathlib import Path
from app import JARVISAgent, llm_client, llm_type, llm_model, embedder
from tools.search import search_tool, multi_hop_search_tool
from tools.file_parser import file_parser_tool
from tools.image_parser import image_parser_tool
from tools.calculator import calculator_tool
from tools.document_retriever import document_retriever_tool
from tools.duckduckgo_search import duckduckgo_search_tool
from tools.weather_info import weather_info_tool
from tools.hub_stats import hub_stats_tool
from tools.guest_info import guest_info_retriever_tool
from tools.file_fetcher import fetch_task_file
from tools.answer_generator import preprocess_question, filter_results
from state import validate_state, reset_state, JARVISState
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
async def test_tools():
"""Test all tools."""
logger.info("Testing Search Tool (SerpAPI)...")
try:
if not os.getenv("SERPAPI_API_KEY"):
logger.warning("Search Warning: SERPAPI_API_KEY not set")
else:
result = await search_tool.ainvoke({"query": "What is the capital of France?"})
logger.info(f"Search Result: {result}")
except Exception as e:
logger.error(f"Search Error: {e}")
logger.info("Testing Multi-Hop Search Tool...")
try:
result = await multi_hop_search_tool.ainvoke({
"query": "What is the population of France's capital?",
"steps": 2,
"llm_client": llm_client,
"llm_type": llm_type,
"llm_model": llm_model
})
logger.info(f"Multi-Hop Search Result: {result}")
except Exception as e:
logger.error(f"Multi-Hop Search Error: {e}")
logger.info("Testing DuckDuckGo Search Tool...")
try:
result = await duckduckgo_search_tool.ainvoke({
"query": "What is the capital of France?",
"original_query": "What is the capital of France?",
"embedder": embedder
})
logger.info(f"DuckDuckGo Result: {result}")
except Exception as e:
logger.error(f"DuckDuckGo Error: {e}")
logger.info("Testing Weather Info Tool...")
try:
if not os.getenv("OPENWEATHERMAP_API_KEY"):
logger.warning("Weather Warning: OPENWEATHERMAP_API_KEY not set")
else:
result = await weather_info_tool.ainvoke({"location": "London"})
logger.info(f"Weather Result: {result}")
except Exception as e:
logger.error(f"Weather Error: {e}")
logger.info("Testing Document Retriever Tool...")
try:
from PyPDF2 import PdfWriter
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tmp:
writer = PdfWriter()
from PyPDF2.generic import NameObject, create_string_object
page = writer.add_blank_page(width=72, height=72)
page[NameObject("/Contents")] = create_string_object("Sample document content for testing.")
writer.write(tmp)
tmp_path = tmp.name
result = await document_retriever_tool.ainvoke({
"task_id": "test_task",
"query": "Sample question",
"file_path": tmp_path
})
logger.info(f"Document Retriever Result: {result}")
os.unlink(tmp_path)
except Exception as e:
logger.error(f"Document Retriever Error: {e}")
logger.info("Testing Image Parser Tool...")
try:
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
# Create a minimal PNG (1x1 pixel)
from PIL import Image
img = Image.new('RGB', (1, 1), color='white')
img.save(tmp.name, 'PNG')
tmp_path = tmp.name
result = await image_parser_tool.ainvoke({"task_id": "test_task", "file_path": tmp_path})
logger.info(f"Image Parser Result: {result}")
os.unlink(tmp_path)
except Exception as e:
logger.error(f"Image Parser Error: {e}")
logger.info("Testing File Parser Tool...")
try:
with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as tmp:
tmp.write(b"Sample text file content")
tmp_path = tmp.name
result = await file_parser_tool.ainvoke({
"task_id": "test_task",
"file_type": "txt",
"file_path": tmp_path,
"query": "What is in the file?"
})
logger.info(f"File Parser Result: {result}")
os.unlink(tmp_path)
except Exception as e:
logger.error(f"File Parser Error: {e}")
logger.info("Testing Calculator Tool...")
try:
result = await calculator_tool.ainvoke({"expression": "2 + 2"})
logger.info(f"Calculator Result: {result}")
except Exception as e:
logger.error(f"Calculator Error: {e}")
logger.info("Testing Hub Stats Tool...")
try:
if not os.getenv("HUGGINGFACEHUB_API_TOKEN"):
logger.warning("Hub Stats Warning: HUGGINGFACEHUB_API_TOKEN not set")
else:
result = await hub_stats_tool.ainvoke({"author": "meta-llama"})
logger.info(f"Hub Stats Result: {result}")
except Exception as e:
logger.error(f"Hub Stats Error: {e}")
logger.info("Testing Guest Info Retriever Tool...")
try:
result = await guest_info_retriever_tool.ainvoke({"query": "Who is the guest named John?"})
logger.info(f"Guest Info Result: {result}")
except Exception as e:
logger.error(f"Guest Info Error: {e}")
async def test_file_fetcher():
"""Test file fetcher."""
logger.info("Testing File Fetcher...")
try:
result = await fetch_task_file("8e867cd7-cff9-4e6c-867a-ff5ddc2550be", "Sample question with data")
logger.info(f"File Fetcher Result: {result}")
except Exception as e:
logger.error(f"File Fetcher Error: {e}")
async def test_answer_generator():
"""Test answer generator functions."""
logger.info("Testing Preprocess Question...")
try:
result = await preprocess_question("What's the weather in Paris?")
logger.info(f"Preprocess Question Result: {result}")
except Exception as e:
logger.error(f"Preprocess Question Error: {e}")
logger.info("Testing Filter Results...")
try:
results = ["Paris is the capital of France.", "Florida is a state.", "Paris is in Texas."]
filtered = filter_results(results, "What is the capital of France?")
logger.info(f"Filter Results: {filtered}")
except Exception as e:
logger.error(f"Filter Results Error: {e}")
async def test_state_management():
"""Test state management functions."""
logger.info("Testing Reset State...")
try:
state = reset_state("test_task", "What is the capital of France?")
logger.info(f"Reset State Result: {state}")
except Exception as e:
logger.error(f"Reset State Error: {e}")
logger.info("Testing Validate State...")
try:
invalid_state = {"task_id": "", "question": ""}
validate_state(invalid_state)
logger.error("Validate State should have failed")
except ValueError as e:
logger.info(f"Validate State Error (expected): {e}")
try:
valid_state = reset_state("test_task", "Sample question")
validated = validate_state(valid_state)
logger.info(f"Validate State Result: {validated}")
except Exception as e:
logger.error(f"Validate State Error: {e}")
async def test_agent():
"""Test JARVISAgent with various cases."""
logger.info("Testing JARVISAgent (Simple Question)...")
try:
agent = JARVISAgent()
answer = await agent.process_question("test_task", "What is the capital of France?")
logger.info(f"JARVISAgent Answer: {answer}")
except Exception as e:
logger.error(f"JARVISAgent Error: {e}")
logger.info("Testing JARVISAgent (Edge Case: Empty Question)...")
try:
agent = JARVISAgent()
answer = await agent.process_question("test_task", "")
logger.info(f"JARVISAgent Empty Question Answer: {answer}")
except Exception as e:
logger.info(f"JARVISAgent Empty Question Error (expected): {e}")
async def main():
required_envs = [
"HUGGINGFACEHUB_API_TOKEN",
"TOGETHER_API_KEY",
"OPENWEATHERMAP_API_KEY",
"SERPAPI_API_KEY"
]
for env in required_envs:
if not os.getenv(env):
logger.warning(f"{env} not set, some tools may fail")
await test_tools()
await test_file_fetcher()
await test_answer_generator()
await test_state_management()
await test_agent()
if __name__ == "__main__":
try:
asyncio.run(main())
except Exception as e:
logger.error(f"Test script failed: {e}") |