another example

This commit is contained in:
erikkaum 2024-12-17 17:01:34 +01:00
parent 3e0a851606
commit 6b05056a7c
5 changed files with 104 additions and 14 deletions

View File

@ -23,5 +23,6 @@ RUN pip install --no-cache-dir -r requirements.txt
# Install the package # Install the package
RUN pip install -e . RUN pip install -e .
# Command to run when container starts COPY server.py /app/server.py
CMD ["python"]
CMD ["python", "/app/server.py"]

View File

@ -1,12 +1,39 @@
from agents.search import DuckDuckGoSearchTool from agents.search import DuckDuckGoSearchTool
from agents.docker_alternative import DockerPythonInterpreter from agents.docker_alternative import DockerPythonInterpreter
test = """
from agents.tools import Tool
class DummyTool(Tool):
name = "echo"
description = '''Perform a web search based on your query (think a Google search) then returns the top search results as a list of dict elements.
Each result has keys 'title', 'href' and 'body'.'''
inputs = {
"cmd": {"type": "string", "description": "The search query to perform."}
}
output_type = "any"
def forward(self, cmd: str) -> str:
return cmd
"""
container = DockerPythonInterpreter() container = DockerPythonInterpreter()
tools = [DuckDuckGoSearchTool] output = container.execute("x = 5")
print(f"first output: {output}")
output = container.execute("print(x)")
print(f"second output: {output}")
output = container.execute("res = web_search(query='whats the capital of Cambodia?'); print(res)", tools=tools) breakpoint()
print("---------")
output = container.execute(test)
print(output)
output = container.execute("res = DummyTool(cmd='echo this'); print(res)")
print(output) print(output)
container.stop() container.stop()

14
examples/dummytool.py Normal file
View File

@ -0,0 +1,14 @@
from agents.tools import Tool
class DummyTool(Tool):
name = "echo"
description = """Perform a web search based on your query (think a Google search) then returns the top search results as a list of dict elements.
Each result has keys 'title', 'href' and 'body'."""
inputs = {
"cmd": {"type": "string", "description": "The search query to perform."}
}
output_type = "any"
def forward(self, cmd: str) -> str:
return cmd

46
server.py Normal file
View File

@ -0,0 +1,46 @@
import socket
import sys
import traceback
import io
exec_globals = {}
exec_locals = {}
def execute_code(code):
stdout = io.StringIO()
stderr = io.StringIO()
sys.stdout = stdout
sys.stderr = stderr
try:
exec(code, exec_globals, exec_locals)
except Exception as e:
traceback.print_exc(file=stderr)
output = stdout.getvalue()
error = stderr.getvalue()
# Restore original stdout and stderr
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
return output + error
def start_server(host='0.0.0.0', port=65432):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((host, port))
s.listen()
print(f"Server listening on {host}:{port}")
while True:
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
data = conn.recv(1024)
if not data:
break
code = data.decode('utf-8')
output = execute_code(code)
conn.sendall(output.encode('utf-8'))
if __name__ == "__main__":
start_server()

View File

@ -1,6 +1,7 @@
import docker import docker
from typing import List, Optional from typing import List, Optional
import warnings import warnings
import socket
from agents.tools import Tool from agents.tools import Tool
@ -18,7 +19,7 @@ class DockerPythonInterpreter:
try: try:
self.container = self.client.containers.run( self.container = self.client.containers.run(
"pyrunner:latest", "pyrunner:latest",
"tail -f /dev/null", ports={'65432/tcp': 65432},
detach=True, detach=True,
remove=True, remove=True,
) )
@ -37,10 +38,11 @@ class DockerPythonInterpreter:
""" """
Execute Python code in the container and return stdout and stderr Execute Python code in the container and return stdout and stderr
""" """
tool_instance = tools[0]()
import_code = f""" if tools != None:
tool_instance = tools[0]()
import_code = f"""
module_path = '{tool_instance.__class__.__module__}' module_path = '{tool_instance.__class__.__module__}'
class_name = '{tool_instance.__class__.__name__}' class_name = '{tool_instance.__class__.__name__}'
@ -50,14 +52,14 @@ module = importlib.import_module(module_path)
web_search = getattr(module, class_name)() web_search = getattr(module, class_name)()
""" """
full_code = import_code + "\n" + code code = import_code + "\n" + code
try: try:
exec_command = self.container.exec_run( # Connect to the server running inside the container
cmd=["python", "-c", full_code], with socket.create_connection(('localhost', 65432)) as sock:
) sock.sendall(code.encode('utf-8'))
output = exec_command.output output = sock.recv(4096)
return output.decode('utf-8') return output.decode('utf-8')
except Exception as e: except Exception as e:
return f"Error executing code: {str(e)}" return f"Error executing code: {str(e)}"