kevinhug commited on
Commit
6093500
·
1 Parent(s): e5d2671

knowledge graph

Browse files
Files changed (3) hide show
  1. app.py +21 -2
  2. knowledge.py +79 -0
  3. requirements.txt +7 -1
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  from rag import rbc_product
3
  from tool import rival_product
4
  from graphrag import reasoning
5
-
6
  with gr.Blocks() as demo:
7
  with gr.Tab("RAG"):
8
  gr.Markdown("""
@@ -122,4 +122,23 @@ Low APR and great customer service. I would highly recommend if you’re looking
122
  btn_recommend.click(fn=reasoning, inputs=[in_verbatim, in_question], outputs=out_product)
123
 
124
 
125
- demo.launch(allowed_paths=["./xgb","./ts"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from rag import rbc_product
3
  from tool import rival_product
4
  from graphrag import reasoning
5
+ from knowledge import graph
6
  with gr.Blocks() as demo:
7
  with gr.Tab("RAG"):
8
  gr.Markdown("""
 
122
  btn_recommend.click(fn=reasoning, inputs=[in_verbatim, in_question], outputs=out_product)
123
 
124
 
125
+ with gr.Tab("Knowledge Graph"):
126
+ gr.Markdown("""
127
+ Objective: Explain concept in knowledge graph structured output
128
+ ================================================
129
+ """)
130
+ in_verbatim = gr.Textbox(label="Question")
131
+ out_product = gr.Image(label="Knowledge Graph")
132
+
133
+ gr.Examples(
134
+ [
135
+ [
136
+ "Explain me about red flags in transaction pattern for fraud detection"
137
+ ]
138
+ ],
139
+ [in_verbatim]
140
+ )
141
+ btn_recommend = gr.Button("Graph It!")
142
+ btn_recommend.click(fn=graph, inputs=in_verbatim, outputs=out_product)
143
+
144
+ demo.launch(allowed_paths=["./"])
knowledge.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ import instructor
3
+
4
+ from pydantic import BaseModel, Field
5
+ from typing import List
6
+ from graphviz import Digraph
7
+
8
+ class Node(BaseModel, frozen=True):
9
+ """
10
+ Node representing concept in the subject domain
11
+ """
12
+ id: int
13
+ label: str = Field(..., description = "description of the concept concept in the subject domain")
14
+ color: str
15
+
16
+ class Edge(BaseModel, frozen=True):
17
+ """
18
+ Edge representing relationship between concepts in the subject domain
19
+ """
20
+ source: int = Field(..., description = "source representing concept in the subject domain")
21
+ target: int = Field(..., description = "target representing concept in the subject domain")
22
+ label: str = Field(..., description = "description representing relationship between concepts in the subject domain")
23
+ color: str = "black"
24
+
25
+ class KnowledgeGraph(BaseModel):
26
+ """
27
+ graph representation of subject domain
28
+ """
29
+ nodes: List[Node] = Field(..., default_factory=list)
30
+ edges: List[Edge] = Field(..., default_factory=list)
31
+
32
+ from groq import Groq
33
+ import os
34
+ # Initialize with API key
35
+ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
36
+
37
+ # Enable instructor patches for Groq client
38
+ client = instructor.from_groq(client)
39
+ """
40
+ client = instructor.from_openai(
41
+ OpenAI(
42
+ base_url="http://localhost:11434/v1",
43
+ api_key="ollama",
44
+ ),
45
+ mode=instructor.Mode.JSON,
46
+ )
47
+ """
48
+ def generate_graph(input) -> KnowledgeGraph:
49
+ return client.chat.completions.create(
50
+ model='llama-3.1-8b-instant', #"llama3.2",
51
+ max_retries=5,
52
+ messages=[
53
+ {
54
+ "role": "user",
55
+ "content": f"Help me understand the following by describing it as a detailed knowledge graph: {input}",
56
+ }
57
+ ],
58
+ response_model=KnowledgeGraph,
59
+ )
60
+
61
+
62
+ def visualize_knowledge_graph(kg: KnowledgeGraph):
63
+ dot = Digraph(comment="Knowledge Graph")
64
+
65
+ # Add nodes
66
+ for node in kg.nodes:
67
+ dot.node(str(node.id), node.label, color=node.color)
68
+
69
+ # Add edges
70
+ for edge in kg.edges:
71
+ dot.edge(str(edge.source), str(edge.target), label=edge.label, color=edge.color)
72
+
73
+ # Render the graph
74
+ dot.render("knowledge_graph", format="png")
75
+
76
+ def graph(query):
77
+ graph = generate_graph(query)
78
+ visualize_knowledge_graph(graph)
79
+ return "./knowledge_graph.png"
requirements.txt CHANGED
@@ -11,6 +11,7 @@ llama-index
11
  faiss-cpu
12
  tavily-python
13
 
 
14
  #llama-index-llms-litellm
15
 
16
  #llama-index-llms-huggingface-api
@@ -24,4 +25,9 @@ langchain-community
24
  pandas
25
  #gradio-client
26
  pillow
27
- numpy
 
 
 
 
 
 
11
  faiss-cpu
12
  tavily-python
13
 
14
+ #GRAPHRAG
15
  #llama-index-llms-litellm
16
 
17
  #llama-index-llms-huggingface-api
 
25
  pandas
26
  #gradio-client
27
  pillow
28
+ numpy
29
+
30
+ #KNOWLEDGE GRAPH
31
+ graphviz
32
+ pydantic
33
+ instructor[groq]