Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- .env +4 -0
- app.py +17 -0
- rag_core.py +76 -0
- requirements.txt +19 -0
.env
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
groqapi_key = "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"
|
| 2 |
+
|
| 3 |
+
TELEGRAM_BOT_TOKEN="7553452749:AAEnZDN2-ksgc1k2BWiVeuhPPu4oZLsjFhw"
|
| 4 |
+
RAG_API_URL=http://localhost:8000/ask
|
app.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import asyncio
|
| 4 |
+
from rag_core import rag_chain
|
| 5 |
+
|
| 6 |
+
app = FastAPI(title="RAG API")
|
| 7 |
+
|
| 8 |
+
class QuestionRequest(BaseModel):
|
| 9 |
+
question: str
|
| 10 |
+
|
| 11 |
+
@app.post("/ask")
|
| 12 |
+
async def ask_question(payload: QuestionRequest):
|
| 13 |
+
try:
|
| 14 |
+
result = await asyncio.to_thread(rag_chain.invoke, payload.question)
|
| 15 |
+
return {"answer": result}
|
| 16 |
+
except Exception as e:
|
| 17 |
+
raise HTTPException(status_code=500, detail=str(e))
|
rag_core.py
ADDED
|
@@ -0,0 +1,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 |
+
from dotenv import load_dotenv
|
| 11 |
+
import os
|
| 12 |
+
|
| 13 |
+
load_dotenv()
|
| 14 |
+
|
| 15 |
+
# Configuration
|
| 16 |
+
persist_directory = "./chroma_storage"
|
| 17 |
+
embedding_model = FastEmbedEmbeddings(model_name="BAAI/bge-base-en-v1.5")
|
| 18 |
+
vectorstore = Chroma(
|
| 19 |
+
embedding_function=embedding_model,
|
| 20 |
+
persist_directory=persist_directory
|
| 21 |
+
)
|
| 22 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 2})
|
| 23 |
+
|
| 24 |
+
chat_model = ChatGroq(
|
| 25 |
+
temperature=0.3,
|
| 26 |
+
model_name="llama-3.1-8b-instant",
|
| 27 |
+
api_key=os.getenv("groqapi_key"),
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Prompt RAG
|
| 31 |
+
rag_template = """\
|
| 32 |
+
Use the following context to answer the user's query. If you cannot answer, please respond with 'I don't know'.
|
| 33 |
+
|
| 34 |
+
User's Query:
|
| 35 |
+
{question}
|
| 36 |
+
|
| 37 |
+
Context:
|
| 38 |
+
{context}
|
| 39 |
+
"""
|
| 40 |
+
rag_prompt = ChatPromptTemplate.from_template(rag_template)
|
| 41 |
+
|
| 42 |
+
# SentenceTransformer pour la similarité (si besoin)
|
| 43 |
+
similarity_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 44 |
+
|
| 45 |
+
def calculate_similarity(question, document):
|
| 46 |
+
q_emb = similarity_model.encode(question, convert_to_tensor=True).cpu().detach().numpy()
|
| 47 |
+
d_emb = similarity_model.encode(document, convert_to_tensor=True).cpu().detach().numpy()
|
| 48 |
+
return cosine_similarity([q_emb], [d_emb])[0][0]
|
| 49 |
+
|
| 50 |
+
# Génération de sous-requêtes
|
| 51 |
+
def generate_queries(query: str, llm, num_queries: int = 4):
|
| 52 |
+
query_gen_str = """\
|
| 53 |
+
You are a helpful assistant that generates multiple search queries based on a \
|
| 54 |
+
single input query. Generate {num_queries} search queries, one on each line, \
|
| 55 |
+
related to the following input query:
|
| 56 |
+
Query: {query}
|
| 57 |
+
Queries:
|
| 58 |
+
"""
|
| 59 |
+
query_prompt = ChatPromptTemplate.from_template(query_gen_str)
|
| 60 |
+
formatted_prompt = query_prompt.format(num_queries=num_queries, query=query)
|
| 61 |
+
response = llm.predict(formatted_prompt)
|
| 62 |
+
return response.strip().splitlines()
|
| 63 |
+
|
| 64 |
+
# Récupération de contexte enrichi
|
| 65 |
+
def get_context(query):
|
| 66 |
+
sub_queries = generate_queries(query, chat_model)
|
| 67 |
+
chunks = [retriever.invoke(q) for q in sub_queries]
|
| 68 |
+
return "\n".join(map(str, chunks))
|
| 69 |
+
|
| 70 |
+
# La chaîne complète
|
| 71 |
+
rag_chain = (
|
| 72 |
+
{"context": get_context, "question": RunnablePassthrough()}
|
| 73 |
+
| rag_prompt
|
| 74 |
+
| chat_model
|
| 75 |
+
| StrOutputParser()
|
| 76 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
langchain
|
| 3 |
+
chromadb
|
| 4 |
+
transformers
|
| 5 |
+
groq
|
| 6 |
+
sentence_transformers
|
| 7 |
+
langchain-community
|
| 8 |
+
langchain-core
|
| 9 |
+
transformers
|
| 10 |
+
faiss-cpu
|
| 11 |
+
sentence-transformers
|
| 12 |
+
fastembed
|
| 13 |
+
langchain_experimental
|
| 14 |
+
langchain_openai
|
| 15 |
+
requests
|
| 16 |
+
langchain_groq
|
| 17 |
+
langchain_chroma
|
| 18 |
+
fastapi
|
| 19 |
+
pydantic
|