naman1102 commited on
Commit
4a3e964
·
1 Parent(s): 96ccf55
Files changed (2) hide show
  1. app.py +25 -7
  2. tools.py +28 -0
app.py CHANGED
@@ -11,7 +11,7 @@ from datetime import datetime
11
  from typing import List, Dict, Any, Optional, Annotated
12
  from langgraph.graph import Graph, StateGraph
13
  from langgraph.prebuilt import ToolNode
14
- from tools import simple_search
15
  from openai import OpenAI
16
  from typing_extensions import TypedDict
17
 
@@ -26,6 +26,7 @@ print("trial")
26
  # --- Constants ---
27
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
28
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") # Make sure to set this environment variable
 
29
 
30
  # Create logs directory if it doesn't exist
31
  LOGS_DIR = "question_logs"
@@ -190,11 +191,27 @@ Return ONLY a Python dictionary in this exact format, with no other text or expl
190
  print("\n=== Search Tool ===")
191
  print(f"Search Query: {state['search_query']}")
192
 
193
- # Use the simplified search function
194
- search_results = simple_search(
195
- query=state["search_query"],
196
- max_results=3
197
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
  print("Search Results:")
200
  for i, result in enumerate(search_results, 1):
@@ -204,7 +221,8 @@ Return ONLY a Python dictionary in this exact format, with no other text or expl
204
  state["logs"]["search"] = {
205
  "query": state["search_query"],
206
  "results": search_results,
207
- "timestamp": datetime.now().isoformat()
 
208
  }
209
 
210
  state["history"].append({
 
11
  from typing import List, Dict, Any, Optional, Annotated
12
  from langgraph.graph import Graph, StateGraph
13
  from langgraph.prebuilt import ToolNode
14
+ from tools import simple_search, jina_search_tool
15
  from openai import OpenAI
16
  from typing_extensions import TypedDict
17
 
 
26
  # --- Constants ---
27
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
28
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") # Make sure to set this environment variable
29
+ JINA_API_KEY = os.getenv("JINA_API_KEY") # Get Jina API key from environment
30
 
31
  # Create logs directory if it doesn't exist
32
  LOGS_DIR = "question_logs"
 
191
  print("\n=== Search Tool ===")
192
  print(f"Search Query: {state['search_query']}")
193
 
194
+ # Try Jina search first, fall back to simple search if it fails
195
+ search_results = []
196
+ if JINA_API_KEY:
197
+ try:
198
+ search_results = jina_search_tool(
199
+ query=state["search_query"],
200
+ api_key=JINA_API_KEY
201
+ )
202
+ print("Using Jina search results")
203
+ except Exception as e:
204
+ print(f"Jina search failed: {e}, falling back to simple search")
205
+ search_results = simple_search(
206
+ query=state["search_query"],
207
+ max_results=3
208
+ )
209
+ else:
210
+ print("No Jina API key found, using simple search")
211
+ search_results = simple_search(
212
+ query=state["search_query"],
213
+ max_results=3
214
+ )
215
 
216
  print("Search Results:")
217
  for i, result in enumerate(search_results, 1):
 
221
  state["logs"]["search"] = {
222
  "query": state["search_query"],
223
  "results": search_results,
224
+ "timestamp": datetime.now().isoformat(),
225
+ "search_type": "jina" if JINA_API_KEY and search_results else "simple"
226
  }
227
 
228
  state["history"].append({
tools.py CHANGED
@@ -1,5 +1,7 @@
1
  from typing import List
2
  from duckduckgo_search import DDGS
 
 
3
 
4
  # --- Simple Search Tool ---
5
 
@@ -21,3 +23,29 @@ def simple_search(query: str, max_results: int = 3) -> List[str]:
21
  print("Skipping malformed search result:", r, "Error:", e)
22
 
23
  return results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from typing import List
2
  from duckduckgo_search import DDGS
3
+ import requests
4
+ import os
5
 
6
  # --- Simple Search Tool ---
7
 
 
23
  print("Skipping malformed search result:", r, "Error:", e)
24
 
25
  return results
26
+
27
+ # --- Jina Search Tool ---
28
+
29
+ def jina_search_tool(query: str, api_key: str) -> List[str]:
30
+ """
31
+ Perform a web search using Jina AI's s.jina.ai endpoint and retrieve clean, LLM-friendly content.
32
+ """
33
+ api_endpoint = f"https://s.jina.ai/{query.replace(' ', '+')}"
34
+ headers = {
35
+ "Authorization": f"Bearer {api_key}",
36
+ "Accept": "application/json",
37
+ "User-Agent": "Mozilla/5.0"
38
+ }
39
+
40
+ try:
41
+ response = requests.get(api_endpoint, headers=headers, timeout=10)
42
+ if response.status_code == 200:
43
+ data = response.json()
44
+ contents = [item.get("content", "") for item in data.get("results", [])]
45
+ return contents
46
+ else:
47
+ print(f"Failed to fetch search results: Status code {response.status_code}")
48
+ return []
49
+ except Exception as e:
50
+ print(f"Error fetching search results: {e}")
51
+ return []