MCPcopy
hub / github.com/AsyncFuncAI/deepwiki-open / OpenRouterClient

Class OpenRouterClient

api/openrouter_client.py:19–525  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17log = logging.getLogger(__name__)
18
19class OpenRouterClient(ModelClient):
20 __doc__ = r"""A component wrapper for the OpenRouter API client.
21
22 OpenRouter provides a unified API that gives access to hundreds of AI models through a single endpoint.
23 The API is compatible with OpenAI's API format with a few small differences.
24
25 Visit https://openrouter.ai/docs for more details.
26
27 Example:
28 ```python
29 from api.openrouter_client import OpenRouterClient
30
31 client = OpenRouterClient()
32 generator = adal.Generator(
33 model_client=client,
34 model_kwargs={"model": "openai/gpt-4o"}
35 )
36 ```
37 """
38
39 def __init__(self, *args, **kwargs) -> None:
40 """Initialize the OpenRouter client."""
41 super().__init__(*args, **kwargs)
42 self.sync_client = self.init_sync_client()
43 self.async_client = None # Initialize async client only when needed
44
45 def init_sync_client(self):
46 """Initialize the synchronous OpenRouter client."""
47 from api.config import OPENROUTER_API_KEY
48 api_key = OPENROUTER_API_KEY
49 if not api_key:
50 log.warning("OPENROUTER_API_KEY not configured")
51
52 # OpenRouter doesn't have a dedicated client library, so we'll use requests directly
53 return {
54 "api_key": api_key,
55 "base_url": "https://openrouter.ai/api/v1"
56 }
57
58 def init_async_client(self):
59 """Initialize the asynchronous OpenRouter client."""
60 from api.config import OPENROUTER_API_KEY
61 api_key = OPENROUTER_API_KEY
62 if not api_key:
63 log.warning("OPENROUTER_API_KEY not configured")
64
65 # For async, we'll use aiohttp
66 return {
67 "api_key": api_key,
68 "base_url": "https://openrouter.ai/api/v1"
69 }
70
71 def convert_inputs_to_api_kwargs(
72 self, input: Any, model_kwargs: Dict = None, model_type: ModelType = None
73 ) -> Dict:
74 """Convert AdalFlow inputs to OpenRouter API format."""
75 model_kwargs = model_kwargs or {}
76

Callers 2

handle_websocket_chatFunction · 0.90
chat_completions_streamFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected