srai-telegrambot


Namesrai-telegrambot JSON
Version 0.17.2 PyPI version JSON
download
home_pagehttps://github.com/southriverai/srai-telegrambot
SummaryA library that makes it easier to build ai powered telegram bots.
upload_time2024-08-21 05:28:27
maintainerNone
docs_urlNone
authorJaap Oosterbroek
requires_python<4.0,>=3.10
licenseMIT
keywords srai tools
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # srai-telegrambot
A library that makes it easier to build ai powered telegram bots

# install
pip install srai-telegrambot

## get your token
get your telegrambot token from the botfather as described here https://core.telegram.org/bots/tutorial

## usage
This is how i use the bot:
I rely on mongo db to save messages there are other options!
You can also implement your own version of dao telegrambot.
I will add a in-memory database that i use for testing in the near future

```python
import os
from srai_core.store.database_mongo import DatabaseMongo
from srai_core.tools_env import get_string_from_env

from srai_telegrambot.command.command_chat_id import CommandChatId
from srai_telegrambot.command.command_help import CommandHelp
from srai_telegrambot.command.command_image_tag import CommandImageTag
from srai_telegrambot.dao_telegram_bot import DaoTelegramBot
from srai_telegrambot.mode.text_mode_gpt import TextModeGpt
from srai_telegrambot.mode.text_mode_rag import TextModeRag
from srai_telegrambot.telegram_bot import TelegramBot

if __name__ == "__main__":
    telegram_token = get_string_from_env("SRAI_TELEGRAM_TOKEN")
    connection_string = get_string_from_env("MONGODB_CONNECTION_STRING")
    telegram_root_id = get_string_from_env(
        "TELEGRAM_ROOT_ID"
    )  # this is my user id and it gives me admin rights in the bot

    database_mongo = DatabaseMongo("database_telegrambot", connection_string)
    dao_telegram_bot = DaoTelegramBot(database_mongo)
    bot = TelegramBot(
        token=telegram_token,
        dao_telegram_bot=dao_telegram_bot,
    )

    bot.register_admin(telegram_root_id)

    # register commands
    bot.register_command(CommandHelp())
    bot.register_command(CommandChatId())
    bot.register_command(CommandImageTag())
    bot.register_text_mode(TextModeGpt("you are a chatbot"), False)

    path_dir_vectorstore = os.path.abspath(os.path.join("test", "data", "vectorstore"))
    bot.register_text_mode(TextModeRag(path_dir_vectorstore), True)
    # start bot
    bot.main()
```
### How to build a voice interface to chatgpt
Below is code for a voice interface to chatgpt

```python
from srai_core.store.database_mongo import DatabaseMongo
from srai_core.tools_env import get_string_from_env

from srai_telegrambot.command.command_chat_id import CommandChatId
from srai_telegrambot.command.command_help import CommandHelp
from srai_telegrambot.command.command_image_tag import CommandImageTag
from srai_telegrambot.dao_telegram_bot import DaoTelegramBot
from srai_telegrambot.mode.voice_mode_gpt import VoiceModeGpt
from srai_telegrambot.telegram_bot import TelegramBot

if __name__ == "__main__":
    telegram_token = get_string_from_env("SRAI_TELEGRAM_TOKEN")
    connection_string = get_string_from_env("MONGODB_CONNECTION_STRING")
    telegram_root_id = get_string_from_env(
        "TELEGRAM_ROOT_ID"
    )  # this is my user id and it gives me admin rights in the bot

    database_mongo = DatabaseMongo("database_telegrambot", connection_string)
    dao_telegram_bot = DaoTelegramBot(database_mongo)
    bot = TelegramBot(
        token=telegram_token,
        dao_telegram_bot=dao_telegram_bot,
    )

    bot.register_admin(telegram_root_id)

    # register commands
    bot.register_command(CommandHelp())
    bot.register_command(CommandChatId())
    bot.register_command(CommandImageTag())
    bot.register_voice_mode(VoiceModeGpt("You are a chatbot. Never respond in more that three sentences"), True)


    # start bot
    bot.main()
```


### How to build a RAG telegram bot vectorstore
Below is code for a rag telegram bot

