mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 11:19:56 +00:00
## Summary Adds reasoning capabilities to Python OpenAI ResponsesAgent, bringing parity with the C# implementation. This enables fine-grained control over reasoning effort for O-series models (o1, o3-mini, o4-mini) and gpt-5 that support the reasoning parameter. Fixes #12843 ## Changes ### Core Implementation - **`openai_responses_agent.py`**: Added reasoning configuration support - Constructor-level reasoning effort setting - Per-invocation reasoning effort override capability - Proper parameter validation and model compatibility checks - **`responses_agent_thread_actions.py`**: Extended thread actions to support reasoning parameters - Reasoning effort propagation through thread operations - Metadata preservation for reasoning tokens and summaries ### Sample and Tests - **`responses_agent_reasoning.py`**: Comprehensive demonstration sample - Constructor vs per-invocation reasoning configuration - Function calling integration with reasoning - Reasoning comparison scenarios (low/medium/high effort) - Error handling and troubleshooting guidance - **`test_openai_responses_agent_reasoning.py`**: Full unit test coverage - Parameter validation tests - Integration scenarios with function calling - Edge cases and error conditions ## Features ✅ **Constructor-Level Reasoning**: Set default reasoning effort when creating agents ✅ **Per-Invocation Override**: Override reasoning effort per request ✅ **Priority Hierarchy**: per-invocation > constructor > model default ✅ **Function Calling Compatible**: Works seamlessly with existing plugin system ✅ **Azure OpenAI & OpenAI Support**: Compatible with both service providers ✅ **Model Validation**: Automatic compatibility checks for O-series models nad GÜT-5 ✅ **Metadata Access**: Reasoning tokens and summaries available in response metadata ## Usage Example ```python # Constructor-level reasoning # Constructor-level reasoning configuration agent = AzureResponsesAgent( ai_model_id="gpt-5", client=client, reasoning={"effort": "low"} # Default reasoning for all requests ) # Per-invocation override response = await agent.invoke( "Solve this complex problem step by step", reasoning={"effort": "high"} ) # Invoke with reasoning callback to capture intermediate thoughts response = await agent.invoke( "Analyze this data step by step", reasoning={"effort": "high", "summary": "detailed"}, on_intermediate_message=handle_reasoning_message ) # Streaming with reasoning async for response in agent.invoke_stream( "Explain quantum computing in detail", reasoning={"effort": "high", "summary": "detailed"}, on_intermediate_message=handle_reasoning_message ): print(response.content, end="", flush=True) --------- Co-authored-by: Evan Mattson <[email protected]> Co-authored-by: Evan Mattson <[email protected]>
62 lines
2.6 KiB
Python
62 lines
2.6 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
from semantic_kernel.contents.annotation_content import AnnotationContent
|
|
from semantic_kernel.contents.audio_content import AudioContent
|
|
from semantic_kernel.contents.chat_history import ChatHistory
|
|
from semantic_kernel.contents.chat_message_content import ChatMessageContent
|
|
from semantic_kernel.contents.file_reference_content import FileReferenceContent
|
|
from semantic_kernel.contents.function_call_content import FunctionCallContent
|
|
from semantic_kernel.contents.function_result_content import FunctionResultContent
|
|
from semantic_kernel.contents.history_reducer.chat_history_reducer import ChatHistoryReducer
|
|
from semantic_kernel.contents.history_reducer.chat_history_summarization_reducer import ChatHistorySummarizationReducer
|
|
from semantic_kernel.contents.history_reducer.chat_history_truncation_reducer import ChatHistoryTruncationReducer
|
|
from semantic_kernel.contents.image_content import ImageContent
|
|
from semantic_kernel.contents.realtime_events import (
|
|
RealtimeAudioEvent,
|
|
RealtimeEvent,
|
|
RealtimeEvents,
|
|
RealtimeFunctionCallEvent,
|
|
RealtimeFunctionResultEvent,
|
|
RealtimeImageEvent,
|
|
RealtimeTextEvent,
|
|
)
|
|
from semantic_kernel.contents.reasoning_content import ReasoningContent
|
|
from semantic_kernel.contents.streaming_annotation_content import StreamingAnnotationContent
|
|
from semantic_kernel.contents.streaming_chat_message_content import StreamingChatMessageContent
|
|
from semantic_kernel.contents.streaming_file_reference_content import StreamingFileReferenceContent
|
|
from semantic_kernel.contents.streaming_reasoning_content import StreamingReasoningContent
|
|
from semantic_kernel.contents.streaming_text_content import StreamingTextContent
|
|
from semantic_kernel.contents.text_content import TextContent
|
|
from semantic_kernel.contents.utils.author_role import AuthorRole
|
|
from semantic_kernel.contents.utils.finish_reason import FinishReason
|
|
|
|
__all__ = [
|
|
"AnnotationContent",
|
|
"AudioContent",
|
|
"AuthorRole",
|
|
"ChatHistory",
|
|
"ChatHistoryReducer",
|
|
"ChatHistorySummarizationReducer",
|
|
"ChatHistoryTruncationReducer",
|
|
"ChatMessageContent",
|
|
"FileReferenceContent",
|
|
"FinishReason",
|
|
"FunctionCallContent",
|
|
"FunctionResultContent",
|
|
"ImageContent",
|
|
"RealtimeAudioEvent",
|
|
"RealtimeEvent",
|
|
"RealtimeEvents",
|
|
"RealtimeFunctionCallEvent",
|
|
"RealtimeFunctionResultEvent",
|
|
"RealtimeImageEvent",
|
|
"RealtimeTextEvent",
|
|
"ReasoningContent",
|
|
"StreamingAnnotationContent",
|
|
"StreamingChatMessageContent",
|
|
"StreamingFileReferenceContent",
|
|
"StreamingReasoningContent",
|
|
"StreamingTextContent",
|
|
"TextContent",
|
|
]
|