The VectorKnowledgeBase class provides a Retrieval-Augmented Generation (RAG) system powered by Pinecone, LlamaIndex, and OpenAI embeddings. It enables the agent to query technical manuals and documentation to inform diagnosis and remediation.
Synthesized answer in Spanish with sources appended
Behavior:
Query Rewriting: Generates 5 variations using _rewrite_query()
Retrieval: Fetches top 5 chunks for each query (similarity search)
Deduplication: Removes duplicate nodes by ID
Reranking: Uses Cohere to select the 5 most relevant chunks
Synthesis: Generates answer using GPT-4 with strict rules
Source Attribution: Appends source file names
Example:
from src.core.knowledge import kbresponse = kb.query("How do I fix nginx port conflict?")print(response)
Output:
Un conflicto de puerto en nginx generalmente ocurre cuando otro proceso está usando el puerto 80 o 443.Para solucionarlo:1. Identifica el proceso: `sudo ss -tulpn | grep :80`2. Detén nginx: `sudo service nginx stop`3. Mata el proceso conflictivo: `sudo kill -9 <PID>`4. Reinicia nginx: `sudo service nginx start`**Fuentes:**- nginx_manual.pdf- troubleshooting_guide.pdf
The _rewrite_query() method generates diverse search queries:
def _rewrite_query(self, query_text: str, llm) -> list: # Generates 5 variations: # 1. English with official terminology # 2. Spanish with technical terms # 3. Specific section/chapter names # 4. Keywords/values the answer would contain # 5. Alternative interpretation queries = [ query_text, # Original query "nginx port binding errors and solutions", "errores de enlace de puerto nginx y soluciones", "nginx.conf listen directive configuration", "port 80 address already in use nginx", "how to resolve nginx startup failures" ] return queries[:6]
from src.core.knowledge import kbfor event in kb.stream_query("How to configure PostgreSQL replication?"): if event["event"] == "thinking": print(f"[Status] {event['data']}") elif event["event"] == "message": print(event["data"], end="", flush=True) elif event["event"] == "done": print("\n[Complete]")
Output:
[Status] Analizando tu pregunta...[Status] Optimizando búsqueda...[Status] Consultando base de conocimiento vectorizada...[Status] Leyendo 5 fragmentos relevantes...La replicación en PostgreSQL se configura mediante...**Fuentes:**- postgresql_manual.pdf[Complete]
Use stream_query() for chat interfaces to provide instant feedback while processing.
The system automatically handles bilingual queries:
# English queryresponse_en = kb.query("How to restart nginx?")# Spanish queryresponse_es = kb.query("¿Cómo reiniciar nginx?")# Both return Spanish responses based on English+Spanish docs
from src.core.knowledge import kbtry: if kb: response = kb.query("How to fix database connection?") else: print("Knowledge base not initialized")except Exception as e: print(f"RAG query failed: {e}") # Fallback to LLM without RAG context
The knowledge base is initialized as a global singleton:
# In src/core/knowledge.pykb = Nonedef init_knowledge_base(): global kb try: kb = VectorKnowledgeBase() log("system", "Base de conocimiento inicializada correctamente.") except Exception as e: log("error", f"No se pudo inicializar la base de conocimiento: {e}") kb = None
Usage:
from src.core.knowledge import init_knowledge_base, kb# Initialize at startupinit_knowledge_base()# Use throughout applicationif kb: kb.query("How to optimize PostgreSQL?")