Skip to content

Commit

Permalink
fuck litellm
Browse files Browse the repository at this point in the history
  • Loading branch information
zyddnys committed Nov 8, 2023
1 parent eafb9ce commit 4ae5ab5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
27 changes: 13 additions & 14 deletions manga_translator/translators/chatgpt.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import re
import requests
import openai
import openai.error
import litellm
import litellm.exceptions
import asyncio
import time
from typing import List, Dict

from .common import CommonTranslator, MissingAPIKeyException
from .keys import OPENAI_API_KEY, OPENAI_HTTP_PROXY, OPENAI_API_BASE

CONFIG = None

class GPT3Translator(CommonTranslator):
_LANGUAGE_CODE_MAP = {
'CHS': 'Simplified Chinese',
Expand Down Expand Up @@ -53,17 +53,16 @@ class GPT3Translator(CommonTranslator):

def __init__(self):
super().__init__()
litellm.api_key = OPENAI_API_KEY
litellm.api_base = OPENAI_API_BASE
if not litellm.api_key:
openai.api_key = openai.api_key or OPENAI_API_KEY
openai.api_base = OPENAI_API_BASE
if not openai.api_key:
raise MissingAPIKeyException('Please set the OPENAI_API_KEY environment variable before using the chatgpt translator.')
if OPENAI_HTTP_PROXY:
session = requests.session()
session.proxies = {
proxies = {
'http': 'http://%s' % OPENAI_HTTP_PROXY,
'https': 'http://%s' % OPENAI_HTTP_PROXY
}
litellm.client_session = session
openai.proxy = proxies
self.token_count = 0
self.token_count_last = 0
self.config = None
Expand Down Expand Up @@ -148,13 +147,13 @@ async def _translate(self, from_lang: str, to_lang: str, queries: List[str]) ->
try:
response = await request_task
break
except litellm.exceptions.RateLimitError or openai.error.RateLimitError: # Server returned ratelimit response
except openai.error.RateLimitError: # Server returned ratelimit response
ratelimit_attempt += 1
if ratelimit_attempt >= self._RATELIMIT_RETRY_ATTEMPTS:
raise
self.logger.warn(f'Restarting request due to ratelimiting by openai servers. Attempt: {ratelimit_attempt}')
await asyncio.sleep(2)
except litellm.exceptions.APIError or openai.error.APIError: # Server returned 500 error (probably server load)
except openai.error.APIError: # Server returned 500 error (probably server load)
server_error_attempt += 1
if server_error_attempt >= self._RETRY_ATTEMPTS:
self.logger.error('OpenAI encountered a server error, possibly due to high server load. Use a different translator or try again later.')
Expand All @@ -176,7 +175,7 @@ async def _translate(self, from_lang: str, to_lang: str, queries: List[str]) ->
return translations

async def _request_translation(self, to_lang: str, prompt: str) -> str:
response = await litellm.acompletion(
response = await openai.Completion.acreate(
model='text-davinci-003',
prompt=prompt,
max_tokens=self._MAX_TOKENS // 2, # Assuming that half of the tokens are used for the query
Expand Down Expand Up @@ -258,7 +257,7 @@ async def _request_translation(self, to_lang: str, prompt: str) -> str:
messages.insert(1, {'role': 'user', 'content': self.chat_sample[to_lang][0]})
messages.insert(2, {'role': 'assistant', 'content': self.chat_sample[to_lang][1]})

response = await litellm.acompletion(
response = await openai.ChatCompletion.acreate(
model='gpt-3.5-turbo-0613',
messages=messages,
max_tokens=self._MAX_TOKENS // 2,
Expand Down Expand Up @@ -291,7 +290,7 @@ async def _request_translation(self, to_lang: str, prompt: str) -> str:
messages.insert(1, {'role': 'user', 'content': self._CHAT_SAMPLE[to_lang][0]})
messages.insert(2, {'role': 'assistant', 'content': self._CHAT_SAMPLE[to_lang][1]})

response = await litellm.acompletion(
response = await openai.ChatCompletion.acreate(
model='gpt-4-0613',
messages=messages,
max_tokens=self._MAX_TOKENS // 2,
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ aioshutil
aiofiles
arabic-reshaper
pyhyphen
langcodes
litellm
langcodes

0 comments on commit 4ae5ab5

Please sign in to comment.