Views
No views yet
aksw/text2sparql-Ltext2sparql-L is Large a fine-tuned language model designed to translate natural language questions into SPARQL queries, specifically targeting the DBpedia knowledge graph (2014 version). It is ideal for knowledge-based QA systems and symbolic reasoning agents.unsloth, torch and CUDA dependencies:pip install unsloth torch1from unsloth import FastLanguageModel
2import torch
3
4class SPARQLQueryGenerator:
5 def __init__(self, model_name: str, max_seq_length: int = 2048, load_in_4bit: bool = True):
6 self.model, self.tokenizer = FastLanguageModel.from_pretrained(
7 model_name=model_name,
8 max_seq_length=max_seq_length,
9 load_in_4bit=load_in_4bit
10 )
11 _ = FastLanguageModel.for_inference(self.model)
12
13 def build_prompt(self, question: str) -> list:
14 return [
15 {"role": "system", "content": (
16 "You are an expert data analyst with deep knowledge of SPARQL and the DBpedia ontology.\n"
17 "Your task is to convert a given natural language question into a syntactically correct DBpedia SPARQL query "
18 "that accurately retrieves the answer.\n"
19 "Your output must be a single string containing only the SPARQL query—no additional text, explanation, or commentary.\n"
20 "Ensure that you use the appropriate DBpedia prefixes and follow standard SPARQL syntax."
21 )},
22 {"role": "user", "content": question}
23 ]
24
25 def generate_query(self, question: str, temperature: float = 0.01, max_new_tokens: int = 1024) -> str:
26 messages = self.build_prompt(question)
27 inputs = self.tokenizer.apply_chat_template(
28 messages,
29 tokenize=True,
30 add_generation_prompt=True,
31 return_tensors="pt"
32 ).to("cuda")
33
34 outputs = self.model.generate(
35 input_ids=inputs,
36 max_new_tokens=max_new_tokens,
37 use_cache=True,
38 temperature=temperature,
39 min_p=0.1
40 )
41
42 decoded = self.tokenizer.batch_decode(outputs)[0]
43 return self._extract_sparql(decoded)
44
45 def _extract_sparql(self, decoded_text: str) -> str:
46 start_token = "<|im_start|>assistant\n"
47 end_token = "<|im_end|>"
48 start_index = decoded_text.find(start_token) + len(start_token)
49 sparql = decoded_text[start_index:]
50 return sparql.rstrip(end_token) if sparql.endswith(end_token) else sparql
51
52# --- Using the model ---
53if __name__ == "__main__":
54 generator = SPARQLQueryGenerator(model_name="aksw/text2sparql-L")
55 question = "Which actors were born in Germany?"
56 query = generator.generate_query(question)
57 print(query)Which actors were born in Germany?1PREFIX dbo: <http://dbpedia.org/ontology/>
2PREFIX res: <http://dbpedia.org/resource/>
3PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
4SELECT DISTINCT ?uri WHERE {
5 ?uri rdf:type dbo:Actor .
6 ?uri dbo:birthPlace res:Germany .
7}@misc{text2sparql2025,
author = {Marcos Gôlo, Paulo do Carmo, Edgard Marx, Ricardo Marcacini},
title = {text2SPARQL-L: Natural Language Text to SPARQL for DBpedia},
year = {2025},
howpublished = {\url{https://huggingface.co/aksw/text2sparql-L}},
}