|
@@ -4,10 +4,13 @@ import logging
|
|
|
import os
|
|
import os
|
|
|
import re
|
|
import re
|
|
|
from json import dumps, loads
|
|
from json import dumps, loads
|
|
|
|
|
+from dotenv import load_dotenv
|
|
|
|
|
|
|
|
import openai
|
|
import openai
|
|
|
import requests
|
|
import requests
|
|
|
|
|
|
|
|
|
|
+# Load environment variables
|
|
|
|
|
+load_dotenv()
|
|
|
|
|
|
|
|
class BaseTranslator:
|
|
class BaseTranslator:
|
|
|
def __init__(self, service, lang_out, lang_in, model):
|
|
def __init__(self, service, lang_out, lang_in, model):
|
|
@@ -58,13 +61,14 @@ class OpenAITranslator(BaseTranslator):
|
|
|
lang_in = "en" if lang_in == "auto" else lang_in
|
|
lang_in = "en" if lang_in == "auto" else lang_in
|
|
|
super().__init__(service, lang_out, lang_in, model)
|
|
super().__init__(service, lang_out, lang_in, model)
|
|
|
self.options = {"temperature": 0} # 随机采样可能会打断公式标记
|
|
self.options = {"temperature": 0} # 随机采样可能会打断公式标记
|
|
|
- # OPENAI_BASE_URL
|
|
|
|
|
- # OPENAI_API_KEY
|
|
|
|
|
- self.client = openai.OpenAI()
|
|
|
|
|
|
|
+ # Configure OpenAI client with environment variables
|
|
|
|
|
+ self.client = openai.OpenAI(
|
|
|
|
|
+ api_key=os.getenv('OPENAI_API_KEY')
|
|
|
|
|
+ )
|
|
|
|
|
|
|
|
def translate(self, text) -> str:
|
|
def translate(self, text) -> str:
|
|
|
response = self.client.chat.completions.create(
|
|
response = self.client.chat.completions.create(
|
|
|
- model=self.model,
|
|
|
|
|
|
|
+ model=os.getenv('LLM_MODEL', self.model), # Use env var or fallback to default
|
|
|
**self.options,
|
|
**self.options,
|
|
|
messages=[
|
|
messages=[
|
|
|
{
|
|
{
|
|
@@ -78,4 +82,3 @@ class OpenAITranslator(BaseTranslator):
|
|
|
],
|
|
],
|
|
|
)
|
|
)
|
|
|
return response.choices[0].message.content.strip()
|
|
return response.choices[0].message.content.strip()
|
|
|
-
|
|
|