|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +from typing import Callable, Dict, List, Optional, Union, Any, Tuple |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +from feast import FeatureStore, FeatureView |
| 6 | +from sentence_transformers import SentenceTransformer |
| 7 | +from transformers import RagRetriever |
| 8 | + |
| 9 | + |
| 10 | +class VectorStore(ABC): |
| 11 | + @abstractmethod |
| 12 | + def query( |
| 13 | + self, |
| 14 | + query_vector: Optional[np.ndarray] = None, |
| 15 | + query_string: Optional[str] = None, |
| 16 | + top_k: int = 10, |
| 17 | + ) -> List[Dict[str, Any]]: |
| 18 | + pass |
| 19 | + |
| 20 | + |
| 21 | +class FeastVectorStore(VectorStore): |
| 22 | + def __init__(self, store: FeatureStore, rag_view: FeatureView, features: List[str]): |
| 23 | + self.store = store |
| 24 | + self.rag_view = rag_view |
| 25 | + self.store.apply([rag_view]) |
| 26 | + self.features = features |
| 27 | + |
| 28 | + def query( |
| 29 | + self, |
| 30 | + query_vector: Optional[np.ndarray] = None, |
| 31 | + query_string: Optional[str] = None, |
| 32 | + top_k: int = 10, |
| 33 | + ) -> List[Dict[str, Any]]: |
| 34 | + |
| 35 | + distance_metric = "COSINE" if query_vector is not None else None |
| 36 | + query_list = query_vector.tolist() if query_vector is not None else None |
| 37 | + |
| 38 | + response = self.store.retrieve_online_documents_v2( |
| 39 | + features=self.features, |
| 40 | + query=query_list, |
| 41 | + query_string=query_string, |
| 42 | + top_k=top_k, |
| 43 | + distance_metric=distance_metric, |
| 44 | + ).to_dict() |
| 45 | + |
| 46 | + results = [] |
| 47 | + for feature_name in self.features: |
| 48 | + short_name = feature_name.split(":")[-1] |
| 49 | + feature_values = response[short_name] |
| 50 | + for i, value in enumerate(feature_values): |
| 51 | + if i >= len(results): |
| 52 | + results.append({}) |
| 53 | + results[i][short_name] = value |
| 54 | + |
| 55 | + return results |
| 56 | + |
| 57 | + |
| 58 | +# Dummy index - an index is required by the HF Transformers RagRetriever class |
| 59 | +class FeastIndex: |
| 60 | + def __init__(self, vector_store: VectorStore): |
| 61 | + self.vector_store = vector_store |
| 62 | + |
| 63 | + def get_top_docs(self, query_vectors: np.ndarray, n_docs: int = 5): |
| 64 | + raise NotImplementedError("get_top_docs is not yet implemented.") |
| 65 | + |
| 66 | + def get_doc_dicts(self, doc_ids: List[str]): |
| 67 | + raise NotImplementedError("get_doc_dicts is not yet implemented.") |
| 68 | + |
| 69 | + |
| 70 | +class FeastRAGRetriever(RagRetriever): |
| 71 | + VALID_SEARCH_TYPES = {"text", "vector", "hybrid"} |
| 72 | + |
| 73 | + def __init__( |
| 74 | + self, |
| 75 | + question_encoder_tokenizer, |
| 76 | + question_encoder, |
| 77 | + generator_tokenizer, |
| 78 | + generator_model, |
| 79 | + feast_repo_path: str, |
| 80 | + vector_store: VectorStore, |
| 81 | + search_type: str, |
| 82 | + config: Dict[str, Any], |
| 83 | + index: FeastIndex, |
| 84 | + format_document: Optional[Callable[[Dict[str, Any]], str]] = None, |
| 85 | + id_field: str = "", |
| 86 | + query_encoder_model: Union[str, SentenceTransformer] = "all-MiniLM-L6-v2", |
| 87 | + **kwargs, |
| 88 | + ): |
| 89 | + if search_type.lower() not in self.VALID_SEARCH_TYPES: |
| 90 | + raise ValueError( |
| 91 | + f"Unsupported search_type {search_type}. " |
| 92 | + f"Must be one of: {self.VALID_SEARCH_TYPES}" |
| 93 | + ) |
| 94 | + super().__init__( |
| 95 | + config=config, |
| 96 | + question_encoder_tokenizer=question_encoder_tokenizer, |
| 97 | + generator_tokenizer=generator_tokenizer, |
| 98 | + index=index, |
| 99 | + init_retrieval=False, |
| 100 | + **kwargs, |
| 101 | + ) |
| 102 | + self.question_encoder = question_encoder |
| 103 | + self.generator_model = generator_model |
| 104 | + self.generator_tokenizer = generator_tokenizer |
| 105 | + self.feast = FeatureStore(repo_path=feast_repo_path) |
| 106 | + self.vector_store = vector_store |
| 107 | + self.search_type = search_type.lower() |
| 108 | + self.format_document = format_document or FeastRAGRetriever._default_format_document |
| 109 | + self.id_field = id_field |
| 110 | + |
| 111 | + if isinstance(query_encoder_model, str): |
| 112 | + self.query_encoder = SentenceTransformer(query_encoder_model) |
| 113 | + else: |
| 114 | + self.query_encoder = query_encoder_model |
| 115 | + |
| 116 | + def retrieve(self, question_hidden_states: np.ndarray, n_docs: int, query: Optional[str] = None) -> Tuple[np.ndarray, List[Dict[str, str]]]: |
| 117 | + # Convert hidden states to query vector by pooling |
| 118 | + query_vector = question_hidden_states.mean(dim=1).squeeze().detach().cpu().numpy() |
| 119 | + |
| 120 | + # Decode text query if needed (for hybrid or text search) |
| 121 | + if query is None and self.search_type in ("text", "hybrid"): |
| 122 | + query = self.question_encoder_tokenizer.decode( |
| 123 | + question_hidden_states.argmax(axis=-1), |
| 124 | + skip_special_tokens=True |
| 125 | + ) |
| 126 | + |
| 127 | + if self.search_type == "text": |
| 128 | + results = self.vector_store.query(query_string=query, top_k=n_docs) |
| 129 | + |
| 130 | + elif self.search_type == "vector": |
| 131 | + results = self.vector_store.query(query_vector=query_vector, top_k=n_docs) |
| 132 | + |
| 133 | + elif self.search_type == "hybrid": |
| 134 | + results = self.vector_store.query( |
| 135 | + query_string=query, |
| 136 | + query_vector=query_vector, |
| 137 | + top_k=n_docs |
| 138 | + ) |
| 139 | + else: |
| 140 | + raise ValueError(f"Unsupported search type: {self.search_type}") |
| 141 | + |
| 142 | + # Cosine similarity scoring |
| 143 | + doc_embeddings = np.array([doc["embedding"] for doc in results]) |
| 144 | + query_norm = np.linalg.norm(query_vector) |
| 145 | + doc_norms = np.linalg.norm(doc_embeddings, axis=1) |
| 146 | + |
| 147 | + query_norm = np.maximum(query_norm, 1e-10) |
| 148 | + doc_norms = np.maximum(doc_norms, 1e-10) |
| 149 | + |
| 150 | + similarities = np.dot(doc_embeddings, query_vector) / (doc_norms * query_norm) |
| 151 | + doc_scores = similarities.reshape(1, -1) |
| 152 | + # passage_text is hardcoded at the moment |
| 153 | + doc_dicts = [{"text": doc["passage_text"]} for doc in results] |
| 154 | + |
| 155 | + return doc_scores, doc_dicts |
| 156 | + |
| 157 | + def generate_answer( |
| 158 | + self, query: str, top_k: int = 5, max_new_tokens: int = 100 |
| 159 | + ) -> str: |
| 160 | + # Convert query to hidden states format expected by retrieve |
| 161 | + inputs = self.question_encoder_tokenizer( |
| 162 | + query, return_tensors="pt", padding=True, truncation=True |
| 163 | + ) |
| 164 | + question_hidden_states = self.question_encoder(**inputs).last_hidden_state |
| 165 | + |
| 166 | + # Get documents using retrieve method |
| 167 | + doc_scores, doc_dicts = self.retrieve(question_hidden_states, n_docs=top_k) |
| 168 | + |
| 169 | + # Format context from retrieved documents |
| 170 | + contexts = [doc["text"] for doc in doc_dicts] |
| 171 | + context = "\n\n".join(contexts) |
| 172 | + |
| 173 | + prompt = ( |
| 174 | + f"Use the following context to answer the question. Context:\n{context}\n\n" |
| 175 | + f"Question: {query}\nAnswer:" |
| 176 | + ) |
| 177 | + |
| 178 | + self.generator_tokenizer.pad_token = self.generator_tokenizer.eos_token |
| 179 | + inputs = self.generator_tokenizer( |
| 180 | + prompt, return_tensors="pt", padding=True, truncation=True |
| 181 | + ) |
| 182 | + input_ids = inputs["input_ids"] |
| 183 | + attention_mask = inputs["attention_mask"] |
| 184 | + output_ids = self.generator_model.generate( |
| 185 | + input_ids=input_ids, |
| 186 | + attention_mask=attention_mask, |
| 187 | + max_new_tokens=max_new_tokens, |
| 188 | + pad_token_id=self.generator_tokenizer.pad_token_id, |
| 189 | + ) |
| 190 | + return self.generator_tokenizer.decode(output_ids[0], skip_special_tokens=True) |
| 191 | + |
| 192 | + @staticmethod |
| 193 | + def _default_format_document(doc: Dict[str, Any]) -> str: |
| 194 | + lines = [] |
| 195 | + for key, value in doc.items(): |
| 196 | + # Skip vectors by checking for long float lists |
| 197 | + if ( |
| 198 | + isinstance(value, list) |
| 199 | + and len(value) > 10 |
| 200 | + and all(isinstance(x, (float, int)) for x in value) |
| 201 | + ): |
| 202 | + continue |
| 203 | + lines.append(f"{key.replace('_', ' ').capitalize()}: {value}") |
| 204 | + return "\n".join(lines) |
0 commit comments