iText2KG:使用LLM构建增量知识图谱(KG)
liuian 2024-12-08 16:19 30 浏览
iText2KG
一种由 LLM 驱动的零样本方法,使用大型语言模型构建增量知识图谱(KG)
iText2KG 是一个 Python 包,通过利用大型语言模型从文本文档中提取实体和关系,逐步构建具有已解析实体和关系的一致知识图谱。
它具有零样本能力,无需专门的训练即可跨各个领域提取知识。
它包含四个模块:文档提炼器、增量实体提取器、增量关系提取器和图形集成器与可视化。
- 文档提取器:此模块将原始文档重新表述为预定义的语义块,并由指导 LLM 提取特定信息的模式引导。
- 增量实体提取器:此模块识别并解析语义块内的唯一语义实体,确保实体之间的清晰度和区别。
- 增量关系提取器:此组件处理已解析的实体以检测语义上唯一的关系,解决语义重复的挑战。
- Neo4j 图形集成器:最后一个模块以图形格式可视化关系和实体,利用 Neo4j 进行有效表示。
对于我们的 iText2KG 它包含了两大特点
- 增量构建:iText2KG 允许增量构建 KG,这意味着它可以在新数据可用时不断更新和扩展图,而无需进行大量重新处理。
- 零样本学习:该框架利用 LLM 的零样本功能,使其无需预定义集或外部本体即可运行。这种灵活性使其能够适应各种 KG 构建场景,而无需进行大量训练或微调。
一 、设置模型
在运行 iText2KG 之前,我们先设置好大模型,我这里选择的是 OpenAi 的模型以及 HuggingFace 的 bge-large-zh embedding 模型。这么选择也是考虑到构建 KG 的准确度。
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
import os
os.environ["OPENAI_API_KEY"] = "*****"
openai_api_key = os.environ["OPENAI_API_KEY"]
openai_llm_model = llm = ChatOpenAI(
model="gpt-4o-mini",
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
)
messages = [
(
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
),
("human", "I love programming."),
]
ai_msg=openai_llm_model.invoke(messages)
开始部署我们的 Embedding 模型:
from langchain_huggingface.embeddings import HuggingFaceEmbeddings
openai_embeddings_model = HuggingFaceEmbeddings(model_name="BAAI/bge-large-zh-v1.5")
text = "This is a test document."
query_result = openai_embeddings_model.embed_query(text)
query_result[:3]
doc_result = openai_embeddings_model.embed_documents([text])
二 、使用 iText2KG 构建 KG
我们这里的场景是,给出一篇简历,使用知识图谱将在线职位描述与生成的简历联系起来。
设定目标是评估候选人是否适合这份工作。
我们可以为 iText2KG 的每个模块使用不同的 LLM 或嵌入模型。但是,重要的是确保节点和关系嵌入的维度在各个模型之间保持一致。
如果嵌入维度不同,余弦相似度可能难以准确测量向量距离以进行进一步匹配。
我们的简历放到根目录,加载简历:
from langchain.document_loaders import PyPDFLoader
loader = PyPDFLoader(f"./CV_Emily_Davis.pdf")
pages = loader.load_and_split()
初始化 DocumentDistiller 引入 llm :
from itext2kg.documents_distiller import DocumentsDisiller, CV
document_distiller = DocumentsDisiller(llm_model = openai_llm_model)
信息提炼:
IE_query = '''
# DIRECTIVES :
- Act like an experienced information extractor.
- You have a chunk of a CV.
- If you do not find the right information, keep its place empty.
'''
# 使用定义好的查询和输出数据结构提炼文档。
distilled_cv = document_distiller.distill(documents=[page.page_content.replace("{", '[').replace("}", "]") for page in pages], IE_query=IE_query, output_data_structure=CV)
将提炼后的文档格式化为语义部分。
semantic_blocks_cv = [f"{key} - {value}".replace("{", "[").replace("}", "]") for key, value in distilled_cv.items() if value !=[] and value != "" and value != None]
我们可以自定义输出数据结构,我们这里定义了4种,工作经历模型,岗位,技能,证书。
from pydantic import BaseModel, Field
from typing import List, Optional
class JobResponsibility(BaseModel):
description: str = Field(..., description="A specific responsibility in the job role")
class JobQualification(BaseModel):
skill: str = Field(..., description="A required or preferred skill for the job")
class JobCertification(BaseModel):
certification: str = Field(..., description="Required or preferred certifications for the job")
class JobOffer(BaseModel):
job_offer_title: str = Field(..., description="The job title")
company: str = Field(..., description="The name of the company offering the job")
location: str = Field(..., description="The job location (can specify if remote/hybrid)")
job_type: str = Field(..., description="Type of job (e.g., full-time, part-time, contract)")
responsibilities: List[JobResponsibility] = Field(..., description="List of key responsibilities")
qualifications: List[JobQualification] = Field(..., description="List of required or preferred qualifications")
certifications: Optional[List[JobCertification]] = Field(None, description="Required or preferred certifications")
benefits: Optional[List[str]] = Field(None, description="List of job benefits")
experience_required: str = Field(..., description="Required years of experience")
salary_range: Optional[str] = Field(None, description="Salary range for the position")
apply_url: Optional[str] = Field(None, description="URL to apply for the job")
定义一个招聘工作需求的描述:
job_offer = """
About the Job Offer
THE FICTITIOUS COMPANY
FICTITIOUS COMPANY is a high-end French fashion brand known for its graphic and poetic style, driven by the values of authenticity and transparency upheld by its creator Simon Porte Jacquemus.
Your Role
Craft visual stories that captivate, inform, and inspire. Transform concepts and ideas into visual representations. As a member of the studio, in collaboration with the designers and under the direction of the Creative Designer, you should be able to take written or spoken ideas and convert them into designs that resonate. You need to have a deep understanding of the brand image and DNA, being able to find the style and layout suited to each project.
Your Missions
Translate creative direction into high-quality silhouettes using Photoshop
Work on a wide range of projects to visualize and develop graphic designs that meet each brief
Work independently as well as in collaboration with the studio team to meet deadlines, potentially handling five or more projects simultaneously
Develop color schemes and renderings in Photoshop, categorized by themes, subjects, etc.
Your Profile
Bachelor’s degree (Bac+3/5) in Graphic Design or Art
3 years of experience in similar roles within a luxury brand's studio
Proficiency in Adobe Suite, including Illustrator, InDesign, Photoshop
Excellent communication and presentation skills
Strong organizational and time management skills to meet deadlines in a fast-paced environment
Good understanding of the design process
Freelance cont
继续使用上面方法做信息提炼:
IE_query = '''
# DIRECTIVES :
- Act like an experienced information extractor.
- You have a chunk of a job offer description.
- If you do not find the right information, keep its place empty.
'''
distilled_Job_Offer = document_distiller.distill(documents=[job_offer], IE_query=IE_query, output_data_structure=JobOffer)
print(distilled_Job_Offer)
semantic_blocks_job_offer = [f"{key} - {value}".replace("{", "[").replace("}", "]") for key, value in distilled_Job_Offer.items() if value !=[] and value != "" and value != None]
到这里准备工作完成,简历和工作需求都已经提炼完毕,然后正式开始构建 graph,我们将简历的所有语义块作为一个块传递给了 LLM。
也将工作需求作为另一个语义块传递,也可以在构建图时将语义块分开。
我们需要注意每个块中包含多少信息,然后好将它与其他块连接起来,我们在这里做的就是一次性传递所有语义块。
from itext2kg import iText2KG
itext2kg = iText2KG(llm_model = openai_llm_model, embeddings_model = openai_embeddings_model)
global_ent, global_rel = itext2kg.build_graph(sections=[semantic_blocks_cv], ent_threshold=0.6, rel_threshold=0.6)
global_ent_, global_rel_ = itext2kg.build_graph(sections=[semantic_blocks_job_offer], existing_global_entities = global_ent, existing_global_relationships = global_rel, ent_threshold=0.6, rel_threshold=0.6)
iText2KG 构建 KG 的过程我们看到有很多参数,下面分贝是对每个参数的表示做一些解释:
- llm_model:用于从文本中提取实体和关系的语言模型实例。
- embeddings_model:用于创建提取实体的向量表示的嵌入模型实例。
- sleep_time (int):遇到速率限制或错误时等待的时间(以秒为单位)(仅适用于 OpenAI)。默认为 5 秒。
iText2KG 的 build_graph 参数:
- sections (List[str]):字符串(语义块)列表,其中每个字符串代表文档的一部分,将从中提取实体和关系。
- existing_global_entities (List[dict], optional):与新提取的实体进行匹配的现有全局实体列表。每个实体都表示为一个字典。
- existing_global_relationships (List[dict], optional):与新提取的关系匹配的现有全局关系列表。每个关系都表示为一个字典。
- ent_threshold (float, optional):实体匹配的阈值,用于合并不同部分的实体。默认值为 0.7。
- rel_threshold (float, optional):关系匹配的阈值,用于合并不同部分的关系。默认值为 0.7。
从图中结果看到我们构建过程中的实体,和关联关系。
最后使用 GraphIntegrator 对构建的知识图谱进行可视化。
使用指定的凭据访问图形数据库 Neo4j,并对生成的图形进行可视化,以提供从文档中提取的关系和实体的视觉表示。
from itext2kg.graph_integration import GraphIntegrator
URI = "bolt://3.216.93.32:7687"
USERNAME = "neo4j"
PASSWORD = "selection-cosal-cubes"
new_graph = {}
new_graph["nodes"] = global_ent_
new_graph["relationships"] = global_rel_
GraphIntegrator(uri=URI, username=USERNAME, password=PASSWORD).visualize_graph(json_graph=new_graph)
打开我们的 Neo4j 图形数据库:
可以看到简历和工作需求的匹配关系连接。这样子我们可以灵活的运用 iText2KG 框架做图形关系和实体的增量。
三、总结
本文介绍了使用大型语言模型 ( LLM ) 构建增量知识图谱 ( KG ) 的 iText2KG 框架。一个强大的 KG 构建框架,该框架利用了 LLM 的优势,解决了该领域的重大挑战,并提出了一种模块化方法,可增强不同领域的灵活性和适用性。
- 增强的架构一致性:iText2KG 方法在各种文档类型中实现了高架构一致性,优于由于依赖预定义结构而经常难以保持一致性的传统方法。
- 实体和关系提取的高精度:该框架有效地缓解了与语义重复和未解决实体相关的问题,这些问题在传统方法中普遍存在。这导致更准确和可靠的 KG。
- 减少后处理需求:传统方法通常需要大量的后处理来解决歧义和冗余。iText2KG 通过采用结构化结构来最大限度地减少这种需求。
相关推荐
- python入门到脱坑函数—定义函数_如何定义函数python
-
Python函数定义:从入门到精通一、函数的基本概念函数是组织好的、可重复使用的代码块,用于执行特定任务。在Python中,函数可以提高代码的模块性和重复利用率。二、定义函数的基本语法def函数名(...
- javascript函数的call、apply和bind的原理及作用详解
-
javascript函数的call、apply和bind本质是用来实现继承的,专业点说法就是改变函数体内部this的指向,当一个对象没有某个功能时,就可以用这3个来从有相关功能的对象里借用过来...
- JS中 call()、apply()、bind() 的用法
-
其实是一个很简单的东西,认真看十分钟就从一脸懵B到完全理解!先看明白下面:例1obj.objAge;//17obj.myFun()//小张年龄undefined例2shows(...
- Pandas每日函数学习之apply函数_apply函数python
-
apply函数是Pandas中的一个非常强大的工具,它允许你对DataFrame或Series中的数据应用一个函数,可以是自定义的函数,也可以是内置的函数。apply可以作用于DataF...
- Win10搜索不习惯 换个设定就好了_window10搜索用不了怎么办
-
Windows10的搜索功能是真的方便,这点用惯了Windows10的小伙伴应该都知道,不过它有个小问题,就是Windows10虽然会自动联网搜索,但默认使用微软自家的Bing搜索引擎和Edge...
- 面试秘籍:call、bind、apply的区别,面试官为什么总爱问这三位?
-
引言你有没有发现,每次JavaScript面试,面试官总爱问你call、bind和apply的区别?好像这三个方法成了通关密码,掌握了它们,就能顺利过关。其实不难理解,面试官问这些问题,不...
- 记住这8招,帮你掌握“追拍“摄影技法—摄影早自习第422日
-
杨海英同学提问:请问叶梓老师,我练习追拍时,总也不能把运动的人物拍清晰,速度一般掌握在1/40-1/60,请问您如何把追拍拍的清晰?这跟不同的运动形式有关系吗?请您给讲讲要点,谢谢您!摄影:Damia...
- [Sony] 有点残酷的测试A7RII PK FS7
-
都是好机!手中利器!主要是最近天天研究fs5,想知道fs5与a7rii后期匹配问题,苦等朋友的fs5月底到货,于是先拿手里现有的fs7小测一下,十九八九也能看到fs5的影子,另外也了解一下fs5k标配...
- AndroidStudio_Android使用OkHttp发起Http请求
-
这个okHttp的使用,其实网络上有很多的案例的,但是,如果以前没用过,copy别人的直接用的话,可以发现要么导包导不进来,要么,人家给的代码也不完整,这里自己整理一下.1.引入OkHttp的jar...
- ESL-通过事件控制FreeSWITCH_es事务控制
-
通过事件提供的最底层控制机制,允许我们有效地利用工具箱,适时选择使用其中的单个工具。FreeSWITCH是一个核心交换与混合矩阵,它周围有几十个模块提供各种功能特性。我们完全控制了所有的即时信息,这些...
- 【调试】perf和火焰图_perf生成火焰图
-
简介perf是linux上的性能分析工具,perf可以对event进行统计得到event的发生次数,或者对event进行采样,得到每次event发生时的相关数据(cpu、进程id、运行栈等),利用这些...
- 文本检索控件也玩安卓?dtSearch Engine发布Android测试版
-
dtSearchEngineforLinux(原生64-bit/32-bitC++和JavaAPIs)和dtSearchEngineforWin&.NET(原生64-bi...
- 网站后台莫名增加N个管理员,记一次SQL注入攻击
-
网站没流量,但却经常被SQL注入光顾。最近,网站真的很奇怪,网站后台不光莫名多了很多“管理员”,所有的Wordpres插件还会被自动暂停,导致一些插件支持的页面,如WooCommerce无法正常访问、...
- 多元回归树分析Multivariate Regression Trees,MRT
-
多元回归树(MultivariateRegressionTrees,MRT)是单元回归树的拓展,是一种对一系列连续型变量递归划分成多个类群的聚类方法,是在决策树(decision-trees)基础...
- JMETER性能测试_JMETER性能测试指标
-
jmeter为性能测试提供了一下特色:jmeter可以对测试静态资源(例如js、html等)以及动态资源(例如php、jsp、ajax等等)进行性能测试jmeter可以挖掘出系统最大能处...
- 一周热门
-
-
【验证码逆向专栏】vaptcha 手势验证码逆向分析
-
Python实现人事自动打卡,再也不会被批评
-
Psutil + Flask + Pyecharts + Bootstrap 开发动态可视化系统监控
-
一个解决支持HTML/CSS/JS网页转PDF(高质量)的终极解决方案
-
再见Swagger UI 国人开源了一款超好用的 API 文档生成框架,真香
-
网页转成pdf文件的经验分享 网页转成pdf文件的经验分享怎么弄
-
C++ std::vector 简介
-
飞牛OS入门安装遇到问题,如何解决?
-
系统C盘清理:微信PC端文件清理,扩大C盘可用空间步骤
-
10款高性能NAS丨双十一必看,轻松搞定虚拟机、Docker、软路由
-
- 最近发表
-
- python入门到脱坑函数—定义函数_如何定义函数python
- javascript函数的call、apply和bind的原理及作用详解
- JS中 call()、apply()、bind() 的用法
- Pandas每日函数学习之apply函数_apply函数python
- Win10搜索不习惯 换个设定就好了_window10搜索用不了怎么办
- 面试秘籍:call、bind、apply的区别,面试官为什么总爱问这三位?
- 记住这8招,帮你掌握“追拍“摄影技法—摄影早自习第422日
- [Sony] 有点残酷的测试A7RII PK FS7
- AndroidStudio_Android使用OkHttp发起Http请求
- ESL-通过事件控制FreeSWITCH_es事务控制
- 标签列表
-
- python判断字典是否为空 (50)
- crontab每周一执行 (48)
- aes和des区别 (43)
- bash脚本和shell脚本的区别 (35)
- canvas库 (33)
- dataframe筛选满足条件的行 (35)
- gitlab日志 (33)
- lua xpcall (36)
- blob转json (33)
- python判断是否在列表中 (34)
- python html转pdf (36)
- 安装指定版本npm (37)
- idea搜索jar包内容 (33)
- css鼠标悬停出现隐藏的文字 (34)
- linux nacos启动命令 (33)
- gitlab 日志 (36)
- adb pull (37)
- python判断元素在不在列表里 (34)
- python 字典删除元素 (34)
- vscode切换git分支 (35)
- python bytes转16进制 (35)
- grep前后几行 (34)
- hashmap转list (35)
- c++ 字符串查找 (35)
- mysql刷新权限 (34)