翻訳APIの認証ロジックを改善し、YAMLファイルからプロンプト設定を読み込む機能を追加。新しいフォントファイルを追加し、データパスを更新。

This commit is contained in:
misyaguziya
2025-10-15 18:50:26 +09:00
parent 7d0f63c118
commit f8466bd6e4
15 changed files with 206 additions and 148 deletions

View File

@@ -1,53 +1,23 @@
from langchain_openai import ChatOpenAI
from pydantic import SecretStr
import yaml
from os import path as os_path
_MODELS = [
"plamo-2.0-prime"
]
class PlamoClient:
def __init__(self, api_key: str = "", model: str = "plamo-2.0-prime"):
def __init__(self, api_key: str = "", model: str = "plamo-2.0-prime", root_path: str = None):
self.api_key = api_key
self.base_url = "https://api.platform.preferredai.jp/v1"
self.model = model
self.supported_languages = """
English
Japanese
Korean
French
German
Spanish
Portuguese
Russian
Italian
Dutch
Polish
Turkish
Arabic
Hindi
Thai
Vietnamese
Indonesian
Malay
Filipino
Swedish
Finnish
Danish
Norwegian
Romanian
Czech
Hungarian
Greek
Hebrew
Simplified Chinese
Traditional Chinese
"""
self.prompt_template = f"""
You are a translation assistant that uses the `plamo-translate` tool.
Translate the following text.Supported languages include:{self.supported_languages}
Translate the following text from {{input_lang}} to {{output_lang}}.
output only the translated text without any additional commentary.
"""
# プロンプト設定をYAMLファイルから読み込む
prompt_config = self._load_prompt_config(root_path)
self.supported_languages = prompt_config["supported_languages"]
self.prompt_template = prompt_config["system_prompt"]
self.plamo_llm = ChatOpenAI(
base_url=self.base_url,
model=self.model,
@@ -55,6 +25,25 @@ class PlamoClient:
api_key=SecretStr(self.api_key),
)
def _load_prompt_config(self, root_path: str = None) -> dict:
"""プロンプト設定をYAMLファイルから読み込む"""
prompt_filename = "translation_plamo.yml"
# PyInstallerでビルドされた場合のパス
if root_path and os_path.exists(os_path.join(root_path, "_internal", "prompt", prompt_filename)):
prompt_path = os_path.join(root_path, "_internal", "prompt", prompt_filename)
# src-pythonフォルダから直接実行している場合のパス
elif os_path.exists(os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)):
prompt_path = os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)
# translationフォルダから直接実行している場合のパス
elif os_path.exists(os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)):
prompt_path = os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)
else:
raise FileNotFoundError(f"Prompt file not found: {prompt_filename}")
with open(prompt_path, "r", encoding="utf-8") as f:
return yaml.safe_load(f)
def getListModels(self) -> list[str]:
return _MODELS
@@ -78,13 +67,11 @@ class PlamoClient:
)
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:
@@ -105,7 +92,9 @@ class PlamoClient:
{
"role": "system",
"content": self.prompt_template.format(
input_lang=input_lang, output_lang=output_lang
supported_languages=self.supported_languages,
input_lang=input_lang,
output_lang=output_lang
),
},
{"role": "user", "content": text},
@@ -130,7 +119,8 @@ class PlamoClient:
self.setModel(self.model)
self.translate("Hello World", input_lang="English", output_lang="Japanese")
return True
except Exception:
except Exception as e:
print(f"Error checking AuthKey: {e}")
return False
if __name__ == "__main__":