eaglelandsonce commited on
Commit
4e6748b
1 Parent(s): 6c42ff6

Update crewai/tools/gemini_tools.py

Browse files
Files changed (1) hide show
  1. crewai/tools/gemini_tools.py +70 -1
crewai/tools/gemini_tools.py CHANGED
@@ -1 +1,70 @@
1
- # tools created using gemini
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools created using gemini
2
+
3
+ import json
4
+ import os
5
+
6
+ import google.generativeai as genai
7
+
8
+ # Retrieve API Key from Environment Variable
9
+ GOOGLE_AI_STUDIO = os.environ.get('GOOGLE_AI_STUDIO2')
10
+
11
+ # Ensure the API key is available
12
+ if not GOOGLE_AI_STUDIO:
13
+ raise ValueError("API key not found. Please set the GOOGLE_AI_STUDIO2 environment variable.")
14
+
15
+
16
+
17
+ import requests
18
+ from langchain.tools import tool
19
+
20
+
21
+ class GeminiSearchTools():
22
+ @tool("Gemini search the internet")
23
+ def gemini_search_internet(query):
24
+ """Useful to search the internet
25
+ about a a given topic and return relevant results"""
26
+ top_result_to_return = 4
27
+ url = "https://google.serper.dev/search"
28
+ payload = json.dumps({"q": query})
29
+ headers = {
30
+ 'X-API-KEY': os.environ['SERPER_API_KEY'],
31
+ 'content-type': 'application/json'
32
+ }
33
+ response = requests.request("POST", url, headers=headers, data=payload)
34
+ results = response.json()['organic']
35
+ string = []
36
+ for result in results[:top_result_to_return]:
37
+ try:
38
+ string.append('\n'.join([
39
+ f"Title: {result['title']}", f"Link: {result['link']}",
40
+ f"Snippet: {result['snippet']}", "\n-----------------"
41
+ ]))
42
+ except KeyError:
43
+ next
44
+
45
+ return '\n'.join(string)
46
+
47
+ @tool("Gemini search news on the internet")
48
+ def gemini_search_news(query):
49
+ """Useful to search news about a company, stock or any other
50
+ topic and return relevant results"""""
51
+ top_result_to_return = 4
52
+ url = "https://google.serper.dev/news"
53
+ payload = json.dumps({"q": query})
54
+ headers = {
55
+ 'X-API-KEY': os.environ['SERPER_API_KEY'],
56
+ 'content-type': 'application/json'
57
+ }
58
+ response = requests.request("POST", url, headers=headers, data=payload)
59
+ results = response.json()['news']
60
+ string = []
61
+ for result in results[:top_result_to_return]:
62
+ try:
63
+ string.append('\n'.join([
64
+ f"Title: {result['title']}", f"Link: {result['link']}",
65
+ f"Snippet: {result['snippet']}", "\n-----------------"
66
+ ]))
67
+ except KeyError:
68
+ next
69
+
70
+ return '\n'.join(string)