biattos commited on
Commit
bc91d46
·
verified ·
1 Parent(s): 7f5bd70

Upload 2 files

Browse files
src/instagram/tools/__init__.py ADDED
File without changes
src/instagram/tools/search.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+ import os
4
+
5
+ from langchain.tools import tool
6
+ from langchain_community.document_loaders import WebBaseLoader
7
+
8
+ class SearchTools:
9
+
10
+ @tool('search internet')
11
+ def search_internet(query: str) -> str:
12
+ """
13
+ Use this tool to search the internet for information. This tools returns 5 results from Google search engine.
14
+ """
15
+ return SearchTools.search(query)
16
+
17
+ @tool('search instagram')
18
+ def search_instagram(query: str) -> str:
19
+ """
20
+ Use this tool to search Instagram. This tools returns 5 results from Instagram pages.
21
+ """
22
+ return SearchTools.search(f"site:instagram.com {query}", limit=5)
23
+
24
+ @tool('open page')
25
+ def open_page(url: str) -> str:
26
+ """
27
+ Use this tool to open a webpage and get the content.
28
+ """
29
+ loader = WebBaseLoader(url)
30
+ return loader.load()
31
+
32
+
33
+ def search(query, limit=5):
34
+
35
+ url = "https://google.serper.dev/search"
36
+ payload = json.dumps({
37
+ "q": query,
38
+ "num": limit,
39
+ })
40
+ headers = {
41
+ 'X-API-KEY': os.getenv("SERPER_API_KEY"),
42
+ 'Content-Type': 'application/json'
43
+ }
44
+ response = requests.request("POST", url, headers=headers, data=payload)
45
+ results = response.json()['organic']
46
+
47
+ string = []
48
+ for result in results:
49
+ string.append(f"{result['title']}\n{result['snippet']}\n{result['link']}\n\n")
50
+
51
+ return f"Search results for '{query}':\n\n" + "\n".join(string)
52
+
53
+
54
+ if __name__ == "__main__":
55
+ print(SearchTools.open_page("https://www.python.org/"))