11import re
2- from typing import Dict , Optional
2+ from typing import Dict , List , Optional
33
4- from steamship import Block
4+ from steamship import Block , Steamship
55from steamship .agents .schema import Action , AgentContext , FinishAction , OutputParser , Tool
66
77
@@ -19,7 +19,9 @@ def parse(self, text: str, context: AgentContext) -> Action:
1919 raise RuntimeError (f"Could not parse LLM output: `{ text } `" )
2020
2121 if "AI:" in text :
22- return FinishAction (output = [Block (text = text .split ("AI:" )[- 1 ].strip ())], context = context )
22+ return FinishAction (
23+ output = ReACTOutputParser ._blocks_from_text (context .client , text ), context = context
24+ )
2325
2426 regex = r"Action: (.*?)[\n]*Action Input: (.*)"
2527 match = re .search (regex , text )
@@ -35,3 +37,22 @@ def parse(self, text: str, context: AgentContext) -> Action:
3537 input = [Block (text = action_input )],
3638 context = context ,
3739 )
40+
41+ @staticmethod
42+ def _blocks_from_text (client : Steamship , text : str ) -> List [Block ]:
43+ last_response = text .split ("AI:" )[- 1 ].strip ()
44+
45+ block_id_regex = r"(?:\[Block)?\(?([A-F0-9]{8}\-[A-F0-9]{4}\-[A-F0-9]{4}\-[A-F0-9]{4}\-[A-F0-9]{12})\)?\]?"
46+ remaining_text = last_response
47+ result_blocks : List [Block ] = []
48+ while remaining_text is not None and len (remaining_text ) > 0 :
49+ match = re .search (block_id_regex , remaining_text )
50+ if match :
51+ result_blocks .append (Block (text = remaining_text [0 : match .start ()]))
52+ result_blocks .append (Block .get (client , _id = match .group (1 )))
53+ remaining_text = remaining_text [match .end () :]
54+ else :
55+ result_blocks .append (Block (text = remaining_text ))
56+ remaining_text = ""
57+
58+ return result_blocks
0 commit comments