[Add] OpenRouter: Integrate OpenRouter API for authentication, model management, and translation capabilities.
This commit is contained in:
@@ -639,6 +639,7 @@ class Config:
|
||||
SELECTABLE_GEMINI_MODEL_LIST = ManagedProperty('SELECTABLE_GEMINI_MODEL_LIST', type_=list, serialize=False, mutable_tracking=True)
|
||||
SELECTABLE_OPENAI_MODEL_LIST = ManagedProperty('SELECTABLE_OPENAI_MODEL_LIST', type_=list, serialize=False, mutable_tracking=True)
|
||||
SELECTABLE_GROQ_MODEL_LIST = ManagedProperty('SELECTABLE_GROQ_MODEL_LIST', type_=list, serialize=False, mutable_tracking=True)
|
||||
SELECTABLE_OPENROUTER_MODEL_LIST = ManagedProperty('SELECTABLE_OPENROUTER_MODEL_LIST', type_=list, serialize=False, mutable_tracking=True)
|
||||
SELECTABLE_LMSTUDIO_MODEL_LIST = ManagedProperty('SELECTABLE_LMSTUDIO_MODEL_LIST', type_=list, serialize=False, mutable_tracking=True)
|
||||
SELECTABLE_OLLAMA_MODEL_LIST = ManagedProperty('SELECTABLE_OLLAMA_MODEL_LIST', type_=list, serialize=False, mutable_tracking=True)
|
||||
|
||||
@@ -737,6 +738,7 @@ class Config:
|
||||
SELECTED_GEMINI_MODEL = ManagedProperty('SELECTED_GEMINI_MODEL', type_=str, allowed=_allowed_in_populated('SELECTABLE_GEMINI_MODEL_LIST'))
|
||||
SELECTED_OPENAI_MODEL = ManagedProperty('SELECTED_OPENAI_MODEL', type_=str, allowed=_allowed_in_populated('SELECTABLE_OPENAI_MODEL_LIST'))
|
||||
SELECTED_GROQ_MODEL = ManagedProperty('SELECTED_GROQ_MODEL', type_=str, allowed=_allowed_in_populated('SELECTABLE_GROQ_MODEL_LIST'))
|
||||
SELECTED_OPENROUTER_MODEL = ManagedProperty('SELECTED_OPENROUTER_MODEL', type_=str, allowed=_allowed_in_populated('SELECTABLE_OPENROUTER_MODEL_LIST'))
|
||||
SELECTED_LMSTUDIO_MODEL = ManagedProperty('SELECTED_LMSTUDIO_MODEL', type_=str, allowed=_allowed_in_populated('SELECTABLE_LMSTUDIO_MODEL_LIST'))
|
||||
SELECTED_OLLAMA_MODEL = ManagedProperty('SELECTED_OLLAMA_MODEL', type_=str, allowed=_allowed_in_populated('SELECTABLE_OLLAMA_MODEL_LIST'))
|
||||
|
||||
@@ -818,6 +820,7 @@ class Config:
|
||||
self._SELECTABLE_GEMINI_MODEL_LIST = []
|
||||
self._SELECTABLE_OPENAI_MODEL_LIST = []
|
||||
self._SELECTABLE_GROQ_MODEL_LIST = []
|
||||
self._SELECTABLE_OPENROUTER_MODEL_LIST = []
|
||||
self._SELECTABLE_LMSTUDIO_MODEL_LIST = []
|
||||
self._SELECTABLE_OLLAMA_MODEL_LIST = []
|
||||
|
||||
@@ -943,6 +946,7 @@ class Config:
|
||||
"Gemini_API": None,
|
||||
"OpenAI_API": None,
|
||||
"Groq_API": None,
|
||||
"OpenRouter_API": None,
|
||||
}
|
||||
self._USE_EXCLUDE_WORDS = True
|
||||
self._SELECTED_TRANSLATION_COMPUTE_DEVICE = copy.deepcopy(self.SELECTABLE_COMPUTE_DEVICE_LIST[0])
|
||||
@@ -952,6 +956,7 @@ class Config:
|
||||
self._SELECTED_GEMINI_MODEL = None
|
||||
self._SELECTED_OPENAI_MODEL = None
|
||||
self._SELECTED_GROQ_MODEL = None
|
||||
self._SELECTED_OPENROUTER_MODEL = None
|
||||
self._LMSTUDIO_URL = "http://127.0.0.1:1234/v1"
|
||||
self._SELECTED_LMSTUDIO_MODEL = None
|
||||
self._SELECTED_OLLAMA_MODEL = None
|
||||
@@ -1057,6 +1062,7 @@ class Config:
|
||||
('SELECTED_GEMINI_MODEL', 'SELECTABLE_GEMINI_MODEL_LIST'),
|
||||
('SELECTED_OPENAI_MODEL', 'SELECTABLE_OPENAI_MODEL_LIST'),
|
||||
('SELECTED_GROQ_MODEL', 'SELECTABLE_GROQ_MODEL_LIST'),
|
||||
('SELECTED_OPENROUTER_MODEL', 'SELECTABLE_OPENROUTER_MODEL_LIST'),
|
||||
('SELECTED_LMSTUDIO_MODEL', 'SELECTABLE_LMSTUDIO_MODEL_LIST'),
|
||||
('SELECTED_OLLAMA_MODEL', 'SELECTABLE_OLLAMA_MODEL_LIST'),
|
||||
]
|
||||
|
||||
@@ -1997,6 +1997,103 @@ class Controller:
|
||||
}
|
||||
return response
|
||||
|
||||
@staticmethod
|
||||
def getOpenRouterAuthKey(*args, **kwargs) -> dict:
|
||||
return {"status":200, "result":config.AUTH_KEYS["OpenRouter_API"]}
|
||||
|
||||
def setOpenRouterAuthKey(self, data, *args, **kwargs) -> dict:
|
||||
printLog("Set OpenRouter Auth Key", data)
|
||||
translator_name = "OpenRouter_API"
|
||||
try:
|
||||
data = str(data)
|
||||
if len(data) >= 20: # OpenRouter API key basic validation
|
||||
result = model.authenticationTranslatorOpenRouterAuthKey(auth_key=data)
|
||||
if result is True:
|
||||
key = data
|
||||
auth_keys = config.AUTH_KEYS
|
||||
auth_keys[translator_name] = key
|
||||
config.AUTH_KEYS = auth_keys
|
||||
config.SELECTABLE_TRANSLATION_ENGINE_STATUS[translator_name] = True
|
||||
config.SELECTABLE_OPENROUTER_MODEL_LIST = model.getTranslatorOpenRouterModelList()
|
||||
self.run(200, self.run_mapping["selectable_openrouter_model_list"], config.SELECTABLE_OPENROUTER_MODEL_LIST)
|
||||
if config.SELECTED_OPENROUTER_MODEL not in config.SELECTABLE_OPENROUTER_MODEL_LIST:
|
||||
config.SELECTED_OPENROUTER_MODEL = config.SELECTABLE_OPENROUTER_MODEL_LIST[0]
|
||||
model.setTranslatorOpenRouterModel(model=config.SELECTED_OPENROUTER_MODEL)
|
||||
self.run(200, self.run_mapping["selected_openrouter_model"], config.SELECTED_OPENROUTER_MODEL)
|
||||
model.updateTranslatorOpenRouterClient()
|
||||
self.updateTranslationEngineAndEngineList()
|
||||
response = {"status":200, "result":config.AUTH_KEYS[translator_name]}
|
||||
else:
|
||||
response = {
|
||||
"status":400,
|
||||
"result":{
|
||||
"message":"Authentication failure of OpenRouter auth key",
|
||||
"data": config.AUTH_KEYS[translator_name]
|
||||
}
|
||||
}
|
||||
else:
|
||||
response = {
|
||||
"status":400,
|
||||
"result":{
|
||||
"message":"OpenRouter auth key is not valid",
|
||||
"data": config.AUTH_KEYS[translator_name]
|
||||
}
|
||||
}
|
||||
except Exception as e:
|
||||
errorLogging()
|
||||
response = {
|
||||
"status":400,
|
||||
"result":{
|
||||
"message":f"Error {e}",
|
||||
"data": config.AUTH_KEYS[translator_name]
|
||||
}
|
||||
}
|
||||
return response
|
||||
|
||||
def delOpenRouterAuthKey(self, *args, **kwargs) -> dict:
|
||||
translator_name = "OpenRouter_API"
|
||||
auth_keys = config.AUTH_KEYS
|
||||
auth_keys[translator_name] = None
|
||||
config.AUTH_KEYS = auth_keys
|
||||
config.SELECTABLE_TRANSLATION_ENGINE_STATUS[translator_name] = False
|
||||
self.updateTranslationEngineAndEngineList()
|
||||
return {"status":200, "result":config.AUTH_KEYS[translator_name]}
|
||||
|
||||
def getOpenRouterModelList(self, *args, **kwargs) -> dict:
|
||||
return {"status":200, "result": config.SELECTABLE_OPENROUTER_MODEL_LIST}
|
||||
|
||||
def getOpenRouterModel(self, *args, **kwargs) -> dict:
|
||||
return {"status":200, "result":config.SELECTED_OPENROUTER_MODEL}
|
||||
|
||||
def setOpenRouterModel(self, data, *args, **kwargs) -> dict:
|
||||
printLog("Set OpenRouter Model", data)
|
||||
try:
|
||||
data = str(data)
|
||||
result = model.setTranslatorOpenRouterModel(model=data)
|
||||
if result is True:
|
||||
config.SELECTED_OPENROUTER_MODEL = data
|
||||
model.setTranslatorOpenRouterModel(model=config.SELECTED_OPENROUTER_MODEL)
|
||||
model.updateTranslatorOpenRouterClient()
|
||||
response = {"status":200, "result":config.SELECTED_OPENROUTER_MODEL}
|
||||
else:
|
||||
response = {
|
||||
"status":400,
|
||||
"result":{
|
||||
"message":"OpenRouter model is not valid",
|
||||
"data": config.SELECTED_OPENROUTER_MODEL
|
||||
}
|
||||
}
|
||||
except Exception as e:
|
||||
errorLogging()
|
||||
response = {
|
||||
"status":400,
|
||||
"result":{
|
||||
"message":f"Error {e}",
|
||||
"data": config.SELECTED_OPENROUTER_MODEL
|
||||
}
|
||||
}
|
||||
return response
|
||||
|
||||
def getTranslatorLMStudioConnection(self, *args, **kwargs) -> dict:
|
||||
return {"status":200, "result":model.getTranslatorLMStudioConnected()}
|
||||
|
||||
|
||||
@@ -60,6 +60,8 @@ run_mapping = {
|
||||
"selected_openai_model":"/run/selected_openai_model",
|
||||
"selectable_groq_model_list":"/run/selectable_groq_model_list",
|
||||
"selected_groq_model":"/run/selected_groq_model",
|
||||
"selectable_openrouter_model_list":"/run/selectable_openrouter_model_list",
|
||||
"selected_openrouter_model":"/run/selected_openrouter_model",
|
||||
"selectable_lmstudio_model_list":"/run/selectable_lmstudio_model_list",
|
||||
"selected_lmstudio_model":"/run/selected_lmstudio_model",
|
||||
"selectable_ollama_model_list":"/run/selectable_ollama_model_list",
|
||||
|
||||
@@ -266,6 +266,23 @@ class Model:
|
||||
self.ensure_initialized()
|
||||
self.translator.updateGroqClient()
|
||||
|
||||
def authenticationTranslatorOpenRouterAuthKey(self, auth_key: str) -> bool:
|
||||
result = self.translator.authenticationOpenRouterAuthKey(auth_key, root_path=config.PATH_LOCAL)
|
||||
return result
|
||||
|
||||
def getTranslatorOpenRouterModelList(self) -> list[str]:
|
||||
self.ensure_initialized()
|
||||
return self.translator.getOpenRouterModelList()
|
||||
|
||||
def setTranslatorOpenRouterModel(self, model: str) -> bool:
|
||||
self.ensure_initialized()
|
||||
result = self.translator.setOpenRouterModel(model=model)
|
||||
return result
|
||||
|
||||
def updateTranslatorOpenRouterClient(self) -> None:
|
||||
self.ensure_initialized()
|
||||
self.translator.updateOpenRouterClient()
|
||||
|
||||
def getTranslatorLMStudioConnected(self) -> bool:
|
||||
self.ensure_initialized()
|
||||
return self.translator.getLMStudioConnected()
|
||||
|
||||
@@ -773,3 +773,7 @@ Ollama:
|
||||
Groq_API:
|
||||
source: *openai_langs
|
||||
target: *openai_langs
|
||||
|
||||
OpenRouter_API:
|
||||
source: *openai_langs
|
||||
target: *openai_langs
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
system_prompt: |
|
||||
You are a helpful translation assistant.
|
||||
Supported languages:
|
||||
{supported_languages}
|
||||
|
||||
Translate the user provided text from {input_lang} to {output_lang}.
|
||||
Return ONLY the translated text. Do not add quotes or extra commentary.
|
||||
145
src-python/models/translation/translation_openrouter.py
Normal file
145
src-python/models/translation/translation_openrouter.py
Normal file
@@ -0,0 +1,145 @@
|
||||
from openai import OpenAI
|
||||
from langchain_openai import ChatOpenAI
|
||||
from pydantic import SecretStr
|
||||
|
||||
try:
|
||||
from .translation_languages import translation_lang
|
||||
from .translation_utils import loadPromptConfig
|
||||
except Exception:
|
||||
import sys
|
||||
from os import path as os_path
|
||||
sys.path.append(os_path.dirname(os_path.dirname(os_path.dirname(os_path.abspath(__file__)))))
|
||||
from translation_languages import translation_lang, loadTranslationLanguages
|
||||
from translation_utils import loadPromptConfig
|
||||
translation_lang = loadTranslationLanguages(path=".", force=True)
|
||||
|
||||
def _authentication_check(api_key: str) -> bool:
|
||||
"""Check if the provided API key is valid by attempting to list models.
|
||||
"""
|
||||
try:
|
||||
client = OpenAI(
|
||||
api_key=api_key,
|
||||
base_url="https://openrouter.ai/api/v1",
|
||||
)
|
||||
client.models.list()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def _get_available_text_models(api_key: str) -> list[str]:
|
||||
"""Extract only OpenRouter models suitable for translation and chat applications.
|
||||
"""
|
||||
client = OpenAI(
|
||||
api_key=api_key,
|
||||
base_url="https://openrouter.ai/api/v1",
|
||||
)
|
||||
res = client.models.list()
|
||||
allowed_models = []
|
||||
|
||||
for model in res.data:
|
||||
model_id = model.id
|
||||
|
||||
# 除外対象のキーワード
|
||||
exclude_keywords = [
|
||||
"whisper", # 音声認識
|
||||
"embedding", # 埋め込み
|
||||
"image", # 画像生成
|
||||
"tts", # 音声合成
|
||||
"audio", # 音声系
|
||||
"search", # 検索補助モデル
|
||||
"transcribe", # 音声→文字起こし
|
||||
"diarize", # 話者分離
|
||||
"vision" # 画像入力系
|
||||
]
|
||||
|
||||
# 除外キーワードが含まれているモデルをスキップ
|
||||
if any(kw in model_id.lower() for kw in exclude_keywords):
|
||||
continue
|
||||
|
||||
# テキスト処理用モデルのみ対象
|
||||
allowed_models.append(model_id)
|
||||
|
||||
allowed_models.sort()
|
||||
return allowed_models
|
||||
|
||||
class OpenRouterClient:
|
||||
"""OpenRouter API Translation wrapper using OpenAI-compatible endpoint.
|
||||
|
||||
OpenRouter provides access to various LLM models via a unified API.
|
||||
The API endpoint: https://openrouter.ai/api/v1
|
||||
"""
|
||||
def __init__(self, root_path: str = None):
|
||||
self.api_key = None
|
||||
self.model = None
|
||||
self.base_url = "https://openrouter.ai/api/v1"
|
||||
|
||||
prompt_config = loadPromptConfig(root_path, "translation_openrouter.yml")
|
||||
self.supported_languages = list(translation_lang["OpenRouter_API"]["source"].keys())
|
||||
self.prompt_template = prompt_config["system_prompt"]
|
||||
|
||||
self.openrouter_llm = None
|
||||
|
||||
def getModelList(self) -> list[str]:
|
||||
return _get_available_text_models(self.api_key) if self.api_key else []
|
||||
|
||||
def getAuthKey(self) -> str:
|
||||
return self.api_key
|
||||
|
||||
def setAuthKey(self, api_key: str) -> bool:
|
||||
result = _authentication_check(api_key)
|
||||
if result:
|
||||
self.api_key = api_key
|
||||
return result
|
||||
|
||||
def getModel(self) -> str:
|
||||
return self.model
|
||||
|
||||
def setModel(self, model: str) -> bool:
|
||||
if model in self.getModelList():
|
||||
self.model = model
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def updateClient(self) -> None:
|
||||
self.openrouter_llm = ChatOpenAI(
|
||||
base_url=self.base_url,
|
||||
model=self.model,
|
||||
api_key=SecretStr(self.api_key),
|
||||
streaming=False,
|
||||
)
|
||||
|
||||
def translate(self, text: str, input_lang: str, output_lang: str) -> str:
|
||||
system_prompt = self.prompt_template.format(
|
||||
supported_languages=self.supported_languages,
|
||||
input_lang=input_lang,
|
||||
output_lang=output_lang,
|
||||
)
|
||||
messages = [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": text},
|
||||
]
|
||||
|
||||
resp = self.openrouter_llm.invoke(messages)
|
||||
content = ""
|
||||
if isinstance(resp.content, str):
|
||||
content = resp.content
|
||||
elif isinstance(resp.content, list):
|
||||
for part in resp.content:
|
||||
if isinstance(part, str):
|
||||
content += part
|
||||
elif isinstance(part, dict) and "content" in part and isinstance(part["content"], str):
|
||||
content += part["content"]
|
||||
return content.strip()
|
||||
|
||||
if __name__ == "__main__":
|
||||
AUTH_KEY = input("OPENROUTER_API_KEY: ")
|
||||
client = OpenRouterClient()
|
||||
client.setAuthKey(AUTH_KEY)
|
||||
models = client.getModelList()
|
||||
if models:
|
||||
print("Available models:", models)
|
||||
model = input("Select a model: ")
|
||||
client.setModel(model)
|
||||
client.updateClient()
|
||||
print(client.translate("こんにちは世界", "Japanese", "English"))
|
||||
@@ -16,6 +16,7 @@ try:
|
||||
from .translation_lmstudio import LMStudioClient
|
||||
from .translation_ollama import OllamaClient
|
||||
from .translation_groq import GroqClient
|
||||
from .translation_openrouter import OpenRouterClient
|
||||
except Exception:
|
||||
import sys
|
||||
sys.path.append(os_path.dirname(os_path.dirname(os_path.dirname(os_path.abspath(__file__)))))
|
||||
@@ -27,6 +28,7 @@ except Exception:
|
||||
from translation_lmstudio import LMStudioClient
|
||||
from translation_ollama import OllamaClient
|
||||
from translation_groq import GroqClient
|
||||
from translation_openrouter import OpenRouterClient
|
||||
|
||||
import ctranslate2
|
||||
import transformers
|
||||
@@ -53,6 +55,7 @@ class Translator:
|
||||
self.gemini_client: Optional[GeminiClient] = None
|
||||
self.openai_client: Optional[OpenAIClient] = None
|
||||
self.groq_client: Optional[GroqClient] = None
|
||||
self.openrouter_client: Optional[OpenRouterClient] = None
|
||||
self.lmstudio_client: LMStudioClient[LMStudioClient] = None
|
||||
self.ollama_client: OllamaClient[OllamaClient] = None
|
||||
self.ctranslate2_translator: Any = None
|
||||
@@ -213,6 +216,40 @@ class Translator:
|
||||
"""Update the Groq client (fetch available models)."""
|
||||
self.groq_client.updateClient()
|
||||
|
||||
def authenticationOpenRouterAuthKey(self, auth_key: str, root_path: str = None) -> bool:
|
||||
"""Authenticate OpenRouter API with the provided key.
|
||||
|
||||
Returns True on success, False on failure.
|
||||
"""
|
||||
self.openrouter_client = OpenRouterClient(root_path=root_path)
|
||||
if self.openrouter_client.setAuthKey(auth_key):
|
||||
return True
|
||||
else:
|
||||
self.openrouter_client = None
|
||||
return False
|
||||
|
||||
def getOpenRouterModelList(self) -> list[str]:
|
||||
"""Get available OpenRouter models.
|
||||
|
||||
Returns a list of model names, or an empty list on failure.
|
||||
"""
|
||||
if self.openrouter_client is None:
|
||||
return []
|
||||
return self.openrouter_client.getModelList()
|
||||
|
||||
def setOpenRouterModel(self, model: str) -> bool:
|
||||
"""Change the OpenRouter model used for translation.
|
||||
|
||||
Returns True on success, False on failure.
|
||||
"""
|
||||
if self.openrouter_client is None:
|
||||
return False
|
||||
return self.openrouter_client.setModel(model)
|
||||
|
||||
def updateOpenRouterClient(self) -> None:
|
||||
"""Update the OpenRouter client (fetch available models)."""
|
||||
self.openrouter_client.updateClient()
|
||||
|
||||
def getLMStudioConnected(self) -> bool:
|
||||
"""Get LM Studio connection status.
|
||||
|
||||
@@ -455,6 +492,15 @@ class Translator:
|
||||
input_lang=source_language,
|
||||
output_lang=target_language,
|
||||
)
|
||||
case "OpenRouter_API":
|
||||
if self.openrouter_client is None:
|
||||
result = False
|
||||
else:
|
||||
result = self.openrouter_client.translate(
|
||||
message,
|
||||
input_lang=source_language,
|
||||
output_lang=target_language,
|
||||
)
|
||||
case "LMStudio":
|
||||
if self.lmstudio_client is None:
|
||||
result = False
|
||||
|
||||
Reference in New Issue
Block a user