Downloads · 30 days
74
0% of all-time downloads
neo4j/text2cypher-gemma-2-9b-it-finetuned-2024v1
text2cypher-gemma-2-9b-it-finetuned-2024v1 is a text generation model from neo4j. Use it when you need the model to write or continue text. It is set up for peft. The card lists the license as gemma.
This model serves as a demonstration of how fine-tuning foundational models using the Neo4j-Text2Cypher(2024) Dataset (link) can enhance performance on the Text2Cypher task.\ Please note, this is part of ongoing resea…
Downloads · 30 days
74
0% of all-time downloads
All-time downloads
17.9K
Public
Repo size
1.6 GB
Likes
36
Public
Click a slice to open those files.
.safetensors864 MB · 98%
From the Hugging Face model README
This model serves as a demonstration of how fine-tuning foundational models using the Neo4j-Text2Cypher(2024) Dataset (link) can enhance performance on the Text2Cypher task.
Please note, this is part of ongoing research and exploration, aimed at highlighting the dataset's potential rather than a production-ready solution.
Base model: google/gemma-2-9b-it
Dataset: neo4j/text2cypher-2024v1
An overview of the finetuned models and benchmarking results are shared at Link1 and Link2
Have ideas or insights? Contact us: Neo4j/Team-GenAI
<!-- - **Developed by:** [More Information Needed] - **Funded by [optional]:** [More Information Needed] - **Shared by [optional]:** [More Information Needed] - **Model type:** [More Information Needed] - **Language(s) (NLP):** [More Information Needed] - **License:** [More Information Needed] - **Finetuned from model [optional]:** [More Information Needed] --> <!-- ### Model Sources [optional] <!-- Provide the basic links for the model. --> <!-- - **Repository:** [More Information Needed] - **Paper [optional]:** [More Information Needed] - **Demo [optional]:** [More Information Needed] --> <!-- ## Uses --> <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. --> <!-- ### Direct Use --> <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. --> <!-- [More Information Needed] --> <!-- ### Downstream Use [optional] --> <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app --> <!-- [More Information Needed] --> <!-- ### Out-of-Scope Use --> <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. --> <!-- [More Information Needed] -->We need to be cautious about a few risks:
Also check the related blogpost:Link
<!-- ### Recommendations --> <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. --> <!-- Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations. --> <!-- ## How to Get Started with the Model Use the code below to get started with the model. [More Information Needed] -->Used RunPod with following setup:
from peft import PeftModel, PeftConfig
import torch
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
)
instruction = (
"Generate Cypher statement to query a graph database. "
"Use only the provided relationship types and properties in the schema. \n"
"Schema: {schema} \n Question: {question} \n Cypher output: "
)
def prepare_chat_prompt(question, schema) -> list[dict]:
chat = [
{
"role": "user",
"content": instruction.format(
schema=schema, question=question
),
}
]
return chat
def _postprocess_output_cypher(output_cypher: str) -> str:
# Remove any explanation. E.g. MATCH...\n\n**Explanation:**\n\n -> MATCH...
# Remove cypher indicator. E.g.```cypher\nMATCH...```` --> MATCH...
# Note: Possible to have both:
# E.g. ```cypher\nMATCH...````\n\n**Explanation:**\n\n --> MATCH...
partition_by = "**Explanation:**"
output_cypher, _, _ = output_cypher.partition(partition_by)
output_cypher = output_cypher.strip("`\n")
output_cypher = output_cypher.lstrip("cypher\n")
output_cypher = output_cypher.strip("`\n ")
return output_cypher
# Model
model_name = "neo4j/text2cypher-gemma-2-9b-it-finetuned-2024v1"
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
quantization_config=bnb_config,
torch_dtype=torch.bfloat16,
attn_implementation="eager",
low_cpu_mem_usage=True,
)
# Question
question = "What are the movies of Tom Hanks?"
schema = "(:Actor)-[:ActedIn]->(:Movie)" # Check the NOTE below on creating your own schemas
new_message = prepare_chat_prompt(question=question, schema=schema)
prompt = tokenizer.apply_chat_template(new_message, add_generation_prompt=True, tokenize=False)
inputs = tokenizer(prompt, return_tensors="pt", padding=True)
# Any other parameters
model_generate_parameters = {
"top_p": 0.9,
"temperature": 0.2,
"max_new_tokens": 512,
"do_sample": True,
"pad_token_id": tokenizer.eos_token_id,
}
inputs.to(model.device)
model.eval()
with torch.no_grad():
tokens = model.generate(**inputs, **model_generate_parameters)
tokens = tokens[:, inputs.input_ids.shape[1] :]
raw_outputs = tokenizer.batch_decode(tokens, skip_special_tokens=True)
outputs = [_postprocess_output_cypher(output) for output in raw_outputs]
print(outputs)
> ["MATCH (a:Actor {Name: 'Tom Hanks'})-[:ActedIn]->(m:Movie) RETURN m"]
neo4j-graphrag package::SchemaReader functionsThis model is a fine-tuned derivative of the Gemma base model. It is governed by the Gemma Terms of Use, including restrictions such as:
See the included GEMMA_LICENSE and NOTICE files for full details.