mirror of
				https://github.com/cdryzun/tg_bot_collections.git
				synced 2025-11-04 16:56:43 +08:00 
			
		
		
		
	@ -5,6 +5,7 @@ from openai import OpenAI
 | 
				
			|||||||
from telebot import TeleBot
 | 
					from telebot import TeleBot
 | 
				
			||||||
from telebot.types import Message
 | 
					from telebot.types import Message
 | 
				
			||||||
from expiringdict import ExpiringDict
 | 
					from expiringdict import ExpiringDict
 | 
				
			||||||
 | 
					from rich import print
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from . import *
 | 
					from . import *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -16,8 +17,8 @@ markdown_symbol.link = "🔗"  # If you want, Customizing the link symbol
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
CHATGPT_API_KEY = environ.get("OPENAI_API_KEY")
 | 
					CHATGPT_API_KEY = environ.get("OPENAI_API_KEY")
 | 
				
			||||||
CHATGPT_BASE_URL = environ.get("OPENAI_API_BASE") or "https://api.openai.com/v1"
 | 
					CHATGPT_BASE_URL = environ.get("OPENAI_API_BASE") or "https://api.openai.com/v1"
 | 
				
			||||||
CHATGPT_MODEL = "gpt-3.5-turbo"
 | 
					CHATGPT_MODEL = "gpt-4o-mini-2024-07-18"
 | 
				
			||||||
CHATGPT_PRO_MODEL = "gpt-4o-2024-05-13"
 | 
					CHATGPT_PRO_MODEL = "gpt-4o-mini-2024-07-18"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
client = OpenAI(api_key=CHATGPT_API_KEY, base_url=CHATGPT_BASE_URL)
 | 
					client = OpenAI(api_key=CHATGPT_API_KEY, base_url=CHATGPT_BASE_URL)
 | 
				
			||||||
@ -30,6 +31,7 @@ chatgpt_pro_player_dict = ExpiringDict(max_len=1000, max_age_seconds=600)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def chatgpt_handler(message: Message, bot: TeleBot) -> None:
 | 
					def chatgpt_handler(message: Message, bot: TeleBot) -> None:
 | 
				
			||||||
    """gpt : /gpt <question>"""
 | 
					    """gpt : /gpt <question>"""
 | 
				
			||||||
 | 
					    print(message)
 | 
				
			||||||
    m = message.text.strip()
 | 
					    m = message.text.strip()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    player_message = []
 | 
					    player_message = []
 | 
				
			||||||
@ -126,19 +128,20 @@ def chatgpt_pro_handler(message: Message, bot: TeleBot) -> None:
 | 
				
			|||||||
    try:
 | 
					    try:
 | 
				
			||||||
        r = client.chat.completions.create(
 | 
					        r = client.chat.completions.create(
 | 
				
			||||||
            messages=player_message,
 | 
					            messages=player_message,
 | 
				
			||||||
            max_tokens=4096,
 | 
					 | 
				
			||||||
            model=CHATGPT_PRO_MODEL,
 | 
					            model=CHATGPT_PRO_MODEL,
 | 
				
			||||||
            stream=True,
 | 
					            stream=True,
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        s = ""
 | 
					        s = ""
 | 
				
			||||||
        start = time.time()
 | 
					        start = time.time()
 | 
				
			||||||
        for chunk in r:
 | 
					        for chunk in r:
 | 
				
			||||||
            if chunk.choices[0].delta.content is None:
 | 
					            print(chunk)
 | 
				
			||||||
                break
 | 
					            if chunk.choices:
 | 
				
			||||||
            s += chunk.choices[0].delta.content
 | 
					                if chunk.choices[0].delta.content is None:
 | 
				
			||||||
            if time.time() - start > 1.2:
 | 
					                    break
 | 
				
			||||||
                start = time.time()
 | 
					                s += chunk.choices[0].delta.content
 | 
				
			||||||
                bot_reply_markdown(reply_id, who, s, bot, split_text=False)
 | 
					                if time.time() - start > 1.2:
 | 
				
			||||||
 | 
					                    start = time.time()
 | 
				
			||||||
 | 
					                    bot_reply_markdown(reply_id, who, s, bot, split_text=False)
 | 
				
			||||||
        # maybe not complete
 | 
					        # maybe not complete
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            bot_reply_markdown(reply_id, who, s, bot, split_text=True)
 | 
					            bot_reply_markdown(reply_id, who, s, bot, split_text=True)
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user