AgAgentMini / agentmini.py
AGofficial's picture
Upload agentmini.py
672cd1c verified
import os
import re
import requests
from tqdm import tqdm
def ask(prompt: str) -> str:
url = "http://text.pollinations.ai/"
fullurl = url + prompt.replace(" ", "%20")
response = requests.get(fullurl)
return response.text
if not os.path.exists("agent"):
os.makedirs("agent")
def save_to_file(filename, prompt, context=""):
filename = os.path.join('agent', filename)
print("Creating file:", filename)
full_prompt = context + prompt if context else prompt
print(f"Saving to file: {filename}\nPrompt:\n{full_prompt}\n")
with open(filename, "w", encoding="utf-8") as file:
file.write(full_prompt)
def create_image(filename, prompt):
print(f"Creating image: {filename}")
prompt = re.sub(r'\s+', ' ', prompt).strip()
url = f"https://pollinations.ai/p/{prompt}?seed=2025&nologo=true&model=flux"
response = requests.get(url)
with open(os.path.join("agent", filename), "wb") as f:
f.write(response.content)
def parse_and_run(response):
code_blocks = re.findall(r'```python(.*?)```', response, re.DOTALL)
for block in tqdm(code_blocks, desc="Executing Code Blocks", unit="block"):
code = block.strip()
try:
exec(code, globals())
except Exception as e:
print(f"\nError executing code block:\n{code}\n\nException: {e}")
def run_agent(task):
system_prompt = """
You are an AI agent, your response must be in python code, using only the following functions:
save_to_file(filename, prompt, context)
create_image(filename, prompt)
save_to_file takes a filename and a prompt, passes it to the LLM, and saves the result. If context is given, it will be loaded and prepended to the prompt. Do not use context on the first file.
Your responses should be python code that calls these functions, do not use any other functions or libraries. make sure to make manifest.json, index.html, style.css, app.js, and logo.png files for web apps, and use the create_image function to create images.
the prompt in create_image should be a short description of the image you want to create.
You may also create readme.md files, .py files, .c files, and any other files apropriate for the task.
Your task is:
"""
full_prompt = system_prompt + task
print("\nSending task to agent...\n")
response = ask(full_prompt)
parse_and_run(response)
if __name__ == "__main__":
print("What task shall I perform, sir?")
task_input = input("> ").strip()
if task_input:
run_agent(task_input)
print("\nAgent has completed the task.")
else:
print("No task provided, sir.")