Spaces:
Sleeping
Sleeping
Update rag_core.py
Browse files- rag_core.py +75 -76
rag_core.py
CHANGED
|
@@ -1,76 +1,75 @@
|
|
| 1 |
-
# rag_core.py
|
| 2 |
-
from langchain_community.embeddings.fastembed import FastEmbedEmbeddings
|
| 3 |
-
from langchain_chroma import Chroma
|
| 4 |
-
from langchain_core.prompts import ChatPromptTemplate
|
| 5 |
-
from langchain_core.runnables import RunnablePassthrough
|
| 6 |
-
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
-
from langchain_groq import ChatGroq
|
| 8 |
-
from sentence_transformers import SentenceTransformer
|
| 9 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
| 10 |
-
|
| 11 |
-
import os
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
| 74 |
-
|
|
| 75 |
-
|
| 76 |
-
)
|
|
|
|
| 1 |
+
# rag_core.py
|
| 2 |
+
from langchain_community.embeddings.fastembed import FastEmbedEmbeddings
|
| 3 |
+
from langchain_chroma import Chroma
|
| 4 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 5 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 6 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
+
from langchain_groq import ChatGroq
|
| 8 |
+
from sentence_transformers import SentenceTransformer
|
| 9 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 10 |
+
|
| 11 |
+
import os
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Configuration
|
| 15 |
+
persist_directory = "./chroma_storage"
|
| 16 |
+
embedding_model = FastEmbedEmbeddings(model_name="BAAI/bge-base-en-v1.5")
|
| 17 |
+
vectorstore = Chroma(
|
| 18 |
+
embedding_function=embedding_model,
|
| 19 |
+
persist_directory=persist_directory
|
| 20 |
+
)
|
| 21 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 2})
|
| 22 |
+
|
| 23 |
+
chat_model = ChatGroq(
|
| 24 |
+
temperature=0.3,
|
| 25 |
+
model_name="llama-3.1-8b-instant",
|
| 26 |
+
api_key=os.getenv("groqapi_key"),
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Prompt RAG
|
| 30 |
+
rag_template = """\
|
| 31 |
+
Use the following context to answer the user's query. If you cannot answer, please respond with 'I don't know'.
|
| 32 |
+
|
| 33 |
+
User's Query:
|
| 34 |
+
{question}
|
| 35 |
+
|
| 36 |
+
Context:
|
| 37 |
+
{context}
|
| 38 |
+
"""
|
| 39 |
+
rag_prompt = ChatPromptTemplate.from_template(rag_template)
|
| 40 |
+
|
| 41 |
+
# SentenceTransformer pour la similarité (si besoin)
|
| 42 |
+
similarity_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 43 |
+
|
| 44 |
+
def calculate_similarity(question, document):
|
| 45 |
+
q_emb = similarity_model.encode(question, convert_to_tensor=True).cpu().detach().numpy()
|
| 46 |
+
d_emb = similarity_model.encode(document, convert_to_tensor=True).cpu().detach().numpy()
|
| 47 |
+
return cosine_similarity([q_emb], [d_emb])[0][0]
|
| 48 |
+
|
| 49 |
+
# Génération de sous-requêtes
|
| 50 |
+
def generate_queries(query: str, llm, num_queries: int = 4):
|
| 51 |
+
query_gen_str = """\
|
| 52 |
+
You are a helpful assistant that generates multiple search queries based on a \
|
| 53 |
+
single input query. Generate {num_queries} search queries, one on each line, \
|
| 54 |
+
related to the following input query:
|
| 55 |
+
Query: {query}
|
| 56 |
+
Queries:
|
| 57 |
+
"""
|
| 58 |
+
query_prompt = ChatPromptTemplate.from_template(query_gen_str)
|
| 59 |
+
formatted_prompt = query_prompt.format(num_queries=num_queries, query=query)
|
| 60 |
+
response = llm.predict(formatted_prompt)
|
| 61 |
+
return response.strip().splitlines()
|
| 62 |
+
|
| 63 |
+
# Récupération de contexte enrichi
|
| 64 |
+
def get_context(query):
|
| 65 |
+
sub_queries = generate_queries(query, chat_model)
|
| 66 |
+
chunks = [retriever.invoke(q) for q in sub_queries]
|
| 67 |
+
return "\n".join(map(str, chunks))
|
| 68 |
+
|
| 69 |
+
# La chaîne complète
|
| 70 |
+
rag_chain = (
|
| 71 |
+
{"context": get_context, "question": RunnablePassthrough()}
|
| 72 |
+
| rag_prompt
|
| 73 |
+
| chat_model
|
| 74 |
+
| StrOutputParser()
|
| 75 |
+
)
|
|
|