license: cc-by-nc-sa-4.0
Triplex:用于知识图谱构建的尖端大语言模型
知识图谱(如微软的Graph RAG)能增强RAG方法,但构建成本高昂。Triplex可将知识图谱创建成本降低98%,以GPT-4六十分之一的价格实现更优性能,并通过SciPhi的R2R框架支持本地图谱构建。
Triplex是SciPhi.AI基于Phi3-3.8B微调的模型,专为从非结构化数据构建知识图谱设计。其工作原理是从文本或其他数据源提取三元组——由主语、谓语和宾语构成的简单陈述。

性能基准

使用方式
import json
from transformers import AutoModelForCausalLM, AutoTokenizer
def triplextract(model, tokenizer, text, entity_types, predicates):
input_format = """执行命名实体识别(NER)并从文本中提取知识图谱三元组。NER用于识别给定实体类型的命名实体,三元组提取则通过指定谓词识别实体间关系。
**实体类型:**
{entity_types}
**谓词:**
{predicates}
**文本:**
{text}
"""
message = input_format.format(
entity_types = json.dumps({"entity_types": entity_types}),
predicates = json.dumps({"predicates": predicates}),
text = text)
messages = [{'role': 'user', 'content': message}]
input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt = True, return_tensors="pt").to("cuda")
output = tokenizer.decode(model.generate(input_ids=input_ids, max_length=2048)[0], skip_special_tokens=True)
return output
model = AutoModelForCausalLM.from_pretrained("sciphi/triplex", trust_remote_code=True).to('cuda').eval()
tokenizer = AutoTokenizer.from_pretrained("sciphi/triplex", trust_remote_code=True)
entity_types = [ "位置", "职位", "日期", "城市", "国家", "数字" ]
predicates = [ "人口", "面积" ]
text = """
旧金山[24]正式名称为旧金山市县,是北加州的商业、金融和文化中心。
截至2022年,旧金山常住人口为808,437人,是加利福尼亚州人口第四大城市,仅次于洛杉矶、圣地亚哥和圣何塞。
"""
prediction = triplextract(model, tokenizer, text, entity_types, predicates)
print(prediction)
商业使用条款
我们致力于让Triplex广泛可用,但作为初创机构仍需考虑商业因素。研究和个人使用不受限制,但对商业用途设有以下条款:
模型权重采用cc-by-nc-sa-4.0许可,但对最近12个月总收入低于500万美元的组织免除限制。如需解除GPL许可要求(双重许可)或超收入限额的商业使用,请联系founders@sciphi.ai。
引用文献
@misc{pimpalgaonkar2024triplex,
author = {皮姆帕尔冈卡, 施雷亚斯 and 特雷梅林, 诺兰 and 科尔格罗夫, 欧文},
title = {Triplex:用于知识图谱构建的尖端大语言模型},
year = {2024},
url = {https://huggingface.co/sciphi/triplex}
}