davideuler
commited on
Commit
·
b8b57c9
1
Parent(s):
fa67ada
allow configuration of OPENAI_API_BASE, and use gpt-4o-mini by default; fix command line example
Browse files- deep_translator/chatgpt.py +15 -4
- deep_translator/constants.py +3 -1
- docs/README.rst +9 -4
deep_translator/chatgpt.py
CHANGED
@@ -4,7 +4,11 @@ import os
|
|
4 |
from typing import List, Optional
|
5 |
|
6 |
from deep_translator.base import BaseTranslator
|
7 |
-
from deep_translator.constants import
|
|
|
|
|
|
|
|
|
8 |
from deep_translator.exceptions import ApiKeyException
|
9 |
|
10 |
|
@@ -19,19 +23,23 @@ class ChatGptTranslator(BaseTranslator):
|
|
19 |
source: str = "auto",
|
20 |
target: str = "english",
|
21 |
api_key: Optional[str] = os.getenv(OPEN_AI_ENV_VAR, None),
|
22 |
-
model: Optional[str] = "gpt-
|
|
|
23 |
**kwargs,
|
24 |
):
|
25 |
"""
|
26 |
@param api_key: your openai api key.
|
27 |
@param source: source language
|
28 |
@param target: target language
|
|
|
|
|
29 |
"""
|
30 |
if not api_key:
|
31 |
raise ApiKeyException(env_var=OPEN_AI_ENV_VAR)
|
32 |
|
33 |
self.api_key = api_key
|
34 |
self.model = model
|
|
|
35 |
|
36 |
super().__init__(source=source, target=target, **kwargs)
|
37 |
|
@@ -42,12 +50,15 @@ class ChatGptTranslator(BaseTranslator):
|
|
42 |
"""
|
43 |
import openai
|
44 |
|
45 |
-
|
|
|
|
|
|
|
46 |
|
47 |
prompt = f"Translate the text below into {self.target}.\n"
|
48 |
prompt += f'Text: "{text}"'
|
49 |
|
50 |
-
response =
|
51 |
model=self.model,
|
52 |
messages=[
|
53 |
{
|
|
|
4 |
from typing import List, Optional
|
5 |
|
6 |
from deep_translator.base import BaseTranslator
|
7 |
+
from deep_translator.constants import (
|
8 |
+
OPEN_AI_ENV_VAR,
|
9 |
+
OPEN_AI_BASE_URL_ENV_VAR,
|
10 |
+
OPEN_AI_MODEL_ENV_VAR,
|
11 |
+
)
|
12 |
from deep_translator.exceptions import ApiKeyException
|
13 |
|
14 |
|
|
|
23 |
source: str = "auto",
|
24 |
target: str = "english",
|
25 |
api_key: Optional[str] = os.getenv(OPEN_AI_ENV_VAR, None),
|
26 |
+
model: Optional[str] = os.getenv(OPEN_AI_MODEL_ENV_VAR, "gpt-4o-mini"),
|
27 |
+
base_url: Optional[str] = os.getenv(OPEN_AI_BASE_URL_ENV_VAR, None),
|
28 |
**kwargs,
|
29 |
):
|
30 |
"""
|
31 |
@param api_key: your openai api key.
|
32 |
@param source: source language
|
33 |
@param target: target language
|
34 |
+
@param model: OpenAI model to use
|
35 |
+
@param base_url: custom OpenAI API base URL
|
36 |
"""
|
37 |
if not api_key:
|
38 |
raise ApiKeyException(env_var=OPEN_AI_ENV_VAR)
|
39 |
|
40 |
self.api_key = api_key
|
41 |
self.model = model
|
42 |
+
self.base_url = base_url
|
43 |
|
44 |
super().__init__(source=source, target=target, **kwargs)
|
45 |
|
|
|
50 |
"""
|
51 |
import openai
|
52 |
|
53 |
+
client = openai.OpenAI(
|
54 |
+
api_key=self.api_key,
|
55 |
+
base_url=self.base_url if self.base_url else None
|
56 |
+
)
|
57 |
|
58 |
prompt = f"Translate the text below into {self.target}.\n"
|
59 |
prompt += f'Text: "{text}"'
|
60 |
|
61 |
+
response = client.chat.completions.create(
|
62 |
model=self.model,
|
63 |
messages=[
|
64 |
{
|
deep_translator/constants.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
__copyright__ = "Copyright (C) 2020 Nidhal Baccouri"
|
2 |
|
3 |
|
4 |
-
OPEN_AI_ENV_VAR = "
|
|
|
|
|
5 |
DEEPL_ENV_VAR = "DEEPL_API_KEY"
|
6 |
LIBRE_ENV_VAR = "LIBRE_API_KEY"
|
7 |
MSFT_ENV_VAR = "MICROSOFT_API_KEY"
|
|
|
1 |
__copyright__ = "Copyright (C) 2020 Nidhal Baccouri"
|
2 |
|
3 |
|
4 |
+
OPEN_AI_ENV_VAR = "OPENAI_API_KEY"
|
5 |
+
OPEN_AI_BASE_URL_ENV_VAR = "OPENAI_API_BASE"
|
6 |
+
OPEN_AI_MODEL_ENV_VAR = "OPENAI_MODEL"
|
7 |
DEEPL_ENV_VAR = "DEEPL_API_KEY"
|
8 |
LIBRE_ENV_VAR = "LIBRE_API_KEY"
|
9 |
MSFT_ENV_VAR = "MICROSOFT_API_KEY"
|
docs/README.rst
CHANGED
@@ -592,8 +592,11 @@ ChatGpt Translator
|
|
592 |
There are two required attributes, namely "api_key" (string) and "target" (string or list).
|
593 |
Attribute "source" is optional.
|
594 |
|
595 |
-
You can provide your api key as an argument or you can export it as an env var
|
596 |
-
e.g.
|
|
|
|
|
|
|
597 |
|
598 |
.. code-block:: python
|
599 |
|
@@ -824,13 +827,15 @@ To translate a string or line of text:
|
|
824 |
|
825 |
.. code-block:: console
|
826 |
|
827 |
-
$ deep_translator google --source "english" --target "german" --text "happy coding"
|
|
|
828 |
|
829 |
Alternate short option names, along with using language abbreviations:
|
830 |
|
831 |
.. code-block:: console
|
832 |
|
833 |
-
$
|
|
|
834 |
|
835 |
|
836 |
Finally, to retrieve a list of available languages for a given translator:
|
|
|
592 |
There are two required attributes, namely "api_key" (string) and "target" (string or list).
|
593 |
Attribute "source" is optional.
|
594 |
|
595 |
+
You can provide your api key, api base as an argument or you can export it as an env var
|
596 |
+
e.g.
|
597 |
+
`export OPENAI_API_KEY="your_key"
|
598 |
+
export OPENAI_API_BASE=https://api.openai.com/v1
|
599 |
+
`
|
600 |
|
601 |
.. code-block:: python
|
602 |
|
|
|
827 |
|
828 |
.. code-block:: console
|
829 |
|
830 |
+
$ deep_translator --translator google --source "english" --target "german" --text "happy coding"
|
831 |
+
$
|
832 |
|
833 |
Alternate short option names, along with using language abbreviations:
|
834 |
|
835 |
.. code-block:: console
|
836 |
|
837 |
+
$ dt -trans google -src "en" -tg "de" -txt "happy coding"
|
838 |
+
$ dt -trans chatgpt -src "en" -tg "ja" -txt "happy coding"
|
839 |
|
840 |
|
841 |
Finally, to retrieve a list of available languages for a given translator:
|