Final_Assignment_Template / multiple_tools.py
cowrycode's picture
Update multiple_tools.py
212ef46 verified
raw
history blame
1.94 kB
import os
import json
from dotenv import load_dotenv
from llama_index.core.tools import FunctionTool
from llama_index.tools.google import GoogleSearchToolSpec
from llama_index.tools.wikipedia import WikipediaToolSpec
load_dotenv()
google_key = os.getenv("GOOGLE_SECRET_KEY")
my_search_engine = os.getenv("Google_WebSearch_Engine")
g_search = GoogleSearchToolSpec(key=google_key, engine=my_search_engine, num=3)
#Wikipedia Search Tool
wikipedia_tool = WikipediaToolSpec()
wikipedia_search_tool = FunctionTool.from_defaults(wikipedia_tool.search_data)
def google_web_search(query : str) -> str:
"""
Searches the web and returns the most accurate response for a user query.
Args:
query (str): The query string to search for.
Returns:
str: The snippet of the first search result along with its source link.
"""
result = g_search.google_search(query)
# data = result.content
if isinstance(result, str):
import ast
result = ast.literal_eval(result)
first = result[0]
snippet = first["snippet"]
link = first["link"]
return f"Result: {snippet} Source: {link}"
google_web_search_tool = FunctionTool.from_defaults(google_web_search)
def round_to_two_decimals(value):
"""
Round a number to two decimal places.
Args:
value (float): The value to be round to 2 decimal places.
Raises:
ValueError: If the 'value' is not an integer or a float.
"""
return round(float(value), 2)
round_to_two_decimals_tool = FunctionTool.from_defaults(round_to_two_decimals)
def text_inverter(text: str) -> str:
"""
Handles sentence writen backward:
- Reverses it and returns the reverse version
- Ignore if text is not written backwords
Args:
text (str): The text writen backwards to be reversed
"""
decoded = text[::-1]
print(decoded)
text_inverter_tool = FunctionTool.from_defaults(text_inverter)