Questa repo mostra come costruire un “chatbot” controllato che:
- chiama due sorgenti (arXiv e OpenAlex) via server MCP
- fonde i risultati
- (opzionale) usa Ollama per una sintesi con citazioni obbligatorie
Nota importante: la “chat” qui è una cella interattiva nel notebook 02 (ti chiede la domanda e quali sorgenti usare). Non è una chat UI grafica stile ChatGPT: è una demo tecnica riproducibile.
cd mcp-demo
python -m venv .venv
source .venv/bin/activatepip install fastmcp httpx arxivVai su https://ollama.com e installa.
ollama serveollama pull llama3.2- VS Code → File → Open Folder… → seleziona
mcp-demo
- Cmd+Shift+P → “Python: Select Interpreter”
- scegli quello dentro
mcp-demo/.venv/...
- apri
02_orchestrator_fusion.ipynb
-
Serve a generare/riscrivere i file:
servers/arxiv_server.pyservers/openalex_server.py- (eventuali file CLI)
-
Lo esegui solo se devi (ri)creare i file o hai incollato nuove versioni di server/client.
✅ Quando eseguirlo:
- prima volta (se i server non esistono)
- quando cambi codice “generator” nel notebook 00
- È solo un banco prova: controlli che i server rispondano e vedi il formato delle risposte.
- Non è necessario per usare la chat del 02.
-
È il notebook che userai ogni volta.
-
Dentro trovi:
-
fetch()→ chiama i server MCP -
answer()→ costruisce la risposta (no LLM o con LLM) -
una cella “chat” dove inserisci:
- la domanda
- quale sorgente usare (arXiv / OpenAlex / entrambe)
- se usare il LLM
-
-
Restart Kernel (consigliato)
-
Esegui in ordine le celle che definiscono funzioni (di solito sono le prime)
- Cell import
unpack()detect_domain()pick_sources()fetch()e helper- fusione senza LLM
- LLM (Ollama)
answer()
✅ Metodo semplice: usa “Run All” (Esegui tutto) una volta dopo il restart.
Perché: in notebook, se modifichi una funzione ma non riesegui la cella, il kernel continua a usare la versione vecchia.
Nel 02 devi avere (o creare) UNA cella dedicata tipo questa. Questa è la “chat” interattiva:
# --- CHAT CELL (VS Code Notebook) ---
q = input("Domanda: ").strip()
src = input("Sorgenti (a=arxiv, o=openalex, b=both) [b]: ").strip().lower() or "b"
use_arxiv = (src in ["a", "b"])
use_openalex = (src in ["o", "b"])
llm = input("Usare LLM? (y/n) [n]: ").strip().lower() or "n"
use_llm = (llm == "y")
k_str = input("Quanti risultati per sorgente? [3]: ").strip() or "3"
k = int(k_str)
model = "llama3.2" # cambia se usi un altro modello Ollama
out = await answer(
q,
k=k,
use_llm=use_llm,
model=model,
use_arxiv=use_arxiv,
use_openalex=use_openalex
)
print(out)-
premi
▶️ Run sulla cella -
VS Code ti chiede:
-
Domanda
-
Sorgente:
a= solo arXivo= solo OpenAlexb= entrambe
-
LLM:
yon -
k: numero risultati
-
Esempio:
- Domanda:
agentic RAG evaluation - Sorgenti:
b - LLM:
y - k:
3
How are agentic RAG systems evaluated, and what benchmarks and metrics are used?
What are best practices for evaluating retrieval-augmented generation systems in real-world applications?
product managementinnovation management
Se scegli o (solo OpenAlex) è normale che risponda usando OpenAlex.
Se scegli a (solo arXiv) deve usare SOLO arXiv.
Se non succede:
-
quasi sempre è perché non hai rieseguito la cella che definisce
fetch()/answer() -
fai:
- Restart Kernel
- Run All
- riprova la chat cell
In notebook non usare asyncio.run(...).
Usa direttamente:
out = await answer("...", k=3)È un parsing non “null-safe” nel server OpenAlex.
Correggi servers/openalex_server.py rendendo robusti i campi (primary_location/source possono essere None).
È rate limit: il notebook ha retry/backoff. Se capita spesso:
- aspetta qualche secondo e riprova
- usa solo OpenAlex per test rapidi
Un orchestratore che fa “chat” in notebook e che può attaccare/staccare sorgenti, recuperare documenti e (opzionale) farli riassumere da un LLM con citazioni verificabili.