Commit ·
f9891e3
1
Parent(s): bd1f662
Update generate_knowledge_graph.py
Browse files
generate_knowledge_graph.py
CHANGED
|
@@ -8,12 +8,11 @@ from pyvis.network import Network
|
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
import os
|
| 10 |
import asyncio
|
| 11 |
-
import streamlit as st
|
| 12 |
|
| 13 |
load_dotenv()
|
| 14 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
|
| 16 |
-
llm = ChatOpenAI(temperature=0, model_name="gpt-4o
|
| 17 |
graph_transformer = LLMGraphTransformer(llm=llm)
|
| 18 |
|
| 19 |
async def extract_graph_data(text):
|
|
@@ -71,13 +70,8 @@ def visualize_graph(graph_documents):
|
|
| 71 |
return net
|
| 72 |
|
| 73 |
|
| 74 |
-
@st.cache_data
|
| 75 |
-
def get_graph_data_cached(text):
|
| 76 |
-
return asyncio.run(extract_graph_data(text))
|
| 77 |
-
|
| 78 |
-
|
| 79 |
def generate_knowledge_graph(text):
|
| 80 |
-
graph_documents =
|
| 81 |
net = visualize_graph(graph_documents)
|
| 82 |
return net, graph_documents
|
| 83 |
|
|
@@ -102,7 +96,7 @@ def answer_question_with_graph(question, graph_documents, k_relations=7):
|
|
| 102 |
context = "\n".join([doc.page_content for doc in retrieved_docs])
|
| 103 |
|
| 104 |
prompt = PromptTemplate(
|
| 105 |
-
template='''Tu es un assistant expert qui répond aux questions en se basant UNIQUEMENT sur ce sous-ensemble de relations extraites d'un graphe de connaissances.\n\nContexte (Relations pertinentes trouvées) :\n{context}\n\nQuestion : {question}\n\nRéponds de manière claire et concise en anglais.
|
| 106 |
input_variables=["context", "question"]
|
| 107 |
)
|
| 108 |
|
|
|
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
import os
|
| 10 |
import asyncio
|
|
|
|
| 11 |
|
| 12 |
load_dotenv()
|
| 13 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 14 |
|
| 15 |
+
llm = ChatOpenAI(temperature=0, model_name="gpt-4o")
|
| 16 |
graph_transformer = LLMGraphTransformer(llm=llm)
|
| 17 |
|
| 18 |
async def extract_graph_data(text):
|
|
|
|
| 70 |
return net
|
| 71 |
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
def generate_knowledge_graph(text):
|
| 74 |
+
graph_documents = asyncio.run(extract_graph_data(text))
|
| 75 |
net = visualize_graph(graph_documents)
|
| 76 |
return net, graph_documents
|
| 77 |
|
|
|
|
| 96 |
context = "\n".join([doc.page_content for doc in retrieved_docs])
|
| 97 |
|
| 98 |
prompt = PromptTemplate(
|
| 99 |
+
template='''Tu es un assistant expert qui répond aux questions en se basant UNIQUEMENT sur ce sous-ensemble de relations extraites d'un graphe de connaissances.\n\nContexte (Relations pertinentes trouvées) :\n{context}\n\nQuestion : {question}\n\nRéponds de manière claire et concise en anglais. Réponds directement à la question sans jamais faire référence au contexte, aux relations fournies, au document ou au graphe (ne commence pas par "Based on...", "According to the...", etc.). Mentionne le contexte uniquement si la réponse n'y figure pas (dans ce cas, réponds explicitement : "I cannot answer this question based on the provided context.").''',
|
| 100 |
input_variables=["context", "question"]
|
| 101 |
)
|
| 102 |
|