Run tool and optionally log the result. :param function_calls: function call dict received from chat completion :param log: whether to log the tool action result :return: generator of tool action result log dicts if log is True else None
(self, function_calls, log=False)
| 292 | return logs |
| 293 | |
| 294 | async def run_tools(self, function_calls, log=False): |
| 295 | """ |
| 296 | Run tool and optionally log the result. |
| 297 | :param function_calls: function call dict received from chat completion |
| 298 | :param log: whether to log the tool action result |
| 299 | :return: generator of tool action result log dicts if log is True else None |
| 300 | """ |
| 301 | # Append function_calls message |
| 302 | self.chat_completion_messages.append({"role": "assistant", "function_calls": function_calls}) |
| 303 | |
| 304 | tool_inputs = [] |
| 305 | |
| 306 | for function_call in function_calls: |
| 307 | function_call_id = function_call["id"] |
| 308 | tool_name = function_call["name"] |
| 309 | arguments = function_call.get("arguments") or {} |
| 310 | |
| 311 | if tool_name == self.retrieval_tool_name: |
| 312 | # Retrieval |
| 313 | query_text = arguments.get("query_text") |
| 314 | if not query_text: |
| 315 | raise MessageGenerationException("Error occurred when retrieving related documents") |
| 316 | |
| 317 | retrieval_content, retrieval_results = await query_assistant_retrieval( |
| 318 | assistant=self.assistant, |
| 319 | query_text=query_text, |
| 320 | ) |
| 321 | |
| 322 | logger.debug(f"Retrieval query: {query_text}") |
| 323 | logger.debug(f"Retrieval result: {str(retrieval_results)[:200]}...") |
| 324 | |
| 325 | # Logging for retrieval |
| 326 | if log: |
| 327 | retrieval_output_log_dict = build_retrieval_output_log_dict( |
| 328 | session_id=self.session_id, |
| 329 | event_id=function_call_id, |
| 330 | retrieval_result=retrieval_results, |
| 331 | ) |
| 332 | if self.save_logs: |
| 333 | self.logs.append(retrieval_output_log_dict) |
| 334 | yield retrieval_output_log_dict |
| 335 | |
| 336 | # Append tool result message |
| 337 | self.chat_completion_messages.append( |
| 338 | {"role": "function", "content": retrieval_content, "id": function_call_id} |
| 339 | ) |
| 340 | |
| 341 | else: |
| 342 | tool_input = ToolInput( |
| 343 | type=self.tool_dict[tool_name].type, |
| 344 | tool_id=self.tool_dict[tool_name].tool_id, |
| 345 | tool_call_id=function_call_id, |
| 346 | arguments=arguments, |
| 347 | ) |
| 348 | tool_inputs.append(tool_input) |
| 349 | |
| 350 | if tool_inputs: |
| 351 | if log: |
no test coverage detected