llama_index [问题]: NebulaGraph RAG

wf82jlnq  于 2个月前  发布在  其他
关注(0)|答案(4)|浏览(29)

问题验证

  • 我已经在文档和Discord上寻找答案。

问题

我想使用NebulaGraph来执行RAG。现在所有的数据都已经存储在NG中了,接下来我应该做什么?我需要查询所有的数据然后再进行RAG吗?你能给我一个演示吗?

erhoui1w

erhoui1w1#

要使用NebulaGraph进行检索增强生成(RAG)操作,并结合你现有的数据,请按照以下步骤进行:

  1. 安装所需软件包:
%pip install llama-index-llms-openai
%pip install llama-index-embeddings-openai
%pip install llama-index-graph-stores-nebula
%pip install llama-index-llms-azure-openai
%pip install nebula3-python
%pip install ipython-ngql networkx pyvis
  1. 设置OpenAI或Azure OpenAI:
    对于OpenAI:
import os
from llama_index.llms.openai import OpenAI
from llama_index.core import Settings

os.environ["OPENAI_API_KEY"] = "INSERT OPENAI KEY"

llm = OpenAI(temperature=0, model="gpt-3.5-turbo")
Settings.llm = llm
Settings.chunk_size = 512

对于Azure OpenAI:

import os
import openai
from llama_index.llms.azure_openai import AzureOpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.core import Settings

openai.api_type = "azure"
openai.api_base = "https://<foo-bar>.openai.azure.com"
openai.api_version = "2022-12-01"
os.environ["OPENAI_API_KEY"] = "<your-openai-key>"
openai.api_key = os.getenv("OPENAI_API_KEY")

llm = AzureOpenAI(
    model="<foo-bar-model>",
    engine="<foo-bar-deployment>",
    temperature=0,
    api_key=openai.api.key,
    api_type=openai.api_type,
    api_base=openai.api_base,
    api_version=openai.api_version,
)

embedding_model = OpenAIEmbedding(
    model="text-embedding-ada-002",
    deployment_name="<foo-bar-deployment>",
    api_key=openai.api.key,
    api_base=openai.api.base,
    api_type=openai.api.type,
    api_version=openai.api.version,
)

Settings.llm = llm
Settings.chunk_size = 512
Settings.embed_model = embedding_model
  1. 准备NebulaGraph:
    确保你已经运行了具有定义数据模式的NebulaGraph。

  2. 示例化NebulaGraphStore:

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. 使用LlamaIndex构建知识图谱:
from llama_index.core import KnowledgeGraphIndex, SimpleDirectoryReader

documents = SimpleDirectoryReader(
    "../../../../examples/paul_graham_essay/data"
).load_data()

index = KnowledgeGraphIndex.from_documents(
    documents,
    storage_context=storage_context,
    max_triplets_per_chunk=2,
    space_name=space_name,
    edge_types=edge_types,
    rel_prop_names=rel_prop_names,
    tags=tags,
)
  1. 执行图RAG查询:
query_engine = index.as_query_engine()

response = query_engine.query("Tell me more about Interleaf")
from IPython.display import display, Markdown
display(Markdown(f"<b>{response}</b>"))
  1. 可视化图RAG:
%ngql --address 127.0.0.1 --port 9669 --user root --password <password>
%%ngql
USE paul_graham_essay;
MATCH p=(n)-[*1..2]-()
  WHERE id(n) IN ['Interleaf', 'history', 'Software', 'Company'] 
RETURN p LIMIT 100;
%ng_draw

通过遵循这些步骤,你可以利用NebulaGraph对你现有的数据进行检索增强生成(RAG)。这个过程包括加载你的数据、对其进行索引、存储索引、查询它以及评估结果以确保准确性和相关性。这样可以让你通过将特定的数据融入到LLM 1(https://github.com/run-llama/llama_index/blob/main/docs/docs/examples/query_engine/knowledge_graph_rag_query_engine.ipynb)2(https://github.com/run-llama/llama_index/blob/main/docs/docs/examples/query_engine/knowledge_graph_query_engine.ipynb)3(https://github.com/run-llama/llama_index/blob/main/docs/docs/getting_started/concepts.md)4(https://github.com/run-llama/llama_index/blob/main/docs/docs/examples/index_structs/knowledge_graph/NebulaGraphKGIndexDemo.ipynb) 生成的响应中,来增强你的LLM应用的能力。如果你想继续与 Dosu 对话,请提及 @dosu。

1mrurvl1

1mrurvl12#

我的数据已经保存在NebulaGraph中,而不是本地数据。

h9vpoimq

h9vpoimq3#

除非你用llama-index构建它,否则你能做的最好的事情就是text2cypher。

e7arh2l6

e7arh2l64#

除非你用llama-index构建了它,否则你能做的最好的事情就是text2cypher。

我想问一个关于llama-index的问题。目前,我只在llama-index中找到了KnowledgeGraphIndex.from_documents函数来实现它,但这个函数似乎只是提取本地数据实体关系并将其存储在Neo4j中。既然我已经将提取的实体和关系放入Neo4j中,接下来我应该做什么?从NG中提取数据,然后将其输入到这个函数中吗?

相关问题