A Human-in-the-Loop retriever for LangChain that pauses execution to wait for a Telegram reply.
Project description
telegram-retriever
telegram-retriever is a functional, human-in-the-loop extension for LangChain. It allows an LLM agent to pause execution, send a query to a specific Telegram user, and synchronously wait for a text-based reply.
🧠 The Functional Pipeline
The retriever follows a strict data-flow architecture:
- Dispatch: Sends the AI's question to the target chat via the Telegram Bot API.
- Poll: Enters a stateless polling loop to fetch updates.
- Filter: Validates incoming data to ensure it is a text-based "Reply-To" message from the correct user.
- Transform: Converts the validated Telegram message into a LangChain
Document.
🚀 Installation
pip install telegram-retriever
🛠 Usage
Synchronous (Blocking)
Perfect for scripts where the process should wait for human intervention.
import os
from telegram_retriever import TelegramRetriever
retriever = TelegramRetriever(
bot_token=os.getenv("TELEGRAM_BOT_TOKEN"),
chat_id=os.getenv("TELEGRAM_CHAT_ID")
)
# Execution pauses here until the human replies on Telegram
docs = retriever.invoke("Do you approve the budget for Q3?")
print(f"Human response: {docs[0].page_content}")
Asynchronous (Non-blocking)
Recommended for FastAPI or LangServe applications to keep the event loop free.
docs = await retriever.ainvoke("Should I trigger the deployment?")
⚙️ Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
bot_token |
SecretStr |
Required | Your Telegram Bot API Token. |
chat_id |
str |
Required | The target User ID or Group ID. |
polling_timeout |
float |
600.0 |
Seconds to wait before timing out. |
polling_interval |
float |
2.0 |
Seconds between update checks. |
🧪 Development
The project is built on pure functions, making testing simple and reliable.
# Install test dependencies
pip install .[test]
# Run the functional test suite
pytest
🎮 Demo: Human-in-the-Loop Workflow
This demo uses the script located at examples/chatbot.py to showcase how the AI agent (via DSPy) intelligently decides when human intervention is necessary.
How it Works:
- Direct AI Response (Autonomous): When the user asks a straightforward math question ("What's 127 x 23?"), the AI handles it locally using its internal knowledge. It does not trigger a Telegram notification because the task is simple and clear.
- Human-in-the-Loop (Triggered): When the user asks a subjective or context-heavy question ("What's so special about 67?"), the AI recognizes its own limitations.
- Telegram Integration: The AI pauses, sends the query to the human expert via Telegram, and waits for a reply.
- Synthesis: Once the human replies ("It's an internet meme"), the AI synthesizes this "expert context" into a comprehensive final answer for the user.
(Note: Sensitive Telegram identifiers have been safely redacted in this image.)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file telegram_retriever-0.1.4.tar.gz.
File metadata
- Download URL: telegram_retriever-0.1.4.tar.gz
- Upload date:
- Size: 169.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab43ae98bc9d52c6b9ea09da7633c455b5c4cacf310cf4c5aded41594e410108
|
|
| MD5 |
2fa9979c229403a4b38cbe0ea5a4481a
|
|
| BLAKE2b-256 |
74841d19428f96dceb1015e0f553b0d26135bfb434c25be19549db2481111553
|
File details
Details for the file telegram_retriever-0.1.4-py3-none-any.whl.
File metadata
- Download URL: telegram_retriever-0.1.4-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a7b3457c1589ea96d0b7fbef4a631460aef3e215a7691c56555c30954895800
|
|
| MD5 |
cd06884f95f0f15a8fe95bff116750d9
|
|
| BLAKE2b-256 |
2734d40ed72a97022e27c9e26b75de713f126a4d31f687666b8e1c8899b66fd0
|