@@ -122,6 +122,26 @@ def __init__(self, **kwargs):
122
122
123
123
super ().__init__ (** kwargs )
124
124
125
+ def _detect_tool (self , message : Union [str ,
126
+ dict ]) -> Tuple [bool , str , str , str ]:
127
+ assert isinstance (message , str )
128
+ text = message
129
+ func_name , func_args = None , None
130
+ i = text .rfind (ACTION_TOKEN )
131
+ j = text .rfind (ARGS_TOKEN )
132
+ k = text .rfind (OBSERVATION_TOKEN )
133
+ if 0 <= i < j : # If the text has `Action` and `Action input`,
134
+ if k < j : # but does not contain `Observation`,
135
+ # then it is likely that `Observation` is ommited by the LLM,
136
+ # because the output text may have discarded the stop word.
137
+ text = text .rstrip () + OBSERVATION_TOKEN # Add it back.
138
+ k = text .rfind (OBSERVATION_TOKEN )
139
+ func_name = text [i + len (ACTION_TOKEN ):j ].strip ()
140
+ func_args = text [j + len (ARGS_TOKEN ):k ].strip ()
141
+ text = text [:k ] # Discard '\nObservation:'.
142
+
143
+ return (func_name is not None ), func_name , func_args , text
144
+
125
145
def _run (self ,
126
146
user_request ,
127
147
history : Optional [List [Dict ]] = None ,
0 commit comments