Skip to content

Commit

Permalink
Added use_httpx option for OpenAI client
Browse files Browse the repository at this point in the history
  • Loading branch information
machinewrapped committed Apr 3, 2024
1 parent efc70b9 commit f6818d3
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion PySubtitle/Providers/OpenAI/ChatGPTClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _send_messages(self, messages : list[str], temperature):
result = self.client.chat.completions.create(
model=self.model,
messages=messages,
temperature=temperature
temperature=temperature,
)

if self.aborted:
Expand Down
6 changes: 5 additions & 1 deletion PySubtitle/Providers/OpenAI/OpenAIClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

try:
import openai
import httpx

from PySubtitle.Helpers import FormatMessages
from PySubtitle.SubtitleError import TranslationError, TranslationImpossibleError
Expand Down Expand Up @@ -30,7 +31,10 @@ def __init__(self, settings : dict):

logging.info(f"Translating with OpenAI model {self.model or 'default'}, Using API Base: {openai.base_url}")

self.client = openai.OpenAI(api_key=openai.api_key, base_url=openai.base_url)
# Optionally use httpx for requests
http_client = httpx.Client(base_url=openai.base_url, follow_redirects=True) if self.api_base and self.settings.get('use_httpx') else None

self.client = openai.OpenAI(api_key=openai.api_key, base_url=openai.base_url, http_client=http_client)

@property
def api_key(self):
Expand Down
4 changes: 4 additions & 0 deletions PySubtitle/Providers/Provider_OpenAI.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self, settings : dict):
'rate_limit': settings.get('rate_limit', GetEnvFloat('OPENAI_RATE_LIMIT')),
"free_plan": settings.get('free_plan', os.getenv('OPENAI_FREE_PLAN') == "True"),
'max_instruct_tokens': settings.get('max_instruct_tokens', int(os.getenv('MAX_INSTRUCT_TOKENS', 2048))),
'use_httpx': settings.get('use_httpx', os.getenv('OPENAI_USE_HTTPX', "False") == "True")
})

self.refresh_when_changed = ['api_key', 'api_base', 'model']
Expand Down Expand Up @@ -71,6 +72,9 @@ def GetOptions(self) -> dict:
'api_base': (str, "The base URL to use for requests - leave as default unless you know you need something else"),
}

if self.api_base:
options['use_httpx'] = (bool, "Use the httpx library for requests. May help if you receive a 307 redirect error with a custom api_base")

if self.api_key:
models = self.available_models
if models:
Expand Down
2 changes: 2 additions & 0 deletions gpt-subtrans.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
parser.add_argument('--batchthreshold', type=float, default=None, help="Number of seconds between lines to consider for batching")
parser.add_argument('--debug', action='store_true', help="Run with DEBUG log level")
parser.add_argument('--description', type=str, default=None, help="A brief description of the film to give context")
parser.add_argument('--httpx', action='store_true', help="Use the httpx library for custom api_base requests. May help if you receive a 307 redirect error.")
parser.add_argument('--includeoriginal', action='store_true', help="Include the original text in the translated subtitles")
parser.add_argument('--instruction', action='append', type=str, default=None, help="An instruction for the AI translator")
parser.add_argument('--instructionfile', type=str, default=None, help="Name/path of a file to load instructions from")
Expand Down Expand Up @@ -95,6 +96,7 @@
'substitutions': ParseSubstitutions(args.substitution),
'target_language': args.target_language,
'temperature': args.temperature,
'use_httpx': args.httpx,
'write_backup': args.writebackup,
})

Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ gpt-subtrans path/to/my/subtitles.srt --moviename "My Awesome Movie" --ratelimit
- `-b`, `--apibase`:
API base URL if you are using a custom instance. if it is not set, the default URL will be used.

- '--httpx':
Use the [HTTPX library](https://github.com/projectdiscovery/httpx) for requests (only supported if apibase is specified)

- `-m`, `--model`:
Specify the [AI model](https://platform.openai.com/docs/models) to use for translation

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ requests
darkdetect
setuptools
pyside6
httpx

0 comments on commit f6818d3

Please sign in to comment.