Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,34 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Weaviate-instellingen
|
| 7 |
WEAVIATE_URL = "https://ojk79flbtdqux0f4uua2qa.c0.europe-west3.gcp.weaviate.cloud/v1/graphql"
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
# Functie om
|
| 16 |
-
def fetch_relevant_docs(
|
| 17 |
query = {
|
| 18 |
"query": f"""
|
| 19 |
{{
|
| 20 |
Get {{
|
| 21 |
DigiD_Document_Vectorized(
|
| 22 |
-
nearText: {{ concepts: ["{
|
| 23 |
limit: 3
|
| 24 |
) {{
|
| 25 |
title
|
|
@@ -30,45 +39,61 @@ def fetch_relevant_docs(query_text):
|
|
| 30 |
}}
|
| 31 |
"""
|
| 32 |
}
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
"Authorization": f"Bearer {WEAVIATE_API_KEY}",
|
| 36 |
-
"X-Weaviate-Api-Key": WEAVIATE_API_KEY,
|
| 37 |
-
"X-Weaviate-Cluster-Url": "https://ojk79flbtdqux0f4uua2qa.c0.europe-west3.gcp.weaviate.cloud",
|
| 38 |
-
"Content-Type": "application/json"
|
| 39 |
-
}
|
| 40 |
-
|
| 41 |
-
response = requests.post(WEAVIATE_URL, json=query, headers=headers)
|
| 42 |
|
| 43 |
if response.status_code == 200:
|
| 44 |
data = response.json()
|
| 45 |
-
print("DEBUG: Volledige Weaviate API response:", json.dumps(data, indent=2)) # Volledige API-response printen
|
| 46 |
-
|
| 47 |
documents = data.get("data", {}).get("Get", {}).get("DigiD_Document_Vectorized", [])
|
|
|
|
| 48 |
if not documents:
|
| 49 |
-
print("⚠️ Geen documenten gevonden voor de query:
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
else:
|
| 52 |
-
print(f"❌ Fout bij ophalen documentatie: {response.status_code}")
|
| 53 |
-
return
|
| 54 |
-
|
| 55 |
-
#
|
| 56 |
-
def
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
else:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
return "Check de logs voor opgehaalde documentatie!"
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
demo = gr.ChatInterface(
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
| 74 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
|
| 5 |
+
# API Keys ophalen uit Hugging Face Secrets
|
| 6 |
+
WEAVIATE_API_KEY = os.getenv("WEAVIATE_API_KEY")
|
| 7 |
+
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
|
| 8 |
|
| 9 |
# Weaviate-instellingen
|
| 10 |
WEAVIATE_URL = "https://ojk79flbtdqux0f4uua2qa.c0.europe-west3.gcp.weaviate.cloud/v1/graphql"
|
| 11 |
+
HEADERS = {
|
| 12 |
+
"Authorization": f"Bearer {WEAVIATE_API_KEY}",
|
| 13 |
+
"X-Weaviate-Api-Key": WEAVIATE_API_KEY,
|
| 14 |
+
"Content-Type": "application/json"
|
| 15 |
+
}
|
| 16 |
|
| 17 |
+
# **Nieuwe consistente system message**
|
| 18 |
+
SYSTEM_MESSAGE = (
|
| 19 |
+
"Je bent een formele, feitelijke en beknopte digitale assistent bij Logius. "
|
| 20 |
+
"Je helpt gebruikers met vragen over DigiD op basis van officiële documentatie. "
|
| 21 |
+
"Antwoorden zijn kort, bondig, feitelijk en eenvoudig te begrijpen."
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
# Functie om relevante documenten uit Weaviate op te halen
|
| 25 |
+
def fetch_relevant_docs(user_query):
|
| 26 |
query = {
|
| 27 |
"query": f"""
|
| 28 |
{{
|
| 29 |
Get {{
|
| 30 |
DigiD_Document_Vectorized(
|
| 31 |
+
nearText: {{ concepts: ["{user_query}"] }},
|
| 32 |
limit: 3
|
| 33 |
) {{
|
| 34 |
title
|
|
|
|
| 39 |
}}
|
| 40 |
"""
|
| 41 |
}
|
| 42 |
+
|
| 43 |
+
response = requests.post(WEAVIATE_URL, json=query, headers=HEADERS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
if response.status_code == 200:
|
| 46 |
data = response.json()
|
|
|
|
|
|
|
| 47 |
documents = data.get("data", {}).get("Get", {}).get("DigiD_Document_Vectorized", [])
|
| 48 |
+
|
| 49 |
if not documents:
|
| 50 |
+
print(f"⚠️ Geen documenten gevonden voor de query: {user_query}")
|
| 51 |
+
return "Er is geen relevante documentatie gevonden voor deze vraag."
|
| 52 |
+
|
| 53 |
+
# Combineer de documentatie in een nette tekst
|
| 54 |
+
combined_text = "\n\n".join(
|
| 55 |
+
[f"**{doc['title']}**\n{doc['content']}\nBron: {doc['url']}" for doc in documents]
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
return combined_text
|
| 59 |
+
|
| 60 |
else:
|
| 61 |
+
print(f"❌ Fout bij ophalen documentatie: {response.status_code}, {response.text}")
|
| 62 |
+
return "Er is een fout opgetreden bij het ophalen van de documentatie."
|
| 63 |
+
|
| 64 |
+
# Functie om antwoord te genereren met Mistral
|
| 65 |
+
def generate_answer(user_query):
|
| 66 |
+
documentation = fetch_relevant_docs(user_query)
|
| 67 |
+
|
| 68 |
+
if "Er is geen relevante documentatie" in documentation:
|
| 69 |
+
return "Ik heb geen relevante documentatie kunnen vinden voor je vraag."
|
| 70 |
+
|
| 71 |
+
mistral_url = "https://api.mistral.ai/v1/chat/completions"
|
| 72 |
+
mistral_headers = {
|
| 73 |
+
"Authorization": f"Bearer {MISTRAL_API_KEY}",
|
| 74 |
+
"Content-Type": "application/json"
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
payload = {
|
| 78 |
+
"model": "mistral-medium",
|
| 79 |
+
"messages": [
|
| 80 |
+
{"role": "system", "content": SYSTEM_MESSAGE},
|
| 81 |
+
{"role": "user", "content": user_query},
|
| 82 |
+
{"role": "assistant", "content": documentation}
|
| 83 |
+
],
|
| 84 |
+
"temperature": 0.3,
|
| 85 |
+
"max_tokens": 512
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
response = requests.post(mistral_url, json=payload, headers=mistral_headers)
|
| 89 |
+
|
| 90 |
+
if response.status_code == 200:
|
| 91 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 92 |
else:
|
| 93 |
+
return f"Fout bij genereren antwoord: {response.status_code}, {response.text}"
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
# Gradio UI
|
| 96 |
+
demo = gr.ChatInterface(generate_answer)
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
demo.launch()
|