Knowledge Graph

img

Knowledge graphs in the library are represented using the KnowledgeLLM class. This component in the llamarch library that provides a simple interface to work with knowledge graphs.

KnowledgeLLM

Source code in llamarch/patterns/knowledge_graph/__init__.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class KnowledgeLLM:
    def __init__(self, knowledge_graph: GraphDB, llm: LLM):
        """
        Initialize the KnowledgeLLM with a knowledge graph and a language model.

        Parameters
        ----------
        knowledge_graph : GraphDB
            The knowledge graph instance that will be used to store and retrieve knowledge.
        llm : LLM
            The language model instance that will be used to generate ontologies and respond to queries.
        """
        self.knowledge_graph = knowledge_graph
        self.llm = llm

    def query_knowledge_graph(self, query):
        """
        Query the knowledge graph for relevant data.

        Parameters
        ----------
        query : str
            The query to be executed on the knowledge graph.

        Returns
        -------
        Any
            The result from the knowledge graph query.
        """
        return self.knowledge_graph.read_data(query)

    def update_knowledge_graph(self, query, parameters=None):
        """
        Update the knowledge graph with new data.

        Parameters
        ----------
        query : str
            The query to modify the knowledge graph (e.g., adding or updating nodes).
        parameters : dict, optional
            Optional parameters for the query (default is None).
        """
        self.knowledge_graph.write_data(query, parameters)

    def generate_ontology(self, text):
        """
        Generate an ontology from the provided text using the language model.

        Parameters
        ----------
        text : str
            The text from which the ontology will be generated.

        Returns
        -------
        str
            The generated ontology.

        Notes
        -----
        The generated ontology will be inserted into the knowledge graph.
        """
        prompt = f"Generate an ontology from the following text: {text}"
        ontology = self.llm.generate(prompt)
        # Insert the generated ontology into the knowledge graph
        self.update_knowledge_graph(
            "CREATE (n:Ontology {data: $ontology})", {"ontology": ontology})
        return ontology

    def respond_to_query(self, query):
        """
        Generate a response to a query by combining information from the knowledge graph and the language model.

        Parameters
        ----------
        query : str
            The query to be answered using both the knowledge graph and language model.

        Returns
        -------
        str
            The generated response to the query.

        Notes
        -----
        This method combines the results from a knowledge graph query with the language model's response.
        """
        graph_result = self.query_knowledge_graph(query)
        prompt = f"Given this knowledge: {graph_result}, answer the following query: {query}"
        print(prompt)
        return self.llm(prompt)

__init__(knowledge_graph, llm)

Initialize the KnowledgeLLM with a knowledge graph and a language model.

Parameters:
  • knowledge_graph (GraphDB) –

    The knowledge graph instance that will be used to store and retrieve knowledge.

  • llm (LLM) –

    The language model instance that will be used to generate ontologies and respond to queries.

Source code in llamarch/patterns/knowledge_graph/__init__.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
def __init__(self, knowledge_graph: GraphDB, llm: LLM):
    """
    Initialize the KnowledgeLLM with a knowledge graph and a language model.

    Parameters
    ----------
    knowledge_graph : GraphDB
        The knowledge graph instance that will be used to store and retrieve knowledge.
    llm : LLM
        The language model instance that will be used to generate ontologies and respond to queries.
    """
    self.knowledge_graph = knowledge_graph
    self.llm = llm

query_knowledge_graph(query)

Query the knowledge graph for relevant data.

Parameters:
  • query (str) –

    The query to be executed on the knowledge graph.

Returns:
  • Any

    The result from the knowledge graph query.

Source code in llamarch/patterns/knowledge_graph/__init__.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def query_knowledge_graph(self, query):
    """
    Query the knowledge graph for relevant data.

    Parameters
    ----------
    query : str
        The query to be executed on the knowledge graph.

    Returns
    -------
    Any
        The result from the knowledge graph query.
    """
    return self.knowledge_graph.read_data(query)

update_knowledge_graph(query, parameters=None)

Update the knowledge graph with new data.

Parameters:
  • query (str) –

    The query to modify the knowledge graph (e.g., adding or updating nodes).

  • parameters (dict, default: None ) –

    Optional parameters for the query (default is None).

Source code in llamarch/patterns/knowledge_graph/__init__.py
36
37
38
39
40
41
42
43
44
45
46
47
def update_knowledge_graph(self, query, parameters=None):
    """
    Update the knowledge graph with new data.

    Parameters
    ----------
    query : str
        The query to modify the knowledge graph (e.g., adding or updating nodes).
    parameters : dict, optional
        Optional parameters for the query (default is None).
    """
    self.knowledge_graph.write_data(query, parameters)

generate_ontology(text)

Generate an ontology from the provided text using the language model.

Parameters:
  • text (str) –

    The text from which the ontology will be generated.

Returns:
  • str

    The generated ontology.

Notes

The generated ontology will be inserted into the knowledge graph.

Source code in llamarch/patterns/knowledge_graph/__init__.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def generate_ontology(self, text):
    """
    Generate an ontology from the provided text using the language model.

    Parameters
    ----------
    text : str
        The text from which the ontology will be generated.

    Returns
    -------
    str
        The generated ontology.

    Notes
    -----
    The generated ontology will be inserted into the knowledge graph.
    """
    prompt = f"Generate an ontology from the following text: {text}"
    ontology = self.llm.generate(prompt)
    # Insert the generated ontology into the knowledge graph
    self.update_knowledge_graph(
        "CREATE (n:Ontology {data: $ontology})", {"ontology": ontology})
    return ontology

respond_to_query(query)

Generate a response to a query by combining information from the knowledge graph and the language model.

Parameters:
  • query (str) –

    The query to be answered using both the knowledge graph and language model.

Returns:
  • str

    The generated response to the query.

Notes

This method combines the results from a knowledge graph query with the language model's response.

Source code in llamarch/patterns/knowledge_graph/__init__.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def respond_to_query(self, query):
    """
    Generate a response to a query by combining information from the knowledge graph and the language model.

    Parameters
    ----------
    query : str
        The query to be answered using both the knowledge graph and language model.

    Returns
    -------
    str
        The generated response to the query.

    Notes
    -----
    This method combines the results from a knowledge graph query with the language model's response.
    """
    graph_result = self.query_knowledge_graph(query)
    prompt = f"Given this knowledge: {graph_result}, answer the following query: {query}"
    print(prompt)
    return self.llm(prompt)