sarath2803 commited on
Commit
be0a6bb
·
verified ·
1 Parent(s): d60b2f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -10
app.py CHANGED
@@ -11,7 +11,9 @@ from typing import TypedDict, List, Dict, Any, Optional,Callable
11
  from langgraph.graph import StateGraph, END
12
  from langchain_openai import ChatOpenAI
13
  from langchain_core.messages import HumanMessage
 
14
  from langchain_community.document_loaders import WikipediaLoader
 
15
  from youtube_transcript_api import YouTubeTranscriptApi
16
 
17
  # (Keep Constants as is)
@@ -87,19 +89,45 @@ def divide(a:int,b:int)->float:
87
  raise ValueError("Division by zero is not allowed.")
88
  return a / b
89
 
 
 
 
 
 
 
 
 
 
 
90
  search_tool = DuckDuckGoSearchTool()
91
 
92
  @tool
93
  def web_search(query: str) -> str:
94
- """
95
- Performs a web search for the given query.
96
  Args:
97
- query (str): The search query.
98
- Returns:
99
- str: The search results as a string.
100
- """
101
- result=search_tool(query)
102
- return f"Search results for '{query}' : {result}."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  @tool
105
  def wikipedia_tool(query: str) -> str:
@@ -163,7 +191,7 @@ class BasicAgent:
163
  token=os.environ["OPENROUTER_API_KEY"]
164
  self.system_prompt= """You are an expert assistant with knowledge about everything.
165
  You will be given a task to do and you need to do it with the help of tools provied.
166
- if you are asked about a person or anything related to a work you can use the web_search tool or the wikipedia_tool to come up with the most suitable answer.
167
  If you are given any mathematical tasks you can use the add,subtract,multiply and divide tools provided.
168
  If you are given a youtube video link use the youtube_transcript_tool to transcibe the audio and to find the answer of the query being asked.
169
  If you are asked something related to how many ,use the web_search tool to directly find the answer ,if not available use wikipedia_tool and use your senses to find out the answer yourself and always return a number to such queries.
@@ -182,7 +210,7 @@ class BasicAgent:
182
  provider="openrouter" # Explicitly set to openrouter
183
  )
184
  self.agent= CodeAgent(
185
- tools = [add, subtract, multiply, divide, web_search, image_generation_tool,youtube_transcript_tool, wikipedia_tool],
186
  model=model,
187
  )
188
  def __call__(self, question: str, context: str = "") -> str:
 
11
  from langgraph.graph import StateGraph, END
12
  from langchain_openai import ChatOpenAI
13
  from langchain_core.messages import HumanMessage
14
+ from langchain_community.tools.tavily_search import TavilySearchResults
15
  from langchain_community.document_loaders import WikipediaLoader
16
+ from langchain_community.document_loaders import ArxivLoader
17
  from youtube_transcript_api import YouTubeTranscriptApi
18
 
19
  # (Keep Constants as is)
 
89
  raise ValueError("Division by zero is not allowed.")
90
  return a / b
91
 
92
+ @tool
93
+ def modulus(a: int, b: int) -> int:
94
+ """Get the modulus of two numbers.
95
+
96
+ Args:
97
+ a: first int
98
+ b: second int
99
+ """
100
+ return a % b
101
+
102
  search_tool = DuckDuckGoSearchTool()
103
 
104
  @tool
105
  def web_search(query: str) -> str:
106
+ """Search Tavily for a query and return maximum 3 results.
107
+
108
  Args:
109
+ query: The search query."""
110
+ search_docs = TavilySearchResults(max_results=3).invoke(query=query)
111
+ formatted_search_docs = "\n\n---\n\n".join(
112
+ [
113
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
114
+ for doc in search_docs
115
+ ])
116
+ return {"web_results": formatted_search_docs}
117
+
118
+ @tool
119
+ def arvix_search(query: str) -> str:
120
+ """Search Arxiv for a query and return maximum 3 result.
121
+
122
+ Args:
123
+ query: The search query."""
124
+ search_docs = ArxivLoader(query=query, load_max_docs=3).load()
125
+ formatted_search_docs = "\n\n---\n\n".join(
126
+ [
127
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
128
+ for doc in search_docs
129
+ ])
130
+ return {"arvix_results": formatted_search_docs}
131
 
132
  @tool
133
  def wikipedia_tool(query: str) -> str:
 
191
  token=os.environ["OPENROUTER_API_KEY"]
192
  self.system_prompt= """You are an expert assistant with knowledge about everything.
193
  You will be given a task to do and you need to do it with the help of tools provied.
194
+ if you are asked about a person or anything related to a work you can use the web_search tool or the wikipedia_tool or arvix_search tool to come up with the most suitable answer.
195
  If you are given any mathematical tasks you can use the add,subtract,multiply and divide tools provided.
196
  If you are given a youtube video link use the youtube_transcript_tool to transcibe the audio and to find the answer of the query being asked.
197
  If you are asked something related to how many ,use the web_search tool to directly find the answer ,if not available use wikipedia_tool and use your senses to find out the answer yourself and always return a number to such queries.
 
210
  provider="openrouter" # Explicitly set to openrouter
211
  )
212
  self.agent= CodeAgent(
213
+ tools = [add, subtract, multiply, divide,modulus,arvix_search, web_search, image_generation_tool,youtube_transcript_tool, wikipedia_tool],
214
  model=model,
215
  )
216
  def __call__(self, question: str, context: str = "") -> str: