File size: 4,181 Bytes
034a1e6 aa07e9d 034a1e6 1575481 034a1e6 3031ece 034a1e6 3031ece 034a1e6 a1f8fd1 034a1e6 a1f8fd1 3031ece 034a1e6 8c772f5 034a1e6 1575481 034a1e6 4d4b9ae 034a1e6 3031ece 034a1e6 3031ece 034a1e6 3b0ec68 034a1e6 3031ece baa2201 3b0ec68 baa2201 034a1e6 baa2201 034a1e6 3b0ec68 baa2201 034a1e6 baa2201 034a1e6 baa2201 034a1e6 aa07e9d 034a1e6 3b0ec68 ec7377b 3031ece 4d4b9ae 034a1e6 a1f8fd1 4d4b9ae 8e527d0 4d4b9ae 034a1e6 3031ece 1575481 034a1e6 a1f8fd1 |
1 2 3 4 5 6 7 8 9 10 11 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
import os
from fastapi import FastAPI, HTTPException
from huggingface_hub import InferenceClient
from rdflib import Graph
from pydantic import BaseModel
# Configurazione API Hugging Face
API_KEY = os.getenv("HF_API_KEY")
client = InferenceClient(api_key=API_KEY)
# File RDF
RDF_FILE = "Progetto.rdf"
# Carica un riassunto del file RDF
def load_rdf_summary():
if os.path.exists(RDF_FILE):
g = Graph()
g.parse(RDF_FILE, format="xml")
classes = set()
properties = set()
for s, _, o in g.triples((None, None, None)):
if "Class" in str(o) or "rdfs:Class" in str(o):
classes.add(s)
if "Property" in str(o):
properties.add(s)
classes_summary = "\n".join([f"- Classe: {cls}" for cls in classes])
properties_summary = "\n".join([f"- Proprietà: {prop}" for prop in properties])
return f"Classi:\n{classes_summary}\n\nProprietà:\n{properties_summary}"
return "Nessun dato RDF trovato."
rdf_context = load_rdf_summary()
print("RDF Summary:", rdf_context) # Debug
# Valida le query SPARQL
def validate_sparql_query(query, rdf_file_path):
try:
g = Graph()
# Caricamento del file RDF dal percorso
g.parse(rdf_file_path, format="xml")
g.query(query) # Prova ad eseguire la query
return True
except Exception as e:
print(f"Errore durante la validazione della query SPARQL: {e}")
return False
# FastAPI app
app = FastAPI()
# Modello di input per richieste POST
class QueryRequest(BaseModel):
message: str
max_tokens: int = 2048
temperature: float = 0.7
# Messaggio di sistema con RDF incluso
def create_system_message(rdf_context):
return f"""
Sei un assistente esperto nella generazione di query SPARQL basate su ontologie RDF.
Ecco un riassunto dell'ontologia su cui devi lavorare:
{rdf_context}
Il tuo compito:
- Genera esclusivamente query SPARQL valide in UNA SOLA RIGA.
- Rispondi solo se la domanda è pertinente alle classi e proprietà fornite.
- Se non puoi rispondere, di': "Non posso generare una query SPARQL per questa richiesta."
"""
async def generate_response(message, max_tokens, temperature):
system_message = create_system_message(rdf_context)
print("System Message:", system_message) # Debug
print("User Message:", message) # Debug
messages = [
{"role": "system", "content": system_message},
{"role": "user", "content": message}
]
try:
response = client.chat.completions.create(
model="Qwen/Qwen2.5-72B-Instruct",
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
top_p=0.7,
stream=False
)
print("Raw Response:", response) # Debug risposta grezza
return response['choices'][0]['message']['content'].replace("\n", " ").strip()
except Exception as e:
print(f"Errore nell'elaborazione: {str(e)}")
raise HTTPException(status_code=500, detail=f"Errore nell'elaborazione: {str(e)}")
# Endpoint per generare query SPARQL
@app.post("/generate-query/")
async def generate_query(request: QueryRequest):
response = await generate_response(request.message, request.max_tokens, request.temperature)
print("Risposta generata dal modello:", response) # Debug
if not (response.startswith("SELECT") or response.startswith("ASK")):
return {
"query": None,
"explanation": "Non posso generare una query SPARQL per questa richiesta. Assicurati che la domanda sia coerente con i dati RDF forniti."
}
if not validate_sparql_query(response, RDF_FILE):
return {
"query": None,
"explanation": "La query generata non è valida rispetto alla base di conoscenza RDF. Assicurati di chiedere informazioni che siano presenti nell'ontologia."
}
return {"query": response, "explanation": "Ecco la query generata correttamente in una riga pronta per GraphDB."}
# Endpoint di test
@app.get("/")
async def root():
return {"message": "Il server è attivo e pronto a generare query SPARQL!"}
|