
LANGCHAIN — Can a Knowledge Graph be Utilized to Implement a DevOps RAG Application?
Technology is best when it brings people together. — Matt Mullenweg
Implementing a DevOps RAG application using a knowledge graph can be a powerful approach to handling both structured and unstructured data. In this tutorial, we will walk through a scenario of implementing a knowledge graph-based RAG application with LangChain to support your DevOps team. We will cover the Neo4j environment setup, dataset management, Neo4j Vector index, Graph Cypher search, and the implementation of a knowledge graph agent.
Neo4j Environment Setup
To use Neo4j in your Python code, you can start by setting up a Neo4j 5.11 or greater instance. You can start a free instance on Neo4j Aura or set up a local instance by downloading the Neo4j Desktop application. Then, you can use the following code snippet to connect to the Neo4j instance:
from langchain.graphs import Neo4jGraph
url = "neo4j+s://databases.neo4j.io"
username ="neo4j"
password = ""
graph = Neo4jGraph(
url=url,
username=username,
password=password
)Dataset
You can fetch information from various data sources to populate your knowledge graph. In this example, we fetch information from a JSON file and import it into the Neo4j graph with the following code:
import requests
url = "URL_TO_JSON_FILE"
import_query = requests.get(url).json()['query']
graph.query(import_query)Neo4j Vector index
The next step is to implement a vector index search for finding relevant tasks by their name and description. You can achieve this with the following code:
from langchain.vectorstores.neo4j_vector import Neo4jVector
from langchain.embeddings.openai import OpenAIEmbeddings
os.environ['OPENAI_API_KEY'] = "OPENAI_API_KEY"
vector_index = Neo4jVector.from_existing_graph(
OpenAIEmbeddings(),
url=url,
username=username,
password=password,
index_name='tasks',
node_label="Task",
text_node_properties=['name', 'description', 'status'],
embedding_node_property='embedding',
)Graph Cypher search
LangChain provides a GraphCypherQAChain, which generates Cypher queries for interacting with the Neo4j graph database. You can set it up and utilize it with the following code:
from langchain.chains import GraphCypherQAChain
graph.refresh_schema()
cypher_chain = GraphCypherQAChain.from_llm(
cypher_llm = ChatOpenAI(temperature=0, model_name='gpt-4'),
qa_llm = ChatOpenAI(temperature=0), graph=graph, verbose=True,
)Knowledge graph agent
You can create an agent that combines the structured and unstructured parts of the knowledge graph. In this example, we create an agent with two tools for handling different types of queries:
from langchain.agents import initialize_agent, Tool, AgentType
tools = [
Tool(
name="Tasks",
func=vector_qa.run,
description="""Useful when you need to answer questions about descriptions of tasks.
Not useful for counting the number of tasks.
Use full question as input.
""",
),
Tool(
name="Graph",
func=cypher_chain.run,
description="""Useful when you need to answer questions about microservices,
their dependencies or assigned people. Also useful for any sort of
aggregation like counting the number of tasks, etc.
Use full question as input.
""",
),
]
mrkl = initialize_agent(
tools,
ChatOpenAI(temperature=0, model_name='gpt-4'),
agent=AgentType.OPENAI_FUNCTIONS, verbose=True
)These code snippets provide a practical demonstration of utilizing a knowledge graph to implement a DevOps RAG application. By following these examples, you can effectively integrate structured and unstructured data into your DevOps workflows using LangChain and Neo4j.
