项目简介
llmgraph 使您能够从给定的源实体维基百科页面创建 GraphML、GEXF 和 HTML 格式(通过 pyvis 生成)的知识图。知识图谱是通过从 ChatGPT 或 LiteLLM 支持的其他大型语言模型 (LLMs) 中提取世界知识来生成的。
有关知识图的背景,请参阅 Computerphile 的 YouTube 概述
特征
-
给定源实体,创建知识图。
-
使用ChatGPT(或另一个指定的LLM)来提取世界知识。
-
生成 HTML、GraphML 和 GEXF 格式的知识图。
-
自定义提示支持许多实体类型和关系。
-
缓存支持有效地迭代增长知识图。
-
输出
total tokens
用于了解LLM成本(即使默认运行仅为约1美分)。
-
可定制模型(默认为 OpenAI
gpt-3.5-turbo
以提高速度和成本)。
安装
您可以使用 pip 安装 llmgraph,最好安装到 Python 虚拟环境中:
或者,查看使用 llmgraph 的示例笔记本,您可以直接在 Google Colab 中运行。
示例输出
除了 GraphML 和 GEXF 格式之外,还可以查看 HTML pyvis 物理启用图:
人工智能示例
llmgraph 用法
用法示例
上面的示例是使用以下命令生成的,该命令需要 entity_type 和带引号的 entity_wikipedia 源 URL:
llmgraph machine-learning "https://en.wikipedia.org/wiki/Artificial_intelligence" --levels 3
此示例根据给定的起始节点 Artificial Intelligence 创建一个 3 级图。
默认情况下使用 OpenAI,您需要在运行之前设置环境变量“ OPENAI_API_KEY ”。有关更多信息,请参阅 OpenAI 文档。 total tokens used 随着运行的进行而输出。作为参考,这个 3 级示例总共使用了 7,650 个 gpt-3.5-turbo 代币,截至 2023 年 10 月约为 1.5 美分。
您还可以指定不同的 LLM 提供程序,包括使用本地 ollama 模型运行。您应该能够指定 LiteLLM 支持的任何内容,如下所述:https://docs.litellm.ai/docs/providers。请注意,提取相关实体的提示已使用 OpenAI 进行测试,可能不适用于其他模型。
本地 ollama/llama2 模型示例:
llmgraph machine-learning "https://en.wikipedia.org/wiki/Artificial_intelligence" --levels 3 --llm-model ollama/llama2 --llm-base-url http://localhost:
entity_type 设置用于查找要包含在图表中的相关实体的 LLM 提示。完整列表可以在promps.yaml中看到,包括以下实体类型:
-
automobile
-
book
-
computer-game
-
concepts-general
-
creative-general
-
documentary
-
food
-
machine-learning
-
movie
-
music
-
people-historical
-
podcast
-
software-engineering
-
tv
必需的参数
可选参数
-
--entity-root (文本):可选的根实体名称覆盖(如果与维基百科页面标题不同)[默认值:无]
-
--levels (整数):从中央根实体构建的深度级别数[默认值:2]
-
--max-sum-total-tokens (INTEGER):图形生成的最大标记总和[默认值:200000]
-
--output-folder (文本):写入输出的文件夹位置[默认值:./_output/]
-
--llm-model (文本):型号名称[默认:gpt-3.5-turbo]
-
--llm-temp (FLOAT): LLM 温度值 [默认值: 0.0]
-
--llm-base-url (文本):LLM 将使用自定义基本 URL,而不是自动
URL [默认值:无]
-
--version :显示llmgraph版本并退出。
-
--help :显示此消息并退出。
更多 HTML 输出示例
以下是不同实体类型和根实体的 HTML 图形输出的更多示例(带有生成命令和查看完整交互式图形的链接)。
安装 llmgraph 来创建您自己的知识图谱!请随意在上面的问题部分中使
知识图概念示例
Command to generate above concepts-general graph:
llmgraph concepts-general "https://en.wikipedia.org/wiki/Knowledge_graph" --levels 4
View entire graph: concepts-general_knowledge-graph_v1.0.0_level4_fully_connected.html
盗梦空间电影示例
Command to generate above movie graph:
llmgraph movie "https://en.wikipedia.org/wiki/Inception" --levels 4
View entire graph: movie_inception_v1.0.0_level4_fully_connected.html
OpenAI公司示例
Command to generate above company graph:
llmgraph company "https://en.wikipedia.org/wiki/OpenAI" --levels 4
View entire graph: company_openai_v1.0.0_level4_fully_connected.html
约翰·冯·诺依曼人的例子
Command to generate above people-historical graph:
llmgraph people-historical "https://en.wikipedia.org/wiki/John_von_Neumann" --levels 4
View entire graph: people-historical_john-von-neumann_v1.0.0_level4_fully_connected.html
用于生成图形的提示示例
以下是带有占位符的提示模板示例,用于从给定源实体生成相关实体。这会递归地应用来创建知识图,并根据需要合并重复的节点。
You are knowledgeable about {knowledgeable_about}.
List, in json array format, the top {top_n} {entities} most like '{{entity_root}}'
with Wikipedia link, reasons for similarity, similarity on scale of 0 to 1.
Format your response in json array format as an array with column names: 'name', 'wikipedia_link', 'reason_for_similarity', and 'similarity'.
Example response: {{{{"name": "Example {entity}","wikipedia_link": "https://en.wikipedia.org/wiki/Example_{entity_underscored}","reason_for_similarity": "Reason for similarity","similarity": 0.5}}}}
它在主要测试的 LLM(OpenAI gpt-3.5-turbo)上运行良好。结果还可以,但使用 Llama2 时效果不佳。提示的真实来源和其他详细信息可以在promps.yaml中查看。
每个实体类型都有自定义占位符,例如 concepts-general 和 documentary :
concepts-general:
system: You are a highly knowledgeable ontologist and creator of knowledge graphs.
knowledgeable_about: many concepts and ontologies.
entities: concepts
entity: concept name
top_n: 5
documentary:
system: You are knowledgeable about documentaries of all types, and genres.
knowledgeable_about: documentaries of all types, and genres
entities: Documentaries
entity: Documentary
top_n: 5
缓存的 LLM API 调用
对 LLM API(和维基百科)的每次调用都会本地缓存在 .joblib_cache 文件夹中。这允许恢复中断的运行,而无需重复相同的调用。它还允许使用更高的 --level 选项重新运行,以重用较低级别运行的结果(假设相同的实体类型和源)。
未来的改进