Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add lib deployment [WIP]

Tomas Valentacd74cbcd0ef2
diff --git a/api-service/agent/base.py b/api-service/agent/base.py
index 8ffec0efd..789e6282e 100644
--- a/api-service/agent/base.py
+++ b/api-service/agent/base.py
@@ -1,337 +1,89 @@
-import asyncio
-import uuid
+from abc import ABC, abstractmethod
+from typing import Dict, Literal
-from abc import abstractmethod
-from typing import Any, Dict, List, Literal, cast
-from agent.output.parse_output import ToolLog
-from agent.output.output_stream_parser import OutputStreamParser, Step
-from agent.output.token_callback_handler import TokenCallbackHandler
-from agent.output.work_queue import WorkQueue
-from langchain.callbacks.base import AsyncCallbackManager
-from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
-from langchain.chains.llm import LLMChain
-from langchain.schema import BaseMessage
-from langchain.tools import BaseTool
-from langchain.prompts.chat import (
- AIMessagePromptTemplate,
- BaseMessagePromptTemplate,
- ChatPromptTemplate,
- HumanMessagePromptTemplate,
- SystemMessagePromptTemplate,
-)
-from langchain.agents.tools import InvalidTool
-from agent.tools.base import create_tools
-from database.database import DeploymentState
-from models.base import ModelConfig, PromptPart, get_model
-from database import db
-from session import NodeJSPlayground
+# The schema for agent config and interaction data is serializable and
+# the format for interations with the agent is based on a modified JSON-RPC 2.0 spec (should we use less modified JSON-RPC?).
+# This way is to have a schema for communication and a tools that can for example take
+# the implementation of AgentBase class and generate REST server that implements the schema
+# that then be deployed on our platform.
-class RewriteStepsException(Exception):
- pass
+# For local testing there can be a CLI that takes the implementation of AgentBase class
+# and just starts the server and exposes exposes the functionality with a premade web UI where you can test the agent immediately.
+# Later we can start supporting other ways to generate the server, for example a gRPC or WS server.
+# This is orthogonal to the environment where the agent is deployed.
+# The agent can still can be deployed as a Python task, in a Firecracker VM, as a docker container, etc.
-class Agent:
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- self.should_pause = False
- self.canceled = False
- self.rewriting_steps = False
- self.llm_generation = None
- self.can_resume = asyncio.Event()
- @staticmethod
- async def _run_tool(
- tool_log: ToolLog,
- name_to_tool_map: Dict[str, BaseTool],
- ) -> str:
- # Otherwise we lookup the tool
- if tool_log["tool_name"] in name_to_tool_map:
- tool = name_to_tool_map[tool_log["tool_name"]]
- # We then call the tool on the tool input to get an observation
- observation = await tool.arun(
- tool_log["tool_input"],
- verbose=True,
- )
- else:
- observation = await InvalidTool().arun( # type: ignore
- tool_name=tool_log["tool_name"],
- tool_input=tool_log["tool_input"],
- verbose=True,
- )
- return observation
+JsonValue = None | int | str | bool
+JsonType = None | Dict[str, JsonValue | "JsonType"]
- @staticmethod
- def _get_prompt_part(
- prompts: List[PromptPart],
- role: Literal["user", "system"],
- type: str,
- ):
- return (
- next(
- prompt
- for prompt in prompts
- if prompt.role == role and prompt.type == type
- )
- # Double the parenthesses to escape them because the string will be prompt templated.
- .content.replace("{", "{{").replace("}", "}}")
- )
- @staticmethod
- def _create_prompt(model_config: ModelConfig, tools: List[BaseTool]):
- system_template = "\n\n".join(
- [
- Agent._get_prompt_part(model_config.prompt, "system", "prefix"),
- "\n".join([f"{tool.name}: {tool.description}" for tool in tools]),
- Agent._get_prompt_part(model_config.prompt, "system", "suffix"),
- ]
- )
- human_template = "\n\n".join(
- [
- Agent._get_prompt_part(model_config.prompt, "user", "prefix"),
- # "{agent_scratchpad}",
- ]
- )
+class AgentLog:
+ type: Literal["log", "error", "output"]
+ timestamp: str
+ data: JsonType
- messages: List[BaseMessage | BaseMessagePromptTemplate] = [
- SystemMessagePromptTemplate.from_template(system_template),
- HumanMessagePromptTemplate.from_template(human_template),
- # TODO: Check if the results are better when we add scratchpad as AI message
- AIMessagePromptTemplate.from_template("{agent_scratchpad}"),
- ]
- return ChatPromptTemplate(
- input_variables=["agent_scratchpad"],
- messages=messages,
- )
- async def _run(
- self,
- run_id: str,
- project_id: str,
- model_config: ModelConfig,
- ):
- async def change_state(state: DeploymentState) -> None:
- print(f"Run '{run_id}'", state.value, flush=True)
- await db.upsert_deployment_state(
- run_id=run_id,
- project_id=project_id,
- state=state,
- )
-
- playground = None
- await change_state(DeploymentState.Generating)
- try:
- # -----
- # Initialize callback manager and handler for controlling token stream
- callback_manager = AsyncCallbackManager([StreamingStdOutCallbackHandler()])
- self.token_handler = TokenCallbackHandler()
- callback_manager.add_handler(self.token_handler)
-
- # -----
- # Create tools
- # Create playground for code tools
- playground = NodeJSPlayground(get_envs=lambda: db.get_env_vars(project_id))
- tools = list(
- create_tools(
- run_id=run_id,
- playground=playground,
- )
- )
- self.tool_names = [tool.name for tool in tools]
- tool_map = {tool.name: tool for tool in tools}
-
- # Assign callback manager to tools
- for tool in tools:
- tool.callback_manager = callback_manager
-
- # -----
- # Create LLM from specified model
- llm_chain = LLMChain(
- llm=get_model(model_config, callback_manager),
- prompt=self._create_prompt(model_config, tools),
- callback_manager=callback_manager,
- )
-
- # -----
- # Create log handlers
- # Used for sending logs to db/frontend without blocking
- ws_streamer = WorkQueue[List[Step]](
- on_workload=lambda steps: self._notify("steps", {"steps": steps})
- )
- db_streamer = WorkQueue[List[Step]](
- on_workload=lambda steps: db.upsert_deployment_steps(
- run_id=self.run_id,
- steps=steps,
- project_id=project_id,
- )
- )
-
- def stream_steps(steps: List[Step]):
- db_streamer.schedule(steps)
- ws_streamer.schedule(steps)
-
- # Used for parsing token stream into logs+actions
- self._output_parser = OutputStreamParser(tool_names=self.tool_names)
-
- # This function is used to inject information from previous steps to the current prompt
- # TODO: Improve the scratchpad handling and prompts for templates
- def get_agent_scratchpad():
- steps = self._output_parser.get_steps()
-
- print("stps >><<", steps)
- if (
- len(steps) == 0
- or len(steps) == 1
- and not self._output_parser._token_buffer
- ):
- return ""
-
- agent_scratchpad = ""
- for step in steps:
- agent_scratchpad += step["output"]
-
- tool_logs = (
- cast(ToolLog, action)
- for action in step["logs"]
- if action["type"] == "tool"
- )
-
- tool_outputs = "\n".join(
- log.get("tool_output", "")
- for log in tool_logs
- if log.get("tool_output")
- )
+AgentConfig = JsonType
- if tool_outputs:
- agent_scratchpad += f"\nObservation: {tool_outputs}\nThought:"
- return f"This is your previous work (you can continue where you left off):\n{agent_scratchpad}"
+class AgentInteraction:
+ interaction_id: str
+ type: str
+ data: JsonType
- print("Generating...", flush=True)
- while True:
- try:
- await self._check_interrupt()
- # Query the LLM in background
- scratchpad = get_agent_scratchpad()
- self.llm_generation = asyncio.create_task(
- llm_chain.agenerate(
- [
- {
- "agent_scratchpad": scratchpad,
- "stop": "Observation:",
- }
- ]
- )
- )
- while True:
- await self._check_interrupt()
- # Consume the tokens from LLM
- token = await self.token_handler.retrieve_token()
- if token is None:
- break
+class AgentInteractionResponse:
+ data: JsonType
- await self._check_interrupt()
- self._output_parser.ingest_token(token)
- stream_steps(self._output_parser.get_steps())
- current_step = self._output_parser.get_current_step()
+class AgentInteractionRequest:
+ interaction_id: str
+ type: str
+ data: JsonType
- # Check if the run is finished
- if any(
- keyword in current_step["output"]
- for keyword in ("Final Answer", "FinalAnswer")
- ):
- break
- for tool in (
- cast(ToolLog, action)
- for action in current_step["logs"]
- if action["type"] == "tool"
- ):
- await self._check_interrupt()
- tool_output = await self._run_tool(
- tool,
- name_to_tool_map=tool_map,
- )
- await self._check_interrupt()
- self._output_parser.ingest_tool_output(tool_output)
- stream_steps(self._output_parser.get_steps())
+class AgentBase(ABC):
+ """
+ This is an abstract class that defines the interface for an agent.
- except RewriteStepsException:
- print("REWRITING")
- continue
+ The methods defined here are the ones needed to run an agent.
+ They closely follow our spec for agent deployment.
- await change_state(DeploymentState.Finished)
- except:
- await change_state(DeploymentState.Error)
- raise
- finally:
- if playground is not None:
- playground.close()
- await self._close()
-
- async def _check_interrupt(self):
- if self.canceled:
- raise Exception("Canceled")
-
- if self.should_pause:
- await self.can_resume.wait()
- self.can_resume.clear()
-
- if self.rewriting_steps:
- self.rewriting_steps = False
- raise RewriteStepsException()
+ Class implementing this interface can be used to deploy agents via the "library deployment" as a Python task
+ or via the FC by wrapping the class in a server that exposes the methods and starting the server in an FC.
+ """
+ @classmethod
@abstractmethod
- async def _notify(self, method: str, params: Dict[str, Any]):
+ def create(cls, config: AgentConfig) -> "AgentBase":
+ """Create an agent with the given config without starting it."""
pass
@abstractmethod
- async def _close():
+ async def start(self):
+ """Start the agent. Started agent can immediately be interacting with the the world."""
pass
- async def start(self, project_id: str, model_config: Dict[str, Any]):
- print("Start agent run")
- self.project_id = project_id
- self.run_id = str(uuid.uuid4())
- asyncio.create_task(
- self._run(
- run_id=self.run_id,
- project_id=project_id,
- model_config=ModelConfig(**model_config),
- )
- )
- return {"run_id": self.run_id}
-
- async def pause(self):
- print("Pause agent run")
- self.should_pause = True
+ @abstractmethod
+ async def stop(self):
+ """Stop the agent and clean used resources. Stopped agent cannot be started again."""
+ pass
- async def resume(self):
- print("Resume agent run")
- self.should_pause = False
- self.can_resume.set()
+ @abstractmethod
+ async def interaction(
+ self,
+ interaction: AgentInteraction,
+ ) -> AgentInteractionResponse:
+ pass
- async def cancel(self):
- print("Cancel agent run")
- self.canceled = True
- if self.llm_generation:
- self.llm_generation.cancel()
- await self._close()
+ @abstractmethod
+ async def handle_log(self, log: AgentLog):
+ pass
- async def rewrite_steps(self, steps: List[Step]):
- print("Rewrite agent run steps")
- await self.pause()
- self.rewriting_steps = True
- if self.llm_generation:
- self.llm_generation.cancel()
- # TODO: Communicate the rewriting in model prompt - inform about what was rewritten.
- # TODO: Handle token that is generated after a rewrite - if the current step seems "finished" the model still outputs string (usually something like . or :).
- self._output_parser = OutputStreamParser(
- tool_names=self.tool_names,
- steps=steps[:-1],
- buffered_step=steps[-1],
- )
- await self.resume()
+ @abstractmethod
+ async def handle_interaction_request(self, request: AgentInteractionRequest):
+ pass
diff --git a/api-service/agent/simple_agent.py b/api-service/agent/simple_agent.py
new file mode 100644
index 000000000..daa79f1b7
--- /dev/null
+++ b/api-service/agent/simple_agent.py
@@ -0,0 +1,338 @@
+import asyncio
+import uuid
+
+from abc import abstractmethod
+from typing import Any, Dict, List, Literal, cast
+from agent.output.parse_output import ToolLog
+from agent.output.output_stream_parser import OutputStreamParser, Step
+from agent.output.token_callback_handler import TokenCallbackHandler
+from agent.output.work_queue import WorkQueue
+from langchain.callbacks.base import AsyncCallbackManager
+from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
+from langchain.chains.llm import LLMChain
+from langchain.schema import BaseMessage
+from langchain.tools import BaseTool
+from langchain.prompts.chat import (
+ AIMessagePromptTemplate,
+ BaseMessagePromptTemplate,
+ ChatPromptTemplate,
+ HumanMessagePromptTemplate,
+ SystemMessagePromptTemplate,
+)
+from langchain.agents.tools import InvalidTool
+
+from agent.tools.base import create_tools
+from database.database import DeploymentState
+from models.base import ModelConfig, PromptPart, get_model
+from agent.base import AgentBase
+from database import db
+from session import NodeJSPlayground
+
+
+class RewriteStepsException(Exception):
+ pass
+
+
+class SimpleAgent(AgentBase):
+ def __init__(self, config: ModelConfig):
+ self.config = config
+ self.should_pause = False
+ self.canceled = False
+ self.rewriting_steps = False
+ self.llm_generation = None
+ self.can_resume = asyncio.Event()
+
+ @staticmethod
+ async def _run_tool(
+ tool_log: ToolLog,
+ name_to_tool_map: Dict[str, BaseTool],
+ ) -> str:
+ # Otherwise we lookup the tool
+ if tool_log["tool_name"] in name_to_tool_map:
+ tool = name_to_tool_map[tool_log["tool_name"]]
+ # We then call the tool on the tool input to get an observation
+ observation = await tool.arun(
+ tool_log["tool_input"],
+ verbose=True,
+ )
+ else:
+ observation = await InvalidTool().arun( # type: ignore
+ tool_name=tool_log["tool_name"],
+ tool_input=tool_log["tool_input"],
+ verbose=True,
+ )
+ return observation
+
+ @staticmethod
+ def _get_prompt_part(
+ prompts: List[PromptPart],
+ role: Literal["user", "system"],
+ type: str,
+ ):
+ return (
+ next(
+ prompt
+ for prompt in prompts
+ if prompt.role == role and prompt.type == type
+ )
+ # Double the parenthesses to escape them because the string will be prompt templated.
+ .content.replace("{", "{{").replace("}", "}}")
+ )
+
+ @staticmethod
+ def _create_prompt(model_config: ModelConfig, tools: List[BaseTool]):
+ system_template = "\n\n".join(
+ [
+ SimpleAgent._get_prompt_part(model_config.prompt, "system", "prefix"),
+ "\n".join([f"{tool.name}: {tool.description}" for tool in tools]),
+ SimpleAgent._get_prompt_part(model_config.prompt, "system", "suffix"),
+ ]
+ )
+ human_template = "\n\n".join(
+ [
+ SimpleAgent._get_prompt_part(model_config.prompt, "user", "prefix"),
+ "{agent_scratchpad}",
+ ]
+ )
+
+ messages: List[BaseMessage | BaseMessagePromptTemplate] = [
+ SystemMessagePromptTemplate.from_template(system_template),
+ HumanMessagePromptTemplate.from_template(human_template),
+ # TODO: Check if the results are better when we add scratchpad as AI message
+ # AIMessagePromptTemplate.from_template("{agent_scratchpad}"),
+ ]
+ return ChatPromptTemplate(
+ input_variables=["agent_scratchpad"],
+ messages=messages,
+ )
+
+ async def _run(
+ self,
+ run_id: str,
+ project_id: str,
+ model_config: ModelConfig,
+ ):
+ async def change_state(state: DeploymentState) -> None:
+ print(f"Run '{run_id}'", state.value, flush=True)
+ await db.upsert_deployment_state(
+ run_id=run_id,
+ project_id=project_id,
+ state=state,
+ )
+
+ playground = None
+ await change_state(DeploymentState.Generating)
+ try:
+ # -----
+ # Initialize callback manager and handler for controlling token stream
+ callback_manager = AsyncCallbackManager([StreamingStdOutCallbackHandler()])
+ self.token_handler = TokenCallbackHandler()
+ callback_manager.add_handler(self.token_handler)
+
+ # -----
+ # Create tools
+ # Create playground for code tools
+ playground = NodeJSPlayground(get_envs=lambda: db.get_env_vars(project_id))
+ tools = list(
+ create_tools(
+ run_id=run_id,
+ playground=playground,
+ )
+ )
+ self.tool_names = [tool.name for tool in tools]
+ tool_map = {tool.name: tool for tool in tools}
+
+ # Assign callback manager to tools
+ for tool in tools:
+ tool.callback_manager = callback_manager
+
+ # -----
+ # Create LLM from specified model
+ llm_chain = LLMChain(
+ llm=get_model(model_config, callback_manager),
+ prompt=self._create_prompt(model_config, tools),
+ callback_manager=callback_manager,
+ )
+
+ # -----
+ # Create log handlers
+ # Used for sending logs to db/frontend without blocking
+ ws_streamer = WorkQueue[List[Step]](
+ on_workload=lambda steps: self._notify("steps", {"steps": steps})
+ )
+ db_streamer = WorkQueue[List[Step]](
+ on_workload=lambda steps: db.upsert_deployment_steps(
+ run_id=self.run_id,
+ steps=steps,
+ project_id=project_id,
+ )
+ )
+
+ def stream_steps(steps: List[Step]):
+ db_streamer.schedule(steps)
+ ws_streamer.schedule(steps)
+
+ # Used for parsing token stream into logs+actions
+ self._output_parser = OutputStreamParser(tool_names=self.tool_names)
+
+ # This function is used to inject information from previous steps to the current prompt
+ # TODO: Improve the scratchpad handling and prompts for templates
+ def get_agent_scratchpad():
+ steps = self._output_parser.get_steps()
+
+ print("stps >><<", steps)
+ if (
+ len(steps) == 0
+ or len(steps) == 1
+ and not self._output_parser._token_buffer
+ ):
+ return ""
+
+ agent_scratchpad = ""
+ for step in steps:
+ agent_scratchpad += step["output"]
+
+ tool_logs = (
+ cast(ToolLog, action)
+ for action in step["logs"]
+ if action["type"] == "tool"
+ )
+
+ tool_outputs = "\n".join(
+ log.get("tool_output", "")
+ for log in tool_logs
+ if log.get("tool_output")
+ )
+
+ if tool_outputs:
+ agent_scratchpad += f"\nObservation: {tool_outputs}\nThought:"
+
+ return f"This is your previous work (you can continue where you left off):\n{agent_scratchpad}"
+
+ print("Generating...", flush=True)
+ while True:
+ try:
+ await self._check_interrupt()
+ # Query the LLM in background
+ scratchpad = get_agent_scratchpad()
+ self.llm_generation = asyncio.create_task(
+ llm_chain.agenerate(
+ [
+ {
+ "agent_scratchpad": scratchpad,
+ "stop": "Observation:",
+ }
+ ]
+ )
+ )
+ while True:
+ await self._check_interrupt()
+ # Consume the tokens from LLM
+ token = await self.token_handler.retrieve_token()
+
+ if token is None:
+ break
+
+ await self._check_interrupt()
+ self._output_parser.ingest_token(token)
+ stream_steps(self._output_parser.get_steps())
+
+ current_step = self._output_parser.get_current_step()
+
+ # Check if the run is finished
+ if any(
+ keyword in current_step["output"]
+ for keyword in ("Final Answer", "FinalAnswer")
+ ):
+ break
+
+ for tool in (
+ cast(ToolLog, action)
+ for action in current_step["logs"]
+ if action["type"] == "tool"
+ ):
+ await self._check_interrupt()
+ tool_output = await self._run_tool(
+ tool,
+ name_to_tool_map=tool_map,
+ )
+ await self._check_interrupt()
+ self._output_parser.ingest_tool_output(tool_output)
+ stream_steps(self._output_parser.get_steps())
+
+ except RewriteStepsException:
+ print("REWRITING")
+ continue
+
+ await change_state(DeploymentState.Finished)
+ except:
+ await change_state(DeploymentState.Error)
+ raise
+ finally:
+ if playground is not None:
+ playground.close()
+ await self._close()
+
+ async def _check_interrupt(self):
+ if self.canceled:
+ raise Exception("Canceled")
+
+ if self.should_pause:
+ await self.can_resume.wait()
+ self.can_resume.clear()
+
+ if self.rewriting_steps:
+ self.rewriting_steps = False
+ raise RewriteStepsException()
+
+ @abstractmethod
+ async def _notify(self, method: str, params: Dict[str, Any]):
+ pass
+
+ @abstractmethod
+ async def _close():
+ pass
+
+ async def start(self, project_id: str, model_config: Dict[str, Any]):
+ print("Start agent run")
+ self.project_id = project_id
+ self.run_id = str(uuid.uuid4())
+ asyncio.create_task(
+ self._run(
+ run_id=self.run_id,
+ project_id=project_id,
+ model_config=ModelConfig(**model_config),
+ )
+ )
+ return {"run_id": self.run_id}
+
+ async def pause(self):
+ print("Pause agent run")
+ self.should_pause = True
+
+ async def resume(self):
+ print("Resume agent run")
+ self.should_pause = False
+ self.can_resume.set()
+
+ async def cancel(self):
+ print("Cancel agent run")
+ self.canceled = True
+ if self.llm_generation:
+ self.llm_generation.cancel()
+ await self._close()
+
+ async def rewrite_steps(self, steps: List[Step]):
+ print("Rewrite agent run steps")
+ await self.pause()
+ self.rewriting_steps = True
+ if self.llm_generation:
+ self.llm_generation.cancel()
+ # TODO: Communicate the rewriting in model prompt - inform about what was rewritten.
+ # TODO: Handle token that is generated after a rewrite - if the current step seems "finished" the model still outputs string (usually something like . or :).
+ self._output_parser = OutputStreamParser(
+ tool_names=self.tool_names,
+ steps=steps[:-1],
+ buffered_step=steps[-1],
+ )
+ await self.resume()
diff --git a/api-service/agent/ws.py b/api-service/agent/ws.py
index f4ea1a4a8..cb034ad4f 100644
--- a/api-service/agent/ws.py
+++ b/api-service/agent/ws.py
@@ -1,10 +1,10 @@
from typing import Any, Dict, List
from fastapi import WebSocket
-from agent.base import Agent
+from agent.simple_agent import SimpleAgent
-class WebsocketAgentRun(Agent):
+class WebsocketAgentRun(SimpleAgent):
jsonrpc_version = "2.0"
def __init__(self, websocket: WebSocket, **kwargs):
diff --git a/api-service/app.py b/api-service/app.py
index 093ad9db2..13d1822f5 100644
--- a/api-service/app.py
+++ b/api-service/app.py
@@ -3,7 +3,10 @@ from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from models.base import ModelConfig
+
+from deployment.library_deployment import LibraryDeployment
from agent.ws import WebsocketAgentRun
+from agent.simple_agent import SimpleAgent
# TODO: Fix proxying - https://fastapi.tiangolo.com/advanced/behind-a-proxy/
app = FastAPI(title="e2b-api-service")
@@ -15,6 +18,8 @@ app.add_middleware(
allow_headers=["*"],
)
+deployment_manager = LibraryDeployment(agent_factory=SimpleAgent.create)
+debug_agent_manager = LibraryDeployment(agent_factory=SimpleAgent.create)
@app.websocket("/dev/agent")
async def ws_agent_run(websocket: WebSocket):
@@ -30,20 +35,21 @@ async def health():
class DeployConfig(BaseModel):
pass
+
class AgentCreateBody(BaseModel):
model_config: ModelConfig
deploy_config: DeployConfig
-async def create_agent(body: AgentCreateBody):
- pass
-
-
class AgentRunBody(BaseModel):
instructions: str
+async def create_agent(body: AgentCreateBody):
+ await deployment_manager.create_deployed_agent(body.model_config)
+
+
@app.post("/agent/{id}/run")
async def run_agent(id: str, body: AgentRunBody):
pass
diff --git a/api-service/deployment/adapter.py b/api-service/deployment/adapter.py
index f86643e22..eef664f50 100644
--- a/api-service/deployment/adapter.py
+++ b/api-service/deployment/adapter.py
@@ -1,22 +1,33 @@
-from typing import List
+from typing import List, Tuple
from abc import ABC, abstractmethod
-from agent.base import Agent
+from agent.base import AgentBase, AgentConfig
class DeploymentAdapter(ABC):
+ """
+ This is an abstract class that defines the interface for a deployment adapter.
+
+ With this interface, we can create different deployment adapters that can be used
+ to deploy and manage agents in different ways.
+
+ For example, we can have a deployment adapter that deploys agents created by our library (without the need to use FCs right now) as Python tasks,
+ a deployment adapter that deploys agents with arbiratry code in a Firecracker VM when we need it,
+ or a deployment adapter that deploys agents as a docker container later.
+ """
+
@abstractmethod
- async def create_deployed_agent(self, deployment):
+ async def create_deployed_agent(self, config: AgentConfig) -> Tuple[str, AgentBase]:
pass
@abstractmethod
- async def remove_deployed_agent(self, deploymen_id: str):
+ async def remove_deployed_agent(self, deployment_id: str):
pass
@abstractmethod
- async def get_deployed_agent(self, deployment_id: str) -> Agent:
+ async def get_deployed_agent(self, deployment_id: str) -> AgentBase:
pass
@abstractmethod
- async def get_deployed_agents(self) -> List[Agent]:
+ async def get_deployed_agents(self) -> List[Tuple[str, AgentBase]]:
pass
diff --git a/api-service/deployment/library_deployment.py b/api-service/deployment/library_deployment.py
index f792ce926..409725fa5 100644
--- a/api-service/deployment/library_deployment.py
+++ b/api-service/deployment/library_deployment.py
@@ -1,24 +1,31 @@
-from typing import Dict
+import uuid
-from deployment.adapter import DeploymentAdapter
-from agent.base import Agent
+from typing import Callable, Dict, List, Tuple
-deployments: Dict[str, Agent] = {}
+from deployment.adapter import DeploymentAdapter
+from agent.base import AgentBase, AgentConfig
class LibraryDeployment(DeploymentAdapter):
- def __init__(self, deployment):
- self.deployment = deployment
-
- async def create_deployed_agent(self, config:):
+ def __init__(self, agent_factory: Callable[[AgentConfig], AgentBase]) -> None:
+ self._agent_factory = agent_factory
+ self._deployments: Dict[str, AgentBase] = {}
- pass
+ async def create_deployed_agent(self, config: AgentConfig):
+ agent = self._agent_factory(config)
+ id = str(uuid.uuid4())
+ self._deployments[id] = agent
+ return (id, agent)
async def remove_deployed_agent(self, deployment_id: str):
- pass
+ agent = await self.get_deployed_agent(deployment_id)
+ await agent.stop()
+ del self._deployments[deployment_id]
async def get_deployed_agent(self, deployment_id: str):
- pass
+ return self._deployments[deployment_id]
- async def get_deployed_agents(self):
- pass
+ async def get_deployed_agents(self) -> List[Tuple[str, AgentBase]]:
+ return [
+ (deployment_id, agent) for deployment_id, agent in self._deployments.items()
+ ]