Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ from typing import List, Dict, Any, Annotated
|
|
10 |
from langgraph.graph import Graph, StateGraph
|
11 |
from typing_extensions import TypedDict
|
12 |
from openai import OpenAI
|
|
|
13 |
|
14 |
# -------------------------
|
15 |
# Utility helpers
|
@@ -42,32 +43,37 @@ def jina_search_tool(query: str, api_key: str, max_results: int = 5) -> List[str
|
|
42 |
print(f"Query: {query}")
|
43 |
print(f"Max Results: {max_results}")
|
44 |
|
45 |
-
|
46 |
-
print(f"
|
47 |
|
48 |
headers = {
|
49 |
"Authorization": f"Bearer {api_key}",
|
50 |
"Accept": "application/json",
|
51 |
-
"User-Agent": "Mozilla/5.0"
|
52 |
}
|
53 |
print("Headers:", {k: v[:10] + "..." if k == "Authorization" else v for k, v in headers.items()})
|
54 |
|
55 |
try:
|
56 |
print("\nSending request to Jina API...")
|
57 |
-
|
58 |
-
print(f"Response Status: {
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
raise RuntimeError(f"Jina search failed with status {resp.status_code}: {resp.text[:200]}")
|
63 |
|
64 |
-
data = resp.json()
|
65 |
print("\nResponse Data Structure:")
|
66 |
print(f"Keys in response: {list(data.keys())}")
|
67 |
print(f"Number of results: {len(data.get('results', []))}")
|
68 |
|
69 |
-
results = [
|
|
|
|
|
|
|
|
|
70 |
print(f"\nRetrieved {len(results)} results")
|
|
|
|
|
|
|
71 |
|
72 |
if not results:
|
73 |
print("WARNING: No results found in the response")
|
@@ -83,7 +89,7 @@ def jina_search_tool(query: str, api_key: str, max_results: int = 5) -> List[str
|
|
83 |
raise
|
84 |
except json.JSONDecodeError as e:
|
85 |
print(f"ERROR: Failed to parse JSON response: {str(e)}")
|
86 |
-
print(f"Raw response: {
|
87 |
raise
|
88 |
except Exception as e:
|
89 |
print(f"ERROR: Unexpected error: {str(e)}")
|
|
|
10 |
from langgraph.graph import Graph, StateGraph
|
11 |
from typing_extensions import TypedDict
|
12 |
from openai import OpenAI
|
13 |
+
from urllib.parse import quote_plus
|
14 |
|
15 |
# -------------------------
|
16 |
# Utility helpers
|
|
|
43 |
print(f"Query: {query}")
|
44 |
print(f"Max Results: {max_results}")
|
45 |
|
46 |
+
url = f"https://s.jina.ai/?q={quote_plus(query)}"
|
47 |
+
print(f"URL: {url}")
|
48 |
|
49 |
headers = {
|
50 |
"Authorization": f"Bearer {api_key}",
|
51 |
"Accept": "application/json",
|
52 |
+
"User-Agent": "Mozilla/5.0"
|
53 |
}
|
54 |
print("Headers:", {k: v[:10] + "..." if k == "Authorization" else v for k, v in headers.items()})
|
55 |
|
56 |
try:
|
57 |
print("\nSending request to Jina API...")
|
58 |
+
r = requests.get(url, headers=headers, timeout=15)
|
59 |
+
print(f"Response Status: {r.status_code}")
|
60 |
|
61 |
+
r.raise_for_status()
|
62 |
+
data = r.json()
|
|
|
63 |
|
|
|
64 |
print("\nResponse Data Structure:")
|
65 |
print(f"Keys in response: {list(data.keys())}")
|
66 |
print(f"Number of results: {len(data.get('results', []))}")
|
67 |
|
68 |
+
results = [
|
69 |
+
f"{item['title']} – {item['url']}\n{item['content']}"
|
70 |
+
for item in data.get("results", [])[:max_results]
|
71 |
+
]
|
72 |
+
|
73 |
print(f"\nRetrieved {len(results)} results")
|
74 |
+
if results:
|
75 |
+
print("\nFirst result preview:")
|
76 |
+
print(results[0][:200] + "..." if len(results[0]) > 200 else results[0])
|
77 |
|
78 |
if not results:
|
79 |
print("WARNING: No results found in the response")
|
|
|
89 |
raise
|
90 |
except json.JSONDecodeError as e:
|
91 |
print(f"ERROR: Failed to parse JSON response: {str(e)}")
|
92 |
+
print(f"Raw response: {r.text[:500]}")
|
93 |
raise
|
94 |
except Exception as e:
|
95 |
print(f"ERROR: Unexpected error: {str(e)}")
|