Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -42,28 +42,38 @@ def get_text_embedding(text):
|
|
42 |
|
43 |
return embeddings.embed_query(text)
|
44 |
|
45 |
-
# Supposons que vous ayez déjà chargé vos chunks de texte
|
46 |
-
# Remplacez ceci par le chargement réel de vos chunks
|
47 |
-
chunks = ["Ceci est le premier chunk de texte.", "Ceci est le deuxième chunk de texte."]
|
48 |
-
|
49 |
# Charger l'index FAISS
|
50 |
doc_path = hf_hub_download(repo_id="xavierbarbier/rag_ngap", filename="resource/embeddings_ngap.faiss", repo_type="space")
|
51 |
index = faiss.read_index(doc_path)
|
52 |
|
53 |
def qa(question):
|
54 |
-
"""Fonction principale pour répondre à la question en utilisant RAG."""
|
55 |
if not GEMINI_API_KEY:
|
56 |
-
return "Erreur : La clé API Gemini n'est pas configurée."
|
57 |
|
58 |
question_embeddings = np.array([get_text_embedding(question)])
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
prompt = f"""
|
64 |
Context information is below.
|
65 |
---------------------
|
66 |
-
{
|
67 |
---------------------
|
68 |
Given the context information and not prior knowledge, answer the query.
|
69 |
Query: {question}
|
|
|
42 |
|
43 |
return embeddings.embed_query(text)
|
44 |
|
|
|
|
|
|
|
|
|
45 |
# Charger l'index FAISS
|
46 |
doc_path = hf_hub_download(repo_id="xavierbarbier/rag_ngap", filename="resource/embeddings_ngap.faiss", repo_type="space")
|
47 |
index = faiss.read_index(doc_path)
|
48 |
|
49 |
def qa(question):
|
50 |
+
"""Fonction principale pour répondre à la question en utilisant RAG avec jusqu'à 5 chunks."""
|
51 |
if not GEMINI_API_KEY:
|
52 |
+
return "Erreur : La clé API Gemini n'est pas configurée.", ""
|
53 |
|
54 |
question_embeddings = np.array([get_text_embedding(question)])
|
55 |
+
k = 5 # Nombre de voisins à récupérer
|
56 |
+
D, I = index.search(question_embeddings, k=k) # distances et indices
|
57 |
+
|
58 |
+
retrieved_chunks = []
|
59 |
+
if I.size > 0:
|
60 |
+
for indices in I.tolist():
|
61 |
+
for index in indices:
|
62 |
+
# Vérifiez si l'index est valide avant d'accéder à la liste chunks
|
63 |
+
if 0 <= index < len(chunks):
|
64 |
+
retrieved_chunks.append(chunks[index])
|
65 |
+
else:
|
66 |
+
print(f"Avertissement : Index FAISS hors de portée de la liste chunks: {index}")
|
67 |
+
|
68 |
+
if not retrieved_chunks:
|
69 |
+
context = "Aucune information pertinente trouvée."
|
70 |
+
else:
|
71 |
+
context = "\n---------------------\n".join(retrieved_chunks)
|
72 |
|
73 |
prompt = f"""
|
74 |
Context information is below.
|
75 |
---------------------
|
76 |
+
{context}
|
77 |
---------------------
|
78 |
Given the context information and not prior knowledge, answer the query.
|
79 |
Query: {question}
|