cowrycode commited on
Commit
b70a23a
·
verified ·
1 Parent(s): 3fe7267

Update multiple_tools.py

Browse files
Files changed (1) hide show
  1. multiple_tools.py +58 -0
multiple_tools.py CHANGED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ from dotenv import load_dotenv
4
+ from llama_index.core.tools import FunctionTool
5
+ from llama_index.tools.google import GoogleSearchToolSpec
6
+
7
+ load_dotenv()
8
+ google_key = os.getenv("GOOGLE_SECRET_KEY")
9
+ my_search_engine = os.getenv("Google_WebSearch_Engine")
10
+
11
+ g_search = GoogleSearchToolSpec(key=google_key, engine=my_search_engine, num=3)
12
+
13
+ def google_web_search(query : str) -> str:
14
+ """
15
+ Searches the web and returns the most accurate response for a user query.
16
+
17
+ Args:
18
+ query (str): The query string to search for.
19
+
20
+ Returns:
21
+ str: The snippet of the first search result along with its source link.
22
+ """
23
+ result = g_search.google_search(query)
24
+ # data = result.content
25
+ if isinstance(result, str):
26
+ import ast
27
+ result = ast.literal_eval(result)
28
+ first = result[0]
29
+ snippet = first["snippet"]
30
+ link = first["link"]
31
+ return f"Result: {snippet} Source: {link}"
32
+
33
+ google_web_search_tool = FunctionTool.from_defaults(google_web_search)
34
+
35
+
36
+ def round_to_two_decimals(value):
37
+ """
38
+ Round a number to two decimal places.
39
+ Args:
40
+ value (float): The value to be round to 2 decimal places.
41
+ Raises:
42
+ ValueError: If the 'value' is not an integer or a float.
43
+ """
44
+ return round(float(value), 2)
45
+
46
+ round_to_two_decimals_tool = FunctionTool.from_defaults(round_to_two_decimals)
47
+
48
+ def text_inverter(text: str) -> str:
49
+ """
50
+ Handles sentence writen backward:
51
+ - Reverses it and returns the reverse version
52
+ - Ignore if text is not written backwords
53
+ Args:
54
+ text (str): The text writen backwards to be reversed
55
+ """
56
+ decoded = text[::-1]
57
+ print(decoded)
58
+ text_inverter_tool = FunctionTool.from_defaults(text_inverter)