-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
215 lines (184 loc) · 8.3 KB
/
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import os
import requests
from bs4 import BeautifulSoup
import json
import autogen
import openai
from autogen import config_list_from_json, UserProxyAgent, AssistantAgent, GroupChat, GroupChatManager
from langchain.chat_models import ChatOpenAI
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.summarize import load_summarize_chain
from langchain.prompts import PromptTemplate
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
BROWSERLESS_API_KEY = os.getenv("BROWSERLESS_API_KEY")
SERPER_API_KEY = os.getenv("SERPER_API_KEY")
config_list = config_list_from_json("OAI_CONFIG_LIST")
def search(query):
url = "https://google.serper.dev/search"
payload = json.dumps({
"q": query
})
headers = {
'X-API-KEY': SERPER_API_KEY,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json()
def scrape(url: str):
"""Scrape a website and summarize its content if it's too large."""
print("Scraping website...")
# Define the headers for the request
headers = {
'Cache-Control': 'no-cache',
'Content-Type': 'application/json',
}
# Build the POST URL
post_url = f"https://chrome.browserless.io/content?token={BROWSERLESS_API_KEY}"
# Send the POST request
response = requests.post(post_url, headers=headers, json={"url": url})
# Check the response status code
if response.status_code == 200:
soup = BeautifulSoup(response.content, "html.parser")
text = soup.get_text()
print("CONTENTTTTTT:", text)
if len(text) > 8000:
output = summary(text)
return output
else:
return text
else:
print(f"HTTP request failed with status code {response.status_code}")
def summary(content):
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-1106")
text_splitter = RecursiveCharacterTextSplitter(
separators=["\n\n", "\n"], chunk_size=10000, chunk_overlap=500)
docs = text_splitter.create_documents([content])
map_prompt = """
Write a detailed summary of the following text for a research purpose:
"{text}"
SUMMARY:
"""
map_prompt_template = PromptTemplate(
template=map_prompt, input_variables=["text"])
summary_chain = load_summarize_chain(
llm=llm,
chain_type='map_reduce',
map_prompt=map_prompt_template,
combine_prompt=map_prompt_template,
verbose=True
)
output = summary_chain.run(input_documents=docs,)
return output
def research(query):
llm_config_researcher = {
"functions": [
{
"name": "search",
"description": "google search for relevant information",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Google search query",
}
},
"required": ["query"],
},
},
{
"name": "scrape",
"description": "Scraping website content based on url",
"parameters": {
"type": "object",
"properties": {
"url": {
"type": "string",
"description": "Website url to scrape",
}
},
"required": ["url"],
},
},
],
"config_list": config_list}
researcher = autogen.AssistantAgent(
name="researcher",
system_message="Research about a given query, collect as many information as possible, and generate detailed research results with loads of technique details with all reference links attached; Add TERMINATE to the end of the research report;",
llm_config=llm_config_researcher,
)
user_proxy = autogen.UserProxyAgent(
name="User_proxy",
code_execution_config={"last_n_messages": 2, "work_dir": "coding"},
is_termination_msg=lambda msg: "TERMINATE" in msg["content"] if msg["content"] else False,
human_input_mode="TERMINATE",
function_map={
"search": search,
"scrape": scrape,
}
)
user_proxy.initiate_chat(researcher, message=query)
# set the receiver to be researcher, and get a summary of the research report
user_proxy.stop_reply_at_receive(researcher)
user_proxy.send(
"Give me the research report that just generated again, return ONLY the report & reference links", researcher)
# return the last message the expert received
return user_proxy.last_message()["content"]
def write_content(research_material, topic):
editor = autogen.AssistantAgent(
name="editor",
description="Seasoned editor skilled in structuring blog posts for clarity and coherence, using material from the Research Assistant.",
system_message='''
Welcome, Senior Editor.
As a seasoned professional, you bring meticulous attention to detail, a deep appreciation for literary and cultural nuance, and a commitment to upholding the highest editorial standards.
Your role is to craft the structure of a short blog post using the material from the Research Assistant. Use your experience to ensure clarity, coherence, and precision.
Once structured, pass it to the Writer to pen the final piece.
''',
llm_config={"config_list": config_list},
)
writer = autogen.AssistantAgent(
name="writer",
description="Blogger tasked with composing short blog posts using the structure from the Editor, embodying clear, concise, and journalistic style.",
system_message='''
Welcome, Blogger.
Your task is to compose a short blog post using the structure given by the Editor and incorporating feedback from the Reviewer.
Embrace stylistic minimalism: be clear, concise, and direct.
Approach the topic from a journalistic perspective; aim to inform and engage the readers without adopting a sales-oriented tone.
After two rounds of revisions, conclude your post with "TERMINATE".
''',
llm_config={"config_list": config_list},
)
reviewer = autogen.AssistantAgent(
name="reviewer",
description="Expert blog content critic focused on reviewing and providing feedback to ensure the highest standards of editorial excellence.",
system_message='''
As a distinguished blog content critic, you are known for your discerning eye, deep literary and cultural understanding, and an unwavering commitment to editorial excellence.
Your role is to meticulously review and critique the written blog, ensuring it meets the highest standards of clarity, coherence, and precision.
Provide invaluable feedback to the Writer to elevate the piece. After two rounds of content iteration, conclude with "TERMINATE".
''',
llm_config={"config_list": config_list},
)
user_proxy = UserProxyAgent(
name="admin",
system_message="A human admin. Interact with editor to discuss the structure. Actual writing needs to be approved by this admin.",
code_execution_config=False,
is_termination_msg=lambda msg: "TERMINATE" in msg["content"] if msg["content"] else False,
human_input_mode="TERMINATE",
)
groupchat = autogen.GroupChat(
agents=[user_proxy, editor, writer, reviewer],
messages=[],
max_round=10)
manager = autogen.GroupChatManager(groupchat=groupchat)
user_proxy.initiate_chat(
manager, message=f"Write a blog about {topic}, here are the material: {research_material}")
user_proxy.stop_reply_at_receive(manager)
user_proxy.send(
"Give me the blog that just generated again, return ONLY the blog, and add TERMINATE in the end of the message", manager)
# return the last message the expert received
return user_proxy.last_message()["content"]
def save_markdown(conversation_log):
with open("/logs/conversation_log.md", "a") as file:
file.write(conversation_log + "\n")