Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
4 changes: 2 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import streamlit as st
import streamlit.components.v1 as components # For embedding custom HTML
from generate_knowledge_graph import generate_knowledge_graph

# This will start the application and open it in your default web browser (typically at http://localhost:8501).
# Set up Streamlit page configuration
st.set_page_config(
page_icon=None,
Expand All @@ -28,7 +28,7 @@

if uploaded_file is not None:
# Read the uploaded file content and decode it as UTF-8 text
text = uploaded_file.read().decode("utf-8")
text = uploaded_file.read().decode("utf-8", errors="replace")

# Button to generate the knowledge graph
if st.sidebar.button("Generate Knowledge Graph"):
Expand Down
94 changes: 94 additions & 0 deletions data/NikkeiBP_LLDs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
Log Action ID Input Action Output Intention Rationale Annotation Tools/Knowledge
1 1 no input �����̃R�X�p���ǂ����̂���肷�� �o�b�t�@ ������̌����������邽�� no rationale no annotation no tools/knowledge
1 2 �o�b�t�@ �T�C�N���^�C���̂΂�‚������炷 no output ���̒l������ƁA�����Ƀo�b�t�@���m�ۂ���K�v���������� no rationale no annotation no tools/knowledge
1 3 no input �����l���𑝂₷ �œK�Ȑl���͕s�������A�Ƃ肠�����ő�� �����̓{�g���l�b�N�ɂȂ肪���Ȃ��� no rationale "�E���z�I�Ȑ��Y�V�X�e���Ƃ��Ă�
�������Ԃ�0���ǂ�
�E�܂��A����̖��ݒ�ł͐l����Ȃǂ��l������Ă��炸���������܂蔽�f���Ă��Ȃ����߁A�ׂ����l�@���s���Ă��Ȃ�" no tools/knowledge
1 4 "�œK�Ȑl���͕s�������A
�Ƃ肠�����ő��" ���ׂē��������ꍇ�̃|�C���g���v�Z���� 675.7 �ő�̃R�X�g�𒲂ׂ邽�� no rationale no annotation no tools/knowledge
1 5 675.7 �|�C���g�Ɛ��Y���̃o�����X���l�����ĕ��j�𗧂Ă� "�o�b�t�@�̓R�X�g�I�ɂ͏��Ȃ����A���Y���ւ̉e���͕s��
��ƃJ�b�g�̓T�C�N���^�C�����ł������A���ɍ������
���̃l�b�N�͍ő�܂œ������Ă��ǂ�����" no intention no rationale no annotation no tools/knowledge
1 6 "�o�b�t�@�̓R�X�g�I�ɂ͏��Ȃ����A���Y���ւ̉e���͕s��
��ƃJ�b�g�̓T�C�N���^�C�����ł������A���ɍ������
���̃l�b�N�͍ő�܂œ������Ă��ǂ�����" ��ƃJ�b�g�A����H���̃T�C�N���^�C�����ő�܂Ō��炷 no output no intention no rationale no annotation no tools/knowledge
1 7 no input �n����ނ̃T�C�N���^�C�����ő�܂Ō��炷 no output no intention no rationale no annotation no tools/knowledge
1 8 no input �T�u���C���͂��̂܂܂ɂ��� no output ���C���H���ɑ΂��ăT�C�N���^�C�����Z���A�e���x���Ⴂ���� no rationale no annotation no tools/knowledge
1 9 ���P�āF�R�X�g440 EAZY GO�ɒl����͂��� no output no intention no rationale no annotation no tools/knowledge
1 10 no input ���ϐ��Y�ʂ��m�F���� "�R�X�g�F440
���Y�ʁF77.1" no intention no rationale no annotation no tools/knowledge
1 11 �R�X�g�F440���Y�ʁF77.1 ���̉��P�Ă𐶐����� "100�|�C���g�ύX���Ă���������
�ω��ʂ𔼕���50�ɂ���" ���Ԃ̈Ă𐶐����� no rationale no annotation no tools/knowledge
1 12 "100�|�C���g�ύX���Ă���������
�ω��ʂ𔼕���50�ɂ���" �V�~�����[�V�����Ő��Y�ʂƃR�X�g���m�F���� "�R�X�g�F240
���Y�ʁF69.45" no intention no rationale no annotation no tools/knowledge
1 13 "�R�X�g�F240
���Y�ʁF69.45" ���̉��P�Ă𐶐����� ���Ԃ̃p�����[�^�𐶐�����B�����n����t���‚•ύX���邽�߁A�T�C�N���^�C���������������ő�܂Ō��炷�B ���Ԃ̈Ă𐶐����� no rationale no annotation no tools/knowledge
1 14 ���Ԃ̃p�����[�^�𐶐�����B�����n����t���‚•ύX���邽�߁A�T�C�N���^�C���������������ő�܂Ō��炷�B �V�~�����[�V�����Ő��Y�ʂƃR�X�g���m�F���� "�R�X�g�F340
���Y�ʁF76.87" no intention no rationale no annotation no tools/knowledge
1 15 "�R�X�g�F340
���Y�ʁF76.87" �ŗǂ̈Ă�I������ ���P��3 no intention ��3�͈�1�ƂقƂ�ǐ��Y�ʂ��ς��Ȃ����߁A�R�X�g�̏��Ȃ���3��I������ ��3�͈�1�ƂقƂ�ǐ��Y�ʂ��ς��Ȃ����߁A�R�X�g�̏��Ȃ���3��I������ no tools/knowledge
2 16 no input �l�b�N�H������肷�� no output no intention no rationale no annotation no tools/knowledge
2 17 no input ���Y���C���̃��f����g�ݗ��Ă� no output no intention no rationale no annotation no tools/knowledge
2 18 no input �V�~�����[�V���������s���� no output no intention no rationale no annotation no tools/knowledge
2 19 no input ���Y�����m�F���� no output no intention no rationale no annotation no tools/knowledge
2 20 no input �ғ����Ԃ��m�F���� no output no intention no rationale no annotation no tools/knowledge
2 21 no input �������Ԃ��m�F���� no output no intention no rationale no annotation no tools/knowledge
2 22 no input �V�~�����[�V�����̐U�镑�����m�F���� no output no intention no rationale no annotation no tools/knowledge
2 23 �^�}�S�T���h���C���̏o������50���ȉ��ƒႢ "�^�}�S�T���h���C����
��������P�_��T��" no output no intention no rationale no annotation no tools/knowledge
2 24 no input ���P�p�����[�^��I������ �����l�� no intention no rationale no annotation no tools/knowledge
2 25 �����l�� �����l������l���₷ �o�����F60�����x no intention no rationale no annotation no tools/knowledge
2 26 �o�����F60�����x �ʂ̉��P�_��T������ no output no intention �z��ȏ�ɉ��P���Ȃ� no annotation no tools/knowledge
2 27 no input ���P�p�����[�^��I������ �T�C�N���^�C�� no intention no rationale no annotation no tools/knowledge
2 28 �T�C�N���^�C�� �^�}�S�ƃn�����H�H����CT��50�ɒZ�k���� �o�����F60�����x no intention no rationale no annotation no tools/knowledge
2 29 �o�����F60�����x �o�b�t�@�[�T�C�Y���m�F���� �o�b�t�@�[�T�C�Y���O�a���Ă���A�o�����ɉe�����Ă���”\�������� no intention no rationale no annotation no tools/knowledge
2 30 �o�b�t�@�[�T�C�Y���O�a���Ă���A�o�����ɉe�����Ă���”\�������� ���P����o�b�t�@�[�T�C�Y��I�肷�� B1�̃o�b�t�@�[�T�C�Y no intention no rationale no annotation no tools/knowledge
2 31 B1�̃o�b�t�@�[�T�C�Y B1�̃o�b�t�@�[�T�C�Y��3����5�ɕύX �o�����F63.12% no intention no rationale no annotation no tools/knowledge
2 32 �o�����F63.12% ���̉��P�p�����[�^��T������ �n���J�b�g�H�� no intention ���ʂ͕ς��Ȃ������̂ŁA����ȏ�̐l�������͂��܂�e�����Ȃ� no annotation no tools/knowledge
2 33 �n���J�b�g�H�� �n���J�b�g�H����CT��5������ no output no intention no rationale no annotation no tools/knowledge
2 34 no input �o�b�t�@�[�T�C�Y3�ɖ߂� �o�b�t�@�[�T�C�Y��ύX����Ƃǂ��ŃX�^�b�N���Ă��邩�����肵�₷�� no intention no rationale no annotation no tools/knowledge
2 35 �o�b�t�@�[�T�C�Y��ύX����Ƃǂ��ŃX�^�b�N���Ă��邩�����肵�₷�� ���̉��P�p�����[�^��T������ no output no intention no rationale no annotation no tools/knowledge
2 36 no input ���P����H����I�肷�� ��H�� no intention no rationale no annotation no tools/knowledge
2 37 ��H�� ��H����CT��5�k�߂� "�o�����F69%,�|�C���g�F120" no intention no rationale no annotation no tools/knowledge
2 38 "�o�����F69%,�|�C���g�F120" MC2��CT��45�ɕύX �o�����F69�� �n���J�b�g�H���ɏd�݂����肻�� CT��ύX����Əo��������Ԍ��サ������ no annotation no tools/knowledge
2 39 �o�����F69�� �}�e���A���t���[�̃f�[�^�𕪐͂��� B1�ł̃}�e���A���t���[�̌X�Ίp�x���傫���Ȃ��Ă��� no intention no rationale no annotation no tools/knowledge
2 40 B1�ł̃}�e���A���t���[�̌X�Ίp�x���傫���Ȃ��Ă��� B1�̏����݌ɂ�5�ɕύX���� �o�����F70�� no intention no rationale no annotation no tools/knowledge
2 41 �o�����F70�� B3�̏����݌ɂ�5�ɕύX���� �o�����F70�� no intention no rationale no annotation no tools/knowledge
2 42 �o�����F70�� "M3,4��CT��45�ɕύX����" "�o�����F70.89%
�����|�C���g�F121" no intention no rationale no annotation no tools/knowledge
2 43 "�o�����F70.89%
�����|�C���g�F121" "M3,4��CT��50�ɖ߂�" no output no intention "M3,4�̓|�C���g�g�������Ɍ��������ω����Ȃ�����" no annotation no tools/knowledge
3 44 no input ���P�O�̏o�����𒲂ׂ� �o����:55.20 no intention no rationale no annotation no tools/knowledge
3 45 �o����:55.20 ���P�Ώۂ̗D�揇�ʂ����߂� "1.�}�V��
2.�o�b�t�@
3.�l" ���P���j�����߂��� �o�����ɉe����^���₷���ƍl�����邩�� no annotation "�E�}�V��
�E�o�b�t�@
�E�l"
3 46 "1.�}�V��
2.�o�b�t�@
3.�l" ���P�Ώۂ����߂� �}�V�� no intention �D�揇�ʂ��ł��������� no annotation no tools/knowledge
3 47 �}�V�� �}�V����CT�𒲂ׂ� �e�}�V����CT no intention no rationale no annotation no tools/knowledge
3 48 �e�}�V����CT ���P�Ώۂ̃}�V�������߂� "M2,M3" no intention no rationale no annotation no tools/knowledge
3 49 "M2,M3" M2��M3��CT�̕ύX�”\�͈͂𒲂ׂ� "M2:45-55,M3:45-55" no intention no rationale no annotation no tools/knowledge
3 50 "M2:45-55,M3:45-55" M2��M3��CT�����P���� "M2:45,M3:45" �ő�̉��P�Ō��ʂ�m�肽������ �����”\��CT�̉����l������ no annotation no tools/knowledge
3 51 "M2:45,M3:45" �o�����𒲂ׂ� �o����:55.33 ���ʂ�m�肽�� no rationale no annotation no tools/knowledge
3 52 �o����:55.33 ���P�O�Ɖ��P��̏o�������r���� "���P�O:55.20,���P��:55.33" no intention no rationale no annotation no tools/knowledge
3 53 "���P�O:55.20,���P��:55.33" ���P���j��]������ �}�V�����P�ɂ����ʂ͏����� no intention no rationale no annotation no tools/knowledge
3 54 �}�V�����P�ɂ����ʂ͏����� ���ʂ����������R���l���� ���R�FCT�̓{�g���l�b�N�ł͂Ȃ����� no intention no rationale no annotation no tools/knowledge
3 55 ���R�FCT�̓{�g���l�b�N�ł͂Ȃ����� B6�̃o�b�t�@�̃T�C�Y��ύX���� B6�̃o�b�t�@�T�C�Y:10 no intention no rationale no annotation no tools/knowledge
3 56 B6�̃o�b�t�@�T�C�Y:10 �ύX�Ă�]������ B6�o�b�t�@�T�C�Y�ύX�ɂ����ʂ͏����� no intention no rationale no annotation no tools/knowledge
3 57 B6�o�b�t�@�T�C�Y�ύX�ɂ����ʂ͏����� "B1,B3,B4�̃o�b�t�@��ύX����" "B1�o�b�t�@�F10,B3�o�b�t�@�F10,B4�o�b�t�@�F10" �o�b�t�@�ύX�ɂ��o�����ւ̉e���𒲂ׂ��� no rationale no annotation no tools/knowledge
3 58 "B1�o�b�t�@�F10,B3�o�b�t�@�F10,B4�o�b�t�@�F10" �ύX�Ă�]������ ���ʏ����� no intention no rationale no annotation no tools/knowledge
3 59 ���ʏ����� �����l����ύX���� "5:63,4�F63.17,3�F63.19,2�F63.11" �ő�̉��P����������ŃR�X�g�팸���������Ă������� no rationale no annotation no tools/knowledge
3 60 "5:63,4�F63.17,3�F63.19,2�F63.11" �����l�������肷�� �����l���F2 no intention �R�X�g�΃p�t�H�[�}���X���ł��ǂ����� no annotation no tools/knowledge
3 61 �����l���F2 �eMC��CT�������l�ɍ��킹�� "M2:45,M3:45,M4:45" no intention no rationale no annotation no tools/knowledge
3 62 "M2:45,M3:45,M4:45" �R�X�g�𒲂ׂ� �R�X�g:75 no intention no rationale no annotation no tools/knowledge
3 63 �R�X�g:75 �eMC��CT��ύX���� "M2:50,M3:50,M4:50" no intention no rationale no annotation no tools/knowledge
3 64 "M2:50,M3:50,M4:50" �R�X�g�𒲂ׂ� �R�X�g:69.17 no intention no rationale no annotation no tools/knowledge
3 65 �R�X�g:69.17 CT�ƃR�X�g�̊֌W�𒲂ׂ� no output no intention no rationale no annotation no tools/knowledge
3 66 no input �o�b�t�@�̃T�C�Y��ύX���� B1�̃o�b�t�@:3 no intention no rationale no annotation no tools/knowledge
3 67 B1�̃o�b�t�@:3 �ύX�”\�ȃo�b�t�@�𒲂ׂ� "B3,B6" no intention no rationale no annotation no tools/knowledge
3 68 "B3,B6" �ύX�”\�ȃo�b�t�@�̃T�C�Y��ύX���� "B3:3,B6:3" no intention no rationale no annotation no tools/knowledge
3 69 "B3:3,B6:3" �c��̃o�b�t�@�̌��ʂ𒲂ׂ� "B4:���ʏ�����,B5:���ʏ�����" no intention no rationale no annotation no tools/knowledge
3 70 "B4:���ʏ�����,B5:���ʏ�����" ���܂ł̉��P����𐮗����� �v���b�g�} no intention no rationale no annotation no tools/knowledge
105 changes: 102 additions & 3 deletions generate_knowledge_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
from dotenv import load_dotenv
import os
import asyncio

import re
import datetime

# Load the .env file
load_dotenv()
# Get API key from environment variable
api_key = os.getenv("OPENAI_API_KEY")
# api_key = input("Please enter your OpenAI API key: ")
# os.environ["OPENAI_API_KEY"] = api_key

llm = ChatOpenAI(temperature=0, model_name="gpt-4o")
llm = ChatOpenAI(temperature=0, model_name="gpt-5")

graph_transformer = LLMGraphTransformer(llm=llm)

Expand Down Expand Up @@ -123,5 +126,101 @@ def generate_knowledge_graph(text):
pyvis.network.Network: The visualized network graph object.
"""
graph_documents = asyncio.run(extract_graph_data(text))
# Recorde the generated graph in Mearmaid format
_export_graph_to_mermaid(graph_documents, filename="./graphs/knowledge_graph.mmd")
net = visualize_graph(graph_documents)
return net
return net

## My Added Fuctions Start Here ##
def _export_graph_to_mermaid(graph_documents, direction="TD", include_types=True, filename=None):
"""
Convert GraphDocument nodes and relationships to a Mermaid diagram string and optionally save it.

Args:
graph_documents (list): A list of GraphDocument objects (as returned by the graph transformer).
direction (str): Mermaid graph direction, e.g. "TD" (top-down), "LR" (left-right). Default "TD".
include_types (bool): If True, include node types in labels and relationship types as edge labels.
filename (str or None): If provided, save the mermaid text to this file path.

Returns:
str: The generated Mermaid diagram as a string (starts with "graph <direction>").

Example:
mermaid_text = export_graph_to_mermaid(graph_documents, direction="LR", filename="graph.mmd")
"""
if not graph_documents:
return f"graph {direction}\n"

if filename == None:
now = datetime.datetime.now()
filename = f"./knowledge_graph{now.year}-{now.month}-{now.day}-{now.hour}-{now.minute}.mmd"

doc = graph_documents[0]
nodes = getattr(doc, "nodes", []) or []
relationships = getattr(doc, "relationships", []) or []

def _sanitize_id(s):
# Keep IDs safe for Mermaid (alphanumeric and underscores)
return re.sub(r"[^A-Za-z0-9_]", "_", str(s))

def _escape_label(s):
# Escape double quotes for Mermaid label usage
return str(s).replace('"', '\\"')

id_map = {}
for node in nodes:
raw_id = getattr(node, "id", None)
if raw_id is None:
continue
safe_id = _sanitize_id(raw_id)
# Ensure unique safe ids if collisions occur
suffix = 1
base = safe_id
while safe_id in id_map.values():
suffix += 1
safe_id = f"{base}_{suffix}"
id_map[raw_id] = safe_id

lines = [f"graph {direction}"]

# Declare nodes (ensures isolated nodes are shown)
for node in nodes:
raw_id = getattr(node, "id", None)
if raw_id is None:
continue
safe_id = id_map[raw_id]
label_parts = [raw_id]
if include_types:
node_type = getattr(node, "type", None)
if node_type:
label_parts.append(f"({node_type})")
label = "\\n".join(_escape_label(p) for p in label_parts)
lines.append(f'{safe_id}["{label}"]')

# Add edges with optional relationship type labels
for rel in relationships:
src_raw = getattr(getattr(rel, "source", None), "id", None)
tgt_raw = getattr(getattr(rel, "target", None), "id", None)
if src_raw not in id_map or tgt_raw not in id_map:
# skip edges that reference unknown nodes
continue
src = id_map[src_raw]
tgt = id_map[tgt_raw]
rel_label = ""
if include_types:
rel_type = getattr(rel, "type", None)
if rel_type:
rel_label = f'|{_escape_label(str(rel_type))}|'
lines.append(f"{src} -->{rel_label} {tgt}")

mermaid_text = "\n".join(lines) + "\n"

if filename:
try:
with open(filename, "w", encoding="utf-8") as f:
f.write(mermaid_text)
except Exception:
# fail silently but still return the text
pass

return mermaid_text
81 changes: 81 additions & 0 deletions graphs/knowledge_graph.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
graph TD
____["システム\n(System)"]
B1["B1\n(Buffer)"]
B3["B3\n(Buffer)"]
B4["B4\n(Buffer)"]
B5["B5\n(Buffer)"]
B6["B6\n(Buffer)"]
M2["M2\n(Machine)"]
M3["M3\n(Machine)"]
M4["M4\n(Machine)"]
___["コスト\n(Metric)"]
__["効率\n(Metric)"]
_____2["ポイント\n(Metric)"]
_______["サイクルタイム\n(Metric)"]
________2["バッファサイズ\n(Metric)"]
________["シミュレーション\n(Process)"]
___3["ケース3\n(Concept)"]
Eazy_Go["Eazy Go\n(Tool)"]
____440["コスト:440\n(Value)"]
___77_1["効率:77.1\n(Value)"]
____240["コスト:240\n(Value)"]
___69_45["効率:69.45\n(Value)"]
____340["コスト:340\n(Value)"]
___76_87["効率:76.87\n(Value)"]
____75["コスト:75\n(Value)"]
____69_17["コスト:69.17\n(Value)"]
___55_20["効率:55.20\n(Value)"]
___55_33["効率:55.33\n(Value)"]
_____675_7["ポイント:675.7\n(Value)"]
_____121["ポイント:121\n(Value)"]
Ct_45["Ct:45\n(Value)"]
Ct_50["Ct:50\n(Value)"]
________10["バッファサイズ:10\n(Value)"]
________3["バッファサイズ:3\n(Value)"]
____ -->|INCLUDES| B1
____ -->|INCLUDES| B3
____ -->|INCLUDES| B4
____ -->|INCLUDES| B5
____ -->|INCLUDES| B6
____ -->|INCLUDES| M2
____ -->|INCLUDES| M3
____ -->|INCLUDES| M4
____ -->|HAS| ___
____ -->|HAS| __
____ -->|HAS| _____2
M2 -->|HAS| _______
M3 -->|HAS| _______
M4 -->|HAS| _______
B1 -->|HAS| ________2
B3 -->|HAS| ________2
B4 -->|HAS| ________2
B6 -->|HAS| ________2
___ -->|VALUE| ____440
__ -->|VALUE| ___77_1
___ -->|VALUE| ____240
__ -->|VALUE| ___69_45
___ -->|VALUE| ____340
__ -->|VALUE| ___76_87
__ -->|VALUE| ___55_20
__ -->|VALUE| ___55_33
___ -->|VALUE| ____75
___ -->|VALUE| ____69_17
_____2 -->|VALUE| _____675_7
_____2 -->|VALUE| _____121
M2 -->|VALUE| Ct_45
M3 -->|VALUE| Ct_45
M4 -->|VALUE| Ct_45
M2 -->|VALUE| Ct_50
M3 -->|VALUE| Ct_50
M4 -->|VALUE| Ct_50
B1 -->|VALUE| ________10
B3 -->|VALUE| ________10
B4 -->|VALUE| ________10
B6 -->|VALUE| ________10
B1 -->|VALUE| ________3
B3 -->|VALUE| ________3
B6 -->|VALUE| ________3
____ -->|SELECTED| ___3
____ -->|USES| Eazy_Go
________ -->|MEASURES| ___
________ -->|MEASURES| __
Loading