-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutils.py
More file actions
75 lines (61 loc) · 2.53 KB
/
utils.py
File metadata and controls
75 lines (61 loc) · 2.53 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
#!/usr/bin/env python3
"""
Utility functions for the Deep Research Agent
This module contains shared utility functions that can be imported
by multiple modules without creating circular dependencies.
"""
import asyncio
import logging
import os
from datetime import datetime
from typing import List, Optional
import aiohttp
from pydantic import BaseModel
# Setup logging
logger = logging.getLogger(__name__)
class SearchResult(BaseModel):
"""Individual search result from Jina AI"""
title: str
url: str
content: str
description: Optional[str] = None
published_time: Optional[datetime] = None
relevance: float = 0.0 # Add relevance field for compatibility
class JinaSearchResponse(BaseModel):
"""Complete response from Jina AI search"""
results: List[SearchResult] = []
total_results: int = 0
query_used: str = ""
async def search_jina_ai(query: str) -> JinaSearchResponse:
"""Search using Jina AI and return structured results."""
try:
api_key = os.getenv("JINA_API_KEY")
if not api_key:
logger.warning("JINA_API_KEY not found, returning empty results")
return JinaSearchResponse(query_used=query)
url = "https://s.jina.ai/"
headers = {"Authorization": f"Bearer {api_key}", "Accept": "application/json"}
async with aiohttp.ClientSession() as session:
async with session.get(f"{url}{query}", headers=headers) as response:
if response.status == 200:
data = await response.json()
results = []
for item in data.get("data", [])[:10]: # Limit to 10 results
results.append(
SearchResult(
title=item.get("title", ""),
url=item.get("url", ""),
content=item.get("content", ""),
description=item.get("description", ""),
relevance=0.8, # Default relevance score
)
)
return JinaSearchResponse(
results=results, total_results=len(results), query_used=query
)
else:
logger.error(f"Jina AI search failed: {response.status}")
return JinaSearchResponse(query_used=query)
except Exception as e:
logger.error(f"Jina AI search error: {e}")
return JinaSearchResponse(query_used=query)