Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit a45cf8e
Author: ned <github@edoardo.fyi>
Date:   Wed Jun 14 20:26:26 2023 +0200

    added functions note to README

commit 72b5527
Merge: fdd76ee 5fd70f4
Author: ned <11541888+n3d1117@users.noreply.github.com>
Date:   Wed Jun 14 12:42:24 2023 +0200

    Merge pull request n3d1117#341 from n3d1117/feature/rate-limit-retry

    Auto retry while rate limited using tenacity

commit 5fd70f4
Author: ned <github@edoardo.fyi>
Date:   Wed Jun 14 12:41:41 2023 +0200

    Update requirements.txt

commit 96fb76e
Author: ned <github@edoardo.fyi>
Date:   Wed Jun 14 12:35:23 2023 +0200

    auto retry while rate limited using tenacity

commit fdd76ee
Author: ned <github@edoardo.fyi>
Date:   Wed Jun 14 10:42:10 2023 +0200

    Fixes n3d1117#336

commit 4760ff1
Author: ned <github@edoardo.fyi>
Date:   Wed Jun 14 10:34:34 2023 +0200

    update badge

commit 3d6247f
Merge: 1433207 74dfdb8
Author: ned <11541888+n3d1117@users.noreply.github.com>
Date:   Wed Jun 14 10:29:16 2023 +0200

    Merge pull request n3d1117#340 from n3d1117/new-models

    Support new models and update dependencies

commit 74dfdb8
Author: ned <github@edoardo.fyi>
Date:   Wed Jun 14 10:27:06 2023 +0200

    support new models and update dependencies

    `gpt-3.5-turbo-0613`, `gpt-3.5-turbo-16k`, `gpt-3.5-turbo-16k-0613`, `gpt-4-0613`, `gpt-4-32k-0613`

commit 1433207
Merge: 1c8b633 1df1c8b
Author: ned <11541888+n3d1117@users.noreply.github.com>
Date:   Sat May 20 12:47:29 2023 +0200

    Merge pull request n3d1117#321 from nmeln/add-ukrainian-translation

    Add Ukrainian translation.

commit 1df1c8b
Author: Nazarii Melnychuk <melnychuk.nazarii@gmail.com>
Date:   Fri May 19 14:01:45 2023 +0300

    Update README with a mention of the new translation

commit df13d72
Author: Nazarii Melnychuk <melnychuk.nazarii@gmail.com>
Date:   Fri May 19 13:52:17 2023 +0300

    change the translation for the "prompt" strings

commit 5238437
Author: Nazarii Melnychuk <melnychuk.nazarii@gmail.com>
Date:   Fri May 19 13:45:00 2023 +0300

    Add ukrainian (uk) translation.

commit 1c8b633
Merge: 3023c45 c1c6802
Author: ned <11541888+n3d1117@users.noreply.github.com>
Date:   Wed May 17 09:32:31 2023 +0200

    Merge pull request n3d1117#318 from danfmaia/fix/transcription-price

    fix: transcription price

commit c1c6802
Author: Dan Maia <danilofmaia@gmail.com>
Date:   Tue May 16 17:55:03 2023 -0300

    fix: transcription_price config variable

commit 3023c45
Merge: 14b8457 ce1e7c6
Author: ned <11541888+n3d1117@users.noreply.github.com>
Date:   Sun May 7 15:21:35 2023 +0200

    Merge pull request n3d1117#300 from gkpiccoli/main

    pt-br translation

commit ce1e7c6
Author: Gustavo Kureck Piccoli <gustavokpiccoli@gmail.com>
Date:   Sat May 6 16:15:55 2023 -0300

    Add pt-br translations and README.md tag n flag

commit 14b8457
Author: ned <github@edoardo.fyi>
Date:   Sat May 6 12:31:17 2023 +0200

    update README.md

commit 075df1e
Author: ned <github@edoardo.fyi>
Date:   Sat May 6 12:30:55 2023 +0200

    move utils funcs to standalone file

commit e9bed78
Merge: 0fede20 b37946c
Author: ned <11541888+n3d1117@users.noreply.github.com>
Date:   Sat May 6 11:09:12 2023 +0200

    Merge pull request n3d1117#298 from stanislavlysenko0912/reply-messages

    Add support for quoting replied messages

