forked from agiresearch/AIOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AIOS Modularization Pt.1: Functional Components (agiresearch#199)
* feat: pip fallback when conda isn't installed * feat: hooks * feat: functional components * bug: extract partial hooks into global sequence * style: dev tests passing / remove cache files * style: remove unused import * style: ruff checks sigh
- Loading branch information
Showing
18 changed files
with
325 additions
and
129 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from concurrent.futures import ThreadPoolExecutor, Future, as_completed | ||
from typing import Any | ||
|
||
from aios.llm_core.llms import LLM | ||
|
||
from aios.scheduler.fifo_scheduler import FIFOScheduler | ||
|
||
from aios.hooks.types.llm import AgentSubmitDeclaration, FactoryParams, LLMParams, SchedulerParams, LLMRequestQueue, QueueGetMessage, QueueAddMessage, QueueCheckEmpty | ||
from aios.hooks.validate import validate | ||
|
||
from aios.hooks.stores import queue as QueueStore, processes as ProcessStore | ||
|
||
from aios.hooks.utils import generate_random_string | ||
|
||
from pyopenagi.agents.agent_factory import AgentFactory | ||
from pyopenagi.agents.agent_process import AgentProcessFactory | ||
|
||
@validate(LLMParams) | ||
def useKernel(params: LLMParams) -> LLM: | ||
return LLM(**params.model_dump()) | ||
|
||
def useLLMRequestQueue() -> tuple[LLMRequestQueue, QueueGetMessage, QueueAddMessage, QueueCheckEmpty]: | ||
r_str = generate_random_string() | ||
_ = LLMRequestQueue() | ||
|
||
QueueStore.LLM_REQUEST_QUEUE[r_str] = _ | ||
|
||
def getMessage(): | ||
return QueueStore.getMessage(_) | ||
|
||
def addMessage(message: str): | ||
return QueueStore.addMessage(_, message) | ||
|
||
def isEmpty(): | ||
return QueueStore.isEmpty(_) | ||
|
||
|
||
return _, getMessage, addMessage, isEmpty | ||
|
||
@validate(SchedulerParams) | ||
def useFIFOScheduler(params: SchedulerParams): | ||
if params.get_queue_message is None: | ||
|
||
from aios.hooks.stores._global import global_llm_req_queue_get_message | ||
|
||
params.get_queue_message = global_llm_req_queue_get_message | ||
|
||
scheduler = FIFOScheduler(**params.model_dump()) | ||
|
||
def startScheduler(): | ||
scheduler.start() | ||
|
||
def stopScheduler(): | ||
scheduler.stop() | ||
|
||
return startScheduler, stopScheduler | ||
|
||
|
||
@validate(FactoryParams) | ||
def useFactory(params: FactoryParams): | ||
process_factory = AgentProcessFactory() | ||
|
||
agent_factory = AgentFactory( | ||
agent_process_factory=process_factory, | ||
agent_log_mode=params.log_mode, | ||
) | ||
|
||
thread_pool = ThreadPoolExecutor(max_workers=params.max_workers) | ||
|
||
@validate(AgentSubmitDeclaration) | ||
def submitAgent(declaration_params: AgentSubmitDeclaration) -> None: | ||
_submitted_agent: Future = thread_pool.submit( | ||
agent_factory.run_agent, | ||
declaration_params.agent_name, | ||
declaration_params.task_input | ||
) | ||
|
||
ProcessStore.addProcess(_submitted_agent) | ||
|
||
def awaitAgentExecution() -> list[dict[str, Any]]: | ||
res = [] | ||
|
||
for r in as_completed(ProcessStore.AGENT_PROCESSES): | ||
_ = r.result() | ||
res.append(_) | ||
|
||
return res | ||
|
||
|
||
return submitAgent, awaitAgentExecution | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#global vars | ||
|
||
from aios.hooks.llm import useLLMRequestQueue | ||
|
||
global_llm_req_queue, global_llm_req_queue_get_message, global_llm_req_queue_add_message, global_llm_req_queue_is_empty = useLLMRequestQueue() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from concurrent.futures import Future | ||
|
||
AGENT_PROCESSES: list[Future] = [] | ||
|
||
def addProcess(p: Future) -> None: | ||
AGENT_PROCESSES.append(p) | ||
|
||
def clearProcesses() -> None: | ||
AGENT_PROCESSES.clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from aios.hooks.types.llm import LLMRequestQueue | ||
|
||
LLM_REQUEST_QUEUE: dict[str, LLMRequestQueue] = {} | ||
|
||
def getMessage(q: LLMRequestQueue): | ||
return q.get(block=True, timeout=1) | ||
|
||
def addMessage(q: LLMRequestQueue, message: str): | ||
q.put(message) | ||
|
||
return None | ||
|
||
def isEmpty(q: LLMRequestQueue): | ||
return q.empty() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from pydantic import BaseModel | ||
from typing import Any, TypeAlias, Callable | ||
|
||
from queue import Queue | ||
|
||
from pyopenagi.agents.agent_process import AgentProcess | ||
|
||
LLMRequestQueue: TypeAlias = Queue[AgentProcess] | ||
|
||
QueueGetMessage: TypeAlias = Callable[[], AgentProcess] | ||
QueueAddMessage: TypeAlias = Callable[[str], None] | ||
QueueCheckEmpty: TypeAlias = Callable[[], bool] | ||
|
||
class LLMParams(BaseModel): | ||
llm_name: str | ||
max_gpu_memory: dict | None = None, | ||
eval_device: str | None = None, | ||
max_new_tokens: int = 256, | ||
log_mode: str = "console", | ||
use_backend: str | None = None | ||
|
||
|
||
class SchedulerParams(BaseModel): | ||
llm: Any | ||
log_mode: str | ||
get_queue_message: QueueGetMessage | None | ||
|
||
|
||
class FactoryParams(BaseModel): | ||
log_mode: str = "console", | ||
max_workers: int = 500 | ||
|
||
class AgentSubmitDeclaration(BaseModel): | ||
agent_name: str | ||
task_input: str | int | float | dict | tuple | list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import random | ||
import string | ||
|
||
def generate_random_string(length: int = 6) -> str: | ||
return ''.join(random.choices(string.ascii_letters, k=length)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from pydantic import BaseModel, ValidationError | ||
from typing import Callable, Type | ||
|
||
def validate(model_class: Type[BaseModel]): | ||
""" | ||
Decorator factory to validate and parse parameters using a specified Pydantic model. | ||
:param model_class: The Pydantic model class to validate against | ||
""" | ||
def decorator(func: Callable): | ||
def wrapper(*args, **kwargs): | ||
try: | ||
params = model_class(**kwargs) | ||
|
||
return func(params) | ||
except ValidationError as e: | ||
print(f"Validation error: {e}") | ||
return None | ||
return wrapper | ||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.