Downloads · 30 days
8
28% of all-time downloads
julioc-p/mistral_en_txt_sparql_4bit
mistral_en_txt_sparql_4bit is a machine learning model from julioc-p. Use it for the machine learning task on the model card, and read the license before you ship it in a product. It is set up for peft. The card lists the license as mit.
This model is a fine-tuned version of mistralai/Mistral-7B-Instruct-v0.1 for generating SPARQL queries from English natural language questions, specifically targeting the Wikidata knowledge graph.
Downloads · 30 days
8
28% of all-time downloads
All-time downloads
29
Public
Repo size
1.4 GB
Likes
0
Public
Click a slice to open those files.
.safetensors168 MB · 100%
From the Hugging Face model README
This model is a fine-tuned version of mistralai/Mistral-7B-Instruct-v0.1 for generating SPARQL queries from English natural language questions, specifically targeting the Wikidata knowledge graph.
It was fine-tuned using QLoRA. It takes an English natural language question as input and aims to produce a corresponding SPARQL query that can be executed against the Wikidata knowledge graph. It is part of a series of experiments to investigate the impact of continual multilingual pre-training on cross-lingual transferability and task-specific performance. Uses 4-bit quantization.
mistralai/Mistral-7B-Instruct-v0.1The following Python script provides an example of how to load the model and tokenizer using the Hugging Face Transformers and PEFT libraries to generate a SPARQL query.
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
import re
# Model ID for the Mistral English v1.1 fine-tuned model
model_id = "julioc-p/mistral_en_txt_sparql_4bit"
base_model_for_tokenizer = "mistralai/Mistral-7B-Instruct-v0.1"
# Configuration for 4-bit quantization
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=False,
)
# Load the model and tokenizer
model = AutoModelForCausalLM.from_pretrained(
model_id,
quantization_config=bnb_config,
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(base_model_for_tokenizer)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
model.config.pad_token_id = tokenizer.pad_token_id
def extract_sparql(text):
match = re.search(
r"(SELECT|ASK|CONSTRUCT|DESCRIBE)(.*?)\}",
text,
re.DOTALL | re.IGNORECASE | re.MULTILINE,
)
if match:
sparql_query = match.group(0).strip()
sparql_query = re.sub(
r"^\s*```sparql\n", "", sparql_query, flags=re.IGNORECASE | re.MULTILINE
)
sparql_query = re.sub(r"\n```\s*$", "", sparql_query)
return sparql_query.strip()
match_simple = re.search(
r"(SELECT|ASK|CONSTRUCT|DESCRIBE).*?\}", text, re.DOTALL | re.IGNORECASE
)
if match_simple:
return match_simple.group(0).strip()
return ""
# --- Example usage ---
question = "What is the boiling point of water?"
knowledge_graph_target = "Wikidata"
prompt_content = f"Write a SparQL query that answers this request: '{question}' from the knowledge graph {knowledge_graph_target}."
chat_template = [
{"role": "user", "content": prompt_content},
]
inputs = tokenizer.apply_chat_template(
chat_template,
tokenize=True,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
# Generate the output
with torch.no_grad():
outputs = model.generate(
input_ids=inputs, # Pass input_ids directly
attention_mask=inputs.attention_mask if hasattr(inputs, 'attention_mask') else None, # Pass attention_mask if present
max_new_tokens=512,
do_sample=True,
pad_token_id=tokenizer.pad_token_id
)
# Decode only the generated part
input_length = inputs.input_ids.shape[1] if hasattr(inputs, 'input_ids') else inputs.shape[1]
generated_text_assistant_part = tokenizer.decode(outputs[0][input_length:], skip_special_tokens=True)
cleaned_sparql = extract_sparql(generated_text_assistant_part)
print(f"Question: {question}")
print(f"Generated SPARQL: {cleaned_sparql}")
print(f"Raw generated text (assistant part): {generated_text_assistant_part}")
The model was fine-tuned on a subset of the julioc-p/Question-Sparql dataset. Specifically, a 35,000-sample English subset filtered to include only Wikidata-related queries was used.
The following hyperparameters were used for the fine-tuning:
r (LoRA rank): 16lora_alpha: 16lora_dropout: 0.1bias: "none"task_type: "CAUSAL_LM"target_modules: "q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"num_train_epochs: 5per_device_train_batch_size: 1gradient_accumulation_steps: 8gradient_checkpointing: Trueoptim: "paged_adamw_32bit"learning_rate: 1e-5weight_decay: 0.05bf16: Falsefp16: Truemax_grad_norm: 1.0warmup_ratio: 0.01lr_scheduler_type: "cosine"group_by_length: Truepacking: Falseload_in_4bit: Truebnb_4bit_quant_type: "nf4"bnb_4bit_compute_dtype: torch.float16bnb_4bit_use_double_quant: Falsejulioc-p/Question-Sparql dataset (Wikidata-focused).The primary evaluation metrics used were the QALD standard macro-averaged F1-score, Precision, and Recall. Non-executable queries resulted in P, R, F1 = 0. The percentage of Executable Queries was also tracked.
On QALD-10 (English, N=391):
On v1 Test Set (English, N=3500):
transformers, peft (0.13.2), bitsandbytes, trl, PyTorch.4.39.3)0.43.0)0.8.6)torch==2.1.0)