MCPcopy Create free account
hub / github.com/InternScience/SciReason / format

Method format

opencompass/lagent/agents/react.py:19–72  ·  view source on GitHub ↗

Generate the ReAct format prompt. Args: chat_history (List[Dict]): The history log in previous runs. inner_step (List[Dict]): The log in the current run. action_executor (ActionExecutor): the action manager to execute actions.

(self,
               chat_history: List[Dict],
               inner_step: List[Dict],
               action_executor: ActionExecutor,
               force_stop: bool = False)

Source from the content-addressed store, hash-verified

17 self.merge_adjacent_role = False
18
19 def format(self,
20 chat_history: List[Dict],
21 inner_step: List[Dict],
22 action_executor: ActionExecutor,
23 force_stop: bool = False) -> list:
24 """Generate the ReAct format prompt.
25
26 Args:
27 chat_history (List[Dict]): The history log in previous runs.
28 inner_step (List[Dict]): The log in the current run.
29 action_executor (ActionExecutor): the action manager to
30 execute actions.
31 force_stop (boolean): whether force the agent to give responses
32 under pre-defined turns.
33
34 Returns:
35 List[Dict]: ReAct format prompt.
36 """
37
38 call_protocol = self.call_protocol.format(
39 tool_description=action_executor.get_actions_info(),
40 action_names=action_executor.action_names(),
41 thought=self.thought['begin'],
42 action=self.action['begin'],
43 action_input=self.action_input['begin'],
44 response=self.response['begin'],
45 finish=self.finish['begin'],
46 )
47 formatted = []
48 formatted.append(
49 dict(role=self.first_system_role, content=call_protocol))
50 formatted += chat_history
51 formatted += inner_step
52 if force_stop:
53 formatted.append(
54 dict(role=self.system_role, content=self.force_stop))
55
56 if self.merge_adjacent_role and formatted:
57 merged = [formatted[0]] # Add the first dict
58
59 for d in formatted[1:]:
60 # If the 'role' of current dict matches with the 'role' of the
61 # last dict in merged list,
62 # append its 'content' to the 'content' of the last dict.
63 if d['role'] == merged[-1]['role']:
64 merged[-1]['content'] += d['content']
65 else:
66 # If 'role' does not match, add it as a new dict in the
67 # merged list
68 merged.append(d)
69
70 return merged
71
72 return formatted
73
74
75class ReAct(_ReAct):

Callers 5

initializeMethod · 0.45
resetMethod · 0.45
chatMethod · 0.45
chatMethod · 0.45
chatMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected