xintongsong commented on code in PR #98:
URL: https://github.com/apache/flink-agents/pull/98#discussion_r2259224077
##########
python/flink_agents/api/agent.py:
##########
@@ -16,10 +16,141 @@
# limitations under the License.
#################################################################################
from abc import ABC
+from typing import Any, Callable, Dict, List, Tuple, Type
+from flink_agents.api.chat_models.chat_model import BaseChatModel
+from flink_agents.api.events.event import Event
+from flink_agents.api.prompts.prompt import Prompt
-class Agent(ABC): # noqa: B024
- """Base class for defining agent logic. Currently, Agent is
- an empty interface, we may add interactive method like add_action(),
- add_model() later.
+
+class Agent(ABC):
+ """Base class for defining agent logic.
+
+
+ Example:
+ Users have two ways to create an Agent
+
+ * Declare an Agent with decorators
+ ::
+
+ class MyAgent(Agent):
+ @action(InputEvent)
+ @staticmethod
+ def my_action(event: Event, ctx: RunnerContext) -> None:
+ action logic
+
+ @chat_model
+ @staticmethod
+ def my_chat_model() -> Tuple[Type[BaseChatModel], Dict[str,
Any]]:
+ return ChatModel, {"name": xxx, "arg1": xxx}
+ * Add actions and resources to an Agent instance
+ ::
+
+ my_agent = Agent()
+ my_agent.add_action(name="my_action",
+ events=[InputEvent],
+ func=action_function)
+ .add_chat_model(name="my_chat_model",
+ chat_model=ChatModel
+ arg1=xxx)
"""
+ _actions: Dict[str, Tuple[List[Type[Event]], Callable]]
+ _prompts: Dict[str, Prompt]
+ _tools: Dict[str, Callable]
+ _chat_models: Dict[str, Tuple[Type[BaseChatModel], Dict[str, Any]]]
+
+ def __init__(self) -> None:
+ """Init method."""
+ self._actions = {}
+ self._prompts = {}
+ self._tools = {}
+ self._chat_models = {}
+
+ def add_action(self, name: str, events: List[Type[Event]], func: Callable)
-> "Agent":
+ """Add action to agent.
+
+ Parameters
+ ----------
+ name : str
+ The name of the action, should be unique in the same Agent.
+ events: List[Type[Event]]
+ The type of events listened by this action.
+ func: Callable
+ The function to be executed when receive listened events.
+
+ Returns:
+ -------
+ Agent
+ The modified Agent instance.
+ """
+ if name in self._actions:
+ msg = f"Action {name} already defined"
+ raise ValueError(msg)
+ self._actions[name] = (events, func)
+ return self
+
+ def add_prompt(self, name: str, prompt: Prompt) -> "Agent":
+ """Add prompt to agent.
+
+ Parameters
+ ----------
+ name : str
+ The name of the prompt, should be unique in the same Agent.
+ prompt: Prompt
+ The prompt to be used in the agent.
+
+ Returns:
+ -------
+ Agent
+ The modified Agent instance.
+ """
+ if name in self._prompts:
+ msg = f"Prompt {name} already defined"
+ raise ValueError(msg)
+ self._prompts[name] = prompt
+ return self
+
+ def add_function_tool(self, name: str, func: Callable) -> "Agent":
Review Comment:
Let's call it `add_tool` for now, to align with the decorators.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]