|
| 1 | +# Search API Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `search_api.py` module provides a command-line interface for the Ollama Web Search functionality. This script is called by the Rust backend to perform web searches and return structured JSON results. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +### Basic Search |
| 10 | + |
| 11 | +```bash |
| 12 | +python3 ollama-web-search/main.py --query "What is the capital of France?" --json |
| 13 | +``` |
| 14 | + |
| 15 | +### With Thinking Mode |
| 16 | + |
| 17 | +```bash |
| 18 | +python3 ollama-web-search/main.py --query "Latest AI developments" --json --thinking |
| 19 | +``` |
| 20 | + |
| 21 | +### Interactive Mode |
| 22 | + |
| 23 | +```bash |
| 24 | +python3 ollama-web-search/main.py |
| 25 | +``` |
| 26 | + |
| 27 | +## Command Line Arguments |
| 28 | + |
| 29 | +| Argument | Description | Default | |
| 30 | +|----------|-------------|---------| |
| 31 | +| `--query` | Search query string | Required in JSON mode | |
| 32 | +| `--json` | Output results in JSON format | False | |
| 33 | +| `--thinking` | Enable AI thinking mode | False | |
| 34 | +| `--model` | Ollama model to use | From config | |
| 35 | +| `--history` | Show search history | False | |
| 36 | +| `--config` | Show configuration | False | |
| 37 | + |
| 38 | +## JSON Response Format |
| 39 | + |
| 40 | +### Successful Response |
| 41 | + |
| 42 | +```json |
| 43 | +{ |
| 44 | + "success": true, |
| 45 | + "user_query": "What is the capital of France?", |
| 46 | + "search_queries": ["capital France"], |
| 47 | + "summaries": [ |
| 48 | + { |
| 49 | + "query": "capital France", |
| 50 | + "summary": "Paris is the capital and largest city of France...", |
| 51 | + "sources": ["https://en.wikipedia.org/wiki/Paris"], |
| 52 | + "title": "Paris - Wikipedia" |
| 53 | + } |
| 54 | + ], |
| 55 | + "sources": ["https://en.wikipedia.org/wiki/Paris"], |
| 56 | + "error": null |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Error Response |
| 61 | + |
| 62 | +```json |
| 63 | +{ |
| 64 | + "success": false, |
| 65 | + "user_query": "search query", |
| 66 | + "search_queries": [], |
| 67 | + "summaries": [], |
| 68 | + "sources": [], |
| 69 | + "error": "Error description" |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Configuration |
| 74 | + |
| 75 | +### Configuration File (`config.json`) |
| 76 | + |
| 77 | +```json |
| 78 | +{ |
| 79 | + "model": "qwen3:0.6b", |
| 80 | + "searxng_instances": [ |
| 81 | + "http://localhost:32768", |
| 82 | + "https://search.inetol.net/search", |
| 83 | + "https://searx.be/search", |
| 84 | + "https://search.brave4u.com/search", |
| 85 | + "https://priv.au/search" |
| 86 | + ], |
| 87 | + "max_results": 8, |
| 88 | + "timeout": 10, |
| 89 | + "max_retries": 3, |
| 90 | + "history_file": "search_history.json", |
| 91 | + "enable_colors": true, |
| 92 | + "streaming_delay": 0.02 |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### Environment Variables |
| 97 | + |
| 98 | +| Variable | Description | Default | |
| 99 | +|----------|-------------|---------| |
| 100 | +| `OLLAMA_HOST` | Ollama server host | `localhost:11434` | |
| 101 | + |
| 102 | +## API Classes and Methods |
| 103 | + |
| 104 | +### WebSearchAssistant |
| 105 | + |
| 106 | +Main class that handles web search operations. |
| 107 | + |
| 108 | +#### Methods |
| 109 | + |
| 110 | +##### `generate_search_query(question: str, thinking: bool = False) -> Optional[str]` |
| 111 | + |
| 112 | +Generates an optimized search query from a user question. |
| 113 | + |
| 114 | +**Parameters:** |
| 115 | +- `question`: User's original question |
| 116 | +- `thinking`: Enable thinking mode for query generation |
| 117 | + |
| 118 | +**Returns:** Optimized search query string or None if generation fails |
| 119 | + |
| 120 | +##### `browse_web(query: str) -> Optional[List[Dict]]` |
| 121 | + |
| 122 | +Searches the web using configured SearxNG instances. |
| 123 | + |
| 124 | +**Parameters:** |
| 125 | +- `query`: Search query string |
| 126 | + |
| 127 | +**Returns:** List of search results or None if all instances fail |
| 128 | + |
| 129 | +##### `select_best_result(question: str, query: str, results: List[Dict], thinking: bool = False) -> Optional[Tuple[str, str]]` |
| 130 | + |
| 131 | +Uses AI to select the most relevant search result. |
| 132 | + |
| 133 | +**Parameters:** |
| 134 | +- `question`: Original user question |
| 135 | +- `query`: Generated search query |
| 136 | +- `results`: List of search results |
| 137 | +- `thinking`: Enable thinking mode |
| 138 | + |
| 139 | +**Returns:** Tuple of (title, url) or None if selection fails |
| 140 | + |
| 141 | +##### `retrieve_page_information(url: str) -> Optional[str]` |
| 142 | + |
| 143 | +Retrieves and cleans webpage content using Jina Reader API. |
| 144 | + |
| 145 | +**Parameters:** |
| 146 | +- `url`: URL to extract content from |
| 147 | + |
| 148 | +**Returns:** Cleaned webpage content or None if extraction fails |
| 149 | + |
| 150 | +##### `model_response(model: str, message: str, max_retries: int = 3, thinking: bool = False) -> Optional[str]` |
| 151 | + |
| 152 | +Gets response from Ollama model with thinking mode control. |
| 153 | + |
| 154 | +**Parameters:** |
| 155 | +- `model`: Ollama model name |
| 156 | +- `message`: Message to send to model |
| 157 | +- `max_retries`: Maximum retry attempts |
| 158 | +- `thinking`: Enable thinking mode |
| 159 | + |
| 160 | +**Returns:** Model response or None if all retries fail |
| 161 | + |
| 162 | +## Search Flow |
| 163 | + |
| 164 | +1. **Query Generation**: Convert user question to optimized search query |
| 165 | +2. **Web Search**: Search using SearxNG instances with fallback |
| 166 | +3. **Result Selection**: AI selects most relevant result |
| 167 | +4. **Content Extraction**: Retrieve webpage content via Jina Reader |
| 168 | +5. **Summarization**: Generate AI summary of extracted content |
| 169 | +6. **Response Formatting**: Return structured JSON response |
| 170 | + |
| 171 | +## Error Handling |
| 172 | + |
| 173 | +### Common Error Types |
| 174 | + |
| 175 | +- **Ollama Connection Error**: Cannot connect to Ollama server |
| 176 | +- **Search Instance Failure**: All SearxNG instances failed |
| 177 | +- **Content Extraction Timeout**: Jina Reader API timeout |
| 178 | +- **Model Response Error**: Ollama model response failure |
| 179 | + |
| 180 | +### Retry Logic |
| 181 | + |
| 182 | +- **Search Instances**: Automatic fallback to next instance |
| 183 | +- **Model Responses**: Exponential backoff retry (up to 3 attempts) |
| 184 | +- **Content Extraction**: Single attempt with timeout |
| 185 | + |
| 186 | +## Integration with Rust Backend |
| 187 | + |
| 188 | +### Rust Command Execution |
| 189 | + |
| 190 | +```rust |
| 191 | +let mut cmd = Command::new("python3"); |
| 192 | +cmd.arg("ollama-web-search/main.py") |
| 193 | + .arg("--query") |
| 194 | + .arg(&query) |
| 195 | + .arg("--json"); |
| 196 | + |
| 197 | +if thinking_mode { |
| 198 | + cmd.arg("--thinking"); |
| 199 | +} |
| 200 | + |
| 201 | +let output = cmd.output()?; |
| 202 | +``` |
| 203 | + |
| 204 | +### Response Parsing |
| 205 | + |
| 206 | +```rust |
| 207 | +let response = String::from_utf8_lossy(&output.stdout); |
| 208 | +let json: serde_json::Value = serde_json::from_str(&response)?; |
| 209 | + |
| 210 | +if json.get("success").and_then(|v| v.as_bool()).unwrap_or(false) { |
| 211 | + // Handle successful response |
| 212 | + format_search_results_from_python(&json, &query) |
| 213 | +} else { |
| 214 | + // Handle error |
| 215 | + let error = json.get("error").and_then(|v| v.as_str()).unwrap_or("Unknown error"); |
| 216 | + Err(format!("Python search failed: {}", error)) |
| 217 | +} |
| 218 | +``` |
| 219 | + |
| 220 | +## Testing |
| 221 | + |
| 222 | +### Unit Tests |
| 223 | + |
| 224 | +```bash |
| 225 | +# Test search query generation |
| 226 | +python3 -c " |
| 227 | +from main import WebSearchAssistant |
| 228 | +assistant = WebSearchAssistant() |
| 229 | +query = assistant.generate_search_query('What is Python?') |
| 230 | +print(f'Generated query: {query}') |
| 231 | +" |
| 232 | +``` |
| 233 | + |
| 234 | +### Integration Tests |
| 235 | + |
| 236 | +```bash |
| 237 | +# Test full search flow |
| 238 | +python3 ollama-web-search/main.py --query "test query" --json |
| 239 | + |
| 240 | +# Test thinking mode |
| 241 | +python3 ollama-web-search/main.py --query "test query" --json --thinking |
| 242 | +``` |
| 243 | + |
| 244 | +### SearxNG Connectivity Test |
| 245 | + |
| 246 | +```bash |
| 247 | +# Test local instance |
| 248 | +curl "http://localhost:32768?q=test&format=json" |
| 249 | + |
| 250 | +# Test public instance |
| 251 | +curl "https://search.inetol.net/search?q=test&format=json" |
| 252 | +``` |
| 253 | + |
| 254 | +## Performance Considerations |
| 255 | + |
| 256 | +### Optimization Tips |
| 257 | + |
| 258 | +1. **Use Local SearxNG**: Significantly faster than public instances |
| 259 | +2. **Adjust Timeouts**: Balance between speed and reliability |
| 260 | +3. **Limit Results**: Reduce `max_results` for faster processing |
| 261 | +4. **Model Selection**: Smaller models respond faster |
| 262 | + |
| 263 | +### Resource Usage |
| 264 | + |
| 265 | +- **Memory**: ~50MB for Python process |
| 266 | +- **Network**: Dependent on search instances and content extraction |
| 267 | +- **CPU**: Model inference for query generation and summarization |
| 268 | + |
| 269 | +## Security Notes |
| 270 | + |
| 271 | +### Data Flow |
| 272 | + |
| 273 | +- User queries are sent to configured SearxNG instances |
| 274 | +- Webpage content is extracted via Jina Reader API |
| 275 | +- All processing happens locally except for web requests |
| 276 | + |
| 277 | +### Privacy |
| 278 | + |
| 279 | +- No logging of user queries (with proper SearxNG configuration) |
| 280 | +- Search history stored locally only |
| 281 | +- Can be configured to use local SearxNG instance only |
| 282 | + |
| 283 | +## Contributing |
| 284 | + |
| 285 | +### Adding Features |
| 286 | + |
| 287 | +1. Fork the repository |
| 288 | +2. Add new methods to `WebSearchAssistant` class |
| 289 | +3. Update JSON response format if needed |
| 290 | +4. Add corresponding tests |
| 291 | +5. Update documentation |
| 292 | + |
| 293 | +### Reporting Issues |
| 294 | + |
| 295 | +Please include: |
| 296 | +- Python version |
| 297 | +- Ollama version and models |
| 298 | +- SearxNG instance details |
| 299 | +- Error messages and logs |
| 300 | +- Steps to reproduce |
0 commit comments