harini-012 commited on
Commit
b020a57
·
verified ·
1 Parent(s): 29fa5fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -28
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import json
2
- from langchain_tavily import TavilySearch
3
  from langgraph.prebuilt import create_react_agent
4
  from langgraph_supervisor import create_supervisor
5
  from langchain_groq import ChatGroq
@@ -13,11 +12,12 @@ import pandas as pd
13
  import json
14
  import traceback
15
  import io
16
- import time
 
17
 
18
  # --- Constants ---
19
  load_dotenv()
20
-
21
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
22
  groq_token = os.getenv("GROQ_API_KEY")
23
  tavily_token = os.getenv("TAVILY_API_KEY")
@@ -39,30 +39,20 @@ system_prompt = """
39
  Route to agents/tools when it materially improves accuracy/recency (esp. factual/date/spec/list questions).
40
  Answer directly for stable explanations/ideation. Keep meta minimal.
41
  """
42
-
43
  prompt_search = """
44
- You are web_research_agent.
45
-
46
- You have one tool: web_search.
47
 
48
- IMPORTANT:
49
- When calling web_search use ONLY:
50
 
51
- {
52
- "query": "<search query>"
53
- }
54
 
55
- Do not provide:
56
- topic,
57
- search_depth,
58
- include_images,
59
- time_range,
60
- exclude_domains,
61
- include_domains,
62
- start_date,
63
- end_date.
64
 
65
- Answer using the tool results.
 
 
 
 
66
  """
67
 
68
  prompt_python_execute = """You are a python code execution agent.
@@ -79,11 +69,7 @@ prompt_chess = """You are a chess position reviewing agent.
79
 
80
 
81
  # --- Tools ---
82
- web_search = TavilySearch(
83
- max_results=5,
84
- topic="general",
85
- tavily_api_key=tavily_token
86
- )
87
 
88
  CHESSVISION_TO_FEN_URL = "http://app.chessvision.ai/predict"
89
  CHESS_MOVE_API = "https://chess-api.com/v1"
@@ -112,7 +98,31 @@ def download_file_as_string(task_id: str) -> str:
112
  else:
113
  raise Exception(f"Failed to download the file. Status code: {response.status_code}")
114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
 
 
 
 
116
  from langchain_core.tools import tool
117
 
118
 
@@ -223,7 +233,7 @@ groq_llm = ChatGroq(
223
  max_retries=2,
224
  api_key=groq_token,
225
  )
226
-
227
  # --- Agents ---
228
  web_research_agent = create_react_agent(
229
  model=groq_llm,
 
1
  import json
 
2
  from langgraph.prebuilt import create_react_agent
3
  from langgraph_supervisor import create_supervisor
4
  from langchain_groq import ChatGroq
 
12
  import json
13
  import traceback
14
  import io
15
+ from tavily import TavilyClient
16
+ from langchain_core.tools import tool
17
 
18
  # --- Constants ---
19
  load_dotenv()
20
+ client = TavilyClient(api_key=os.getenv("TAVILY_API_KEY"))
21
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
22
  groq_token = os.getenv("GROQ_API_KEY")
23
  tavily_token = os.getenv("TAVILY_API_KEY")
 
39
  Route to agents/tools when it materially improves accuracy/recency (esp. factual/date/spec/list questions).
40
  Answer directly for stable explanations/ideation. Keep meta minimal.
41
  """
 
42
  prompt_search = """
43
+ You are a web research agent.
 
 
44
 
45
+ You have one tool:
 
46
 
47
+ web_search(query)
 
 
48
 
49
+ Always use the tool when factual information is required.
 
 
 
 
 
 
 
 
50
 
51
+ After receiving search results:
52
+ - Extract the answer.
53
+ - Return only the final answer.
54
+ - If multiple sources disagree, choose the most reliable source.
55
+ - If the answer is numeric, return only the number.
56
  """
57
 
58
  prompt_python_execute = """You are a python code execution agent.
 
69
 
70
 
71
  # --- Tools ---
72
+
 
 
 
 
73
 
74
  CHESSVISION_TO_FEN_URL = "http://app.chessvision.ai/predict"
75
  CHESS_MOVE_API = "https://chess-api.com/v1"
 
98
  else:
99
  raise Exception(f"Failed to download the file. Status code: {response.status_code}")
100
 
101
+ @tool
102
+ def web_search(query: str) -> str:
103
+ """
104
+ Search the web and return relevant results.
105
+ """
106
+ try:
107
+ result = client.search(
108
+ query=query,
109
+ max_results=5,
110
+ search_depth="advanced"
111
+ )
112
+
113
+ snippets = []
114
+
115
+ for item in result.get("results", []):
116
+ snippets.append(
117
+ f"Title: {item.get('title','')}\n"
118
+ f"Content: {item.get('content','')}\n"
119
+ f"URL: {item.get('url','')}\n"
120
+ )
121
 
122
+ return "\n\n".join(snippets)
123
+
124
+ except Exception as e:
125
+ return f"ERROR: {str(e)}"
126
  from langchain_core.tools import tool
127
 
128
 
 
233
  max_retries=2,
234
  api_key=groq_token,
235
  )
236
+ print("Registered tool:", web_search.name)
237
  # --- Agents ---
238
  web_research_agent = create_react_agent(
239
  model=groq_llm,