""" Pinecone Vector Search — "Built on Rock" context retrieval ────────────────────────────────────────────────────────────────────────────── Uses Pinecone's hosted Inference API (multilingual-e5-large, 1024 dims) to embed queries and retrieve relevant passages from "Built on Rock". No local ML library required — embeddings are generated by Pinecone's servers. If PINECONE_API_KEY is not set or the call fails for any reason, returns '' so all AI calls work normally without book context (graceful degradation). """ import os from functools import lru_cache PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY", "") PINECONE_INDEX = os.environ.get("PINECONE_INDEX_NAME", "venthrive-bff") EMBED_MODEL = "multilingual-e5-large" # 1024 dims, free on Pinecone MIN_SCORE = 0.40 DEFAULT_TOP_K = 3 @lru_cache(maxsize=1) def _get_pinecone(): from pinecone import Pinecone return Pinecone(api_key=PINECONE_API_KEY) def query_book_context(query: str, top_k: int = DEFAULT_TOP_K) -> str: """ Returns the most relevant passages from 'Built on Rock' as a formatted context string for injection into AI system prompts. Returns '' if Pinecone is not configured or the call fails. """ if not PINECONE_API_KEY: return "" try: pc = _get_pinecone() index = pc.Index(PINECONE_INDEX) # Generate query embedding via Pinecone Inference (no local model needed) embedding_result = pc.inference.embed( model=EMBED_MODEL, inputs=[query], parameters={"input_type": "query", "truncate": "END"}, ) query_vector = embedding_result[0].values results = index.query( vector=query_vector, top_k=top_k, include_metadata=True, ) passages = [ match["metadata"]["text"] for match in results.get("matches", []) if match.get("score", 0) >= MIN_SCORE ] if not passages: return "" return ( "RELEVANT CONTEXT FROM 'BUILT ON ROCK' (Business Footing Framework):\n\n" + "\n\n---\n\n".join(passages) + "\n\nApply the BFF philosophy above to ground your advice." ) except Exception as e: print(f"[pinecone] Context retrieval skipped: {e}") return "" # graceful degradation