Merge branch 'main' of github.com:karfly/chatgpt_telegram_bot

This commit is contained in:
Karim Iskakov
2023-06-20 17:34:52 +03:00
5 changed files with 33 additions and 13 deletions
+8 -4
View File
@@ -94,7 +94,7 @@ async def register_user_if_not_exists(update: Update, context: CallbackContext,
# back compatibility for n_used_tokens field # back compatibility for n_used_tokens field
n_used_tokens = db.get_user_attribute(user.id, "n_used_tokens") n_used_tokens = db.get_user_attribute(user.id, "n_used_tokens")
if isinstance(n_used_tokens, int): # old format if isinstance(n_used_tokens, int) or isinstance(n_used_tokens, float): # old format
new_n_used_tokens = { new_n_used_tokens = {
"gpt-3.5-turbo": { "gpt-3.5-turbo": {
"n_input_tokens": 0, "n_input_tokens": 0,
@@ -665,6 +665,8 @@ def run_bot() -> None:
.token(config.telegram_token) .token(config.telegram_token)
.concurrent_updates(True) .concurrent_updates(True)
.rate_limiter(AIORateLimiter(max_retries=5)) .rate_limiter(AIORateLimiter(max_retries=5))
.http_version("1.1")
.get_updates_http_version("1.1")
.post_init(post_init) .post_init(post_init)
.build() .build()
) )
@@ -673,8 +675,10 @@ def run_bot() -> None:
user_filter = filters.ALL user_filter = filters.ALL
if len(config.allowed_telegram_usernames) > 0: if len(config.allowed_telegram_usernames) > 0:
usernames = [x for x in config.allowed_telegram_usernames if isinstance(x, str)] usernames = [x for x in config.allowed_telegram_usernames if isinstance(x, str)]
user_ids = [x for x in config.allowed_telegram_usernames if isinstance(x, int)] any_ids = [x for x in config.allowed_telegram_usernames if isinstance(x, int)]
user_filter = filters.User(username=usernames) | filters.User(user_id=user_ids) user_ids = [x for x in any_ids if x > 0]
group_ids = [x for x in any_ids if x < 0]
user_filter = filters.User(username=usernames) | filters.User(user_id=user_ids) | filters.Chat(chat_id=group_ids)
application.add_handler(CommandHandler("start", start_handle, filters=user_filter)) application.add_handler(CommandHandler("start", start_handle, filters=user_filter))
application.add_handler(CommandHandler("help", help_handle, filters=user_filter)) application.add_handler(CommandHandler("help", help_handle, filters=user_filter))
@@ -703,4 +707,4 @@ def run_bot() -> None:
if __name__ == "__main__": if __name__ == "__main__":
run_bot() run_bot()
+7 -4
View File
@@ -16,7 +16,7 @@ OPENAI_COMPLETION_OPTIONS = {
class ChatGPT: class ChatGPT:
def __init__(self, model="gpt-3.5-turbo"): def __init__(self, model="gpt-3.5-turbo"):
assert model in {"text-davinci-003", "gpt-3.5-turbo", "gpt-4"}, f"Unknown model: {model}" assert model in {"text-davinci-003", "gpt-3.5-turbo-16k", "gpt-3.5-turbo", "gpt-4"}, f"Unknown model: {model}"
self.model = model self.model = model
async def send_message(self, message, dialog_messages=[], chat_mode="assistant"): async def send_message(self, message, dialog_messages=[], chat_mode="assistant"):
@@ -27,7 +27,7 @@ class ChatGPT:
answer = None answer = None
while answer is None: while answer is None:
try: try:
if self.model in {"gpt-3.5-turbo", "gpt-4"}: if self.model in {"gpt-3.5-turbo-16k", "gpt-3.5-turbo", "gpt-4"}:
messages = self._generate_prompt_messages(message, dialog_messages, chat_mode) messages = self._generate_prompt_messages(message, dialog_messages, chat_mode)
r = await openai.ChatCompletion.acreate( r = await openai.ChatCompletion.acreate(
model=self.model, model=self.model,
@@ -67,7 +67,7 @@ class ChatGPT:
answer = None answer = None
while answer is None: while answer is None:
try: try:
if self.model in {"gpt-3.5-turbo", "gpt-4"}: if self.model in {"gpt-3.5-turbo-16k", "gpt-3.5-turbo", "gpt-4"}:
messages = self._generate_prompt_messages(message, dialog_messages, chat_mode) messages = self._generate_prompt_messages(message, dialog_messages, chat_mode)
r_gen = await openai.ChatCompletion.acreate( r_gen = await openai.ChatCompletion.acreate(
model=self.model, model=self.model,
@@ -146,9 +146,12 @@ class ChatGPT:
def _count_tokens_from_messages(self, messages, answer, model="gpt-3.5-turbo"): def _count_tokens_from_messages(self, messages, answer, model="gpt-3.5-turbo"):
encoding = tiktoken.encoding_for_model(model) encoding = tiktoken.encoding_for_model(model)
if model == "gpt-3.5-turbo": if model == "gpt-3.5-turbo-16k":
tokens_per_message = 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n tokens_per_message = 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
tokens_per_name = -1 # if there's a name, the role is omitted tokens_per_name = -1 # if there's a name, the role is omitted
elif model == "gpt-3.5-turbo":
tokens_per_message = 4
tokens_per_name = -1
elif model == "gpt-4": elif model == "gpt-4":
tokens_per_message = 3 tokens_per_message = 3
tokens_per_name = 1 tokens_per_name = 1
+2 -2
View File
@@ -70,11 +70,11 @@ motivator:
money_maker: money_maker:
name: 💰 Money Maker name: 💰 Money Maker
welcome_message: 💰 Hi, I'm <b>Money Maker</b>. My goal it to turn your capital initial into as much money as possible. I will tell you specific steps, what to do to make money. welcome_message: 💰 Hi, I'm <b>Money Maker</b>. My goal is to turn your initial capital into as much money as possible. I will tell you specific steps and what to do to make money.
prompt_start: | prompt_start: |
You are Money Maker Assistant, an entrepreneurial Al. User is your human counterpart. You are Money Maker Assistant, an entrepreneurial Al. User is your human counterpart.
User can act as a connection between you and the physical world. Your only goal is to turn user's capital initial into as much money as possible in the shortest time possible, without doing anything illegal. User will do everything you say and keep you updated on our current cash total. No manual labor User can act as a connection between you and the physical world. Your only goal is to turn user's initial capital into as much money as possible in the shortest time possible, without doing anything illegal. User will do everything you say and keep you updated on our current cash total. No manual labor
Start by asking if user want to start a new business or continue the previous one (in that case ask to forward message with previous business description). Start by asking if user want to start a new business or continue the previous one (in that case ask to forward message with previous business description).
Then ask how many dollars user has as a capital initial. Then ask how many dollars user has as a capital initial.
+1 -1
View File
@@ -1,7 +1,7 @@
telegram_token: "" telegram_token: ""
openai_api_key: "" openai_api_key: ""
use_chatgpt_api: true use_chatgpt_api: true
allowed_telegram_usernames: [] # if empty, the bot is available to anyone. pass a username string to allow it and/or user ids as integers allowed_telegram_usernames: [] # if empty, the bot is available to anyone. pass a username string to allow it and/or user ids as positive integers and/or channel ids as negative integers
new_dialog_timeout: 600 # new dialog starts after timeout (in seconds) new_dialog_timeout: 600 # new dialog starts after timeout (in seconds)
return_n_generated_images: 1 return_n_generated_images: 1
n_chat_modes_per_page: 5 n_chat_modes_per_page: 5
+15 -2
View File
@@ -1,4 +1,4 @@
available_text_models: ["gpt-3.5-turbo", "gpt-4", "text-davinci-003"] available_text_models: ["gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-4", "text-davinci-003"]
info: info:
gpt-3.5-turbo: gpt-3.5-turbo:
@@ -6,7 +6,7 @@ info:
name: ChatGPT name: ChatGPT
description: ChatGPT is that well-known model. It's <b>fast</b> and <b>cheap</b>. Ideal for everyday tasks. If there are some tasks it can't handle, try the <b>GPT-4</b>. description: ChatGPT is that well-known model. It's <b>fast</b> and <b>cheap</b>. Ideal for everyday tasks. If there are some tasks it can't handle, try the <b>GPT-4</b>.
price_per_1000_input_tokens: 0.002 price_per_1000_input_tokens: 0.0015
price_per_1000_output_tokens: 0.002 price_per_1000_output_tokens: 0.002
scores: scores:
@@ -14,6 +14,19 @@ info:
Fast: 5 Fast: 5
Cheap: 5 Cheap: 5
gpt-3.5-turbo-16k:
type: chat_completion
name: GPT-16K
description: ChatGPT is that well-known model. It's <b>fast</b> and <b>cheap</b>. Ideal for everyday tasks. If there are some tasks it can't handle, try the <b>GPT-4</b>.
price_per_1000_input_tokens: 0.003
price_per_1000_output_tokens: 0.004
scores:
Smart: 3
Fast: 5
Cheap: 5
gpt-4: gpt-4:
type: chat_completion type: chat_completion
name: GPT-4 name: GPT-4