Skip to content

Commit

Permalink
doc: todo
Browse files Browse the repository at this point in the history
  • Loading branch information
Byaidu committed Dec 10, 2024
1 parent b24b55a commit 1697ee3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ pdf2zh example.pdf -t 1

- [ ] Support non-PDF/A files

- [ ] Provide API interface

- [ ] Plugins of [Zotero](https://github.com/zotero/zotero) and [Obsidian](https://github.com/obsidianmd/obsidian-releases)

<h2 id="acknowledgement">Acknowledgements</h2>

- Document merging: [PyMuPDF](https://github.com/pymupdf/PyMuPDF)
Expand Down
21 changes: 14 additions & 7 deletions pdf2zh/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,32 +216,39 @@ def translate(self, text) -> str:
messages=self.prompt(text),
)
return response.choices[0].message.content.strip()


class AzureOpenAITranslator(BaseTranslator):
name = "azure-openai"
envs = {
"AZURE_OPENAI_BASE_URL": None, # e.g. "https://xxx.openai.azure.com"
"AZURE_OPENAI_BASE_URL": None, # e.g. "https://xxx.openai.azure.com"
"AZURE_OPENAI_API_KEY": None,
"AZURE_OPENAI_MODEL": "gpt-4o-mini"
"AZURE_OPENAI_MODEL": "gpt-4o-mini",
}

def __init__(self, service, lang_out, lang_in, model, base_url=None, api_key=None):
base_url = os.getenv("AZURE_OPENAI_BASE_URL", self.envs["AZURE_OPENAI_BASE_URL"])
base_url = os.getenv(
"AZURE_OPENAI_BASE_URL", self.envs["AZURE_OPENAI_BASE_URL"]
)
api_version = os.getenv("AZURE_OPENAI_API_VERSION", "2024-06-01")
if not model:
model = os.getenv("AZURE_OPENAI_MODEL", self.envs["AZURE_OPENAI_MODEL"])
super().__init__(service, lang_out, lang_in, model)
self.options = {"temperature": 0}
self.client = openai.AzureOpenAI(azure_endpoint=base_url, azure_deployment=model, api_version=api_version, api_key=api_key)
self.options = {"temperature": 0}
self.client = openai.AzureOpenAI(
azure_endpoint=base_url,
azure_deployment=model,
api_version=api_version,
api_key=api_key,
)

def translate(self, text) -> str:
response = self.client.chat.completions.create(
model=self.model,
**self.options,
messages=self.prompt(text),
)
return response.choices[0].message.content.strip()
return response.choices[0].message.content.strip()


class ZhipuTranslator(OpenAITranslator):
Expand Down

0 comments on commit 1697ee3

Please sign in to comment.