Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.0.9 updated from AIOS #88

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ The folder needs to be the following structure:
If you want to use external tools provided by openagi in your agents, you can follow instructions of setting up tools in [How to setup external tools](./tools.md).
If you want to add new tools for your developing agent,
you need to add a new tool file in the [folder](./pyopenagi/tools/).
- Offline tools: locally-deployed models like stable-diffusion model
- Online tools: APIs like rapid api.

#### Upload agent
If you have developed and tested your agent, and you would like to share your agents, you can use the following to upload your agents
Expand All @@ -45,7 +43,7 @@ python pyopenagi/agents/interact.py --mode upload --agent <author_name/agent_nam
💡Note that the `agent` param must exactly match the folder you put your agent locally.

#### Download agent
If you want to look at implementations of other agents that people developed, you can use the following command:
If you want to look at implementations of other agents that others have developed, you can use the following command:
```
python pyopenagi/agents/interact.py --mode download --agent <author_name/agent_name>
```
Expand Down
14 changes: 7 additions & 7 deletions pyopenagi/agents/agent_factory.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from datetime import datetime
import heapq
from concurrent.futures import ThreadPoolExecutor, as_completed
from threading import Thread, Lock, Event
from threading import Lock, Event
from pympler import asizeof
from .interact import Interactor, list_available_agents
import os
import importlib

class AgentFactory:
def __init__(self, llm, agent_process_queue, agent_process_factory, agent_log_mode):
def __init__(self,
agent_process_queue,
agent_process_factory,
agent_log_mode
):
self.max_aid = 256
self.llm = llm
# self.llm = llm
self.aid_pool = [i for i in range(self.max_aid)]
heapq.heapify(self.aid_pool)
self.agent_process_queue = agent_process_queue
Expand Down Expand Up @@ -60,8 +62,6 @@ def activate_agent(self, agent_name, task_input):
agent = agent_class(
agent_name = agent_name,
task_input = task_input,
llm = self.llm,
agent_process_queue = self.agent_process_queue,
agent_process_factory = self.agent_process_factory,
log_mode = self.agent_log_mode
)
Expand Down
18 changes: 5 additions & 13 deletions pyopenagi/agents/agent_process.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
import os

import time

from queue import Queue

from datetime import datetime

import heapq

from concurrent.futures import ThreadPoolExecutor, as_completed

from threading import Thread, Lock, Event

from pympler import asizeof

from ..utils.chat_template import Query

class AgentProcess:
def __init__(self,
agent_name,
query
query: Query
):
self.agent_name = agent_name
self.query = query
Expand Down Expand Up @@ -79,6 +67,10 @@ def get_time_limit(self):
def set_time_limit(self, time_limit):
self.time_limit = time_limit


class LLMRequestProcess(AgentProcess):
pass

class AgentProcessFactory:
def __init__(self, agent_process_log_mode = None):
self.max_pid = 1024
Expand Down
43 changes: 21 additions & 22 deletions pyopenagi/agents/base_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@
import json

from .agent_process import (
AgentProcess,
# AgentProcessQueue
AgentProcess
)

import logging

import time

from threading import Thread

from datetime import datetime

import numpy as np

from ..utils.logger import AgentLogger

from ..utils.chat_template import Query

import importlib

from ..queues.llm_request_queue import LLMRequestQueue

class CustomizedThread(Thread):
def __init__(self, target, args=()):
super().__init__()
Expand All @@ -41,19 +36,15 @@ class BaseAgent:
def __init__(self,
agent_name,
task_input,
llm,
agent_process_queue,
agent_process_factory,
log_mode: str
):
self.agent_name = agent_name
self.config = self.load_config()
self.tool_names = self.config["tools"]


self.llm = llm
self.agent_process_queue = agent_process_queue
self.agent_process_factory = agent_process_factory

