Spaces:
Runtime error
Runtime error
Update rag_pipeline.py
Browse files- rag_pipeline.py +65 -0
rag_pipeline.py
CHANGED
|
@@ -74,6 +74,71 @@ Frage:
|
|
| 74 |
{query}
|
| 75 |
"""
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# === Anfrage an OpenAI
|
| 78 |
def ask_openai(prompt):
|
| 79 |
if not openai_client:
|
|
|
|
| 74 |
{query}
|
| 75 |
"""
|
| 76 |
|
| 77 |
+
# === Anfrage an OpenAI
|
| 78 |
+
def ask_openai(prompt):
|
| 79 |
+
if not openai_client:
|
| 80 |
+
return "❌ Kein OpenAI API Key gefunden"
|
| 81 |
+
res = openai_client.chat.completions.create(
|
| 82 |
+
model="gpt-4",
|
| 83 |
+
messages=[
|
| 84 |
+
{"role": "system", "content": "Du bist ein hilfsbereiter Catan-Regel-Experte."},
|
| 85 |
+
{"role": "user", "content": prompt}
|
| 86 |
+
]
|
| 87 |
+
)
|
| 88 |
+
return res.choices[0].message.content.strip()
|
| 89 |
+
|
| 90 |
+
# === Anfrage an Groq
|
| 91 |
+
def ask_groq(prompt):
|
| 92 |
+
if not groq_client:
|
| 93 |
+
return "❌ Kein Groq API Key gefunden"
|
| 94 |
+
res = groq_client.chat.completions.create(
|
| 95 |
+
model="llama3-70b-8192",
|
| 96 |
+
messages=[
|
| 97 |
+
{"role": "system", "content": "Du bist ein hilfsbereiter Catan-Regel-Experte."},
|
| 98 |
+
{"role": "user", "content": prompt}
|
| 99 |
+
]
|
| 100 |
+
)
|
| 101 |
+
return res.choices[0].message.content.strip()
|
| 102 |
+
|
| 103 |
+
# === Hauptfunktion mit FAISS-Distanzfilter
|
| 104 |
+
def run_qa_pipeline(query, k=5):
|
| 105 |
+
try:
|
| 106 |
+
retrieved, distances = retrieve(query, k)
|
| 107 |
+
|
| 108 |
+
if not retrieved:
|
| 109 |
+
return "⚠️ Keine relevanten Textstellen gefunden."
|
| 110 |
+
|
| 111 |
+
max_dist = max(distances)
|
| 112 |
+
print(f"ℹ️ Höchste FAISS-Distanz: {max_dist:.4f}")
|
| 113 |
+
|
| 114 |
+
# 🔒 Schwelle für Relevanz (anpassbar)
|
| 115 |
+
if max_dist > 1.0:
|
| 116 |
+
return "🚫 Diese Frage scheint nichts mit Catan zu tun zu haben."
|
| 117 |
+
|
| 118 |
+
prompt = build_prompt(query, retrieved)
|
| 119 |
+
print("📨 Prompt gesendet...")
|
| 120 |
+
|
| 121 |
+
if openai_client:
|
| 122 |
+
answer = ask_openai(prompt)
|
| 123 |
+
elif groq_client:
|
| 124 |
+
answer = ask_groq(prompt)
|
| 125 |
+
else:
|
| 126 |
+
return "⚠️ Kein LLM API-Key vorhanden. Bitte OPENAI_API_KEY oder GROQ_API_KEY hinterlegen."
|
| 127 |
+
|
| 128 |
+
return f"📌 Frage: {query}\n\n📖 Antwort:\n{answer}"
|
| 129 |
+
|
| 130 |
+
except Exception as e:
|
| 131 |
+
return f"❌ Fehler beim Verarbeiten der Anfrage:\n{str(e)}"def build_prompt(query, texts):
|
| 132 |
+
context = "\n\n".join(texts)
|
| 133 |
+
return f"""Beantworte die folgende Frage basierend auf dem Kontext.
|
| 134 |
+
|
| 135 |
+
Kontext:
|
| 136 |
+
{context}
|
| 137 |
+
|
| 138 |
+
Frage:
|
| 139 |
+
{query}
|
| 140 |
+
"""
|
| 141 |
+
|
| 142 |
# === Anfrage an OpenAI
|
| 143 |
def ask_openai(prompt):
|
| 144 |
if not openai_client:
|