Giter VIP home page Giter VIP logo

telegramgpt's Introduction

Telegram Chatbot with OpenAI GPT and SQLite

Overview

This project is a Telegram chatbot built using Python. It utilizes the OpenAI GPT model for generating conversational responses and SQLite for storing chat history. The chatbot is designed to provide concise and helpful answers.

Features

  • Token Management: Efficiently manages the token count for API requests to OpenAI.
  • User Authorization: Only allows authorized users to interact with the bot.
  • Chat History: Stores the chat history in a SQLite database.
  • Environment Variables: Configurable settings using environment variables.

Prerequisites

  • Python 3.x
  • SQLite
  • OpenAI Python package
  • Telegram API
  • Tiktoken Python package
  • VLLM for local AI

Installation

  1. Clone this repository.

  2. Install the required Python packages.

    pip install -r requirements.txt
  3. Set up your environment variables. You can either export them in your shell or create a .env file.

    export TELEGRAM_BOT_KEY=your_telegram_bot_key
    export OPENAI_API_KEY=your_openai_key
    export MAX_TOKENS=16000
    export MODEL_NAME=gpt-3.5-turbo-16k
    export ALLOWED_USERS='[9999999, 1111111]'

    Or in a .env file:

    TELEGRAM_BOT_KEY=your_telegram_bot_key
    OPENAI_API_KEY=your_openai_key
    MAX_TOKENS=16000
    MODEL_NAME=gpt-3.5-turbo-16k
    ALLOWED_USERS=[user_id1, user_id2]
    

Configuration for VLLM

To use the VLLM server instead of the OpenAI API, set the following environment variables in your shell or in a .env file:

export OPENAI_API_KEY=null
export OPENAI_API_BASE=http://localhost:8000/v1

Make sure your VLLM server is running and accessible at the OPENAI_API_BASE URL. For local testing, it should be http://localhost:8000/v1.

Usage

Run the main Python script to start the bot.

python bot.py

Customizing the System Message

You can customize the system message that guides the chatbot's behavior by editing the system.txt file.

Docker Deployment

Configuration

Before building and running the Docker container, make sure to create a config.env file in the same directory as your Dockerfile. Add your Telegram bot API key and OpenAI API key to this file:

TELEGRAM_BOT_KEY=your_telegram_bot_key_here
OPENAI_API_KEY=your_openai_api_key_here
# Add other environment variables as needed

Building the Docker Image

To build the Docker image, navigate to the directory containing the Dockerfile and run:

docker build -t telegramgpt:latest .

This will build a Docker image and tag it as telegramgpt:latest.

Running the Docker Container

To run the Docker container in detached mode, you can use the following command:

docker run -d --name telegramgpt --restart=always --env-file config.env -v ${PWD}/data:/app/data telegramgpt:latest

This command does the following:

  • -d runs the container in detached mode, in the background.
  • --name telegramgpt names the container "telegramgpt".
  • --restart=always ensures the container restarts automatically if it stops.
  • --env-file config.env specifies a file from which to read environment variables, including your Telegram bot and OpenAI API keys.
  • -v ${PWD}/data:/app/data mounts the data directory from your current location to /app/data in the container, allowing the SQLite database to be stored persistently.

Environment Variables

  • TELEGRAM_BOT_KEY: Your Telegram bot API key.
  • OPENAI_API_KEY: Your OpenAI API key. This is required for making API requests to OpenAI.
  • MAX_TOKENS: The maximum number of tokens for API requests (default is 16000).
  • MODEL_NAME: The OpenAI GPT model name (default is gpt-3.5-turbo-16k).
  • ALLOWED_USERS: A JSON array of telegram user IDs that are allowed to interact with the bot (default is an empty array).

Contributing

Feel free to submit pull requests or issues to improve the bot.

telegramgpt's People

Contributors

jmizell avatar

Stargazers

Sergey avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.