forked from agiresearch/OpenAGI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenagi_opentask.py
118 lines (82 loc) · 3.57 KB
/
openagi_opentask.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
"""
Copyright 2023 Yingqiang Ge
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
__author__ = "Yingqiang Ge, Kai Mei"
__copyright__ = "Copyright 2023, OpenAGI"
__date__ = "2023/04/10"
__license__ = "Apache 2.0"
__version__ = "0.0.1"
import argparse
import os
import openai
from langchain import LLMChain, OpenAI, PromptTemplate
from langchain.memory import ConversationBufferMemory
from termcolor import colored
from open_tasks.tools.customized_tools import *
def main():
parser = argparse.ArgumentParser(description='Prepare parameters for running open tasks')
# add arguments to the parser
parser.add_argument("--searchapi_key", type=str, help="Google search API key", required=True)
parser.add_argument("--openai_key", type=str, help="OpenAI key", required=True)
parser.add_argument("--device", type=str, default="cpu")
# parse the arguments
args = parser.parse_args()
os.environ["SERPAPI_API_KEY"] = args.searchapi_key
os.environ["OPENAI_API_KEY"] = args.openai_key
openai.api_key = args.openai_key
llm = OpenAI(temperature=0)
tools = load_tools(["serpapi","llm-math"],llm=llm)
# use llm as the planner
template="""\
You are a planner who is an expert at coming up with a todo list for a given objective.
For each task, utilize one of the provided tools only when needed.
Ensure the list is as short as possible, and tasks in it are relevant, effective and described in a single sentence.
Develop a detailed to-do list to achieve the objective: {objective}
Provided tools:
"""
temp = ""
for tool in tools:
temp = temp + tool.name + ": " + tool.description + "\n"
todo_prompt = PromptTemplate.from_template(template+temp)
print (colored("Please specify the task you want to solve: ", 'red', attrs=['bold']))
objective = input()
prompt = todo_prompt.format(objective=objective)
print(prompt)
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt}
]
)
content = completion.choices[0].message["content"]
print(content)
print("\n")
print (colored("Please decide whether to execute the above plans by typing a number, 0: No, 1: Yes. ", 'red', attrs=['bold']))
executed = input()
# use llm as the executor
if executed == '1':
input_list = content.split("\n")[1:-1]
memory = ConversationBufferMemory(memory_key="chat_history")
agent = initialize_agent(tools, llm, memory=memory, agent="conversational-react-description", verbose=True)
for input_s in input_list:
output = agent.run(input=(input_s))
print(output)
print("Finished!")
print("\n")
print (colored("Please provide a rating for the result, using a scale of 1 to 10.", 'red', attrs=['bold']))
rating = input()
elif executed == '0':
print("Finished!")
else:
print("Invalid Input!")
if __name__ == "__main__":
main()