Spaces:
Running
Running
File size: 2,553 Bytes
e561008 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | """
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
|