Spaces:
Build error
Build error
Update rag_utils.py
Browse files- rag_utils.py +43 -12
rag_utils.py
CHANGED
|
@@ -4,6 +4,14 @@ import pickle
|
|
| 4 |
import numpy as np
|
| 5 |
import re
|
| 6 |
from sentence_transformers import SentenceTransformer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def load_faiss_index(index_path="faiss_index/faiss_index.faiss", doc_path="faiss_index/documents.pkl"):
|
| 9 |
index = faiss.read_index(index_path)
|
|
@@ -24,13 +32,13 @@ def nettoyer_context(context):
|
|
| 24 |
context = context.replace("None", "")
|
| 25 |
return context
|
| 26 |
|
| 27 |
-
import os
|
| 28 |
-
from huggingface_hub import InferenceClient
|
| 29 |
-
|
| 30 |
-
client = InferenceClient("tiiuae/falcon-7b-instruct")
|
| 31 |
-
|
| 32 |
def generate_answer(question, context):
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
{context}
|
| 36 |
|
|
@@ -38,9 +46,32 @@ Formule ta réponse comme un conseiller d’orientation bienveillant, de manièr
|
|
| 38 |
|
| 39 |
Question : {question}
|
| 40 |
Réponse :"""
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import re
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
+
from transformers import AutoTokenizer # Ajouté pour la gestion des tokens
|
| 8 |
+
from huggingface_hub import InferenceClient
|
| 9 |
+
|
| 10 |
+
# Chargement du modèle Falcon-7B
|
| 11 |
+
client = InferenceClient("tiiuae/falcon-7b-instruct")
|
| 12 |
+
|
| 13 |
+
# Chargement du tokenizer (même base que Falcon)
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained("tiiuae/falcon-7b-instruct")
|
| 15 |
|
| 16 |
def load_faiss_index(index_path="faiss_index/faiss_index.faiss", doc_path="faiss_index/documents.pkl"):
|
| 17 |
index = faiss.read_index(index_path)
|
|
|
|
| 32 |
context = context.replace("None", "")
|
| 33 |
return context
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def generate_answer(question, context):
|
| 36 |
+
MAX_TOKENS_TOTAL = 2048
|
| 37 |
+
MAX_NEW_TOKENS = 300
|
| 38 |
+
MAX_PROMPT_TOKENS = MAX_TOKENS_TOTAL - MAX_NEW_TOKENS
|
| 39 |
+
|
| 40 |
+
# Construction initiale du prompt
|
| 41 |
+
base_prompt = f"""Voici des informations sur des établissements et formations :
|
| 42 |
|
| 43 |
{context}
|
| 44 |
|
|
|
|
| 46 |
|
| 47 |
Question : {question}
|
| 48 |
Réponse :"""
|
| 49 |
+
|
| 50 |
+
# Tronquer si le prompt est trop long
|
| 51 |
+
tokens = tokenizer.encode(base_prompt)
|
| 52 |
+
if len(tokens) > MAX_PROMPT_TOKENS:
|
| 53 |
+
# Réduction progressive du contexte uniquement
|
| 54 |
+
context_tokens = tokenizer.encode(context)
|
| 55 |
+
keep_tokens = MAX_PROMPT_TOKENS - len(tokenizer.encode(base_prompt.replace(context, "")))
|
| 56 |
+
truncated_context = tokenizer.decode(context_tokens[:keep_tokens])
|
| 57 |
+
|
| 58 |
+
# Reconstruire le prompt avec contexte réduit
|
| 59 |
+
base_prompt = f"""Voici des informations sur des établissements et formations :
|
| 60 |
+
|
| 61 |
+
{truncated_context}
|
| 62 |
+
|
| 63 |
+
Formule ta réponse comme un conseiller d’orientation bienveillant, de manière fluide et naturelle.
|
| 64 |
+
|
| 65 |
+
Question : {question}
|
| 66 |
+
Réponse :"""
|
| 67 |
+
|
| 68 |
+
print("===== PROMPT ENVOYÉ =====")
|
| 69 |
+
print(base_prompt)
|
| 70 |
+
|
| 71 |
+
response = client.text_generation(prompt=base_prompt, max_new_tokens=MAX_NEW_TOKENS, timeout=30)
|
| 72 |
+
|
| 73 |
+
print("===== RÉPONSE REÇUE =====")
|
| 74 |
+
print(response)
|
| 75 |
+
|
| 76 |
+
return response.get("generated_text", response) # selon format du retour
|
| 77 |
+
|