Make an asynchronous call to the OpenRouter API.
(self, api_kwargs: Dict = None, model_type: ModelType = None)
| 110 | raise ValueError(f"Unsupported model type: {model_type}") |
| 111 | |
| 112 | async def acall(self, api_kwargs: Dict = None, model_type: ModelType = None) -> Any: |
| 113 | """Make an asynchronous call to the OpenRouter API.""" |
| 114 | if not self.async_client: |
| 115 | self.async_client = self.init_async_client() |
| 116 | |
| 117 | # Check if API key is set |
| 118 | if not self.async_client.get("api_key"): |
| 119 | error_msg = "OPENROUTER_API_KEY not configured. Please set this environment variable to use OpenRouter." |
| 120 | log.error(error_msg) |
| 121 | # Instead of raising an exception, return a generator that yields the error message |
| 122 | # This allows the error to be displayed to the user in the streaming response |
| 123 | async def error_generator(): |
| 124 | yield error_msg |
| 125 | return error_generator() |
| 126 | |
| 127 | api_kwargs = api_kwargs or {} |
| 128 | |
| 129 | if model_type == ModelType.LLM: |
| 130 | # Prepare headers |
| 131 | headers = { |
| 132 | "Authorization": f"Bearer {self.async_client['api_key']}", |
| 133 | "Content-Type": "application/json", |
| 134 | "HTTP-Referer": "https://github.com/AsyncFuncAI/deepwiki-open", # Optional |
| 135 | "X-Title": "DeepWiki" # Optional |
| 136 | } |
| 137 | |
| 138 | # Always use non-streaming mode for OpenRouter |
| 139 | api_kwargs["stream"] = False |
| 140 | |
| 141 | # Make the API call |
| 142 | try: |
| 143 | log.info(f"Making async OpenRouter API call to {self.async_client['base_url']}/chat/completions") |
| 144 | log.info(f"Request headers: {headers}") |
| 145 | log.info(f"Request body: {api_kwargs}") |
| 146 | |
| 147 | async with aiohttp.ClientSession() as session: |
| 148 | try: |
| 149 | async with session.post( |
| 150 | f"{self.async_client['base_url']}/chat/completions", |
| 151 | headers=headers, |
| 152 | json=api_kwargs, |
| 153 | timeout=60 |
| 154 | ) as response: |
| 155 | if response.status != 200: |
| 156 | error_text = await response.text() |
| 157 | log.error(f"OpenRouter API error ({response.status}): {error_text}") |
| 158 | |
| 159 | # Return a generator that yields the error message |
| 160 | async def error_response_generator(): |
| 161 | yield f"OpenRouter API error ({response.status}): {error_text}" |
| 162 | return error_response_generator() |
| 163 | |
| 164 | # Get the full response |
| 165 | data = await response.json() |
| 166 | log.info(f"Received response from OpenRouter: {data}") |
| 167 | |
| 168 | # Create a generator that yields the content |
| 169 | async def content_generator(): |
nothing calls this directly
no test coverage detected