avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2558

Abstract

<div id="6892"><pre><span class="hljs-keyword">import</span> requests

<span class="hljs-title">url</span> = <span class="hljs-string">"URL_TO_JSON_FILE"</span> import_query = requests.get(url).json()['query'] <span class="hljs-title">graph</span>.query(import_query)</pre></div><h2 id="94e2">Neo4j Vector index</h2><p id="2f83">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:</p><div id="415b"><pre><span class="hljs-keyword">from</span> langchain.vectorstores.neo4j_vector import Neo4jVector <span class="hljs-keyword">from</span> langchain.embeddings.openai import OpenAIEmbeddings

os.environ[<span class="hljs-string">'OPENAI_API_KEY'</span>] = <span class="hljs-string">"OPENAI_API_KEY"</span>

vector_index = Neo4jVector.from_existing_graph( OpenAIEmbeddings(), <span class="hljs-attribute">url</span>=url, <span class="hljs-attribute">username</span>=username, <span class="hljs-attribute">password</span>=password, <span class="hljs-attribute">index_name</span>=<span class="hljs-string">'tasks'</span>, <span class="hljs-attribute">node_label</span>=<span class="hljs-string">"Task"</span>, text_node_properties=[<span class="hljs-string">'name'</span>, <span class="hljs-string">'description'</span>, <span class="hljs-string">'status'</span>], <span class="hljs-attribute">embedding_node_property</span>=<span class="hljs-string">'embedding'</span>, )</pre></div><h2 id="a7a5">Graph Cypher search</h2><p id="f2ea">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:</p><div id="e189"><pre><span class="hljs-keyword">from</span> langchain.chains import GraphCypherQAChain

graph.refresh_schema() cypher_chain = GraphCypherQAChain.from_llm( cypher_llm = ChatOpenAI(<span class="hljs-attribute">temperature</span>=0, <span class="hljs-attribute">model_name</span>=<span class="hljs-string">'gpt-4'</span>), qa_llm = ChatOpenAI(<span class="hljs-attribute">temperature</span>=0), <span class="hljs-attribute">graph</span>=graph, <span class="hljs-attribute">verbose</span>=<span class="hljs-literal">True</span>, )</pre></div><h2 id="9332">Knowledge graph agent</h2><p id="7e08">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:</p><di

Options

v id="091d"><pre><span class="hljs-keyword">from</span> langchain.agents import initialize_agent, Tool, AgentType

tools = [ Tool( <span class="hljs-attribute">name</span>=<span class="hljs-string">"Tasks"</span>, <span class="hljs-attribute">func</span>=vector_qa.run, <span class="hljs-attribute">description</span>=<span class="hljs-string">""</span><span class="hljs-string">"Useful when you need to answer questions about descriptions of tasks. Not useful for counting the number of tasks. Use full question as input. "</span><span class="hljs-string">""</span>, ), Tool( <span class="hljs-attribute">name</span>=<span class="hljs-string">"Graph"</span>, <span class="hljs-attribute">func</span>=cypher_chain.run, <span class="hljs-attribute">description</span>=<span class="hljs-string">""</span><span class="hljs-string">"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. "</span><span class="hljs-string">""</span>, ), ]

mrkl = initialize_agent( tools, ChatOpenAI(<span class="hljs-attribute">temperature</span>=0, <span class="hljs-attribute">model_name</span>=<span class="hljs-string">'gpt-4'</span>), <span class="hljs-attribute">agent</span>=AgentType.OPENAI_FUNCTIONS, <span class="hljs-attribute">verbose</span>=<span class="hljs-literal">True</span> )</pre></div><div id="438d" class="link-block"> <a href="https://readmedium.com/langchain-how-correct-are-llm-evaluators-2793bb6fdbc1"> <div> <div> <h2>LANGCHAIN — How Correct Are LLM Evaluators?</h2> <div><h3>Artificial intelligence is growing up fast, as are robots whose facial expressions can elicit empathy and make your…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*nu7ZXSdSXeo6aCLEJYoZpg.jpeg)"></div> </div> </div> </a> </div><p id="64e4">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.</p></article></body>

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.

Utilized
Implement
DevOps
Graph
Langchain
Recommended from ReadMedium