Spaces:
Running
Running
Ilyas KHIAT
commited on
Commit
·
9b707db
1
Parent(s):
7b897df
kg
Browse files- audit_page/knowledge_graph.py +63 -11
- requirements.txt +1 -0
audit_page/knowledge_graph.py
CHANGED
@@ -4,7 +4,8 @@ from utils.audit.rag import get_text_from_content_for_doc,get_text_from_content_
|
|
4 |
from streamlit_agraph import agraph, Node, Edge, Config
|
5 |
import random
|
6 |
import math
|
7 |
-
from utils.audit.response_llm import
|
|
|
8 |
|
9 |
def if_node_exists(nodes, node_id):
|
10 |
"""
|
@@ -121,7 +122,7 @@ def convert_neo4j_to_agraph(neo4j_graph, node_colors):
|
|
121 |
edges.append(Edge(source=source_id, label=label, target=target_id))
|
122 |
|
123 |
# Define the configuration for Agraph
|
124 |
-
config = Config(width=1200, height=
|
125 |
|
126 |
# Create the Agraph visualization
|
127 |
|
@@ -129,7 +130,8 @@ def convert_neo4j_to_agraph(neo4j_graph, node_colors):
|
|
129 |
|
130 |
def display_graph(edges, nodes, config):
|
131 |
# Display the Agraph visualization
|
132 |
-
agraph(edges=edges, nodes=nodes, config=config)
|
|
|
133 |
|
134 |
def filter_nodes_by_types(nodes:list[Node], node_types_filter:list) -> list[Node]:
|
135 |
filtered_nodes = []
|
@@ -141,6 +143,13 @@ def filter_nodes_by_types(nodes:list[Node], node_types_filter:list) -> list[Node
|
|
141 |
|
142 |
def kg_main():
|
143 |
#st.set_page_config(page_title="Graphe de connaissance", page_icon="", layout="wide")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
if "graph" not in st.session_state:
|
145 |
st.session_state.graph = None
|
146 |
st.title("Graphe de connaissance")
|
@@ -151,10 +160,8 @@ def kg_main():
|
|
151 |
if "summary" not in st.session_state:
|
152 |
st.session_state.summary = None
|
153 |
|
154 |
-
|
155 |
-
|
156 |
-
st.error("Veuillez d'abord effectuer un audit pour obtenir des recommandations d'agents.")
|
157 |
-
return
|
158 |
|
159 |
audit = st.session_state.audit_simplified
|
160 |
content = st.session_state.audit["content"]
|
@@ -186,11 +193,56 @@ def kg_main():
|
|
186 |
#st.write(graph)
|
187 |
|
188 |
edges,nodes,config = convert_neo4j_to_agraph(graph[0],st.session_state.node_types)
|
189 |
-
filter = st.multiselect("Filtrer selon l'étiquette",st.session_state.node_types.keys(),placeholder="Sélectionner une ou plusieurs étiquettes")
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
node_types = st.session_state.node_types
|
196 |
|
|
|
4 |
from streamlit_agraph import agraph, Node, Edge, Config
|
5 |
import random
|
6 |
import math
|
7 |
+
from utils.audit.response_llm import generate_response_via_langchain
|
8 |
+
from langchain_core.messages import AIMessage, HumanMessage
|
9 |
|
10 |
def if_node_exists(nodes, node_id):
|
11 |
"""
|
|
|
122 |
edges.append(Edge(source=source_id, label=label, target=target_id))
|
123 |
|
124 |
# Define the configuration for Agraph
|
125 |
+
config = Config(width=1200, height=800, directed=True, physics=False, hierarchical=False, nodeSpacing=500)
|
126 |
|
127 |
# Create the Agraph visualization
|
128 |
|
|
|
130 |
|
131 |
def display_graph(edges, nodes, config):
|
132 |
# Display the Agraph visualization
|
133 |
+
return agraph(edges=edges, nodes=nodes, config=config)
|
134 |
+
|
135 |
|
136 |
def filter_nodes_by_types(nodes:list[Node], node_types_filter:list) -> list[Node]:
|
137 |
filtered_nodes = []
|
|
|
143 |
|
144 |
def kg_main():
|
145 |
#st.set_page_config(page_title="Graphe de connaissance", page_icon="", layout="wide")
|
146 |
+
|
147 |
+
|
148 |
+
|
149 |
+
if "audit" not in st.session_state or st.session_state.audit == {}:
|
150 |
+
st.error("Veuillez d'abord effectuer un audit pour visualiser le graphe de connaissance.")
|
151 |
+
return
|
152 |
+
|
153 |
if "graph" not in st.session_state:
|
154 |
st.session_state.graph = None
|
155 |
st.title("Graphe de connaissance")
|
|
|
160 |
if "summary" not in st.session_state:
|
161 |
st.session_state.summary = None
|
162 |
|
163 |
+
if "chat_graph_history" not in st.session_state:
|
164 |
+
st.session_state.chat_graph_history = []
|
|
|
|
|
165 |
|
166 |
audit = st.session_state.audit_simplified
|
167 |
content = st.session_state.audit["content"]
|
|
|
193 |
#st.write(graph)
|
194 |
|
195 |
edges,nodes,config = convert_neo4j_to_agraph(graph[0],st.session_state.node_types)
|
|
|
196 |
|
197 |
+
col1, col2 = st.columns([2.5, 1.5])
|
198 |
+
|
199 |
+
with col1.container(border=True,height=800):
|
200 |
+
filter_col,param_col = st.columns([4,1])
|
201 |
+
with param_col.popover("⚙️"):
|
202 |
+
for node_type,color in st.session_state.node_types.items():
|
203 |
+
color = st.color_picker(f"La couleur de l'entité **{node_type}**",color)
|
204 |
+
st.session_state.node_types[node_type] = color
|
205 |
+
|
206 |
+
filter = filter_col.multiselect("Filtrer selon l'étiquette",st.session_state.node_types.keys(),placeholder="Sélectionner une ou plusieurs étiquettes")
|
207 |
+
if filter:
|
208 |
+
nodes = filter_nodes_by_types(nodes,filter)
|
209 |
+
|
210 |
+
selected = display_graph(edges,nodes,config)
|
211 |
+
|
212 |
+
with col2.container(border=True,height=800):
|
213 |
+
st.markdown("##### Dialoguer avec le graphe")
|
214 |
+
|
215 |
+
for message in st.session_state.chat_graph_history:
|
216 |
+
if isinstance(message, AIMessage):
|
217 |
+
with st.chat_message("AI"):
|
218 |
+
st.markdown(message.content)
|
219 |
+
elif isinstance(message, HumanMessage):
|
220 |
+
with st.chat_message("Moi"):
|
221 |
+
st.write(message.content)
|
222 |
+
|
223 |
+
#check if last message is human message
|
224 |
+
if len(st.session_state.chat_graph_history) > 0:
|
225 |
+
last_message = st.session_state.chat_graph_history[-1]
|
226 |
+
if isinstance(last_message, HumanMessage):
|
227 |
+
with st.chat_message("AI"):
|
228 |
+
retreive = st.session_state.vectorstore.as_retriever()
|
229 |
+
context = retreive.invoke(last_message.content)
|
230 |
+
wrapped_prompt = f"Étant donné le contexte suivant {context}, {last_message.content}"
|
231 |
+
response = st.write_stream(generate_response_via_langchain(wrapped_prompt,stream=True))
|
232 |
+
st.session_state.chat_graph_history.append(AIMessage(content=response))
|
233 |
+
|
234 |
+
if selected is not None:
|
235 |
+
with st.chat_message("AI"):
|
236 |
+
st.markdown(f" EXPLORER LES DONNEES CONTENUES DANS **{selected}**")
|
237 |
+
|
238 |
+
prompts = [f"Extrait moi toutes les informations du noeud ''{selected}'' ➡️",
|
239 |
+
f"Montre moi les conversations autour du noeud ''{selected}'' ➡️"]
|
240 |
+
|
241 |
+
for i,prompt in enumerate(prompts):
|
242 |
+
button = st.button(prompt,key=f"p_{i}",on_click=lambda i=i: st.session_state.chat_graph_history.append(HumanMessage(content=prompts[i])))
|
243 |
+
|
244 |
+
|
245 |
+
|
246 |
|
247 |
node_types = st.session_state.node_types
|
248 |
|
requirements.txt
CHANGED
@@ -23,3 +23,4 @@ python-dotenv
|
|
23 |
langchain-experimental
|
24 |
neo4j
|
25 |
streamlit-agraph
|
|
|
|
23 |
langchain-experimental
|
24 |
neo4j
|
25 |
streamlit-agraph
|
26 |
+
|