Skip to content

nifontovoleg/contextBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ContextBot

EN Python Telegram OpenAI ProxyAPI Context License

Telegram LLM chat with memory: telebot handlers → in-memory dialogue per chat_id → OpenAI-compatible API (OpenAI / ProxyAPI / GenAPI) → usage footer + stdout logs.

Clone → configure .envpython bot.py → message your bot → /reset or clear context to wipe history.


About

Small Python bot for practicing multi-turn dialogue, provider switching, and generation parameters (temperature, max_tokens).

  • Long polling via pyTelegramBotAPI
  • Thread-safe in-RAM history (no database)
  • Chat Completions through the official OpenAI Python client + custom base_url
  • Request params and token usage logged to the console (secrets excluded)

Providers

Provider Env key Default base URL
OpenAI OPENAI_API_KEY https://api.openai.com/v1
ProxyAPI PROXYAPI_KEY https://openai.api.proxyapi.ru/v1
GenAPI GENAPI_KEY set API_BASE_URL manually

Set API_PROVIDER to openai, genapi, or proxyapi. Explicit API_BASE_URL overrides the default.

Features

Feature Description
Contextual chat Stores user/assistant turns per Telegram chat_id
/reset Clears history for the current chat
Text reset clear context, reset, clear history
Multi-provider API_PROVIDER + one of the API keys
Usage footer prompt / completion / total tokens + rough USD estimate
Structured logs Model, temperature, max_tokens, message count, user preview

Tech stack

  • Python 3.11+
  • pyTelegramBotAPI — handlers, long polling
  • openai — Chat Completions client
  • python-dotenv.env configuration

Project layout

contextBot/
├── bot.py                 # /start, /reset, chat handler, token footer
├── config.py              # Settings, provider, keys, base URL
├── context_manager.py     # In-memory store per chat_id
├── api_client.py          # API calls + logging
├── requirements.txt
├── .env.example
└── README.md

Quick start

git clone https://github.com/nifontovoleg/contextBot.git
cd contextBot

python -m venv .venv

# Windows PowerShell
.\.venv\Scripts\Activate.ps1

# macOS / Linux
source .venv/bin/activate

pip install -r requirements.txt
copy .env.example .env   # Windows — use cp on Unix

Minimal .env (ProxyAPI example):

BOT_TOKEN=your_telegram_bot_token
API_PROVIDER=proxyapi
PROXYAPI_KEY=your_proxyapi_key
API_MODEL=gpt-4o-mini
TEMPERATURE=0.7
MAX_TOKENS=512
Variable Description
BOT_TOKEN From @BotFather
API_PROVIDER openai · genapi · proxyapi
OPENAI_API_KEY / GENAPI_KEY / PROXYAPI_KEY One key is enough
API_BASE_URL Optional override for the provider default
API_MODEL Model id (ProxyAPI: e.g. anthropic/claude-…)
TEMPERATURE, MAX_TOKENS Generation settings
SYSTEM_PROMPT Optional system message

Run:

python bot.py

In Telegram: /start, then send messages. History is lost on bot restart (RAM only).


Commands

Input Action
/start, /help Welcome and hints
/reset Clear context for this chat
clear context, reset, clear history Same as /reset
Any other text LLM reply with prior turns

Logging

Stdout includes:

  • API request summary: model, temperature, max_tokens, messages_count, last_user_preview
  • Usage: prompt, completion, total tokens
  • Exceptions from the API client and message handler

BOT_TOKEN and API keys are never printed.


Model tuning report

Change API_MODEL, TEMPERATURE, and MAX_TOKENS in .env, restart, and rerun the same script. Fill the table from the reply footer or logs.

Model temperature max_tokens Run # Effect (concise / creative) Tokens (in / out / total) Approx. cost
gpt-4o-mini 0.3 256 1 Very concise — / — / — ~$…
gpt-4o-mini 0.7 512 2 Balanced dialogue — / — / — ~$…
gpt-4o-mini 1.0 512 3 More creative / verbose — / — / — ~$…
gpt-4o 0.7 512 4 Stronger on hard prompts — / — / — ~$…
your model T N k notes from logs from footer

OpenAI list prices (USD / 1M tokens): gpt-4o-mini $0.15 in / $0.60 out; gpt-4o $2.50 in / $10 out. ProxyAPI / GenAPI: use provider rates → in × price_in + out × price_out.

Sample scenario

  1. “Explain RAG in two sentences.”
  2. “How does that relate to my Telegram bot?” (memory check)
  3. “Shorten your previous answer to one sentence.”

Run /reset before changing model or temperature between experiments.


Troubleshooting

409 Conflict: terminated by other getUpdates request

Two bot processes share the same BOT_TOKEN. Stop extra instances, then start one python bot.py.

API errors in chat

  • Check the matching key for API_PROVIDER
  • Confirm API_BASE_URL and API_MODEL for ProxyAPI / GenAPI
  • Read the traceback in the terminal (not sent to Telegram)

Why this project

Goal Outcome
Telegram bots Handlers and long polling with telebot
LLM integration OpenAI-compatible Chat Completions
Context Multi-turn memory without a DB
Experiments Compare temperature, max_tokens, and models

License

MIT — free for personal and learning use.


Built with ☕ for Telegram bots and LLM context
@nifontovoleg · nifontovv.ru

About

Telegram chat bot with in-memory dialogue context: pyTelegramBotAPI, OpenAI-compatible API (OpenAI, ProxyAPI, GenAPI), /reset, logging and LLM parameter experiments.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages