forked from baidu-baige/LoongFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact_agent_base.py
More file actions
59 lines (47 loc) · 1.69 KB
/
react_agent_base.py
File metadata and controls
59 lines (47 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
"""
This file define reAct AgentBase
"""
from abc import abstractmethod
from typing import Any, List
from agentsdk.message import Message
from evolux.base import AgentBase
class ReactAgentBase(AgentBase):
"""
ReAct Agent base class
"""
supported_hook_types: List[str] = ["pre_run", "post_run", "pre_reason", "post_reason", "pre_act", "post_act",
"pre_observe", "post_observe"]
def __init__(self):
super().__init__()
self._wrap_react_hooks()
def _wrap_react_hooks(self):
"""
Manually wrap methods that don't follow the base class's naming convention.
"""
react_targets = {
"reason": "_reason",
"act": "_act",
"observe": "_observe"
}
for target_name, internal_method_name in react_targets.items():
func = getattr(self, internal_method_name, None)
if not func:
continue
wrapped_func = self._wrap_with_hooks(func, target_name)
setattr(self, internal_method_name, wrapped_func)
async def run(self, *args, **kwargs) -> Message:
"""Main agent logic (must be implemented by subclasses)."""
raise NotImplementedError()
async def interrupt_impl(self):
"""Custom interruption logic (optional for subclasses)."""
pass
@abstractmethod
async def _reason(self, *args, **kwargs) -> Any:
raise NotImplementedError()
@abstractmethod
async def _act(self, *args, **kwargs) -> Any:
raise NotImplementedError()
@abstractmethod
async def _observe(self, *args, **kwargs) -> Any:
raise NotImplementedError()