Spaces:
Sleeping
Sleeping
File size: 1,942 Bytes
b70a23a 212ef46 b70a23a 212ef46 b70a23a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
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)
|