commit b37946c
Author: stanislavlysenko0912 <stanislav.lysenko0912@gmail.com>
Date:   Fri May 5 15:06:39 2023 +0300

    feat(telegram_bot.py): add support for quoting replied messages in group chats

    This allows users to quote a message when replying to it in a group chat, making the conversation context clearer.

commit 0fede20
Merge: 17b1847 dedcd34
Author: ned <11541888+n3d1117@users.noreply.github.com>
Date:   Sun Apr 30 15:35:46 2023 +0200

    Merge pull request n3d1117#284 from BjornB2/main

    Update translations.json

commit dedcd34
Author: BjornB2 <bjorn@heirman.nl>
Date:   Sat Apr 29 09:40:07 2023 +0700

    Update translations.json

    Dutch (nl) translation changes

commit 17b1847
Merge: c2b56db 9c0bc00
Author: ned <11541888+n3d1117@users.noreply.github.com>
Date:   Fri Apr 28 00:50:35 2023 +0200

    Merge pull request n3d1117#280 from pintman/patch-1

    fix: missing comma

commit 9c0bc00
Author: Marco Bakera <pintman@users.noreply.github.com>
Date:   Thu Apr 27 20:59:03 2023 +0200

    fix: missing comma

commit c2b56db
Author: ned <github@edoardo.fyi>
Date:   Thu Apr 27 16:05:21 2023 +0200

    Update README.md

commit 84d8447
Merge: 86cf05c 38eb38c
Author: ned <11541888+n3d1117@users.noreply.github.com>
Date:   Thu Apr 27 16:03:05 2023 +0200

    Merge pull request n3d1117#279 from jnaskali/finnish

    Add Finnish translation

commit 38eb38c
Author: Juhani Naskali <juhani.naskali@gmail.com>
Date:   Thu Apr 27 16:38:11 2023 +0300

    Added Finnish language translation and updated README.md accordingly

commit 86cf05c
Merge: 656f9c2 5da46d8
Author: ned <11541888+n3d1117@users.noreply.github.com>
Date:   Thu Apr 27 10:20:01 2023 +0200

    Merge pull request n3d1117#274 from bugfloyd/feat/farsi_translation

    Add Farsi (Persian) translation

commit 5da46d8
Merge: c98bf07 656f9c2
Author: ned <11541888+n3d1117@users.noreply.github.com>
Date:   Thu Apr 27 10:19:31 2023 +0200

    Merge branch 'main' into feat/farsi_translation

commit 656f9c2
Merge: c83dc8f c5839b1
Author: ned <11541888+n3d1117@users.noreply.github.com>
Date:   Thu Apr 27 10:16:58 2023 +0200

    Merge pull request n3d1117#273 from aes-alienrip/main

    Add Traditional Chinese(zh-tw) support and rename Chinese(cn) to Simplified Chinese (zh-cn)

commit c98bf07
Author: bugfloyd <yashar.hv@gmail.com>
Date:   Sat Apr 22 16:45:42 2023 +0200

    Add language code for Farsi to readme configuration options table

commit d23b960
Author: bugfloyd <yashar.hv@gmail.com>
Date:   Sat Apr 22 16:25:10 2023 +0200

    Add Farsi (Persian) translation

commit c5839b1
Author: aes-alienrip <73251414+aes-alienrip@users.noreply.github.com>
Date:   Sat Apr 22 21:39:39 2023 +0800

    Update README.md

commit 150bbae
Author: aes-alienrip <73251414+aes-alienrip@users.noreply.github.com>
Date:   Sat Apr 22 21:34:52 2023 +0800

    Update translations.json
  • Loading branch information
Dener1111 committed Jun 17, 2023
1 parent df9925f commit c60b013
Show file tree
Hide file tree
Showing 8 changed files with 775 additions and 486 deletions.
61 changes: 36 additions & 25 deletions README.md

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def main():
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logging.getLogger("httpx").setLevel(logging.WARNING)

# Check if the required environment variables are set
required_values = ['TELEGRAM_BOT_TOKEN', 'OPENAI_API_KEY']
Expand Down Expand Up @@ -73,7 +74,7 @@ def main():
'group_trigger_keyword': os.environ.get('GROUP_TRIGGER_KEYWORD', ''),
'token_price': float(os.environ.get('TOKEN_PRICE', 0.002)),
'image_prices': [float(i) for i in os.environ.get('IMAGE_PRICES', "0.016,0.018,0.02").split(",")],
'transcription_price': float(os.environ.get('TOKEN_PRICE', 0.006)),
'transcription_price': float(os.environ.get('TRANSCRIPTION_PRICE', 0.006)),
'bot_language': os.environ.get('BOT_LANGUAGE', 'en'),
}

