feat(providers): Add Anthropic extended thinking and adapt providers to new streaming/file APIs; bump dependencies and update docs, tests and configuration
This commit is contained in:
@@ -1,8 +1,21 @@
|
||||
# SmartAI Project Hints
|
||||
|
||||
## Dependencies
|
||||
|
||||
- Uses `@git.zone/tstest` v3.x for testing (import from `@git.zone/tstest/tapbundle`)
|
||||
- `@push.rocks/smartfile` is kept at v11 to avoid migration to factory pattern
|
||||
- `@anthropic-ai/sdk` v0.71.x with extended thinking support
|
||||
- `@push.rocks/smartrequest` v5.x - uses `response.stream()` + `Readable.fromWeb()` for streaming
|
||||
|
||||
## Important Notes
|
||||
|
||||
- When extended thinking is enabled, temperature parameter must NOT be set (or set to 1)
|
||||
- The `streamNode()` method was removed in smartrequest v5, use `response.stream()` with `Readable.fromWeb()` instead
|
||||
|
||||
## Anthropic Extended Thinking Feature
|
||||
|
||||
### Overview
|
||||
|
||||
The Anthropic provider now supports extended thinking by default across all methods. Extended thinking enables Claude to spend more time reasoning about complex problems before generating responses, leading to higher quality answers for difficult questions.
|
||||
|
||||
### Configuration
|
||||
@@ -22,18 +35,19 @@ const provider = new smartai.AnthropicProvider({
|
||||
|
||||
The `extendedThinking` parameter accepts four modes:
|
||||
|
||||
| Mode | Budget Tokens | Use Case |
|
||||
|------|---------------|----------|
|
||||
| `'quick'` | 2,048 | Lightweight reasoning for simple queries |
|
||||
| `'normal'` | 8,000 | **Default** - Balanced reasoning for most tasks |
|
||||
| `'deep'` | 16,000 | Complex reasoning for difficult problems |
|
||||
| `'off'` | 0 | Disable extended thinking |
|
||||
| Mode | Budget Tokens | Use Case |
|
||||
| ---------- | ------------- | ----------------------------------------------- |
|
||||
| `'quick'` | 2,048 | Lightweight reasoning for simple queries |
|
||||
| `'normal'` | 8,000 | **Default** - Balanced reasoning for most tasks |
|
||||
| `'deep'` | 16,000 | Complex reasoning for difficult problems |
|
||||
| `'off'` | 0 | Disable extended thinking |
|
||||
|
||||
**Default Behavior**: If `extendedThinking` is not specified, it defaults to `'normal'` mode (8,000 tokens).
|
||||
|
||||
### Supported Methods
|
||||
|
||||
Extended thinking is automatically applied to all Anthropic provider methods:
|
||||
|
||||
- `chat()` - Synchronous chat
|
||||
- `chatStream()` - Streaming chat
|
||||
- `vision()` - Image analysis
|
||||
@@ -53,16 +67,19 @@ Extended thinking is automatically applied to all Anthropic provider methods:
|
||||
### Performance and Cost Implications
|
||||
|
||||
**Token Usage**:
|
||||
|
||||
- You are charged for the **full thinking tokens** generated, not just the summary
|
||||
- Higher thinking budgets may result in more thorough reasoning but increased costs
|
||||
- The budget is a **target**, not a strict limit - actual usage may vary
|
||||
|
||||
**Response Quality**:
|
||||
|
||||
- `'quick'`: Fast responses, basic reasoning
|
||||
- `'normal'`: Good balance between quality and speed (recommended for most use cases)
|
||||
- `'deep'`: Highest quality reasoning for complex problems, slower responses
|
||||
|
||||
**Recommendations**:
|
||||
|
||||
- Start with `'normal'` (default) for general usage
|
||||
- Use `'deep'` for complex analytical tasks, philosophy, mathematics, or research
|
||||
- Use `'quick'` for simple factual queries where deep reasoning isn't needed
|
||||
@@ -71,6 +88,7 @@ Extended thinking is automatically applied to all Anthropic provider methods:
|
||||
### Usage Examples
|
||||
|
||||
#### Example 1: Default (Normal Mode)
|
||||
|
||||
```typescript
|
||||
const provider = new smartai.AnthropicProvider({
|
||||
anthropicToken: process.env.ANTHROPIC_TOKEN,
|
||||
@@ -87,6 +105,7 @@ const response = await provider.chat({
|
||||
```
|
||||
|
||||
#### Example 2: Deep Thinking for Complex Analysis
|
||||
|
||||
```typescript
|
||||
const provider = new smartai.AnthropicProvider({
|
||||
anthropicToken: process.env.ANTHROPIC_TOKEN,
|
||||
@@ -103,6 +122,7 @@ const response = await provider.chat({
|
||||
```
|
||||
|
||||
#### Example 3: Quick Mode for Simple Queries
|
||||
|
||||
```typescript
|
||||
const provider = new smartai.AnthropicProvider({
|
||||
anthropicToken: process.env.ANTHROPIC_TOKEN,
|
||||
@@ -119,6 +139,7 @@ const response = await provider.chat({
|
||||
```
|
||||
|
||||
#### Example 4: Disable Thinking
|
||||
|
||||
```typescript
|
||||
const provider = new smartai.AnthropicProvider({
|
||||
anthropicToken: process.env.ANTHROPIC_TOKEN,
|
||||
@@ -135,6 +156,7 @@ const response = await provider.chat({
|
||||
```
|
||||
|
||||
#### Example 5: Extended Thinking with Vision
|
||||
|
||||
```typescript
|
||||
const provider = new smartai.AnthropicProvider({
|
||||
anthropicToken: process.env.ANTHROPIC_TOKEN,
|
||||
@@ -153,14 +175,17 @@ const analysis = await provider.vision({
|
||||
### Testing
|
||||
|
||||
Comprehensive tests for extended thinking are available in:
|
||||
|
||||
- `test/test.thinking.anthropic.ts` - Tests all thinking modes
|
||||
|
||||
Run tests with:
|
||||
|
||||
```bash
|
||||
pnpm test
|
||||
```
|
||||
|
||||
Run specific thinking tests:
|
||||
|
||||
```bash
|
||||
npx tstest test/test.thinking.anthropic.ts --verbose
|
||||
```
|
||||
@@ -168,6 +193,7 @@ npx tstest test/test.thinking.anthropic.ts --verbose
|
||||
### API Reference
|
||||
|
||||
According to Anthropic's documentation:
|
||||
|
||||
- Extended thinking is supported on Claude Sonnet 4.5, 4, 3.7, Haiku 4.5, and Opus 4.1, 4
|
||||
- The current model used is `claude-sonnet-4-5-20250929`
|
||||
- Minimum thinking budget is 1,024 tokens
|
||||
@@ -176,6 +202,7 @@ According to Anthropic's documentation:
|
||||
### Implementation Details
|
||||
|
||||
The extended thinking feature is implemented via:
|
||||
|
||||
1. **Interface**: `IAnthropicProviderOptions.extendedThinking` property
|
||||
2. **Helper Method**: `getThinkingConfig()` private method that maps modes to token budgets
|
||||
3. **API Parameter**: Adds `thinking: { type: 'enabled', budget_tokens: number }` to all API calls
|
||||
|
||||
Reference in New Issue
Block a user