self.tool_list = dict()
self.tools = []
self.load_tools(self.tool_names)
Expand All @@ -69,7 +60,7 @@ def __init__(self,

self.log_mode = log_mode
self.logger = self.setup_logger()
self.logger.log(f"Initialized. \n", level="info")
self.logger.log("Initialized. \n", level="info")

self.set_status("active")
self.set_created_time(time.time())
Expand All @@ -85,6 +76,7 @@ def build_system_instruction(self):

def check_workflow(self, message):
try:
# print(f"Workflow message: {message}")
workflow = json.loads(message)
if not isinstance(workflow, list):
return None
Expand All @@ -103,7 +95,8 @@ def automatic_workflow(self):
response, start_times, end_times, waiting_times, turnaround_times = self.get_response(
query = Query(
messages = self.messages,
tools = None
tools = None,
message_return_type="json"
)
)

Expand Down Expand Up @@ -139,19 +132,25 @@ def snake_to_camel(self, snake_str):
def load_tools(self, tool_names):
for tool_name in tool_names:
org, name = tool_name.split("/")

module_name = ".".join(["pyopenagi", "tools", org, name])

class_name = self.snake_to_camel(name)

tool_module = importlib.import_module(module_name)

tool_class = getattr(tool_module, class_name)

self.tool_list[name] = tool_class()

self.tools.append(tool_class().get_tool_call_format())

def pre_select_tools(self, tool_names):
pre_selected_tools = []
for tool_name in tool_names:
for tool in self.tools:
if tool["function"]["name"] == tool_name:
pre_selected_tools.append(tool)
break

return pre_selected_tools

def setup_logger(self):
logger = AgentLogger(self.agent_name, self.log_mode)
return logger
Expand Down Expand Up @@ -184,7 +183,7 @@ def query_loop(self, query):
# reinitialize agent status
agent_process.set_created_time(current_time)
agent_process.set_response(None)
self.agent_process_queue.put(agent_process)
LLMRequestQueue.add_message(agent_process)

thread.start()
thread.join()
Expand All @@ -206,7 +205,7 @@ def query_loop(self, query):
turnaround_times.append(turnaround_time)
# Re-start the thread if not done

self.agent_process_factory.deactivate_agent_process(agent_process.get_pid())
# self.agent_process_factory.deactivate_agent_process(agent_process.get_pid())

return completed_response, start_times, end_times, waiting_times, turnaround_times

Expand All @@ -219,7 +218,7 @@ def create_agent_request(self, query):
# print("Already put into the queue")
return agent_process

def listen(self, agent_process):
def listen(self, agent_process: AgentProcess):
"""Response Listener for agent

Args:
Expand Down
4 changes: 1 addition & 3 deletions pyopenagi/agents/example/academic_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ class AcademicAgent(ReactAgent):
def __init__(self,
agent_name,
task_input,
llm,
agent_process_queue,
agent_process_factory,
log_mode: str
):
ReactAgent.__init__(self, agent_name, task_input, llm, agent_process_queue, agent_process_factory, log_mode)
ReactAgent.__init__(self, agent_name, task_input, agent_process_factory, log_mode)
self.workflow_mode = "automatic"

def manual_workflow(self):
Expand Down
6 changes: 2 additions & 4 deletions pyopenagi/agents/example/creation_agent/agent.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from ...react_agent import ReactAgent
class AcademicAgent(ReactAgent):
class CreationAgent(ReactAgent):
def __init__(self,
agent_name,
task_input,
llm,
agent_process_queue,
agent_process_factory,
log_mode: str
):
ReactAgent.__init__(self, agent_name, task_input, llm, agent_process_queue, agent_process_factory, log_mode)
ReactAgent.__init__(self, agent_name, task_input, agent_process_factory, log_mode)
self.workflow_mode = "automatic"

def automatic_workflow(self):
Expand Down
3 changes: 1 addition & 2 deletions pyopenagi/agents/example/math_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ class MathAgent(ReactAgent):
def __init__(self,
agent_name,
task_input,
llm, agent_process_queue,
agent_process_factory,
log_mode: str
):
ReactAgent.__init__(self, agent_name, task_input, llm, agent_process_queue, agent_process_factory, log_mode)
ReactAgent.__init__(self, agent_name, task_input, agent_process_factory, log_mode)

def automatic_workflow(self):
return super().automatic_workflow()
Expand Down
7 changes: 2 additions & 5 deletions pyopenagi/agents/example/rec_agent/agent.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@

from ...react_agent import ReactAgent

import numpy as np
class RecAgent(ReactAgent):
def __init__(self,
agent_name,
task_input,
llm,
agent_process_queue,
agent_process_factory,
log_mode
):
ReactAgent.__init__(self, agent_name, task_input, llm, agent_process_queue, agent_process_factory, log_mode)
ReactAgent.__init__(self, agent_name, task_input, agent_process_factory, log_mode)

def automatic_workflow(self):
return super().automatic_workflow()
Expand All @@ -20,7 +17,7 @@ def manual_workflow(self):
workflow = [
{
"message": "identify the tool that you need to call to obtain information.",
"tool_use": ["imdb_top_movies", "imdb_top_series"]
"tool_use": ["imdb/top_movies", "imdb/top_series"]
},
{
"message": "based on the information, give recommendations for the user based on the constrains. ",
Expand Down
3 changes: 1 addition & 2 deletions pyopenagi/agents/example/travel_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ class TravelAgent(ReactAgent):
def __init__(self,
agent_name,
task_input,
llm, agent_process_queue,
agent_process_factory,
log_mode: str
):
ReactAgent.__init__(self, agent_name, task_input, llm, agent_process_queue, agent_process_factory, log_mode)
ReactAgent.__init__(self, agent_name, task_input, agent_process_factory, log_mode)

def automatic_workflow(self):
return super().automatic_workflow()
Expand Down
21 changes: 14 additions & 7 deletions pyopenagi/agents/interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ def check_reqs_installed(self, agent):
result = subprocess.run(['conda', 'list'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Decode the output from bytes to string
with open(reqs_path, "r") as f:
reqs = f.readlines()
reqs = []
lines = f.readlines()
for line in lines:
line = line.replace("\n", "")
if "==" in line:
reqs.append(line.split("==")[0])
else:
reqs.append(line)

output = result.stdout.decode('utf-8')

Expand Down Expand Up @@ -211,11 +218,11 @@ def parse_args():
agent = args.agent

client = Interactor()
client.check_reqs_installed(agent)
# client.check_reqs_installed(agent)
# client = Interactor()
# if mode == "download":
# client.download_agent(agent) # download agents
if mode == "download":
client.download_agent(agent) # download agents

# else:
# assert mode == "upload"
# client.upload_agent(agent) # upload agents
else:
assert mode == "upload"
client.upload_agent(agent) # upload agents
Loading
Loading