forked from jinyoung/dynamic-crew-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_tools.py
84 lines (75 loc) · 2.6 KB
/
search_tools.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import json
import os
import requests
from langchain.tools import tool
class SearchTools():
@tool("Search Internal Documents")
def search_internal_documents(query):
"""Useful to search internal documents based on a given query and return relevant results"""
# url = "http://localhost:8005/retrieve"
url = "http://memento.process-gpt.io/retrieve"
payload = json.dumps({"query": query})
headers = {
'content-type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
results = response.json()
string = []
for item in results:
node = item['node']
metadata = node['metadata']
content = node['text']
metadata_str = '\n'.join([f"{key}: {value}" for key, value in metadata.items()])
string.append('\n'.join([
metadata_str,
"Content:",
content,
"\n-----------------"
]))
return '\n'.join(string)
@tool("Search the internet")
def search_internet(query):
"""Useful to search the internet
about a a given topic and return relevant results"""
top_result_to_return = 4
url = "https://google.serper.dev/search"
payload = json.dumps({"q": query})
headers = {
'X-API-KEY': os.getenv('SERPER_API_KEY', ''),
'content-type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
results = response.json()['organic']
string = []
for result in results[:top_result_to_return]:
try:
string.append('\n'.join([
f"Title: {result['title']}", f"Link: {result['link']}",
f"Snippet: {result['snippet']}", "\n-----------------"
]))
except KeyError:
next
return '\n'.join(string)
@tool("Search news on the internet")
def search_news(query):
"""Useful to search news about a company, stock or any other
topic and return relevant results"""""
top_result_to_return = 4
url = "https://google.serper.dev/news"
payload = json.dumps({"q": query})
headers = {
'X-API-KEY': os.getenv('SERPER_API_KEY', ''),
'content-type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
results = response.json()['news']
string = []
for result in results[:top_result_to_return]:
try:
string.append('\n'.join([
f"Title: {result['title']}", f"Link: {result['link']}",
f"Snippet: {result['snippet']}", "\n-----------------"
]))
except KeyError:
next
return '\n'.join(string)