Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reasoning (extended thinking) for claude 3.7 #750

Open
wants to merge 27 commits into
base: v2
Choose a base branch
from
Open

Conversation

statefb
Copy link
Contributor

@statefb statefb commented Feb 27, 2025

Issue #, if available:
close #745

Description of changes:

This PR adds support for Claude 3.7's reasoning feature (extended thinking), allowing users to see the model's step-by-step thought process when generating responses.

Backend Changes

Core Functionality

  • Added new ReasoningContentModel to represent reasoning content returned by Claude
  • Extended the streaming handler to process reasoning blocks from the API response
  • Added enable_reasoning parameter to conversation APIs
  • Fixed type definitions to handle the new content types
  • Implemented proper state management for reasoning (similar to agent thinking)

Streaming Handler Updates

  • Added PartialReasoningContent type definition to handle reasoning blocks in the stream
  • Added on_reasoning callback to dispatch reasoning tokens to frontend
  • Stream handler now processes both regular responses and reasoning blocks separately

Bot Configuration

  • Added reasoning_params to GenerationParams with budget_tokens setting
  • Set reasonable defaults for reasoning budget (1024 tokens, which is minimum value)
  • Updated bot repository code to handle and persist the reasoning parameters
  • Made necessary adjustments to ensure backward compatibility with existing bots

API & Input

  • Extended ChatInput schema with enable_reasoning flag

Testing

  • Added new tests for reasoning functionality
  • Added test environment variables to invoke us-west-2 model

Disclaimers

  • Used # type: ignore for ReasoningModel due to incomplete boto3-stubs
  • Updated boto3 to version 1.37.0 to support the reasoning feature

Frontend Changes

UI Components

  • Added ButtonReasoning component to toggle reasoning display
  • Created ReasoningCard component to display reasoning content
  • Updated ChatMessage component to handle reasoning content
  • Enhanced input component with reasoning toggle button

State Management

  • Added reasoning state management using XState
  • Integrated reasoning into useChat hook
  • Added model-specific reasoning support detection

Configuration

  • Added budget tokens configuration in the generation parameters UI
  • Updated token limits to accommodate Claude 3.7's capabilities
  • Added relevant translations for reasoning UI elements

Disclaimers

  • Used XState for reasoning which may be considered heavyweight for this use case, but it aligns our existing agent implementation and allows for future expansion
  • Added reasoning state to useChat hook to leverage existing message posting functionality and avoid duplication

Other Changes

  • Added Pyright configuration to exclude specific directories
  • Updated model defaults to support Claude 3.7 features
  • Added proper type definitions throughout the codebase

Streaming response example (convserse API)

event: {'messageStart': {'role': 'assistant'}}
event: {'contentBlockDelta': {'delta': {'reasoningContent': {'text': 'この'}}, 'contentBlockIndex': 0}}
event: {'contentBlockDelta': {'delta': {'reasoningContent': {'text': 'パズルのような'}}, 'contentBlockIndex': 0}}
event: {'contentBlockDelta': {'delta': {'reasoningContent': {'text': '問いかけを'}}, 'contentBlockIndex': 0}}
event: {'contentBlockDelta': {'delta': {'reasoningContent': {'text': '解いていきましょう。'}}, 'contentBlockIndex': 0}}
...
event: {'contentBlockDelta': {'delta': {'reasoningContent': {'signature': 'EqgBCkYQARgCIkD...'}}, 'contentBlockIndex': 0}}
event: {'contentBlockStop': {'contentBlockIndex': 0}}
event: {'contentBlockDelta': {'delta': {'reasoningContent': {'redactedContent': b'ErQBCkYQARgCKkA...'}}, 'contentBlockIndex': 1}}
event: {'contentBlockStop': {'contentBlockIndex': 1}}
event: {'contentBlockDelta': {'delta': {'text': 'このなぞな'}}, 'contentBlockIndex': 5}}
event: {'contentBlockDelta': {'delta': {'text': 'ぞの答えは、'}}, 'contentBlockIndex': 5}}
...

Screenshots

image
image
image
image

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] Add reasoning for Claude Sonnet 3.7
2 participants