Skip to content

Agent Zero AI framework

google gemini support, docker startup fix, console input fix

frdel76ff3c5fcf1e
diff --git a/main.py b/main.py
index 7662d2528..d7527441d 100644
--- a/main.py
+++ b/main.py
@@ -1,10 +1,13 @@
+import signal
+import sys
+import termios
import threading, time, models, os
+import tty
from ansio import application_keypad, mouse_input, raw_input
from ansio.input import InputEvent, get_input_event
from agent import Agent, AgentConfig
from python.helpers.print_style import PrintStyle
from python.helpers.files import read_file
-from pytimedinput import timedInput as timed_input
from python.helpers import files
@@ -17,7 +20,7 @@ def initialize():
# chat_llm = models.get_groq_llama70b(temperature=0.2)
# chat_llm = models.get_groq_llama70b_json(temperature=0.2)
# chat_llm = models.get_groq_llama8b(temperature=0.2)
- chat_llm = models.get_openai_gpt35(temperature=0)
+ # chat_llm = models.get_openai_gpt35(temperature=0)
# chat_llm = models.get_openai_gpt4o(temperature=0)
# chat_llm = models.get_anthropic_opus(temperature=0)
# chat_llm = models.get_anthropic_sonnet(temperature=0)
@@ -28,6 +31,7 @@ def initialize():
# chat_llm = models.get_ollama(model_name="llama3:8b-text-fp16")
# chat_llm = models.get_ollama(model_name="gemma2:latest")
# chat_llm = models.get_ollama(model_name="qwen:14b")
+ chat_llm = models.get_google_chat()
# embedding model used for memory
# embedding_llm = models.get_embedding_openai()
@@ -78,18 +82,21 @@ def chat(agent:Agent):
timeout = agent.get_data("timeout") # how long the agent is willing to wait
if not timeout: # if agent wants to wait for user input forever
PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ('exit' to leave):")
+ import readline # this fixes arrow keys in terminal
user_input = input("> ")
PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")
else: # otherwise wait for user input with a timeout
PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ({timeout}s timeout, 'wait' to wait, 'exit' to leave):")
- user_input = timed_input("> ", timeout=timeout)
+ import readline # this fixes arrow keys in terminal
+ # user_input = timed_input("> ", timeout=timeout)
+ user_input = timeout_input("> ", timeout=timeout)
- if user_input[1]:
+ if not user_input:
user_input = read_file("prompts/fw.msg_timeout.md")
PrintStyle(font_color="white", padding=False).stream(f"{user_input}")
else:
- user_input = user_input[0].strip()
+ user_input = user_input.strip()
if user_input.lower()=="wait": # the user needs more time
user_input = input("> ").strip()
PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")
@@ -113,7 +120,7 @@ def intervention():
Agent.paused = True # stop agent streaming
PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User intervention ('exit' to leave, empty to continue):")
- import readline
+ import readline # this fixes arrow keys in terminal
user_input = input("> ").strip()
PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")
@@ -139,6 +146,21 @@ def capture_keys():
intervent=True
continue
+# User input with timeout
+def timeout_input(prompt, timeout=10):
+ def alarm_handler(signum, frame):
+ raise TimeoutError()
+
+ signal.signal(signal.SIGALRM, alarm_handler)
+ signal.alarm(timeout)
+
+ try:
+ return input(prompt)
+ except TimeoutError:
+ return ""
+ finally:
+ signal.alarm(0) # Cancel the alarm
+
if __name__ == "__main__":
print("Initializing framework...")
diff --git a/models.py b/models.py
index 40def8086..321041c3a 100755
--- a/models.py
+++ b/models.py
@@ -5,6 +5,7 @@ from langchain_openai import ChatOpenAI, OpenAI, OpenAIEmbeddings
from langchain_anthropic import ChatAnthropic
from langchain_groq import ChatGroq
from langchain_huggingface import HuggingFaceEmbeddings
+from langchain_google_genai import ChatGoogleGenerativeAI
# Load environment variables
@@ -81,6 +82,10 @@ def get_ollama_dolphin(api_key=None, temperature=DEFAULT_TEMPERATURE):
def get_ollama_phi(api_key=None, temperature=DEFAULT_TEMPERATURE):
return Ollama(model="phi3:3.8b-mini-instruct-4k-fp16",temperature=temperature)
+def get_google_chat(model_name="gemini-1.5-flash-latest", api_key=None, temperature=DEFAULT_TEMPERATURE):
+ api_key = api_key or get_api_key("google")
+ return ChatGoogleGenerativeAI(model=model_name, temperature=temperature, google_api_key=api_key) # type: ignore
+
def get_embedding_hf(model_name="sentence-transformers/all-MiniLM-L6-v2"):
return HuggingFaceEmbeddings(model_name=model_name)
diff --git a/python/helpers/docker.py b/python/helpers/docker.py
index 80d599ec5..5afd5ad53 100644
--- a/python/helpers/docker.py
+++ b/python/helpers/docker.py
@@ -5,7 +5,7 @@ from typing import Dict, Optional
from python.helpers.files import get_abs_path
class DockerContainerManager:
- def __init__(self, image:str, name:str, ports: Optional[Dict[str, int]] = None, volumes: Optional[Dict[str, Dict[str, str]]] = None):
+ def __init__(self, image: str, name: str, ports: Optional[Dict[str, int]] = None, volumes: Optional[Dict[str, Dict[str, str]]] = None):
self.client = docker.from_env()
self.image = image
self.name = name
@@ -13,8 +13,6 @@ class DockerContainerManager:
self.volumes = volumes
self.container = None
-
-
def cleanup_container(self) -> None:
if self.container:
try:
@@ -26,14 +24,19 @@ class DockerContainerManager:
def start_container(self) -> None:
existing_container = None
- for container in self.client.containers.list():
+ for container in self.client.containers.list(all=True):
if container.name == self.name:
existing_container = container
break
if existing_container:
- #print(f"Container with name '{self.name}' is already running with ID: {existing_container.id}")
- pass
+ if existing_container.status != 'running':
+ print(f"Starting existing container: {self.name} for safe code execution...")
+ existing_container.start()
+ self.container = existing_container
+ else:
+ self.container = existing_container
+ # print(f"Container with name '{self.name}' is already running with ID: {existing_container.id}")
else:
print(f"Initializing docker container {self.name} for safe code execution...")
self.container = self.client.containers.run(
@@ -46,4 +49,3 @@ class DockerContainerManager:
atexit.register(self.cleanup_container)
print(f"Started container with ID: {self.container.id}")
time.sleep(1) # this helps to get SSH ready
-
diff --git a/requirements.txt b/requirements.txt
index 0906b25b7..f27283000 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -6,6 +6,7 @@ langchain-openai==0.1.8
langchain-community==0.2.4
langchain-anthropic==0.1.15
langchain-chroma==0.1.1
+google-generativeai==0.5.4
webcolors==24.6.0
sentence-transformers==3.0.1
pytimedinput==2.0.1
diff --git a/test.py b/test.py
new file mode 100644
index 000000000..1e0590c9f
--- /dev/null
+++ b/test.py
@@ -0,0 +1,5 @@
+from models import get_google_chat
+
+llm = get_google_chat()
+result = llm.invoke("Write a ballad about LangChain")
+print(result.content)