```python
import os

from srai_telegrambot.mode.text_mode_rag import TextModeRag
from srai_telegrambot.telegram_bot_test import TelegramBotTest

path_dir_vectorstore = os.path.abspath(os.path.join("test", "data", "vectorstore"))
mode = TextModeRag(path_dir_vectorstore)
mode.register(TelegramBotTest())

list_path_file = []
list_path_file.append(os.path.abspath(os.path.join("test", "data", "paper_0.pdf")))
list_path_file.append(os.path.abspath(os.path.join("test", "data", "paper_1.pdf")))
list_path_file.append(os.path.abspath(os.path.join("test", "data", "paper_2.pdf")))

for path_file in list_path_file:
    mode.add_path_file_pdf(path_file)
mode.rebuild_vectorstore()
print(mode._handle_text("test_chat", "What is the most important parameter in DCE-CT in stroke?"))
print(mode._handle_text("test_chat", "Please elaborate?"))
```


## Changelog

### 0.17.0
- Added voice support
- Removed inate vectorstore FAISS

### 0.16.0
- Added RAG text mode
- Moved memory databases to core lib

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/southriverai/srai-telegrambot",
    "name": "srai-telegrambot",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": "SRAI, TOOLS",
    "author": "Jaap Oosterbroek",
    "author_email": "jaap.oosterbroek@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/f5/f8/04ecdc6661c52e75d6cb25899358998de78c793195c0e75e336f0ca245ed/srai_telegrambot-0.17.2.tar.gz",
    "platform": null,
    "description": "# srai-telegrambot\nA library that makes it easier to build ai powered telegram bots\n\n# install\npip install srai-telegrambot\n\n## get your token\nget your telegrambot token from the botfather as described here https://core.telegram.org/bots/tutorial\n\n## usage\nThis is how i use the bot:\nI rely on mongo db to save messages there are other options!\nYou can also implement your own version of dao telegrambot.\nI will add a in-memory database that i use for testing in the near future\n\n```python\nimport os\nfrom srai_core.store.database_mongo import DatabaseMongo\nfrom srai_core.tools_env import get_string_from_env\n\nfrom srai_telegrambot.command.command_chat_id import CommandChatId\nfrom srai_telegrambot.command.command_help import CommandHelp\nfrom srai_telegrambot.command.command_image_tag import CommandImageTag\nfrom srai_telegrambot.dao_telegram_bot import DaoTelegramBot\nfrom srai_telegrambot.mode.text_mode_gpt import TextModeGpt\nfrom srai_telegrambot.mode.text_mode_rag import TextModeRag\nfrom srai_telegrambot.telegram_bot import TelegramBot\n\nif __name__ == \"__main__\":\n    telegram_token = get_string_from_env(\"SRAI_TELEGRAM_TOKEN\")\n    connection_string = get_string_from_env(\"MONGODB_CONNECTION_STRING\")\n    telegram_root_id = get_string_from_env(\n        \"TELEGRAM_ROOT_ID\"\n    )  # this is my user id and it gives me admin rights in the bot\n\n    database_mongo = DatabaseMongo(\"database_telegrambot\", connection_string)\n    dao_telegram_bot = DaoTelegramBot(database_mongo)\n    bot = TelegramBot(\n        token=telegram_token,\n        dao_telegram_bot=dao_telegram_bot,\n    )\n\n    bot.register_admin(telegram_root_id)\n\n    # register commands\n    bot.register_command(CommandHelp())\n    bot.register_command(CommandChatId())\n    bot.register_command(CommandImageTag())\n    bot.register_text_mode(TextModeGpt(\"you are a chatbot\"), False)\n\n    path_dir_vectorstore = os.path.abspath(os.path.join(\"test\", \"data\", \"vectorstore\"))\n    bot.register_text_mode(TextModeRag(path_dir_vectorstore), True)\n    # start bot\n    bot.main()\n```\n### How to build a voice interface to chatgpt\nBelow is code for a voice interface to chatgpt\n\n```python\nfrom srai_core.store.database_mongo import DatabaseMongo\nfrom srai_core.tools_env import get_string_from_env\n\nfrom srai_telegrambot.command.command_chat_id import CommandChatId\nfrom srai_telegrambot.command.command_help import CommandHelp\nfrom srai_telegrambot.command.command_image_tag import CommandImageTag\nfrom srai_telegrambot.dao_telegram_bot import DaoTelegramBot\nfrom srai_telegrambot.mode.voice_mode_gpt import VoiceModeGpt\nfrom srai_telegrambot.telegram_bot import TelegramBot\n\nif __name__ == \"__main__\":\n    telegram_token = get_string_from_env(\"SRAI_TELEGRAM_TOKEN\")\n    connection_string = get_string_from_env(\"MONGODB_CONNECTION_STRING\")\n    telegram_root_id = get_string_from_env(\n        \"TELEGRAM_ROOT_ID\"\n    )  # this is my user id and it gives me admin rights in the bot\n\n    database_mongo = DatabaseMongo(\"database_telegrambot\", connection_string)\n    dao_telegram_bot = DaoTelegramBot(database_mongo)\n    bot = TelegramBot(\n        token=telegram_token,\n        dao_telegram_bot=dao_telegram_bot,\n    )\n\n    bot.register_admin(telegram_root_id)\n\n    # register commands\n    bot.register_command(CommandHelp())\n    bot.register_command(CommandChatId())\n    bot.register_command(CommandImageTag())\n    bot.register_voice_mode(VoiceModeGpt(\"You are a chatbot. Never respond in more that three sentences\"), True)\n\n\n    # start bot\n    bot.main()\n```\n\n\n### How to build a RAG telegram bot vectorstore\nBelow is code for a rag telegram bot\n\n```python\nimport os\n\nfrom srai_telegrambot.mode.text_mode_rag import TextModeRag\nfrom srai_telegrambot.telegram_bot_test import TelegramBotTest\n\npath_dir_vectorstore = os.path.abspath(os.path.join(\"test\", \"data\", \"vectorstore\"))\nmode = TextModeRag(path_dir_vectorstore)\nmode.register(TelegramBotTest())\n\nlist_path_file = []\nlist_path_file.append(os.path.abspath(os.path.join(\"test\", \"data\", \"paper_0.pdf\")))\nlist_path_file.append(os.path.abspath(os.path.join(\"test\", \"data\", \"paper_1.pdf\")))\nlist_path_file.append(os.path.abspath(os.path.join(\"test\", \"data\", \"paper_2.pdf\")))\n\nfor path_file in list_path_file:\n    mode.add_path_file_pdf(path_file)\nmode.rebuild_vectorstore()\nprint(mode._handle_text(\"test_chat\", \"What is the most important parameter in DCE-CT in stroke?\"))\nprint(mode._handle_text(\"test_chat\", \"Please elaborate?\"))\n```\n\n\n## Changelog\n\n### 0.17.0\n- Added voice support\n- Removed inate vectorstore FAISS\n\n### 0.16.0\n- Added RAG text mode\n- Moved memory databases to core lib\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A library that makes it easier to build ai powered telegram bots.",
    "version": "0.17.2",
    "project_urls": {
        "Homepage": "https://github.com/southriverai/srai-telegrambot",
        "Repository": "https://github.com/southriverai/srai-telegrambot"
    },
    "split_keywords": [
        "srai",
        " tools"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b6e52840119cbcf052fb59e2fb42216ccb725ebb3d23bcdab26f0aa1a4aeeecc",
                "md5": "46bd6ca301e5e96ad4716b10271c1dd8",
                "sha256": "a8f301f1f8bc12b260b7dd9c44c4ff44bd1f7130fd35549f3698c83baaaef539"
            },
            "downloads": -1,
            "filename": "srai_telegrambot-0.17.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "46bd6ca301e5e96ad4716b10271c1dd8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 14373,
            "upload_time": "2024-08-21T05:28:26",
            "upload_time_iso_8601": "2024-08-21T05:28:26.485529Z",
            "url": "https://files.pythonhosted.org/packages/b6/e5/2840119cbcf052fb59e2fb42216ccb725ebb3d23bcdab26f0aa1a4aeeecc/srai_telegrambot-0.17.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f5f804ecdc6661c52e75d6cb25899358998de78c793195c0e75e336f0ca245ed",
                "md5": "5c95e9387265f9834eda41540449efe9",
                "sha256": "d47fd113c1756f45ca5a6c98cdcbed504b33207affba64633b08fcd9642ce336"
            },
            "downloads": -1,
            "filename": "srai_telegrambot-0.17.2.tar.gz",
            "has_sig": false,
            "md5_digest": "5c95e9387265f9834eda41540449efe9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 9652,
            "upload_time": "2024-08-21T05:28:27",
            "upload_time_iso_8601": "2024-08-21T05:28:27.835158Z",
            "url": "https://files.pythonhosted.org/packages/f5/f8/04ecdc6661c52e75d6cb25899358998de78c793195c0e75e336f0ca245ed/srai_telegrambot-0.17.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-21 05:28:27",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "southriverai",
    "github_project": "srai-telegrambot",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "srai-telegrambot"
}
        
Elapsed time: 0.31578s