基础模型: google/gemma-2-9b-it
库名称: peft
许可证: apache-2.0
数据集:
- neo4j/text2cypher-2024v1
语言:
- en
管道标签: text2text-generation
标签:
- neo4j
- cypher
- text2cypher
模型卡片
模型详情
模型描述
该模型展示了如何利用Neo4j-Text2Cypher(2024)数据集(链接)对基础模型进行微调,以提升Text2Cypher任务的性能。
请注意,这是正在进行的研究和探索的一部分,旨在突出数据集的潜力,而非生产就绪的解决方案。
基础模型: google/gemma-2-9b-it
数据集: neo4j/text2cypher-2024v1
微调模型概览和基准测试结果分享于链接1和链接2
如有想法或见解,请联系我们:Neo4j/Team-GenAI
偏差、风险和限制
我们需要注意以下几点风险:
- 在我们的评估设置中,训练集和测试集来自相同的数据分布(从更大的数据集中采样)。如果数据分布发生变化,结果可能不会遵循相同的模式。
- 使用的数据集是从公开可用的来源收集的。随着时间的推移,基础模型可能会访问训练集和测试集,可能实现类似甚至更好的结果。
另请参阅相关博客文章:链接
训练详情
训练过程
使用RunPod进行训练,配置如下:
- 1 x A100 PCIe
- 31 vCPU 117 GB RAM
- runpod/pytorch:2.4.0-py3.11-cuda12.4.1-devel-ubuntu22.04
- 按需 - 安全云
- 60 GB磁盘
- 60 GB Pod卷
训练超参数
- 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,
)
框架版本
Cypher生成示例
from peft import PeftModel, PeftConfig
import torch
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
)
instruction = (
"生成查询图数据库的Cypher语句。"
"仅使用模式中提供的关系类型和属性。\n"
"模式: {schema} \n 问题: {question} \n Cypher输出: "
)
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:
partition_by = "**解释:**"
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_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 = "汤姆·汉克斯的电影有哪些?"
schema = "(:Actor)-[:ActedIn]->(:Movie)" # 关于创建自己的模式的注意事项见下文
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)
# 其他参数
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包创建模式(参见:SchemaReader.get_schema(...)函数)
- 在您自己的Neo4j数据库中,可以利用
neo4j-graphrag包::SchemaReader
函数