File size: 7,429 Bytes
eb957df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from llama_index.core import Document
from llama_index.core import KnowledgeGraphIndex, ServiceContext, StorageContext
from llama_index.llms.openai import OpenAI
from llama_index.core.graph_stores import SimpleGraphStore
from llama_index.core import SimpleDirectoryReader, load_index_from_storage
from typing import List
from dotenv import load_dotenv
import os
import json
import networkx as nx
from pyvis.network import Network
from datetime import datetime
from retrieve import get_latest_dir
import html

load_dotenv()

llm = OpenAI(
    temperature=0.0, model="gpt-3.5-turbo", api_key=os.getenv("OPENAI_API_KEY")
)
graph_store = SimpleGraphStore()
storage_context = StorageContext.from_defaults(graph_store=graph_store)
service_context = ServiceContext.from_defaults(
    llm=llm, chunk_size=2048, chunk_overlap=24
)


def create_document(input_dir: str) -> List[Document]:
    """
    Create a document from the given directory.

    Args:
    input_dir (str): The input directory to read the documents from.

    Returns:
    List[Document]: The list of documents from the directory.
    """
    reader = SimpleDirectoryReader(
        input_dir, exclude_hidden=True, required_exts=[".json"]
    )

    products_document = []

    for docs in reader.iter_data():
        products_document.extend(docs)

    return products_document


def kg_triplet_extract_fn(text) -> List[str]:
    """
    Extract the triplets from the text.

    Args:
    text (str): The text to extract the triplets from.

    Returns:
    List[str]: The list of triplets extracted from the text.
    """

    json_text = text.split("\n\n")[-1]
    product_spec = json.loads(json_text)
    triplets = []
    product_name = product_spec["name"]
    del product_spec["name"]
    for key, value in product_spec.items():
        triplets.append((product_name, key, value))
    return triplets


def generate_graph_visualization(kg_index):
    """
    Generate a graph visualization from the KG index.

    Args:
    kg_index (KnowledgeGraphIndex): The Knowledge Graph index to generate the visualization from.

    Returns:
    str: The path to the generated graph visualization.
    """

    output_directory = os.getenv("GRAPH_VIS_DIR", "graph_vis")

    # Generate a timestamp for the filename
    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    graph_output_file = f"{timestamp}.html"
    graph_output_path = os.path.join(output_directory, graph_output_file)

    g = kg_index.get_networkx_graph(limit=20000)

    net = Network(
        notebook=False,
        cdn_resources="remote",
        height="800px",
        width="100%",
        select_menu=True,
        filter_menu=False,
    )

    net.from_nx(g)
    net.force_atlas_2based(central_gravity=0.015, gravity=-31)
    net.save_graph(graph_output_path)

    print(f"Graph visualization saved to: {graph_output_path}")
    return graph_output_path


def plot_subgraph(triplets):
    """
    Plot a subgraph from the triplets.

    Args:
    triplets (str): The triplets to plot the subgraph from.

    Returns:
    str: The escaped HTML content to display the subgraph
    """

    G = nx.DiGraph()
    for edge_str in eval(triplets):
        source, action, target = eval(edge_str)
        G.add_edge(source, target, label=action)

    net = Network(notebook=True, cdn_resources="remote", height="400px", width="100%")
    net.from_nx(G)
    net.force_atlas_2based(central_gravity=0.015, gravity=-31)

    html_content = net.generate_html()
    escaped_html = html.escape(html_content)

    return escaped_html


def create_kg(max_features: int = 60):
    """
    Create a Knowledge Graph from the given directory.

    Args:
    max_features (int): The maximum number of features to use for the KG.

    Returns:
    KnowledgeGraphIndex: The Knowledge Graph index.
    """

    input_dir = os.getenv("PROD_SPEC_DIR", "prod_spec")
    product_documents = create_document(input_dir)

    kg_index = KnowledgeGraphIndex.from_documents(
        documents=product_documents,
        max_triplets_per_chunk=max_features,
        storage_context=storage_context,
        service_context=service_context,
        show_progress=True,
        include_embeddings=True,
        kg_triplet_extract_fn=kg_triplet_extract_fn,
    )

    graphvis_path = generate_graph_visualization(kg_index)
    return kg_index, graphvis_path


def persist_kg(kg_index: KnowledgeGraphIndex) -> str:
    """
    Persist the KG index to storage.

    Args:
    kg_index (KnowledgeGraphIndex): The Knowledge Graph index to persist.

    Returns:
    str: The path to the persisted KG index.
    """

    output_dir = os.getenv("GRAPH_DIR", "graphs")
    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    kg_path = f"{output_dir}/{timestamp}"
    kg_index.storage_context.persist(kg_path)
    return kg_path


def load_kg(kg_dir: str) -> KnowledgeGraphIndex:
    """
    Load the KG index from the given directory.

    Args:
    kg_dir (str): The parent directory to load the KG index from.

    Returns:
    KnowledgeGraphIndex: The loaded Knowledge Graph index.
    """

    kg_path = get_latest_dir(kg_dir)

    kg_index = load_index_from_storage(
        StorageContext.from_defaults(persist_dir=kg_path)
    )

    return kg_index


def query(kg_dir: str, query: str):
    """
    Query the KG index for a given query.

    Args:
    kg_dir (str): The directory to load the KG index from.
    query (str): The query to ask the KG index.

    Returns:
    Response: The response from the KG index.
    """

    kg_index = load_kg(kg_dir)
    query_engine = kg_index.as_query_engine(
        include_text=True,
        response_mode="refine",
        graph_store_query_depth=6,
        similarity_top_k=5,
    )
    response = query_engine.query(query)
    return response


def query_graph_qa(graph_rag_index, query, search_level):
    """
    Query the Graph-RAG model for a given query.

    Args:
    graph_rag_index (KnowledgeGraphIndex): The Graph-RAG model index.
    query (str): The query to ask the Graph-RAG model.
    search_level (int): The max search level to use for the Graph-RAG model.

    Returns:
    tuple: The response, reference, and reference text from the Graph-RAG model.
    """

    myretriever = graph_rag_index.as_retriever(
        include_text=True,
        similarity_top_k=search_level,
    )
    query_engine = graph_rag_index.as_query_engine(
        sub_retrievers=[
            myretriever,
        ],
        graph_store_query_depth=6,
        include_text=True,
        similarity_top_k=search_level,
    )

    response = query_engine.query(query)
    nodes = myretriever.retrieve(query)

    reference = []

    for _, value in response.metadata.items():
        if isinstance(value, dict) and "kg_rel_texts" in value:
            reference = value["kg_rel_texts"]
            break

    reference_text = []
    for node in nodes:
        reference_text.append(node.text)

    return response, reference, reference_text


if __name__ == "__main__":
    kg_index, graphvis_path = create_kg()
    persist_kg(kg_index)

    kg_index = load_kg(os.getenv("GRAPH_DIR", "graphs"))
    generate_graph_visualization(kg_index)
    response = query(
        os.getenv("GRAPH_DIR", "graphs"),
        "Tell me the Built-in memory in Apple iPhone 15 Pro Max 256Gb Blue Titanium?",
    )
    print(response)
    key = list(response.metadata)[-1]
    print(response.metadata[key])