Skip to content

Commit 6158c5b

Browse files
authored
Python: Fix FC stepwise planner. (microsoft#6357)
### Motivation and Context The FC stepwise planner was breaking when trying to process the function call result because if a filter is not configured, then the context will be None. We need to only try to extract the FC result if the context is not none, otherwise it will already be present in the chat history. <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description Check that the FC result context is not None, if so, then extract the FC result, otherwise proceed as the FC result is in the chat history. Fixes microsoft#6350. - Removes some extranous logging from json schema addition. <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄
1 parent f1dab8f commit 6158c5b

File tree

5 files changed

+8
-12
lines changed

5 files changed

+8
-12
lines changed

python/samples/concepts/plugins/openai_function_calling_with_custom_plugin.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Copyright (c) Microsoft. All rights reserved.
22

3-
43
import asyncio
54
from typing import Annotated
65

python/semantic_kernel/planners/function_calling_stepwise_planner/function_calling_stepwise_planner.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,13 @@ async def invoke(
196196
request_index=0,
197197
function_call_behavior=prompt_execution_settings.function_call_behavior,
198198
)
199-
frc = FunctionResultContent.from_function_call_content_and_result(
200-
function_call_content=item, result=context.function_result
201-
)
202-
chat_history_for_steps.add_message(message=frc.to_chat_message_content())
199+
if context is not None:
200+
# Only add the function result content to the chat history if the context is present
201+
# which means it wasn't added in the _process_function_call method
202+
frc = FunctionResultContent.from_function_call_content_and_result(
203+
function_call_content=item, result=context.function_result
204+
)
205+
chat_history_for_steps.add_message(message=frc.to_chat_message_content())
203206
except Exception as exc:
204207
frc = FunctionResultContent.from_function_call_content_and_result(
205208
function_call_content=item,

python/semantic_kernel/prompt_template/utils/jinja2_system_helpers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ def _double_close():
6161

6262

6363
def _array(*args, **kwargs):
64-
print(f"Received args: {args}")
6564
return list(args)
6665

6766

python/semantic_kernel/schema/kernel_json_schema_builder.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class KernelJsonSchemaBuilder:
2626
@classmethod
2727
def build(cls, parameter_type: type | str, description: str | None = None) -> dict[str, Any]:
2828
"""Builds JSON schema for a given parameter type."""
29-
print(f"Building schema for type: {parameter_type}")
3029

3130
if isinstance(parameter_type, str):
3231
return cls.build_from_type_name(parameter_type, description)
@@ -56,7 +55,6 @@ def build_model_schema(cls, model: type, description: str | None = None) -> dict
5655
if description:
5756
schema["description"] = description
5857

59-
print(f"Generated schema for model {model}: {schema}")
6058
return schema
6159

6260
@classmethod
@@ -67,13 +65,11 @@ def build_from_type_name(cls, parameter_type: str, description: str | None = Non
6765
if description:
6866
schema["description"] = description
6967

70-
print(f"Generated schema from type name {parameter_type}: {schema}")
7168
return schema
7269

7370
@classmethod
7471
def get_json_schema(cls, parameter_type: type) -> dict[str, Any]:
7572
"""Gets JSON schema for a given parameter type."""
7673
type_name = TYPE_MAPPING.get(parameter_type, "object")
7774
schema = {"type": type_name}
78-
print(f"Generated JSON schema for type {parameter_type}: {schema}")
7975
return schema

python/tests/integration/planning/function_calling_stepwise_planner/test_int_function_calling_stepwise_planner.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020

2121
@pytest.mark.asyncio
22-
@pytest.mark.xfail(reason="This test is flaky and needs investigation.")
2322
async def test_can_execute_function_calling_stepwise_plan(kernel: Kernel):
2423

2524
service_id = "planner"
@@ -47,4 +46,4 @@ async def test_can_execute_function_calling_stepwise_plan(kernel: Kernel):
4746
result = await planner.invoke(kernel, question)
4847
print(f"Q: {question}\nA: {result.final_answer}\n")
4948
assert isinstance(result, FunctionCallingStepwisePlannerResult)
50-
assert 0 < len(result.final_answer) < 100
49+
assert 0 < len(result.final_answer)

0 commit comments

Comments
 (0)