From 32b1a865401921b083c44d8e3d1e643e1835ebf4 Mon Sep 17 00:00:00 2001 From: yalu4 Date: Thu, 25 Apr 2024 16:30:13 +0800 Subject: [PATCH] update --- .../promptflow/tools/common.py | 38 +++++-------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/src/promptflow-tools/promptflow/tools/common.py b/src/promptflow-tools/promptflow/tools/common.py index c003045bbed3..c0d9d9c89a16 100644 --- a/src/promptflow-tools/promptflow/tools/common.py +++ b/src/promptflow-tools/promptflow/tools/common.py @@ -245,7 +245,11 @@ def try_parse_tool_calls(role_prompt): pattern = r"\n*#{0,2}\s*tool_calls:\n*\s*(\[.*?\])" match = re.search(pattern, role_prompt, re.DOTALL) if match: - return match.group(1) + try: + parsed_array = eval(match.group(1)) + return parsed_array + except Exception: + None return None @@ -253,30 +257,6 @@ def is_tool_chunk(last_message): return last_message and "role" in last_message and last_message["role"] == "tool" and "content" not in last_message -def is_assistant_tool_calls_chunk(last_message, chunk): - # An valid tool calls chunk should be like: - # ## tool_calls: - # [{ id: tool_call_id, type: tool_type, function: {name: function_name, arguments: function_args }] - is_candidate = ( - last_message - and "role" in last_message - and last_message["role"] == "assistant" - and "tool_calls" in chunk - ) - # Check if the chunk is not an llm output like this: - # {'content': 'Sorry, ...', 'role': 'assistant', 'function_call': None, 'tool_calls': None} - if is_candidate: - try: - # If the chunk is a serialized dict, it should be an llm output. - # Then possibly it is not an assistant tool calls chunk. - json.loads(chunk) - return False - except Exception: - return True - else: - return False - - def parse_tool_calls_for_assistant(last_message, chunk): parsed_result = try_parse_tool_calls(chunk) error_msg = ( @@ -338,9 +318,11 @@ def parse_chat( parse_tools(last_message, chunk, hash2images, image_detail) continue - if is_assistant_tool_calls_chunk(last_message, chunk): - parse_tool_calls_for_assistant(last_message, chunk) - continue + if last_message and "role" in last_message and last_message["role"] == "assistant": + parsed_result = try_parse_tool_calls(chunk) + if parsed_result is not None: + last_message["tool_calls"] = parsed_result + continue if ( last_message