Expand Down
42 changes: 32 additions & 10 deletions bot/openai_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
from datetime import date
from calendar import monthrange

from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception_type

# Models can be found here: https://platform.openai.com/docs/models/overview
GPT_3_MODELS = ("gpt-3.5-turbo", "gpt-3.5-turbo-0301")
GPT_4_MODELS = ("gpt-4", "gpt-4-0314")
GPT_4_32K_MODELS = ("gpt-4-32k", "gpt-4-32k-0314")
GPT_ALL_MODELS = GPT_3_MODELS + GPT_4_MODELS + GPT_4_32K_MODELS
GPT_3_MODELS = ("gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-3.5-turbo-0613")
GPT_3_16K_MODELS = ("gpt-3.5-turbo-16k", "gpt-3.5-turbo-16k-0613")
GPT_4_MODELS = ("gpt-4", "gpt-4-0314", "gpt-4-0613")
GPT_4_32K_MODELS = ("gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-0613")
GPT_ALL_MODELS = GPT_3_MODELS + GPT_3_16K_MODELS + GPT_4_MODELS + GPT_4_32K_MODELS


def default_max_tokens(model: str) -> int:
Expand All @@ -25,14 +28,24 @@ def default_max_tokens(model: str) -> int:
:param model: The model name
:return: The default number of max tokens
"""
return 1200 if model in GPT_3_MODELS else 2400
base = 1200
if model in GPT_3_MODELS:
return base
elif model in GPT_4_MODELS:
return base * 2
elif model in GPT_3_16K_MODELS:
return base * 4
elif model in GPT_4_32K_MODELS:
return base * 8


# Load translations
parent_dir_path = os.path.join(os.path.dirname(__file__), os.pardir)
translations_file_path = os.path.join(parent_dir_path, 'translations.json')
with open(translations_file_path, 'r', encoding='utf-8') as f:
translations = json.load(f)


def localized_text(key, bot_language):
"""
Return translated text for a key in specified bot_language.
Expand Down Expand Up @@ -134,6 +147,12 @@ async def get_chat_response_stream(self, chat_id: int, query: str):

yield answer, tokens_used

@retry(
reraise=True,
retry=retry_if_exception_type(openai.error.RateLimitError),
wait=wait_fixed(20),
stop=stop_after_attempt(3)
)
async def __common_get_chat_response(self, chat_id: int, query: str, stream=False):
"""
Request a response from the GPT model.
Expand Down Expand Up @@ -179,7 +198,7 @@ async def __common_get_chat_response(self, chat_id: int, query: str, stream=Fals
)

except openai.error.RateLimitError as e:
raise Exception(f"⚠️ _{localized_text('openai_rate_limit', bot_language)}._ ⚠️\n{str(e)}") from e
raise e

except openai.error.InvalidRequestError as e:
raise Exception(f"⚠️ _{localized_text('openai_invalid', bot_language)}._ ⚠️\n{str(e)}") from e
Expand Down Expand Up @@ -272,12 +291,15 @@ async def __summarise(self, conversation) -> str:
return response.choices[0]['message']['content']

def __max_model_tokens(self):
base = 4096
if self.config['model'] in GPT_3_MODELS:
return 4096
return base
if self.config['model'] in GPT_3_16K_MODELS:
return base * 4
if self.config['model'] in GPT_4_MODELS:
return 8192
return base * 2
if self.config['model'] in GPT_4_32K_MODELS:
return 32768
return base * 8
raise NotImplementedError(
f"Max tokens for model {self.config['model']} is not implemented yet."
)
Expand All @@ -295,7 +317,7 @@ def __count_tokens(self, messages) -> int:
except KeyError:
encoding = tiktoken.get_encoding("gpt-3.5-turbo")

if model in GPT_3_MODELS:
if model in GPT_3_MODELS + GPT_3_16K_MODELS:
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
tokens_per_name = -1 # if there's a name, the role is omitted
elif model in GPT_4_MODELS + GPT_4_32K_MODELS:
Expand Down
Loading

0 comments on commit c60b013

Please sign in to comment.