|
| 1 | +# Thinking Budget Feature Implementation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the implementation of the **thinking budget** feature for LEANN, which allows users to control the computational effort for reasoning models like GPT-Oss:20b. |
| 6 | + |
| 7 | +## Feature Description |
| 8 | + |
| 9 | +The thinking budget feature provides three levels of computational effort for reasoning models: |
| 10 | +- **`low`**: Fast responses, basic reasoning (default for simple queries) |
| 11 | +- **`medium`**: Balanced speed and reasoning depth |
| 12 | +- **`high`**: Maximum reasoning effort, best for complex analytical questions |
| 13 | + |
| 14 | +## Implementation Details |
| 15 | + |
| 16 | +### 1. Command Line Interface |
| 17 | + |
| 18 | +Added `--thinking-budget` parameter to both CLI and RAG examples: |
| 19 | + |
| 20 | +```bash |
| 21 | +# LEANN CLI |
| 22 | +leann ask my-index --llm ollama --model gpt-oss:20b --thinking-budget high |
| 23 | + |
| 24 | +# RAG Examples |
| 25 | +python apps/email_rag.py --llm ollama --llm-model gpt-oss:20b --thinking-budget high |
| 26 | +python apps/document_rag.py --llm openai --llm-model o3 --thinking-budget medium |
| 27 | +``` |
| 28 | + |
| 29 | +### 2. LLM Backend Support |
| 30 | + |
| 31 | +#### Ollama Backend (`packages/leann-core/src/leann/chat.py`) |
| 32 | + |
| 33 | +```python |
| 34 | +def ask(self, prompt: str, **kwargs) -> str: |
| 35 | + # Handle thinking budget for reasoning models |
| 36 | + options = kwargs.copy() |
| 37 | + thinking_budget = kwargs.get("thinking_budget") |
| 38 | + if thinking_budget: |
| 39 | + options.pop("thinking_budget", None) |
| 40 | + if thinking_budget in ["low", "medium", "high"]: |
| 41 | + options["reasoning"] = {"effort": thinking_budget, "exclude": False} |
| 42 | +``` |
| 43 | + |
| 44 | +**API Format**: Uses Ollama's `reasoning` parameter with `effort` and `exclude` fields. |
| 45 | + |
| 46 | +#### OpenAI Backend (`packages/leann-core/src/leann/chat.py`) |
| 47 | + |
| 48 | +```python |
| 49 | +def ask(self, prompt: str, **kwargs) -> str: |
| 50 | + # Handle thinking budget for reasoning models |
| 51 | + thinking_budget = kwargs.get("thinking_budget") |
| 52 | + if thinking_budget and thinking_budget in ["low", "medium", "high"]: |
| 53 | + # Check if this is an o-series model |
| 54 | + o_series_models = ["o3", "o3-mini", "o4-mini", "o1", "o3-pro", "o3-deep-research"] |
| 55 | + if any(model in self.model for model in o_series_models): |
| 56 | + params["reasoning_effort"] = thinking_budget |
| 57 | +``` |
| 58 | + |
| 59 | +**API Format**: Uses OpenAI's `reasoning_effort` parameter for o-series models. |
| 60 | + |
| 61 | +### 3. Parameter Propagation |
| 62 | + |
| 63 | +The thinking budget parameter is properly propagated through the LEANN architecture: |
| 64 | + |
| 65 | +1. **CLI** (`packages/leann-core/src/leann/cli.py`): Captures `--thinking-budget` argument |
| 66 | +2. **Base RAG** (`apps/base_rag_example.py`): Adds parameter to argument parser |
| 67 | +3. **LeannChat** (`packages/leann-core/src/leann/api.py`): Passes `llm_kwargs` to LLM |
| 68 | +4. **LLM Interface**: Handles the parameter in backend-specific implementations |
| 69 | + |
| 70 | +## Files Modified |
| 71 | + |
| 72 | +### Core Implementation |
| 73 | +- `packages/leann-core/src/leann/chat.py`: Added thinking budget support to OllamaChat and OpenAIChat |
| 74 | +- `packages/leann-core/src/leann/cli.py`: Added `--thinking-budget` argument |
| 75 | +- `apps/base_rag_example.py`: Added thinking budget parameter to RAG examples |
| 76 | + |
| 77 | +### Documentation |
| 78 | +- `README.md`: Added thinking budget parameter to usage examples |
| 79 | +- `docs/configuration-guide.md`: Added detailed documentation and usage guidelines |
| 80 | + |
| 81 | +### Examples |
| 82 | +- `examples/thinking_budget_demo.py`: Comprehensive demo script with usage examples |
| 83 | + |
| 84 | +## Usage Examples |
| 85 | + |
| 86 | +### Basic Usage |
| 87 | +```bash |
| 88 | +# High reasoning effort for complex questions |
| 89 | +leann ask my-index --llm ollama --model gpt-oss:20b --thinking-budget high |
| 90 | + |
| 91 | +# Medium reasoning for balanced performance |
| 92 | +leann ask my-index --llm openai --model gpt-4o --thinking-budget medium |
| 93 | + |
| 94 | +# Low reasoning for fast responses |
| 95 | +leann ask my-index --llm ollama --model gpt-oss:20b --thinking-budget low |
| 96 | +``` |
| 97 | + |
| 98 | +### RAG Examples |
| 99 | +```bash |
| 100 | +# Email RAG with high reasoning |
| 101 | +python apps/email_rag.py --llm ollama --llm-model gpt-oss:20b --thinking-budget high |
| 102 | + |
| 103 | +# Document RAG with medium reasoning |
| 104 | +python apps/document_rag.py --llm openai --llm-model gpt-4o --thinking-budget medium |
| 105 | +``` |
| 106 | + |
| 107 | +## Supported Models |
| 108 | + |
| 109 | +### Ollama Models |
| 110 | +- **GPT-Oss:20b**: Primary target model with reasoning capabilities |
| 111 | +- **Other reasoning models**: Any Ollama model that supports the `reasoning` parameter |
| 112 | + |
| 113 | +### OpenAI Models |
| 114 | +- **o3, o3-mini, o4-mini, o1**: o-series reasoning models with `reasoning_effort` parameter |
| 115 | +- **GPT-OSS models**: Models that support reasoning capabilities |
| 116 | + |
| 117 | +## Testing |
| 118 | + |
| 119 | +The implementation includes comprehensive testing: |
| 120 | +- Parameter handling verification |
| 121 | +- Backend-specific API format validation |
| 122 | +- CLI argument parsing tests |
| 123 | +- Integration with existing LEANN architecture |
0 commit comments