Unified LLM client supporting multiple providers. Supports OpenAI-compatible APIs including: - OpenRouter - Bianxie - Google Gemini (via OpenAI-compatible endpoint) - Any OpenAI-compatible endpoint
| 13 | |
| 14 | |
| 15 | class LLMClient: |
| 16 | """ |
| 17 | Unified LLM client supporting multiple providers. |
| 18 | |
| 19 | Supports OpenAI-compatible APIs including: |
| 20 | - OpenRouter |
| 21 | - Bianxie |
| 22 | - Google Gemini (via OpenAI-compatible endpoint) |
| 23 | - Any OpenAI-compatible endpoint |
| 24 | """ |
| 25 | |
| 26 | def __init__( |
| 27 | self, |
| 28 | api_key: str, |
| 29 | base_url: str = "https://openrouter.ai/api/v1", |
| 30 | model: str = "google/gemini-3.1-pro-preview", |
| 31 | provider: str = "openrouter", |
| 32 | ): |
| 33 | """ |
| 34 | Initialize LLM client. |
| 35 | |
| 36 | Args: |
| 37 | api_key: API key for the provider |
| 38 | base_url: Base URL for the API endpoint |
| 39 | model: Model name to use |
| 40 | provider: Provider name (openrouter, bianxie, gemini) |
| 41 | """ |
| 42 | self.api_key = api_key |
| 43 | self.base_url = base_url |
| 44 | self.model = model |
| 45 | self.provider = provider |
| 46 | |
| 47 | # Adjust base_url for Gemini |
| 48 | if provider == "gemini" and base_url: |
| 49 | if not base_url.endswith("/openai/") and not base_url.endswith("/openai"): |
| 50 | if base_url.endswith("/"): |
| 51 | self.base_url = base_url + "openai/" |
| 52 | else: |
| 53 | self.base_url = base_url + "/openai/" |
| 54 | |
| 55 | def call( |
| 56 | self, |
| 57 | contents: List[Any], |
| 58 | temperature: float = 0.7, |
| 59 | max_tokens: Optional[int] = None, |
| 60 | ) -> Optional[str]: |
| 61 | """ |
| 62 | Call the LLM with text and optional images. |
| 63 | |
| 64 | Args: |
| 65 | contents: List of content items (strings or PIL Images) |
| 66 | temperature: Sampling temperature |
| 67 | max_tokens: Maximum tokens in response |
| 68 | |
| 69 | Returns: |
| 70 | Response text, or None on failure |
| 71 | """ |
| 72 | try: |
no outgoing calls
no test coverage detected