Spaces:
Sleeping
Sleeping
Update rag_utils.py
Browse files- rag_utils.py +7 -18
rag_utils.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
|
2 |
-
import os
|
3 |
import faiss
|
4 |
import pickle
|
5 |
import numpy as np
|
@@ -7,15 +5,13 @@ import torch
|
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
9 |
|
10 |
-
def load_faiss_index(index_path="faiss_index
|
11 |
index = faiss.read_index(index_path)
|
12 |
with open(doc_path, "rb") as f:
|
13 |
documents = pickle.load(f)
|
14 |
return index, documents
|
15 |
|
16 |
def get_embedding_model():
|
17 |
-
# Pas besoin de token ici, modèle public
|
18 |
-
print("✅ Chargement de l'encodeur multi-qa-MiniLM-L6-cos-v1")
|
19 |
return SentenceTransformer("sentence-transformers/multi-qa-MiniLM-L6-cos-v1")
|
20 |
|
21 |
def query_index(question, index, documents, model, k=3):
|
@@ -24,23 +20,16 @@ def query_index(question, index, documents, model, k=3):
|
|
24 |
return [documents[i] for i in indices[0]]
|
25 |
|
26 |
def generate_answer(question, context):
|
27 |
-
|
28 |
-
model_id = "tiiuae/falcon-rw-1b"
|
29 |
|
30 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id
|
31 |
tokenizer.pad_token = tokenizer.eos_token
|
32 |
|
33 |
-
model = AutoModelForCausalLM.from_pretrained(
|
34 |
-
model_id,
|
35 |
-
token=token,
|
36 |
-
device_map="auto",
|
37 |
-
torch_dtype=torch.float16
|
38 |
-
)
|
39 |
|
40 |
prompt = f"Voici un contexte :\n{context}\n\nQuestion : {question}\nRéponse :"
|
41 |
-
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
42 |
-
outputs = model.generate(**inputs, max_new_tokens=
|
43 |
-
print("🔍 Contexte utilisé pour la génération :")
|
44 |
-
print(context[:500])
|
45 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
46 |
|
|
|
|
|
|
|
|
1 |
import faiss
|
2 |
import pickle
|
3 |
import numpy as np
|
|
|
5 |
from sentence_transformers import SentenceTransformer
|
6 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
7 |
|
8 |
+
def load_faiss_index(index_path="faiss_index.faiss", doc_path="documents.pkl"):
|
9 |
index = faiss.read_index(index_path)
|
10 |
with open(doc_path, "rb") as f:
|
11 |
documents = pickle.load(f)
|
12 |
return index, documents
|
13 |
|
14 |
def get_embedding_model():
|
|
|
|
|
15 |
return SentenceTransformer("sentence-transformers/multi-qa-MiniLM-L6-cos-v1")
|
16 |
|
17 |
def query_index(question, index, documents, model, k=3):
|
|
|
20 |
return [documents[i] for i in indices[0]]
|
21 |
|
22 |
def generate_answer(question, context):
|
23 |
+
model_id = "Salesforce/codegen-350M-mono"
|
|
|
24 |
|
25 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
26 |
tokenizer.pad_token = tokenizer.eos_token
|
27 |
|
28 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
prompt = f"Voici un contexte :\n{context}\n\nQuestion : {question}\nRéponse :"
|
31 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
32 |
+
outputs = model.generate(**inputs, max_new_tokens=128, pad_token_id=tokenizer.eos_token_id)
|
|
|
|
|
33 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
34 |
|
35 |
+
|