Spaces:
Runtime error
Runtime error
Update rag_pipeline.py
Browse files- rag_pipeline.py +84 -2
rag_pipeline.py
CHANGED
|
@@ -1,8 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def run_qa_pipeline(user_query, k=5):
|
| 2 |
retrieved_texts, _, _ = retrieve(user_query, k=k)
|
| 3 |
prompt = build_prompt(user_query, retrieved_texts)
|
| 4 |
-
|
| 5 |
answer_groq = ask_groq_llm(prompt)
|
| 6 |
answer_openai = ask_openai_llm(prompt)
|
| 7 |
|
| 8 |
-
return f" Groq LLaMA 3:\n{answer_groq}\n\n OpenAI GPT-4:\n{answer_openai}"
|
|
|
|
| 1 |
+
# rag_pipeline.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import pickle
|
| 5 |
+
import numpy as np
|
| 6 |
+
import faiss
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 9 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter, SentenceTransformersTokenTextSplitter
|
| 10 |
+
import umap.umap_ as umap
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
from groq import Groq
|
| 13 |
+
from openai import OpenAI
|
| 14 |
+
import tqdm
|
| 15 |
+
|
| 16 |
+
# === Load environment variables (handled by Hugging Face Secrets in deployment) ===
|
| 17 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 18 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 19 |
+
|
| 20 |
+
groq_client = Groq(api_key=groq_api_key) if groq_api_key else None
|
| 21 |
+
openai_client = OpenAI(api_key=openai_api_key) if openai_api_key else None
|
| 22 |
+
|
| 23 |
+
# === Load FAISS Index and Chunks ===
|
| 24 |
+
index = faiss.read_index("faiss/faiss_index.index")
|
| 25 |
+
with open("faiss/chunks_mapping.pkl", "rb") as f:
|
| 26 |
+
token_split_texts = pickle.load(f)
|
| 27 |
+
|
| 28 |
+
# === Load SentenceTransformer model ===
|
| 29 |
+
model = SentenceTransformer("Sahajtomar/German-semantic")
|
| 30 |
+
chunk_embeddings = model.encode(token_split_texts, convert_to_numpy=True)
|
| 31 |
+
|
| 32 |
+
# === Fit UMAP ===
|
| 33 |
+
umap_transform = umap.UMAP(random_state=0, transform_seed=0).fit(chunk_embeddings)
|
| 34 |
+
|
| 35 |
+
def project_embeddings(embeddings, umap_transform):
|
| 36 |
+
umap_embeddings = np.empty((len(embeddings), 2))
|
| 37 |
+
for i, embedding in enumerate(tqdm.tqdm(embeddings, desc="Projecting Embeddings")):
|
| 38 |
+
umap_embeddings[i] = umap_transform.transform([embedding])
|
| 39 |
+
return umap_embeddings
|
| 40 |
+
|
| 41 |
+
def retrieve(query, k=5):
|
| 42 |
+
query_embedding = model.encode([query], convert_to_numpy=True)
|
| 43 |
+
distances, indices = index.search(query_embedding, k)
|
| 44 |
+
retrieved_texts = [token_split_texts[i] for i in indices[0]]
|
| 45 |
+
retrieved_embeddings = np.array([chunk_embeddings[i] for i in indices[0]])
|
| 46 |
+
return retrieved_texts, retrieved_embeddings, distances[0]
|
| 47 |
+
|
| 48 |
+
def build_prompt(query, retrieved_texts):
|
| 49 |
+
context_block = "\n\n".join(retrieved_texts)
|
| 50 |
+
prompt = f"""Beantworte die folgende Frage basierend auf dem gegebenen Kontext.
|
| 51 |
+
|
| 52 |
+
Kontext:
|
| 53 |
+
{context_block}
|
| 54 |
+
|
| 55 |
+
Frage:
|
| 56 |
+
{query}
|
| 57 |
+
"""
|
| 58 |
+
return prompt
|
| 59 |
+
|
| 60 |
+
def ask_groq_llm(prompt):
|
| 61 |
+
if not groq_client:
|
| 62 |
+
return "[Fehler] Kein Groq API Key vorhanden."
|
| 63 |
+
response = groq_client.chat.completions.create(
|
| 64 |
+
model="llama3-70b-8192",
|
| 65 |
+
messages=[
|
| 66 |
+
{"role": "system", "content": "Du bist ein hilfreicher Assistent."},
|
| 67 |
+
{"role": "user", "content": prompt}
|
| 68 |
+
]
|
| 69 |
+
)
|
| 70 |
+
return response.choices[0].message.content.strip()
|
| 71 |
+
|
| 72 |
+
def ask_openai_llm(prompt):
|
| 73 |
+
if not openai_client:
|
| 74 |
+
return "[Fehler] Kein OpenAI API Key vorhanden."
|
| 75 |
+
response = openai_client.chat.completions.create(
|
| 76 |
+
model="gpt-4",
|
| 77 |
+
messages=[
|
| 78 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 79 |
+
{"role": "user", "content": prompt}
|
| 80 |
+
]
|
| 81 |
+
)
|
| 82 |
+
return response.choices[0].message.content.strip()
|
| 83 |
+
|
| 84 |
def run_qa_pipeline(user_query, k=5):
|
| 85 |
retrieved_texts, _, _ = retrieve(user_query, k=k)
|
| 86 |
prompt = build_prompt(user_query, retrieved_texts)
|
|
|
|
| 87 |
answer_groq = ask_groq_llm(prompt)
|
| 88 |
answer_openai = ask_openai_llm(prompt)
|
| 89 |
|
| 90 |
+
return f"\U0001f999 Groq LLaMA 3 Antwort:\n{answer_groq}\n\n\U0001f52e OpenAI GPT-4 Antwort:\n{answer_openai}"
|