从任何文本中提取知识图谱的AI工具kg-gen

项目简介

kg-gen 帮助您从任何纯文本中提取知识图谱,使用 AI。它可以处理小型和大型文本输入,还可以处理对话格式的消息。
为什么生成知识图谱? kg-gen 如果你想:

  • 创建一个图来辅助 RAG(检索增强生成)

  • 创建用于模型训练和测试的图合成数据

  • 将任何文本结构化为图

  • 分析源文本中概念之间的关系

我们通过 LiteLLM 支持基于 API 和本地模型提供商,包括 OpenAI、Ollama、Anthropic、Gemini、Deepseek 等,还使用 DSPy 进行结构化输出生成。 尝试通过运行 tests/ 中的脚本来试用。 运行我们的 KG 基准测试 MINE 的说明在 MINE/
阅读论文:KGGen:使用语言模型从纯文本中提取知识图谱Quick

快速开始

安装模块:

pip install kg-gen

然后导入并使用 kg-gen 。您可以以两种格式之一提供您的文本输入:

A single string 消息对象列表(每个对象具有角色和内容)
以下是一些示例片段:

from kg_gen import KGGen``   ``# Initialize KGGen with optional configuration``kg = KGGen(`  `model="openai/gpt-4o",  # Default model`  `temperature=0.0,        # Default temperature`  `api_key="YOUR_API_KEY"  # Optional if set in environment``)``   ``# EXAMPLE 1: Single string with context``text_input = "Linda is Josh's mother. Ben is Josh's brother. Andrew is Josh's father."``graph_1 = kg.generate(`  `input_data=text_input,`  `context="Family relationships"``)``# Output:` `# entities={'Linda', 'Ben', 'Andrew', 'Josh'}` `# edges={'is brother of', 'is father of', 'is mother of'}` `# relations={('Ben', 'is brother of', 'Josh'),` `#           ('Andrew', 'is father of', 'Josh'),` `#           ('Linda', 'is mother of', 'Josh')}``   ``# EXAMPLE 2: Large text with chunking and clustering``with open('large_text.txt', 'r') as f:`  `large_text = f.read()``   ``# Example input text:``# """``# Neural networks are a type of machine learning model. Deep learning is a subset of machine learning``# that uses multiple layers of neural networks. Supervised learning requires training data to learn``# patterns. Machine learning is a type of AI technology that enables computers to learn from data.``# AI, also known as artificial intelligence, is related to the broader field of artificial intelligence.``# Neural nets (NN) are commonly used in ML applications. Machine learning (ML) has revolutionized``# many fields of study.``# ...``# """``   ``graph_2 = kg.generate(`  `input_data=large_text,`  `chunk_size=5000,  # Process text in chunks of 5000 chars`  `cluster=True      # Cluster similar entities and relations``)``# Output:``# entities={'neural networks', 'deep learning', 'machine learning', 'AI', 'artificial intelligence',` `#          'supervised learning', 'unsupervised learning', 'training data', ...}` `# edges={'is type of', 'requires', 'is subset of', 'uses', 'is related to', ...}` `# relations={('neural networks', 'is type of', 'machine learning'),``#           ('deep learning', 'is subset of', 'machine learning'),``#           ('supervised learning', 'requires', 'training data'),``#           ('machine learning', 'is type of', 'AI'),``#           ('AI', 'is related to', 'artificial intelligence'), ...}``# entity_clusters={``#   'artificial intelligence': {'AI', 'artificial intelligence'},``#   'machine learning': {'machine learning', 'ML'},``#   'neural networks': {'neural networks', 'neural nets', 'NN'}``#   ...``# }``# edge_clusters={``#   'is type of': {'is type of', 'is a type of', 'is a kind of'},``#   'is related to': {'is related to', 'is connected to', 'is associated with'``#  ...}``# }``   ``# EXAMPLE 3: Messages array``messages = [`  `{"role": "user", "content": "What is the capital of France?"},`   `{"role": "assistant", "content": "The capital of France is Paris."}``]``graph_3 = kg.generate(input_data=messages)``# Output:` `# entities={'Paris', 'France'}` `# edges={'has capital'}` `# relations={('France', 'has capital', 'Paris')}``   ``# EXAMPLE 4: Combining multiple graphs``text1 = "Linda is Joe's mother. Ben is Joe's brother."``   ``# Input text 2: also goes by Joe."``text2 = "Andrew is Joseph's father. Judy is Andrew's sister. Joseph also goes by Joe."``   ``graph4_a = kg.generate(input_data=text1)``graph4_b = kg.generate(input_data=text2)``   ``# Combine the graphs``combined_graph = kg.aggregate([graph4_a, graph4_b])``   ``# Optionally cluster the combined graph``clustered_graph = kg.cluster(`  `combined_graph,`  `context="Family relationships"``)``# Output:``# entities={'Linda', 'Ben', 'Andrew', 'Joe', 'Joseph', 'Judy'}` `# edges={'is mother of', 'is father of', 'is brother of', 'is sister of'}` `# relations={('Linda', 'is mother of', 'Joe'),``#           ('Ben', 'is brother of', 'Joe'),``#           ('Andrew', 'is father of', 'Joe'),``#           ('Judy', 'is sister of', 'Andrew')}``# entity_clusters={``#   'Joe': {'Joe', 'Joseph'},``#   ...``# }``# edge_clusters={ ... }

功能

大文本分块
对于长文本,您可以指定一个 chunk_size 参数以将文本分块处理:

