RAG + Ollama: bot de telegram
Claude
prompt:
Aquest és el resum per arrencar el RAG amb Ollama i fer una consulta:
Des del portàtil, recordar que havíem de fer túnel SSH:
ssh -L 6333:127.0.0.1:6333 joan@192.168.1.184
I ara en el servidor:
nohup ollama serve > ~/ollama.log 2>&1 &
docker run -p 6333:6333 -p 6334:6334 \
-v $(pwd)/qdrant_storage:/qdrant/storage:z \
-v $(pwd)/qdrant_snapshots:/qdrant/snapshots:z \
-v $(pwd)/qdrant/config/production.yaml:/qdrant/config/production.yaml:z \
qdrant/qdrant
Puc veure el QDrant a: http://localhost:6333/dashboard
cd ingesta
source .venv/bin/activate
python rag_josep_pla.py "Com descriu Pla Palafrugell?" --model qwen3:8b --mode mixt --top 8
Ara que ja tinc el RAG funcionant, vull fer un bot de telegram per poder fer aquesta mateixa pregunta: "Com descriu Pla Palafrugell?" o qualsevol altra pregunta. Ahir em vas ajudar a fer un petit bot de Telegram, o sigui que lo bàsic ja ho sé. El bot es dirà #josep_pla_bot
Resposta Claude i posada en funcionament
El bot viurà dins la carpeta ingesta/ i cridarà el script rag_josep_pla.py.
pip install python-telegram-bot
Aquí tens bot_josep_pla.py. Posa’l a la carpeta ingesta, al costat de rag_josep_pla.py.
Creem el bot a telegram
Crea el bot a Telegram. Obre un xat amb @BotFather, envia /newbot, tria’n el nom i l’usuari (ha d’acabar en bot). Et donarà un token; guarda’l.
El nom del bot i el nom de l'usuari és: josep_pla_bot
Done! Congratulations on your new bot. You will find it at t.me/josep_pla_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this. Use this token to access the HTTP API: 8608516973:AAEGygZ90F_RvvyhUe7F6WudVS4HySZ1M18 Keep your token secure and store it safely, it can be used by anyone to control your bot. For a description of the Bot API, see this page: https://core.telegram.org/bots/api
3. Amb Ollama i Qdrant ja engegats com sempre, arrenca el bot (en segon pla):
export TELEGRAM_TOKEN="8608516973:AAEGygZ90F_RvvyhUe7F6WudVS4HySZ1M18" nohup python bot_josep_pla.py > ~/bot.log 2>&1 &
4. Obre el bot a Telegram, envia /elmeuid i, amb aquest número, reinicia’l en mode privat:
export ALLOWED_USERS="123456789"
Obro el telegram (al mòbil o a la web), i ja puc enviar preguntes, i també accedir a la configuració del bot:
Quan accedim al bot surt el missatge:
Hola! Sóc el bot de Josep Pla 📚 Escriu-me una pregunta, per exemple: Com descriu Pla Palafrugell? Comandes: /config – mostra la configuració /model <nom> – canvia el model (p. ex. qwen3:8b) /mode <mode> – canvia el mode (p. ex. mixt) /top <n> – nombre de fragments a recuperar /elmeuid – el teu ID de Telegram09:31
I puc canviar les configuracions. Per exemple, canviar de model, o canviar el mode de mixt a 1a_persona.
/config Model: mistral-small3.2:latest Mode: 1a_persona Top: 8
Exemple
Joanillo: per què vós no us heu casat mai?
Pregunta: per què vós no us heu casat mai? Model: mistral-small3.2:latest | Mode: 1a_persona | Chunks recuperats: 8, al prompt: 8 ================================================================================ RESPOSTA ================================================================================ Perquè no he tingut temps, no he fet més que treballar. A més, sempre m'han agradat les dones una mica escandaloses i el matrimoni em sembla una cosa molt seriosa [1]. També he pensat que no haver portat fills al món és el millor que he pogut fer [3]. En realitat, no sé què és l'amor, mai he tingut cap núvia i només he tingut alguns embolics, que he acabat malament [6]. ================================================================================ FONTS ================================================================================ [1] Josep Pla, 78 anys de soledat — Josep Pla, 78 anys de soledat > QUI ESCRIU UNA CARTA ÉS CAPAÇ D'ESCRIURE UN LLIBRE (score 0.580) ...