AG_AGENT_2 / agent.py
AGofficial's picture
Upload 2 files
299e710 verified
from agentllm import AgGPT
import os
import json
class Agent:
def __init__(self, mission):
self.mission = mission
self.llm = AgGPT("agent.gguf")
self.mission_name = self.llm.ask("Based on this mission, create a one-word name for it. the mission is " + mission + " send a single word to identify the mission. send one word only.")
self.folder = f"agent/{self.mission_name}"
print(self.folder)
if not os.path.exists(self.folder):
os.makedirs(self.folder)
def create_file(self, name, content):
with open(f"{self.folder}/{name}", "w") as file:
file.write(content)
def summarize_file(self, path):
with open(path, "r") as file:
content = file.read()
summary = self.llm.ask("Summarize this text: " + content + " in 5 bullet points. send only the summary. ")
return summary
def get_full_project_summary(self):
files = [f for f in os.listdir(self.folder) if os.path.isfile(os.path.join(self.folder, f))]
summary = ""
for i, file in enumerate(files):
with open(os.path.join(self.folder, file), "r") as f:
content = f.read().strip()
if i > 0:
summary += "\n"
summary += f"{file} - {content}"
return summary
def deploy_agent(self):
system = f'''
You are an AI agent that can write files to the computer.
The files you have created so far are:
{self.get_full_project_summary()}
The project is called {self.mission_name} and the folder is called {self.folder}.
You must work on the README.md file and all other project files, you can only respond in this format:
{{
"file_name": "line1\\nline2\\nline3"
}}
if the file already exists it will be overwritten, for example if you want to overwrite the file README.md with the content "Hello World" you will respond with:
{{
"README.md": "Hello World"
}}
You will only respond with the JSON object, nothing else.
to create new files you will use the same format, for example if you want to create a new file called "test.txt" with the content "Hello World" you will respond with:
{{
"test.txt": "Hello World"
}}
You will only respond with the JSON object, nothing else.
The mission objective is: {self.mission}
continue working on the project. you may send more than one file name and content per response as follows:
{{
"README.md": "This is the README\\nIt has multiple lines\\nPretty cool, right?",
"main.py": "print('Hello, world!')\\ndef main():\\n return 42"
}}
continue the project.
Do not assume any module or library, you must create everything with the respective programming language.
create in the readme all the files you will create, and then create the files that you havent created yet. make sure the project is fully functional.
Make sure to write fucntional code and no placeholders, keep iterating.
'''
system += "\nRespond ONLY with the JSON. No explanation, no commentary, just raw JSON."
print(system)
response = self.llm.ask(system)
print(response)
try:
files_to_write = json.loads(response.strip())
except json.JSONDecodeError:
print("Error decoding JSON response. Please check the format.")
for file_name, content in files_to_write.items():
file_path = os.path.join(self.folder, file_name)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
self.deploy_agent()
if __name__ == "__main__":
mission = input("Enter your mission: ")
agent = Agent(mission)
agent.deploy_agent()