mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2026-01-05 22:15:54 +01:00
Merge pull request #91 from carlsverre/gpt-4-support
gpt-4 model support
This commit is contained in:
@@ -6,6 +6,11 @@ import tiktoken
|
||||
|
||||
import openai
|
||||
|
||||
# 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
|
||||
|
||||
class OpenAIHelper:
|
||||
"""
|
||||
@@ -23,6 +28,16 @@ class OpenAIHelper:
|
||||
self.conversations: dict[int: list] = {} # {chat_id: history}
|
||||
self.last_updated: dict[int: datetime] = {} # {chat_id: last_update_timestamp}
|
||||
|
||||
def get_conversation_stats(self, chat_id: int) -> tuple[int, int]:
|
||||
"""
|
||||
Gets the number of messages and tokens used in the conversation.
|
||||
:param chat_id: The chat ID
|
||||
:return: A tuple containing the number of messages and tokens used
|
||||
"""
|
||||
if chat_id not in self.conversations:
|
||||
self.reset_chat_history(chat_id)
|
||||
return (len(self.conversations[chat_id]), self.__count_tokens(self.conversations[chat_id]))
|
||||
|
||||
async def get_chat_response(self, chat_id: int, query: str) -> Union[tuple[str, str], str]:
|
||||
"""
|
||||
Gets a response from the GPT-3 model.
|
||||
@@ -172,8 +187,12 @@ class OpenAIHelper:
|
||||
return response.choices[0]['message']['content']
|
||||
|
||||
def __max_model_tokens(self):
|
||||
if self.config['model'] == "gpt-3.5-turbo" or self.config['model'] == "gpt-3.5-turbo-0301":
|
||||
if self.config['model'] in GPT_3_MODELS:
|
||||
return 4096
|
||||
if self.config['model'] in GPT_4_MODELS:
|
||||
return 8192
|
||||
if self.config['model'] in GPT_4_32K_MODELS:
|
||||
return 32768
|
||||
raise NotImplementedError(
|
||||
f"Max tokens for model {self.config['model']} is not implemented yet."
|
||||
)
|
||||
@@ -190,7 +209,7 @@ class OpenAIHelper:
|
||||
encoding = tiktoken.encoding_for_model(model)
|
||||
except KeyError:
|
||||
encoding = tiktoken.get_encoding("gpt-3.5-turbo")
|
||||
if model == "gpt-3.5-turbo" or model == "gpt-3.5-turbo-0301":
|
||||
if model in GPT_ALL_MODELS:
|
||||
num_tokens = 0
|
||||
for message in messages:
|
||||
num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
|
||||
|
||||
@@ -70,6 +70,9 @@ class ChatGPT3TelegramBot:
|
||||
transcribe_durations = self.usage[user_id].get_current_transcription_duration()
|
||||
cost_today, cost_month = self.usage[user_id].get_current_cost()
|
||||
|
||||
chat_id = update.effective_chat.id
|
||||
chat_messages, chat_token_length = self.openai.get_conversation_stats(chat_id)
|
||||
|
||||
usage_text = f"Today:\n"+\
|
||||
f"{tokens_today} chat tokens used.\n"+\
|
||||
f"{images_today} images generated.\n"+\
|
||||
@@ -80,7 +83,11 @@ class ChatGPT3TelegramBot:
|
||||
f"{tokens_month} chat tokens used.\n"+\
|
||||
f"{images_month} images generated.\n"+\
|
||||
f"{transcribe_durations[2]} minutes and {transcribe_durations[3]} seconds transcribed.\n"+\
|
||||
f"💰 For a total amount of ${cost_month:.2f}"
|
||||
f"💰 For a total amount of ${cost_month:.2f}"+\
|
||||
f"\n----------------------------\n\n"+\
|
||||
f"Current conversation:\n"+\
|
||||
f"{chat_messages} chat messages in history.\n"+\
|
||||
f"{chat_token_length} chat tokens in history.\n"
|
||||
await update.message.reply_text(usage_text)
|
||||
|
||||
async def reset(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
@@ -374,7 +381,7 @@ class ChatGPT3TelegramBot:
|
||||
"""
|
||||
Handles errors in the telegram-python-bot library.
|
||||
"""
|
||||
logging.debug(f'Exception while handling an update: {context.error}')
|
||||
logging.error(f'Exception while handling an update: {context.error}')
|
||||
|
||||
def is_group_chat(self, update: Update) -> bool:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user