赞
踩
创建知识图谱通常涉及到专业的复杂任务,但通过利用Llama Index(LLM)、KnowledgeGraphIndex和GraphStore,我们可以从任何Llama Hub支持的数据源中创建一个相对高效的知识图谱。此外,查询知识图谱通常需要与存储系统相关的特定领域知识(如Cypher),但在LLM和LlamaIndex KnowledgeGraphQueryEngine的帮助下,我们可以用自然语言来完成这些任务!
本文将演示以下步骤:
%pip install llama-index-readers-wikipedia
%pip install llama-index-llms-azure-openai
%pip install llama-index-graph-stores-nebula
%pip install llama-index-llms-openai
%pip install llama-index-embeddings-azure-openai
%pip install llama-index
import os
import logging
import sys
from llama_index.llms.openai import OpenAI
from llama_index.core import Settings
os.environ["OPENAI_API_KEY"] = "sk-..." # 使用实际的API密钥
logging.basicConfig(stream=sys.stdout, level=logging.INFO) # 调试级别输出
# 定义LLM
Settings.llm = OpenAI(temperature=0, model="gpt-3.5-turbo")
Settings.chunk_size = 512
from llama_index.llms.azure_openai import AzureOpenAI from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding # Azure OpenAI配置 api_key = "<api-key>" azure_endpoint = "https://<your-resource-name>.openai.azure.com/" api_version = "2023-07-01-preview" llm = AzureOpenAI( model="gpt-35-turbo-16k", deployment_name="my-custom-llm", api_key=api_key, azure_endpoint=azure_endpoint, api_version=api_version, ) embed_model = AzureOpenAIEmbedding( model="text-embedding-ada-002", deployment_name="my-custom-embedding", api_key=api_key, azure_endpoint=azure_endpoint, api_version=api_version, ) from llama_index.core import Settings Settings.llm = llm Settings.embed_model = embed_model Settings.chunk_size = 512
%pip install ipython-ngql nebula3-python
os.environ["NEBULA_USER"] = "root"
os.environ["NEBULA_PASSWORD"] = "nebula"
os.environ["NEBULA_ADDRESS"] = "127.0.0.1:9669"
space_name = "llamaindex"
edge_types, rel_prop_names = ["relationship"], ["relationship"]
tags = ["entity"]
创建NebulaGraph集群:
curl -fsSL nebula-up.siwei.io/install.sh | bash
或使用NebulaGraph Docker Extension。
配置StorageContext:
from llama_index.core import StorageContext
from llama_index.graph_stores.nebula import NebulaGraphStore
graph_store = NebulaGraphStore(
space_name=space_name,
edge_types=edge_types,
rel_prop_names=rel_prop_names,
tags=tags,
)
storage_context = StorageContext.from_defaults(graph_store=graph_store)
步骤1:从Wikipedia加载数据
from llama_index.core import download_loader
from llama_index.readers.wikipedia import WikipediaReader
loader = WikipediaReader()
documents = loader.load_data(pages=["Guardians of the Galaxy Vol. 3"], auto_suggest=False)
步骤2:使用NebulaGraph作为graph_store生成KnowledgeGraphIndex
from llama_index.core import KnowledgeGraphIndex
kg_index = KnowledgeGraphIndex.from_documents(
documents,
storage_context=storage_context,
max_triplets_per_chunk=10,
space_name=space_name,
edge_types=edge_types,
rel_prop_names=rel_prop_names,
tags=tags,
include_embeddings=True,
)
from llama_index.core.query_engine import KnowledgeGraphQueryEngine
query_engine = KnowledgeGraphQueryEngine(
storage_context=storage_context,
llm=llm,
verbose=True,
)
response = query_engine.query("Tell me about Peter Quill?")
print(response)
如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!
参考资料:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。