Add #[Thinking] attribute for extended thinking / reasoning support#162
Add #[Thinking] attribute for extended thinking / reasoning support#162anilcancakir wants to merge 6 commits intolaravel:0.xfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a #[Thinking] attribute that enables agents to configure extended thinking/reasoning capabilities across multiple AI providers (Anthropic, OpenAI, Gemini, xAI, and Ollama). The implementation follows the existing attribute pattern established by #[MaxTokens], #[Temperature], and #[MaxSteps], while introducing a more complex multi-parameter design to accommodate provider-specific options.
Changes:
- Introduces a new
Thinkingattribute class with three parameters:enabled,budgetTokens, andeffort - Extends
TextGenerationOptionsto parse and store thinking configuration from agent attributes - Implements provider-specific mapping logic in
CreatesPrismTextRequeststo translate the attribute into appropriate Prism provider options
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Attributes/Thinking.php | New attribute class defining enabled flag, budget tokens, and effort level parameters |
| src/Gateway/TextGenerationOptions.php | Parses Thinking attribute via reflection and stores configuration as array in thinking property |
| src/Gateway/Prism/Concerns/CreatesPrismTextRequests.php | Maps thinking options to provider-specific formats (Anthropic, OpenAI, Gemini, xAI, Ollama) |
| tests/Feature/Agents/ThinkingAgent.php | Test agent with budgetTokens configuration |
| tests/Feature/Agents/ThinkingWithEffortAgent.php | Test agent with effort configuration |
| tests/Feature/Agents/ThinkingFullAgent.php | Test agent with all thinking parameters |
| tests/Feature/AgentAttributeTest.php | Unit tests verifying attribute parsing for various configurations |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
I really like #166 implementation for this. |
|
great feature. it is really needed, without thinking config laravel ai is unusable for many newer models that have thinking baked in |
|
I'm updating the PR for latest version now. |
…support Add #[Thinking] attribute to configure extended thinking/reasoning on AI agents with support for Anthropic (budgetTokens), OpenAI (reasoning effort), Gemini (thinkingBudget/thinkingLevel), xAI, and Ollama. Includes test coverage for all attribute parsing combinations.
- Use null-safe array_filter callbacks to preserve false/0 values - Merge OpenAI reasoning options with existing provider options to prevent overwriting structured output schema configuration - Add docblock to Thinking attribute documenting valid effort values - Extract single newInstance() call in TextGenerationOptions to avoid redundant reflection instantiation
825b7f9 to
43647f0
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
Updated with last code and providers. |
|
What keeps this from being merged? |
|
Hi, this feature is a must have, thks for the work ! |
This PR adds a
#[Thinking]attribute that allows agents to opt into extended thinking (reasoning) capabilities offered by modern AI providers. The attribute follows the same pattern as the existing#[MaxTokens],#[Temperature], and#[MaxSteps]attributes.Motivation
Several major providers now support extended thinking / reasoning modes that significantly improve output quality for complex tasks:
budget_tokensreasoning_effort(low, medium, high) for o1/o3 modelsthinkingBudgetandthinkingLevelthinking.enabledthinking.typereasoning_format+reasoning_effortprompt_modeinclude_reasoningreasoning_effort(identical to OpenAI)Currently there is no way to configure this through agent attributes.
Usage
When no
#[Thinking]attribute is present, behavior is unchanged —$options->thinkingis null and no provider options are sent.Provider Mapping
budgetTokensthinking.enabled+thinking.budgetTokenseffortreasoning.efforteffortreasoning.effort(identical to OpenAI)budgetTokens,effortthinkingBudget,thinkingLevelenabledthinking.enabledenabledthinking(bool)enabledthinking.type("enabled"/"disabled")enabled,effortreasoning_format: "parsed"+reasoning_effortenabledprompt_mode: "reasoning"enabledinclude_reasoning(bool)Changes
src/Attributes/Thinking.php— new attribute class withenabled,budgetTokens, andeffortparameterssrc/Gateway/TextGenerationOptions.php— parse#[Thinking]via reflection, addthinkingpropertysrc/Gateway/Prism/Concerns/CreatesPrismTextRequests.php— map thinking options to provider-specific Prism configurations for all 10 providers inwithProviderOptions()Test Coverage
test_thinking_attribute_with_budget_tokens_is_parsed#[Thinking(budgetTokens: 10000)]→ enabled=true, budgetTokens=10000, effort=nulltest_thinking_attribute_with_effort_is_parsed#[Thinking(effort: 'high')]→ enabled=true, budgetTokens=null, effort='high'test_thinking_attribute_with_all_options_is_parsed#[Thinking(budgetTokens: 8000, effort: 'medium')]→ all three values parsedtest_thinking_is_null_when_agent_has_no_thinking_attributetest_thinking_options_are_correct_for_deepseek_scenariotest_thinking_options_are_correct_for_groq_scenarioreasoning_efforttest_thinking_enabled_flag_is_present_for_toggle_providerstest_thinking_effort_maps_to_reasoning_effort_for_azure_and_openaireasoning.effortAll tests pass, Pint clean.