|
@@ -307,6 +307,7 @@ def convert_tools_to_description(tools: list[dict]) -> str:
|
|
|
def convert_fncall_messages_to_non_fncall_messages(
|
|
def convert_fncall_messages_to_non_fncall_messages(
|
|
|
messages: list[dict],
|
|
messages: list[dict],
|
|
|
tools: list[ChatCompletionToolParam],
|
|
tools: list[ChatCompletionToolParam],
|
|
|
|
|
+ add_in_context_learning_example: bool = True,
|
|
|
) -> list[dict]:
|
|
) -> list[dict]:
|
|
|
"""Convert function calling messages to non-function calling messages."""
|
|
"""Convert function calling messages to non-function calling messages."""
|
|
|
messages = copy.deepcopy(messages)
|
|
messages = copy.deepcopy(messages)
|
|
@@ -341,7 +342,7 @@ def convert_fncall_messages_to_non_fncall_messages(
|
|
|
# 2. USER MESSAGES (no change)
|
|
# 2. USER MESSAGES (no change)
|
|
|
elif role == 'user':
|
|
elif role == 'user':
|
|
|
# Add in-context learning example for the first user message
|
|
# Add in-context learning example for the first user message
|
|
|
- if not first_user_message_encountered:
|
|
|
|
|
|
|
+ if not first_user_message_encountered and add_in_context_learning_example:
|
|
|
first_user_message_encountered = True
|
|
first_user_message_encountered = True
|
|
|
# Check tools
|
|
# Check tools
|
|
|
if not (
|
|
if not (
|
|
@@ -751,6 +752,7 @@ def convert_non_fncall_messages_to_fncall_messages(
|
|
|
|
|
|
|
|
def convert_from_multiple_tool_calls_to_single_tool_call_messages(
|
|
def convert_from_multiple_tool_calls_to_single_tool_call_messages(
|
|
|
messages: list[dict],
|
|
messages: list[dict],
|
|
|
|
|
+ ignore_final_tool_result: bool = False,
|
|
|
) -> list[dict]:
|
|
) -> list[dict]:
|
|
|
"""Break one message with multiple tool calls into multiple messages."""
|
|
"""Break one message with multiple tool calls into multiple messages."""
|
|
|
converted_messages = []
|
|
converted_messages = []
|
|
@@ -787,7 +789,7 @@ def convert_from_multiple_tool_calls_to_single_tool_call_messages(
|
|
|
), f'Found pending tool calls but not expect to handle it with role {role}: {pending_tool_calls=}, {message=}'
|
|
), f'Found pending tool calls but not expect to handle it with role {role}: {pending_tool_calls=}, {message=}'
|
|
|
converted_messages.append(message)
|
|
converted_messages.append(message)
|
|
|
|
|
|
|
|
- if len(pending_tool_calls) > 0:
|
|
|
|
|
|
|
+ if not ignore_final_tool_result and len(pending_tool_calls) > 0:
|
|
|
raise FunctionCallConversionError(
|
|
raise FunctionCallConversionError(
|
|
|
f'Found pending tool calls but no tool result: {pending_tool_calls=}'
|
|
f'Found pending tool calls but no tool result: {pending_tool_calls=}'
|
|
|
)
|
|
)
|