Spaces:
Runtime error
Runtime error
Update src/generation.py
Browse files- src/generation.py +26 -28
src/generation.py
CHANGED
|
@@ -1,31 +1,29 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
class
|
| 4 |
-
def __init__(self, model_name=
|
| 5 |
-
self.
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 9 |
-
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
|
| 10 |
-
self.model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 11 |
-
self.prompt_template = """
|
| 12 |
-
You are a customer support chatbot for Rupeia, a financial platform. Provide accurate, concise answers about Investments, Goals, Benefits, Gadgets, and News & Blogs. Use the context and history to respond naturally. If unsure, say: "I’m not sure about that. Could you clarify or ask about Rupeia features?"
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
Answer: """
|
| 18 |
|
| 19 |
-
def
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
from typing import List, Dict
|
| 6 |
|
| 7 |
+
class DocumentRetriever:
|
| 8 |
+
def __init__(self, model_name='all-MiniLM-L6-v2'):
|
| 9 |
+
self.model = SentenceTransformer(model_name)
|
| 10 |
+
self.documents = self._load_documents()
|
| 11 |
+
self.doc_embeddings = self._embed_documents()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def _load_documents(self) -> List[Dict]:
|
| 14 |
+
with open('data/rupeia_document.json', 'r') as f:
|
| 15 |
+
return json.load(f)
|
|
|
|
| 16 |
|
| 17 |
+
def _embed_documents(self) -> np.ndarray:
|
| 18 |
+
texts = [doc['content'] for doc in self.documents]
|
| 19 |
+
return self.model.encode(texts)
|
| 20 |
+
|
| 21 |
+
def retrieve(self, query: str, top_k: int = 3) -> List[Dict]:
|
| 22 |
+
query_embedding = self.model.encode(query)
|
| 23 |
+
scores = np.dot(self.doc_embeddings, query_embedding)
|
| 24 |
+
top_indices = np.argsort(scores)[-top_k:][::-1]
|
| 25 |
+
return [self.documents[i] for i in top_indices]
|
| 26 |
+
|
| 27 |
+
def retrieve_relevant_documents(query: str) -> List[Dict]:
|
| 28 |
+
retriever = DocumentRetriever()
|
| 29 |
+
return retriever.retrieve(query)
|