jumpserver/apps/i18n/_translator/utils.py

35 lines
1.6 KiB
Python
Raw Normal View History

2024-01-15 09:14:38 +00:00
from openai import AsyncOpenAI
class OpenAITranslate:
2024-01-16 13:15:39 +00:00
def __init__(self, key: str | None = None, base_url: str | None = None):
self.client = AsyncOpenAI(api_key=key, base_url=base_url)
2024-01-15 09:14:38 +00:00
async def translate_text(self, text, target_lang="English") -> str | None:
try:
response = await self.client.chat.completions.create(
messages=[
{
"role": "system",
"content": f"Now I ask you to be the translator. "
f"Your goal is to understand the Chinese "
f"I provided you and translate it into {target_lang}. "
f"Please do not use a translation accent when translating, "
f"but translate naturally, smoothly and authentically, "
f"using beautiful and elegant words. way of expression,"
f"If you found word '动作' please translate it to 'Action', because it's short,"
f"If you found word '管理' in menu, you can not translate it, because management is too long in menu"
,
2024-01-15 09:14:38 +00:00
},
{
"role": "user",
"content": text,
},
],
model="gpt-4",
)
except Exception as e:
print("Open AI Error: ", e)
return
return response.choices[0].message.content.strip()