| import os |
| from langchain_pinecone import PineconeVectorStore as LangchainPinecone |
| from langchain_ollama import OllamaEmbeddings |
| from pinecone import Pinecone |
|
|
| class PineconeVectorStore: |
| """ |
| Concrete implementation of the VectorStore protocol using Pinecone. |
| Handles semantic embedding generation and retrieval for pulling clinical context. |
| """ |
| def __init__(self, host: str, embedding_model: str, index_name: str, api_key: str): |
| self.embeddings = OllamaEmbeddings( |
| model=embedding_model, |
| base_url=host, |
| ) |
| |
| |
| self.pc = Pinecone(api_key=api_key) |
| |
| |
| |
| self.index = self.pc.Index(index_name) |
|
|
| self.vectorstore = LangchainPinecone( |
| index=self.index, |
| embedding=self.embeddings, |
| text_key="text" |
| ) |
| |
| |
| |
| |
|
|
| def retrieve(self, query: str, k: int = 8) -> str: |
| if not query or not query.strip(): |
| return "" |
|
|
| try: |
| |
| results = self.vectorstore.max_marginal_relevance_search( |
| query, |
| k=k, |
| fetch_k=max(20, k * 3), |
| lambda_mult=0.7, |
| ) |
| return "\n\n".join([doc.page_content for doc in results]) |
| except Exception as e: |
| print(f"[RAG ERROR] MMR search failed in Pinecone: {e}. Attempting standard similarity fallback.") |
| try: |
| results = self.vectorstore.similarity_search(query, k=k) |
| return "\n\n".join([doc.page_content for doc in results]) |
| except Exception as e2: |
| print(f"[RAG ERROR] Similarity fallback also failed in Pinecone: {e2}") |
| return "" |
|
|