[Update] translation: Integrate Plamo API support with model selection and authentication features

This commit is contained in:
misyaguziya
2025-09-11 14:15:26 +09:00
parent d76a5b9e07
commit bc269bc1fb
6 changed files with 164 additions and 26 deletions

View File

@@ -1,10 +1,15 @@
from langchain_openai import ChatOpenAI
from pydantic import SecretStr
_MODELS = [
"plamo-2.0-prime"
]
class PlamoClient:
def __init__(self, api_key: str):
def __init__(self, api_key: str = "", model: str = "plamo-2.0-prime"):
self.api_key = api_key
self.base_url = "https://api.platform.preferredai.jp/v1"
self.model = "plamo-2.0-prime"
self.model = model
self.supported_languages = """
English
Japanese
@@ -47,10 +52,55 @@ class PlamoClient:
base_url=self.base_url,
model=self.model,
streaming=True,
openai_api_key=self.api_key,
api_key=SecretStr(self.api_key),
)
def translate_text(self, text: str, input_lang: str, output_lang: str):
def getListModels(self) -> list[str]:
return _MODELS
def getAuthKey(self) -> str:
"""現在のAuthKeyを取得する"""
return self.api_key
def getModel(self) -> str:
"""現在のモデルを取得する"""
return self.model
def setAuthKey(self, api_key: str) -> bool:
"""AuthKeyを設定し、成功したかどうかを返す"""
try:
self.api_key = api_key
self.plamo_llm = ChatOpenAI(
base_url=self.base_url,
model=self.model,
streaming=True,
api_key=SecretStr(self.api_key),
)
return True
except Exception as e:
print(f"Error setting AuthKey: {e}")
return False
def setModel(self, model: str) -> bool:
"""モデルを設定し、成功したかどうかを返す"""
if model not in _MODELS:
print(f"Model {model} is not in the supported model list.")
return False
try:
self.model = model
self.plamo_llm = ChatOpenAI(
base_url=self.base_url,
model=self.model,
streaming=True,
api_key=SecretStr(self.api_key),
)
return True
except Exception as e:
print(f"Error setting model: {e}")
return False
def translate(self, text: str, input_lang: str, output_lang: str) -> str:
messages = [
{
"role": "system",
@@ -63,18 +113,45 @@ class PlamoClient:
output = ""
for chunk in self.plamo_llm.stream(messages):
output += chunk.content
if isinstance(chunk.content, str):
output += chunk.content
elif isinstance(chunk.content, list):
for item in chunk.content:
if isinstance(item, str):
output += item
elif isinstance(item, dict):
if "content" in item and isinstance(item["content"], str):
output += item["content"]
return output[:-1]
def checkAuthKey(self) -> bool:
try:
self.setModel(self.model)
self.translate("Hello World", input_lang="English", output_lang="Japanese")
return True
except Exception:
return False
if __name__ == "__main__":
AUTH_KEY = "AUTH_KEY"
text = """
毎朝コーヒーを入れるのがささやかな楽しみになってる
"""
input_lang = "Japanese"
output_lang = "English"
plamo_client = PlamoClient(api_key="AUTH_KEY")
translated_text = plamo_client.translate_text(text, input_lang, output_lang)
print(translated_text)
plamo_client = PlamoClient(api_key=AUTH_KEY)
print("model list:", plamo_client.getListModels())
print("AuthKey:", plamo_client.getAuthKey())
print("Model:", plamo_client.getModel())
print(f"set model: {plamo_client.setModel('plamo-2.0-prime')}")
print(f"set AuthKey: {plamo_client.setAuthKey(AUTH_KEY)}")
print(f"check AuthKey: {plamo_client.checkAuthKey()}")
try:
translated_text = plamo_client.translate(text, input_lang, output_lang)
print(translated_text)
except Exception:
print("Invalid API key. Please check your credentials.")

View File

@@ -35,10 +35,10 @@ class Translator():
self.is_loaded_ctranslate2_model = False
self.is_enable_translators = ENABLE_TRANSLATORS
def authenticationDeepLAuthKey(self, authkey):
def authenticationDeepLAuthKey(self, auth_key: str) -> bool:
result = True
try:
self.deepl_client = DeepLClient(authkey)
self.deepl_client = DeepLClient(auth_key)
self.deepl_client.translate_text("Hello World", target_lang="EN-US")
except Exception:
errorLogging()
@@ -46,11 +46,11 @@ class Translator():
result = False
return result
def authenticationPlamoAuthKey(self, authkey):
def authenticationPlamoAuthKey(self, auth_key: str, model: str) -> bool:
result = True
try:
self.plamo_client = PlamoClient(authkey)
self.plamo_client.translate_text("Hello World", target_lang="English")
self.plamo_client = PlamoClient(auth_key, model=model)
self.plamo_client.checkAuthKey()
except Exception:
errorLogging()
self.plamo_client = None
@@ -153,7 +153,7 @@ class Translator():
if self.plamo_client is None:
result = False
else:
result = self.plamo_client.translate_text(
result = self.plamo_client.translate(
message,
input_lang=source_language,
output_lang=target_language,