Text2cypher Gemma 2 9b It Finetuned 2024v1
该模型是基于google/gemma-2-9b-it微调的Text2Cypher模型,能够将自然语言问题转换为Neo4j图数据库的Cypher查询语句。
下载量 2,093
发布时间 : 9/10/2024
模型简介
该模型展示了如何利用Neo4j-Text2Cypher(2024)数据集对基础模型进行微调,以提升Text2Cypher任务的性能。主要用于将自然语言问题转换为Cypher查询语句。
模型特点
高效的自然语言到Cypher转换
能够准确地将自然语言问题转换为有效的Cypher查询语句
LoRA微调技术
使用参数高效微调技术(LoRA)进行模型适配,保持基础模型能力的同时提升特定任务表现
4-bit量化支持
支持4-bit量化推理,降低硬件资源需求
模型能力
自然语言理解
Cypher查询生成
图数据库交互
使用案例
图数据库查询
演员电影查询
查询特定演员参演的所有电影
生成正确的MATCH (a:Actor)-[:ActedIn]->(m:Movie) RETURN m查询
复杂关系查询
查询满足特定条件的复杂关系路径
根据模式生成多跳查询语句
数据分析
图数据统计
生成统计图数据特征的查询
生成包含COUNT、SUM等聚合函数的查询
🚀 文本到Cypher生成模型
本模型展示了如何使用Neo4j-Text2Cypher(2024)数据集微调基础模型,以提升文本到Cypher任务的性能。这是正在进行的研究和探索的一部分,旨在凸显该数据集的潜力,而非提供一个可用于生产的解决方案。
🚀 快速开始
你可以使用以下代码示例开始使用该模型:
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-Text2Cypher(2024)数据集微调基础模型,以提升文本到Cypher任务性能的方法。
- 这是正在进行的研究和探索的一部分,旨在凸显该数据集的潜力。
📦 安装指南
文档未提及安装步骤,故跳过此章节。
💻 使用示例
基础用法
# 基础用法代码示例
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-Text2Cypher(2024)数据集微调基础模型,以提升文本到Cypher任务的性能。需要注意的是,这是正在进行的研究和探索的一部分,旨在凸显该数据集的潜力,而非提供一个可用于生产的解决方案。
基础模型:google/gemma-2-9b-it 数据集:neo4j/text2cypher-2024v1
微调模型的概述和基准测试结果可在Link1和Link2查看。
如果你有想法或见解,请联系我们:Neo4j/Team-GenAI
偏差、风险和局限性
我们需要注意以下几点风险:
- 在我们的评估设置中,训练集和测试集来自相同的数据分布(从更大的数据集中采样)。如果数据分布发生变化,结果可能不会遵循相同的模式。
- 所使用的数据集是从公开可用的来源收集的。随着时间的推移,基础模型可能会访问训练集和测试集,从而可能获得相似甚至更好的结果。
另请查看相关博客文章:Link
训练详情
训练过程
使用了RunPod,并进行了以下设置:
- 1 x A100 PCIe
- 31 vCPU 117 GB RAM
- runpod/pytorch:2.4.0-py3.11-cuda12.4.1-devel-ubuntu22.04
- On-Demand - Secure Cloud
- 60 GB Disk
- 60 GB Pod Volume
训练超参数
- lora_config = LoraConfig( r=64, lora_alpha=64, target_modules=target_modules, lora_dropout=0.05, bias="none", task_type="CAUSAL_LM", )
- sft_config = SFTConfig( dataset_text_field=dataset_text_field, per_device_train_batch_size=4, gradient_accumulation_steps=8, dataset_num_proc=16, max_seq_length=1600, logging_dir="./logs", num_train_epochs=1, learning_rate=2e-5, save_steps=5, save_total_limit=1, logging_steps=5, output_dir="outputs", optim="paged_adamw_8bit", save_strategy="steps", )
- bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, )
框架版本
- PEFT 0.12.0
关于创建自己的模式的注意事项
- 在我们使用的数据集中,模式已经提供。它们可以通过以下方式创建:
- 直接使用输入数据源提供的模式;
- 使用neo4j-graphrag包创建模式(请查看:SchemaReader.get_schema(...)函数)。
- 在你自己的Neo4j数据库中,你可以使用
neo4j-graphrag package::SchemaReader
函数。
🔧 技术细节
文档未提供足够的技术实现细节(未超过50字的具体技术说明),故跳过此章节。
📄 许可证
本模型使用的许可证为apache-2.0。
Rebel Large
REBEL是一种基于BART的序列到序列模型,用于端到端关系抽取,支持200多种不同关系类型。
知识图谱
Transformers 英语

R
Babelscape
37.57k
219
Nel Mgenre Multilingual
基于mGENRE的多语言生成式实体检索模型,针对历史文本优化,支持100+种语言,特别适配法语、德语和英语的历史文档实体链接。
知识图谱
Transformers 支持多种语言

N
impresso-project
17.13k
2
Biomednlp KRISSBERT PubMed UMLS EL
MIT
KRISSBERT是一个基于知识增强自监督学习的生物医学实体链接模型,通过利用无标注文本和领域知识训练上下文编码器,有效解决实体名称多样性变异和歧义性问题。
知识图谱
Transformers 英语

B
microsoft
4,643
29
Coder Eng
Apache-2.0
CODER是一种知识增强型跨语言医学术语嵌入模型,专注于医学术语规范化任务。
知识图谱
Transformers 英语

C
GanjinZero
4,298
4
Umlsbert ENG
Apache-2.0
CODER是一个基于知识注入的跨语言医学术语嵌入模型,专注于医学术语标准化任务。
知识图谱
Transformers 英语

U
GanjinZero
3,400
13
Text2cypher Gemma 2 9b It Finetuned 2024v1
Apache-2.0
该模型是基于google/gemma-2-9b-it微调的Text2Cypher模型,能够将自然语言问题转换为Neo4j图数据库的Cypher查询语句。
知识图谱
Safetensors 英语
T
neo4j
2,093
22
Triplex
Triplex是SciPhi.AI基于Phi3-3.8B微调的模型,专为从非结构化数据构建知识图谱设计,可将知识图谱创建成本降低98%。
知识图谱
T
SciPhi
1,808
278
Genre Linking Blink
GENRE是一种基于序列到序列方法的实体检索系统,采用微调后的BART架构,通过受约束的束搜索技术生成唯一实体名称。
知识图谱 英语
G
facebook
671
10
Text To Cypher Gemma 3 4B Instruct 2025.04.0
Gemma 3.4B IT 是一个基于文本到文本生成的大语言模型,专门用于将自然语言转换为Cypher查询语言。
知识图谱
Safetensors
T
neo4j
596
2
Mrebel Large
REDFM是REBEL的多语言版本,用于多语言关系抽取任务,支持18种语言的关系三元组提取。
知识图谱
Transformers 支持多种语言

M
Babelscape
573
71
精选推荐AI模型
Llama 3 Typhoon V1.5x 8b Instruct
专为泰语设计的80亿参数指令模型,性能媲美GPT-3.5-turbo,优化了应用场景、检索增强生成、受限生成和推理任务
大型语言模型
Transformers 支持多种语言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一个基于SODA数据集训练的超小型对话模型,专为边缘设备推理设计,体积仅为Cosmo-3B模型的2%左右。
对话系统
Transformers 英语

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基于RoBERTa架构的中文抽取式问答模型,适用于从给定文本中提取答案的任务。
问答系统 中文
R
uer
2,694
98