-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
89 lines (75 loc) · 3.18 KB
/
app.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
from models.llama_3_1_70B import llama_3_1_70B
from tools.reddit_scrapper import reddit_scrapper
from tools.search_tool import search_tool
from tools.scrape_tool import scrape_tool
from tools.reddit_commenter import reddit_commenter
from termcolor import colored
import json
def prepare_system_prompts():
with open("prompts/planner.md", "r") as file:
system_prompt_planner = file.read()
with open("prompts/researcher.md", "r") as file:
system_prompt_researcher = file.read()
with open("prompts/writer.md", "r") as file:
system_prompt_writer = file.read()
return system_prompt_planner, system_prompt_researcher, system_prompt_writer
def chain_of_action(model, system_prompt_planner, system_prompt_researcher, system_prompt_writer):
# Scrape reddit posts for questions
reddit_scrape = "where will the next olympics be held?"
"""
reddit_scrape, post_ids = reddit_scrapper(["askbaking", "1"])
print(f"Somebody in reddit has this question:\n{reddit_scrape}")
"""
# Give question to planner in order to plan the search queries
plan_prompt = reddit_scrape
print(colored("Planning...\n", "magenta"))
plan = model.answer(
system_prompt=system_prompt_planner, prompt=plan_prompt, json=True)
plan = json.loads(plan)
print("For this question I decided to search:")
print(plan["query1"])
print(plan["query2"])
print(plan["query3"] + "\n")
print(colored("Plan done!\n", "magenta"))
# Use the search tool
print(colored("Searching...", "magenta"))
search_results = search_tool(
[plan["query1"], plan["query2"], plan["query3"]])
print(colored("Search done!\n", "magenta"))
# Use the researcher to choose the most crucial info
research_prompt = f"""
{{
"question": "{reddit_scrape}",
"query1": "{plan["query1"]}",
"query2": "{plan["query2"]}",
"query3": "{plan["query3"]}",
"search": "{search_results}"
}}
"""
print(colored("Researching...", "magenta"))
research = model.answer(
system_prompt=system_prompt_researcher, prompt=research_prompt, json=True)
print(colored("Research done!\n", "magenta"))
# Use the web scrapper
print(colored("Scraping...", "magenta"))
research = json.loads(research)
scrape_results = scrape_tool(
[research["url1"], research["url2"], research["url3"]])
print(colored("Scraping done!\n", "magenta"))
# Use the writer
writer_prompt = f"""
{{
"question": "{reddit_scrape}",
"dump": "{scrape_results}"
}}
"""
writer = model.answer(
system_prompt=system_prompt_writer, prompt=writer_prompt, json=False)
print(colored(f"\n\n{writer}", "cyan"))
# Use the reddit_poster tool
# reddit_commenter([post_ids[0], writer["answer"]])
if __name__ == "__main__":
model = llama_3_1_70B()
system_prompt_planner, system_prompt_researcher, system_prompt_writer = prepare_system_prompts()
chain_of_action(model, system_prompt_planner=system_prompt_planner,
system_prompt_researcher=system_prompt_researcher, system_prompt_writer=system_prompt_writer)