This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update async client for open router
- Loading branch information
1 parent
7a18c2e
commit 89984a2
Showing
5 changed files
with
178 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
from .client import OpenRouter | ||
from .async_client import AsyncOpenRouter | ||
from .const import FreeModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import aiohttp | ||
import asyncio | ||
from const import FreeModel | ||
from typing import List, Optional | ||
|
||
|
||
class AsyncOpenRouter: | ||
""" | ||
Manages API interactions with OpenRouter for creating chat completions using AI models asynchronously. | ||
Attributes and methods are analogous to the synchronous version but adapted for async operation. | ||
""" | ||
|
||
def __init__(self, model: str, api_key: str) -> None: | ||
if not api_key: | ||
raise ValueError( | ||
"API key required. Please visit https://openrouter.ai/keys" | ||
) | ||
self.api_key = api_key | ||
self.model = model | ||
self._validate_model(model) | ||
|
||
def get_model_list(self) -> List[str]: | ||
return self.FREE_MODEL_LIST | ||
|
||
async def create_chat_completion( | ||
self, | ||
message: str, | ||
site_url: Optional[str] = None, | ||
app_name: Optional[str] = None, | ||
) -> str: | ||
response = await self.generate_content(message, site_url, app_name) | ||
return response["choices"][0]["message"]["content"] | ||
|
||
async def create_multi_chat_completions( | ||
self, | ||
messages: List[str], | ||
site_url: Optional[str] = None, | ||
app_name: Optional[str] = None, | ||
) -> List[str]: | ||
tasks = [ | ||
self.create_chat_completion(message, site_url, app_name) | ||
for message in messages | ||
] | ||
results = await asyncio.gather(*tasks) | ||
return results | ||
|
||
async def generate_content( | ||
self, | ||
message: str, | ||
site_url: Optional[str] = None, | ||
app_name: Optional[str] = None, | ||
) -> dict: | ||
self._validate_message(message) | ||
self._validate_model(self.model) | ||
|
||
headers = { | ||
"Authorization": f"Bearer {self.api_key}", | ||
"Content-Type": "application/json", | ||
} | ||
if site_url: | ||
headers["HTTP-Referer"] = site_url | ||
if app_name: | ||
headers["X-Title"] = app_name | ||
|
||
data = { | ||
"model": self.model, | ||
"messages": [ | ||
{"role": "user", "content": message}, | ||
], | ||
} | ||
|
||
async with aiohttp.ClientSession() as session: | ||
async with session.post( | ||
"https://openrouter.ai/api/v1/chat/completions", | ||
headers=headers, | ||
json=data, | ||
) as response: | ||
response.raise_for_status() | ||
return await response.json() | ||
|
||
def _validate_message(self, message: str) -> None: | ||
if not isinstance(message, str): | ||
raise ValueError("Message must be a string") | ||
|
||
def _validate_model(self, model: str) -> None: | ||
""" | ||
Checks if the specified model is in the list of free models. | ||
""" | ||
if model not in FreeModel: | ||
print( | ||
"This model may not be free. Please check the following list for costs.\nUsers are responsible for API costs. Visit https://openrouter.ai/docs#models" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from enum import Enum | ||
|
||
|
||
class FreeModel(Enum): | ||
GEMMA_7B = "google/gemma-7b-it:free" | ||
ZEPHYR_7B = "huggingfaceh4/zephyr-7b-beta:free" | ||
MISTRAL_7B = "mistralai/mistral-7b-instruct:free" | ||
CINEMATIKA_7B = "openrouter/cinematika-7b:free" | ||
TOPPY_M_7B = "undi95/toppy-m-7b:free" | ||
MYTHOMIST_7B = "gryphe/mythomist-7b:free" | ||
CAPYBARA_7B = "nousresearch/nous-capybara-7b:free" | ||
OPENCHAT_7B = "openchat/openchat-7b:free" |