graph = kg.generate(`  `input_data=large_text,`  `chunk_size=5000  # Process in chunks of 5000 characters``)

聚类相似实体和关系

您可以聚类相似实体和关系,无论是在生成过程中还是之后:

# During generation``graph = kg.generate(`  `input_data=text,`  `cluster=True,`  `context="Optional context to guide clustering"``)``   ``# Or after generation``clustered_graph = kg.cluster(`  `graph,`  `context="Optional context to guide clustering"``)

聚合多个图

您可以使用聚合方法组合多个图表:

graph1 = kg.generate(input_data=text1)``graph2 = kg.generate(input_data=text2)``combined_graph = kg.aggregate([graph1, graph2])

消息数组处理

处理消息数组时,kg-gen:

  1. 保留每条消息的角色信息

  2. 维护消息顺序和边界

  3. 能提取实体和关系:

  • 消息中提到的概念之间

  • 演讲者(角色)与概念之间

  • 在对话中的多条消息

例如,给定这个对话:

messages = [`  `{"role": "user", "content": "What is the capital of France?"},`  `{"role": "assistant", "content": "The capital of France is Paris."}``]

生成的图形可能包括以下实体:

  "user"  

 "assistant"  
  • “France”

  • “Paris”

并且关系如下:

(user, “asks about”, “France”)

(assistant, “states”, “Paris”)

(Paris, “is capital of”, “France”)

API 参考

KGGen 类

构造函数参数

model : str = “openai/gpt-4o” - 使用的生成模型

  • temperature : 浮点数 = 0.0 - 模型采样的温度

  • api_key : Optional[str] = None - 模型访问的 API 密钥

生成()方法参数
Union[str, List[Dict]] - 文本字符串或消息字典列表
  • model : Optional[str] - 覆盖默认模型

  • api_key : Optional[str] - 覆盖默认 API 密钥

  • context : str = “” - 数据上下文描述

  • chunk_size : 可选[int] - 处理文本块的大小

    `cluster` : 布尔型 = False - 是否在生成后对图进行聚类

  • temperature : Optional[float] - 覆盖默认温度

  • output_folder:可选的路径以保存部分进度

cluster() 方法参数

  • graph 聚类图

  • context : str = “” - 数据上下文描述

  • model : Optional[str] - 覆盖默认模型

  • temperature : Optional[float] - 覆盖默认温度

  • api_key : Optional[str] - 覆盖默认 API 密钥

aggregate() 方法参数

graphs : 图列表 - 要组合的图列表

项目链接

http://github.com/stair-lab/kg-gen

如何学习大模型 AI ?

由于新岗位的生产效率,要优于被取代岗位的生产效率,所以实际上整个社会的生产效率是提升的。

但是具体到个人,只能说是:

“最先掌握AI的人,将会比较晚掌握AI的人有竞争优势”。

这句话,放在计算机、互联网、移动互联网的开局时期,都是一样的道理。

我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。

我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

在这里插入图片描述

第一阶段(10天):初阶应用

该阶段让大家对大模型 AI有一个最前沿的认识,对大模型 AI 的理解超过 95% 的人,可以在相关讨论时发表高级、不跟风、又接地气的见解,别人只会和 AI 聊天,而你能调教 AI,并能用代码将大模型和业务衔接。

  • 大模型 AI 能干什么?
  • 大模型是怎样获得「智能」的?
  • 用好 AI 的核心心法
  • 大模型应用业务架构
  • 大模型应用技术架构
  • 代码示例:向 GPT-3.5 灌入新知识
  • 提示工程的意义和核心思想
  • Prompt 典型构成
  • 指令调优方法论
  • 思维链和思维树
  • Prompt 攻击和防范

第二阶段(30天):高阶应用

该阶段我们正式进入大模型 AI 进阶实战学习,学会构造私有知识库,扩展 AI 的能力。快速开发一个完整的基于 agent 对话机器人。掌握功能最强的大模型开发框架,抓住最新的技术进展,适合 Python 和 JavaScript 程序员。

  • 为什么要做 RAG
  • 搭建一个简单的 ChatPDF
  • 检索的基础概念
  • 什么是向量表示(Embeddings)
  • 向量数据库与向量检索
  • 基于向量检索的 RAG
  • 搭建 RAG 系统的扩展知识
  • 混合检索与 RAG-Fusion 简介
  • 向量模型本地部署

第三阶段(30天):模型训练

恭喜你,如果学到这里,你基本可以找到一份大模型 AI相关的工作,自己也能训练 GPT 了!通过微调,训练自己的垂直大模型,能独立训练开源多模态大模型,掌握更多技术方案。

到此为止,大概2个月的时间。你已经成为了一名“AI小子”。那么你还想往下探索吗?

  • 为什么要做 RAG
  • 什么是模型
  • 什么是模型训练
  • 求解器 & 损失函数简介
  • 小实验2:手写一个简单的神经网络并训练它
  • 什么是训练/预训练/微调/轻量化微调
  • Transformer结构简介
  • 轻量化微调
  • 实验数据集的构建

第四阶段(20天):商业闭环

对全球大模型从性能、吞吐量、成本等方面有一定的认知,可以在云端和本地等多种环境下部署大模型,找到适合自己的项目/创业方向,做一名被 AI 武装的产品经理。

  • 硬件选型
  • 带你了解全球大模型
  • 使用国产大模型服务
  • 搭建 OpenAI 代理
  • 热身:基于阿里云 PAI 部署 Stable Diffusion
  • 在本地计算机运行大模型
  • 大模型的私有化部署
  • 基于 vLLM 部署大模型
  • 案例:如何优雅地在阿里云私有部署开源大模型
  • 部署一套开源 LLM 项目
  • 内容安全
  • 互联网信息服务算法备案

学习是一个过程,只要学习就会有挑战。天道酬勤,你越努力,就会成为越优秀的自己。

如果你能在15天内完成所有的任务,那你堪称天才。然而,如果你能完成 60-70% 的内容,你就已经开始具备成为一名大模型 AI 的正确特征了。

这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值