|
| 1 | +<!-- a15c577b-8dc6-4d03-8ff3-b6c4e48d5b84 d0755526-dc77-4303-8417-173bae94142e --> |
| 2 | +# Model Configuration and Multi-Provider Support |
| 3 | + |
| 4 | +## Overview |
| 5 | + |
| 6 | +Implement a centralized model configuration system that allows configuring different providers and models for each analysis step (`run_llm_analysis`, `run_user_enrichment`, `run_organization_enrichment`) with automatic retry/fallback logic. |
| 7 | + |
| 8 | +## Key Files to Modify |
| 9 | + |
| 10 | +- **New file**: `src/llm/model_config.py` - Centralized configuration |
| 11 | +- **New file**: `src/llm/repo_context.py` - Repository cloning and context generation logic |
| 12 | +- **Modify**: `src/analysis/repositories.py` - Update analysis methods to use new config |
| 13 | +- **Modify**: `src/llm/genai_model.py` - Refactor to PydanticAI with multi-provider support |
| 14 | +- **Modify**: `src/agents/user_enrichment.py` - Use configurable models |
| 15 | +- **Modify**: `src/agents/organization_enrichment.py` - Use configurable models |
| 16 | + |
| 17 | +## Implementation Details |
| 18 | + |
| 19 | +### 1. Create `src/llm/model_config.py` |
| 20 | + |
| 21 | +Dictionary-based configuration structure: |
| 22 | + |
| 23 | +```python |
| 24 | +MODEL_CONFIGS = { |
| 25 | + "run_llm_analysis": [ |
| 26 | + { |
| 27 | + "provider": "openai", |
| 28 | + "model": "gpt-4o", |
| 29 | + "max_retries": 3, |
| 30 | + "temperature": 0.2, |
| 31 | + "max_tokens": 16000, |
| 32 | + "timeout": 600.0, |
| 33 | + }, |
| 34 | + { |
| 35 | + "provider": "openrouter", |
| 36 | + "model": "google/gemini-2.5-flash", |
| 37 | + "max_retries": 3, |
| 38 | + "temperature": 0.2, |
| 39 | + "max_tokens": 16000, |
| 40 | + "timeout": 300.0, |
| 41 | + }, |
| 42 | + { |
| 43 | + "provider": "ollama", |
| 44 | + "model": "llama3.2", |
| 45 | + "base_url": "http://localhost:11434", |
| 46 | + "max_retries": 2, |
| 47 | + "temperature": 0.3, |
| 48 | + "timeout": 600.0, |
| 49 | + }, |
| 50 | + ], |
| 51 | + "run_user_enrichment": [ |
| 52 | + { |
| 53 | + "provider": "openai", |
| 54 | + "model": "gpt-4o-mini", |
| 55 | + "max_retries": 2, |
| 56 | + "temperature": 0.1, |
| 57 | + "max_tokens": 8000, |
| 58 | + "timeout": 300.0, |
| 59 | + }, |
| 60 | + ], |
| 61 | + "run_organization_enrichment": [ |
| 62 | + { |
| 63 | + "provider": "openai", |
| 64 | + "model": "gpt-4o-mini", |
| 65 | + "max_retries": 2, |
| 66 | + "temperature": 0.1, |
| 67 | + "max_tokens": 8000, |
| 68 | + "timeout": 300.0, |
| 69 | + }, |
| 70 | + ], |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +Supported parameters per provider: |
| 75 | + |
| 76 | +- **All providers**: `model`, `max_retries`, `temperature`, `timeout` |
| 77 | +- **OpenAI/OpenRouter/OpenAI-compatible**: `max_tokens`, `top_p`, `frequency_penalty`, `presence_penalty` |
| 78 | +- **Ollama**: `num_predict` (equivalent to max_tokens), `top_k`, `top_p` |
| 79 | +- **OpenAI reasoning models (o3, o4)**: `max_completion_tokens` (instead of max_tokens), no temperature |
| 80 | +- **OpenAI-compatible**: `base_url`, `api_key_env` (name of env var containing API key) |
| 81 | +- **Ollama**: `base_url` (defaults to http://localhost:11434) |
| 82 | + |
| 83 | +Environment variable override support: |
| 84 | + |
| 85 | +- `LLM_ANALYSIS_MODELS` - JSON array for run_llm_analysis models |
| 86 | +- `USER_ENRICHMENT_MODELS` - JSON array for run_user_enrichment models |
| 87 | +- `ORG_ENRICHMENT_MODELS` - JSON array for run_organization_enrichment models |
| 88 | + |
| 89 | +Provider configurations: |
| 90 | + |
| 91 | +- **OpenAI**: Standard OpenAI API |
| 92 | +- **OpenRouter**: Via openrouter.ai endpoint |
| 93 | +- **OpenAI-compatible**: Custom base_url endpoint |
| 94 | +- **Ollama**: Support both local (localhost:11434) and remote URLs |
| 95 | + |
| 96 | +### 2. Refactor `src/llm/genai_model.py` |
| 97 | + |
| 98 | +Convert `llm_request_repo_infos` to use PydanticAI Agent pattern: |
| 99 | + |
| 100 | +- Create PydanticAI agent for repository analysis |
| 101 | +- Implement multi-provider model initialization |
| 102 | +- Add retry logic with exponential backoff (2s, 4s, 8s) |
| 103 | +- Fallback to next model in list after max retries exceeded |
| 104 | +- Keep existing helper functions (clone_repo, extract_git_authors, etc.) |
| 105 | + |
| 106 | +### 3. Initialize Agents at Module Load Time |
| 107 | + |
| 108 | +**`src/llm/genai_model.py`**: |
| 109 | + |
| 110 | +- Read "run_llm_analysis" config at module initialization |
| 111 | +- Create PydanticAI agent with first model from config |
| 112 | +- Implement retry/fallback wrapper that tries models in sequence |
| 113 | +- No changes needed to `repositories.py` - just calls the same function |
| 114 | + |
| 115 | +**`src/agents/user_enrichment.py`**: |
| 116 | + |
| 117 | +- Read "run_user_enrichment" config at module initialization |
| 118 | +- Replace hardcoded `agent = Agent(model=f"openai:{os.getenv('MODEL')}")` with config-driven initialization |
| 119 | +- Wrap agent.run() with retry/fallback logic |
| 120 | +- `enrich_users()` and `enrich_users_from_dict()` remain unchanged |
| 121 | + |
| 122 | +**`src/agents/organization_enrichment.py`**: |
| 123 | + |
| 124 | +- Read "run_organization_enrichment" config at module initialization |
| 125 | +- Replace hardcoded `agent = Agent(model=f"openai:{os.getenv('MODEL')}")` with config-driven initialization |
| 126 | +- Wrap agent.run() with retry/fallback logic |
| 127 | +- `enrich_organizations()` and `enrich_organizations_from_dict()` remain unchanged |
| 128 | + |
| 129 | +This approach means: |
| 130 | + |
| 131 | +- Configuration is loaded once when modules are imported |
| 132 | +- No changes needed to `repositories.py` or function signatures |
| 133 | +- Agents are automatically configured based on model_config.py or env vars |
| 134 | +- Retry/fallback logic is transparent to callers |
| 135 | + |
| 136 | +## Retry Strategy |
| 137 | + |
| 138 | +For each model in the list: |
| 139 | + |
| 140 | +1. Try up to `max_retries` times (default: 3) |
| 141 | +2. Use exponential backoff between retries: 2^attempt seconds (2s, 4s, 8s) |
| 142 | +3. On max retries exceeded, move to next model in list |
| 143 | +4. If all models fail, raise exception with detailed error info |
| 144 | + |
| 145 | +## Provider Support Details |
| 146 | + |
| 147 | +### OpenAI |
| 148 | + |
| 149 | +- Use `pydantic_ai` with model string: `openai:gpt-4o` |
| 150 | +- API key from `OPENAI_API_KEY` env var |
| 151 | + |
| 152 | +### OpenRouter |
| 153 | + |
| 154 | +- Use `pydantic_ai` with custom HTTP client pointing to openrouter.ai |
| 155 | +- API key from `OPENROUTER_API_KEY` env var |
| 156 | + |
| 157 | +### OpenAI-compatible |
| 158 | + |
| 159 | +- Use `pydantic_ai` with custom base_url |
| 160 | +- Config: `{"provider": "openai-compatible", "base_url": "...", "api_key_env": "..."}` |
| 161 | + |
| 162 | +### Ollama |
| 163 | + |
| 164 | +- Support local: `http://localhost:11434` |
| 165 | +- Support remote: custom URL from config |
| 166 | +- Use `pydantic_ai` with model string: `ollama:llama3.2` |
| 167 | +- Config: `{"provider": "ollama", "model": "llama3.2", "base_url": "http://localhost:11434"}` |
| 168 | + |
| 169 | +## Clean Break from Old Approach |
| 170 | + |
| 171 | +- Remove support for old `MODEL` and `PROVIDER` env vars (except for backwards compatibility during transition) |
| 172 | +- All configuration comes from `model_config.py` or the new env var format (JSON arrays) |
| 173 | +- Simplify code by removing old OpenAI client initialization logic |
| 174 | +- Remove deprecated functions: `get_openrouter_response()`, `get_openai_response()` (sync versions) |
| 175 | +- Clean up `genai_model.py` by removing old pattern code |
| 176 | + |
| 177 | +### To-dos |
| 178 | + |
| 179 | +- [ ] Create src/llm/model_config.py with dictionary-based configuration structure and env var override support |
| 180 | +- [ ] Implement provider-specific helper functions for OpenAI, OpenRouter, OpenAI-compatible, and Ollama (local and remote) in model_config.py |
| 181 | +- [ ] Refactor llm_request_repo_infos in genai_model.py to use PydanticAI Agent with multi-provider support and retry/fallback logic |
| 182 | +- [ ] Update user_enrichment.py to support dynamic model configuration with retry/fallback logic |
| 183 | +- [ ] Update organization_enrichment.py to support dynamic model configuration with retry/fallback logic |
| 184 | +- [ ] Update repositories.py analysis methods to load and pass model configurations from model_config.py |
0 commit comments