Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/allcodny_ai-character-telebot.json
T

1 line
7.5 KiB
JSON
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{"repo": "allcodny/ai-character-telebot", "files": {"bot.py": {"d8fc202e3f84e4bf6a2bce43eac3e97dd4cc49f8": {"t": 1785674362.518567, "n": 7154, "h": "78f49cc037800c3f", "c": "import telebot\nimport re\nimport requests\nimport json\nimport base64\nimport os\nimport random\nimport difflib\n\ntry:\n from prompts import *\nexcept ImportError:\n from bot_prompts import *\n print(\"WARNING: This will use a promt template. Create your own prompts.py file.\")\n\ntry:\n from config import *\nexcept ImportError:\n from bot_config import *\n\n\nTG_CHANNEL_ID = 777000 # constant\nadm = ADMIN_ID\n\nlast_media_group = -1\n\nbot = telebot.TeleBot(TOKEN_BOT)\n\n# Gets an image and returns the AI ​​description of the image.\ndef analyze_image(image_data):\n print(\"Image recognition...\")\n\n base64_image = base64.b64encode(image_data).decode('utf-8')\n image_data_url = f\"data:image/jpeg;base64,{base64_image}\"\n\n for n in range(MAX_TRY):\n if n == 0:\n model = MODEL_IM\n else:\n model = MODEL_IM2\n response = requests.post(\n url=\"https://openrouter.ai/api/v1/chat/completions\",\n headers={\n \"Authorization\": f\"Bearer {API_KEY}\"\n },\n data=json.dumps({\n \"model\": model,\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": [\n {\n \"type\": \"text\",\n \"text\": prompt_im\n },\n {\n \"type\": \"image_url\",\n \"image_url\": {\n \"url\": image_data_url\n }\n }\n ]\n }\n ],\n \n })\n )\n print(\"Recognition completed.\")\n\n if response.status_code == 200:\n answer = response.json()[\"choices\"][0][\"message\"][\"content\"]\n if answer is None or answer.strip() == \"\":\n print(\"Empty answer, try again...\")\n else:\n return response.json()[\"choices\"][0][\"message\"][\"content\"]\n else:\n print(f\"Error: {response.status_code}, try again...\")\n \n error = response.status_code if response.status_code != 200 else \"Empty answer\"\n bot.send_message(adm, f\"Error while analyzing the photo: {error}\")\n return None\n\n\n\n# Sending an AI request\ndef get_answer(context, text):\n print(\"Sending request...\")\n prompt = prompt_main + context\n\n for n in range(MAX_TRY):\n response = requests.post(\n url=\"https://openrouter.ai/api/v1/chat/completions\",\n headers={\n \"Authorization\": f\"Bearer {API_KEY}\"\n },\n data=json.dumps({\n \"model\": MODEL,\n \"messages\": [\n { \"role\": \"system\",\n \"content\": prompt\n },\n {\n \"role\": \"user\",\n \"content\": text\n }\n ]\n })\n )\n print(\"Request sent.\")\n\n print(response.status_code)\n if response.status_code == 200:\n answer = response.json()[\"choices\"][0][\"message\"][\"content\"]\n if answer is None or answer.strip() == \"\":\n print(\"Empty answer, try again...\")\n else:\n return answer\n else:\n bot.send_message(adm, f\"Error sending AI request: {response.status_code}\")\n return False\n \n error = response.status_code if response.status_code != 200 else \"Empty answer\"\n bot.send_message(adm, f\"Error sending AI request: {error}\")\n\n\n# Removes duplicate text (a problem with some AI models)\ndef remove_duplicate_text(text, similarity_threshold=0.9):\n lines = text.strip().split('\\n')\n if len(lines) <= 1:\n return text\n \n half = len(lines) // 2\n first_half = lines[:half]\n second_half = lines[half:]\n \n similarity = difflib.SequenceMatcher(None, first_half, second_half).ratio()\n \n if similarity >= similarity_threshold:\n return '\\n'.join(first_half)\n else:\n return text\n\n\n# checks if the bot can respond\ndef can_answer(message, text):\n if PHRASE_BLOCKLIST and text is not None and any(phrase in text for phrase in PHRASE_BLOCKLIST):\n return False\n\n if message.chat.type == 'private': # in private messages\n if not WHITELIST_PRIVATE or message.from_user.id in WHITELIST:\n return True\n else:\n return False\n elif not WHITELIST_CHAT or message.chat.id in WHITE_CHATS: # in chats\n if random.random() < CHANCE_COMMENTS and message.from_user.id == TG_CHANNEL_ID: # comment\n return True\n elif random.random() < CHANCE_REPLY and message.reply_to_message and message.reply_to_message.from_user.id == bot.get_me().id: # The bot responded to the message\n return True\n elif random.random() < CHANCE_CHAT and message.from_user.id != TG_CHANNEL_ID:\n return True\n else:\n return False\n else:\n return False\n\n\n@bot.message_handler(content_types=['text', 'photo'])\ndef echo_message(message):\n global last_media_group\n\n if message.content_type == \"text\":\n text = message.text\n elif message.caption is not None:\n text = message.caption\n else:\n text = \"\"\n \n can_answ = can_answer(message, text)\n if can_answ:\n # If there is a photo, it is recognized and a description is added to the text.\n if message.content_type == \"photo\":\n if not REPLY_ALL_PHOTO and last_media_group == message.media_group_id:\n return\n elif message.media_group_id is not None:\n last_media_group = message.media_group_id\n file_info = bot.get_file(message.photo[-1].file_id)\n file = bot.download_file(file_info.file_path)\n result = analyze_image(file)\n if result is None and ADMIN_DESCRIP_IM:\n send = bot.send_photo(adm, message.photo[-1].file_id, \"Enter a description of the image:\")\n bot.register_next_step_handler(send, get_im_descript, message, text)\n return\n if result is not None:\n text = f\"{text}\\n\\nAttached image: [{result}]\"\n if not text or text.strip() == \"\":\n bot.reply_to(message, \"An empty request was sent.\")\n else:\n send_answer(message, text)\n elif can_answ is None:\n return\n elif not NO_WHITELIST_M.strip() == \"\" and message.chat.type == 'private':\n bot.reply_to(message, NO_WHITELIST_M)\n\ndef get_im_descript(message, mes, text):\n text = f\"{text}\\n\\nAttached image: [{message.text}]\"\n send_answer(mes, text)\n\ndef send_answer(message, text):\n # Additional prompt depending on context\n if message.from_user.id == TG_CHANNEL_ID:\n extra_prompt = prompt_comment\n elif message.chat.type == 'private':\n extra_prompt = prompt_private\n elif message.from_user.id in SPECIAL_IDS:\n extra_prompt = prompt_special\n else:\n extra_prompt = prompt_chat\n\n answer = get_answer(extra_prompt, text)\n if answer:\n answer = re.sub(r'<think>.*?</think>', '', answer, flags=re.DOTALL)\n answer = remove_duplicate_text(answer)\n print(\"Message sends.\")\n bot.reply_to(message, answer)\n\n\nbot.infinity_polling(none_stop=True)"}}}}