MCPcopy Create free account
hub / github.com/Tencent/digitalhuman / Agent

Class Agent

SWF/src/base.py:37–131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

35
36@dataclass
37class Agent:
38 model_config: Dict
39 max_timesteps: int = 10
40 max_retry: int = 5
41 system: str = ""
42 inputs: str = ""
43 # culture: bool = False
44
45 def __post_init__(self) -> None:
46 self.base_urls = self.model_config['base_url']
47 self.api_key = self.model_config.get('api_key', 'EMPTY')
48 self.time_stamp = 0
49 self.history = []
50 self.usage = []
51 self.memory = defaultdict(list)
52
53 @property
54 def model_name(self):
55 return self.model_config['model_name']
56
57 @property
58 def name(self):
59 return self.model_config['name']
60
61 def is_halted(self) -> bool:
62 return self.time_stamp > self.max_timesteps
63
64 def is_finished(self, content) -> bool:
65 # print(content)
66 if '<answer>' in content and '</answer>' in content:
67 return True
68 return False
69
70 # def step(self, tasks: List[Task], *args, **kwargs):
71 # raise NotImplementedError
72
73 @property
74 def cost(self) -> float:
75 """
76 cost = number of gpu / (k token / s) = number of gpu * s / k token
77 How much compute (GPU seconds) is needed to run 1k tokens
78 """
79 speed = self.model_config.get('speed', [])
80 if len(speed) == 0:
81 raise ValueError("speed record is empty")
82 avg_speed = sum([x[1] / (x[0] / 1000) for x in speed]) / len(speed)
83 return avg_speed
84
85
86 def run(self, inst: str, pre_reset=True) -> Response:
87 """
88 inst: instruction
89 """
90 raise NotImplementedError
91
92 def generate(self, messages, **kwargs) -> Dict: #TODO
93 """
94 call LLM to generate response based on the history messages

Callers 2

_runFunction · 0.90
baseline.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected