Upload knowledge_tile_generator.py with huggingface_hub
Browse files- knowledge_tile_generator.py +119 -0
knowledge_tile_generator.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uuid
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
|
| 4 |
+
def generate_unique_id():
|
| 5 |
+
"""一意のナレッジIDを生成します。"""
|
| 6 |
+
return f"ktile-{uuid.uuid4()}"
|
| 7 |
+
|
| 8 |
+
def extract_references(text: str) -> list:
|
| 9 |
+
"""
|
| 10 |
+
テキストから参考文献を抽出します。
|
| 11 |
+
これはダミー実装です。実際には正規表現などを使用します。
|
| 12 |
+
"""
|
| 13 |
+
references = []
|
| 14 |
+
if "ガイドライン" in text:
|
| 15 |
+
references.append("日本循環器学会ガイドライン2023")
|
| 16 |
+
if "文献" in text:
|
| 17 |
+
references.append(text[text.find("文献"):].split()[0])
|
| 18 |
+
return references
|
| 19 |
+
|
| 20 |
+
def create_knowledge_tile(
|
| 21 |
+
deepseek_response: dict,
|
| 22 |
+
coordinates: list,
|
| 23 |
+
topic: str,
|
| 24 |
+
audience_level: str = "intermediate"
|
| 25 |
+
) -> dict:
|
| 26 |
+
"""
|
| 27 |
+
DeepSeekのレスポンスと座標データから、完全なKnowledge Tileオブジェクトを生成します。
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
deepseek_response (dict): 'thinking'と'response'キーを含むAPIレスポンス。
|
| 31 |
+
coordinates (list): coordinate_mapper.pyで生成された座標情報のリスト。
|
| 32 |
+
topic (str): このナレッジタイルのトピック。
|
| 33 |
+
audience_level (str): 対象読者レベル。
|
| 34 |
+
|
| 35 |
+
Returns:
|
| 36 |
+
dict: Ilm-Athensのスキーマに準拠したKnowledge Tile。
|
| 37 |
+
"""
|
| 38 |
+
if not coordinates:
|
| 39 |
+
raise ValueError("座標リストが空です。推論プロセスから座標を生成できませんでした。")
|
| 40 |
+
|
| 41 |
+
# メインの座標は最初の推論ステップのものを使用
|
| 42 |
+
main_coordinate = coordinates[0]["coordinate"]
|
| 43 |
+
|
| 44 |
+
tile = {
|
| 45 |
+
# 基本情報
|
| 46 |
+
"metadata": {
|
| 47 |
+
"knowledge_id": generate_unique_id(),
|
| 48 |
+
"topic": topic,
|
| 49 |
+
"domain": "medical",
|
| 50 |
+
"audience_level": audience_level,
|
| 51 |
+
"created_at": datetime.now().isoformat(),
|
| 52 |
+
"version": "1.0"
|
| 53 |
+
},
|
| 54 |
+
|
| 55 |
+
# コンテンツ
|
| 56 |
+
"content": {
|
| 57 |
+
"thinking_process": deepseek_response.get("thinking", ""),
|
| 58 |
+
"final_response": deepseek_response.get("response", ""),
|
| 59 |
+
"references": extract_references(deepseek_response.get("response", ""))
|
| 60 |
+
},
|
| 61 |
+
|
| 62 |
+
# 座標情報
|
| 63 |
+
"coordinates": {
|
| 64 |
+
"medical_space": main_coordinate["medical_space"],
|
| 65 |
+
"meta_space": main_coordinate["meta_space"],
|
| 66 |
+
"reasoning_path": coordinates # 全推論ステップの座標
|
| 67 |
+
},
|
| 68 |
+
|
| 69 |
+
# 検証状態
|
| 70 |
+
"verification": {
|
| 71 |
+
"status": "pending_review",
|
| 72 |
+
"initial_certainty": main_coordinate["meta_space"][0],
|
| 73 |
+
"reviewers": [],
|
| 74 |
+
"modifications": [],
|
| 75 |
+
"external_sources": []
|
| 76 |
+
},
|
| 77 |
+
|
| 78 |
+
# メタ情報
|
| 79 |
+
"source": {
|
| 80 |
+
"generator": "deepseek-r1",
|
| 81 |
+
"generation_prompt": topic,
|
| 82 |
+
"thinking_depth": len(coordinates)
|
| 83 |
+
},
|
| 84 |
+
|
| 85 |
+
# トレーサビリティ
|
| 86 |
+
"history": [
|
| 87 |
+
{
|
| 88 |
+
"timestamp": datetime.now().isoformat(),
|
| 89 |
+
"action": "created",
|
| 90 |
+
"by": "deepseek-r1",
|
| 91 |
+
"details": f"初期生成:確実性{coordinates[0]['confidence']:.0%}"
|
| 92 |
+
}
|
| 93 |
+
]
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
return tile
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
# --- ダミーデータによる使用例 ---
|
| 100 |
+
from reasoning_chain_extractor import extract_reasoning_chain
|
| 101 |
+
from coordinate_mapper import map_reasoning_to_medical_space
|
| 102 |
+
|
| 103 |
+
dummy_response = {
|
| 104 |
+
'thinking': 'まず、心筋梗塞の定義から始めます。これは心筋への血流が途絶えることで心筋が壊死する状態です。次に、診断のゴールドスタンダードであるトロポニン測定について考慮します。これは~という理由で重要です。さらに心電図の変化も重要な所見です。ST上昇が見られる場合、急性期と判断されます。',
|
| 105 |
+
'response': '急性心筋梗塞は、迅速な診断と治療が求められる救急疾患です。診断は主に、臨床症状(胸痛など)、心電図変化(ST上昇など)、心筋逸脱酵素(特にトロポニン)の上昇を三本柱として行われます。アルゴリズムとしては、まず疑いがあれば直ちに12誘導心電図を記録し、バイタルサインを確認します。ST上昇があれば、緊急カテーテル治療の適応を考慮します。<参考資料> 日本循環器学会ガイドライン2023',
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
topic = "心筋梗塞の急性期診断"
|
| 109 |
+
|
| 110 |
+
# Week 2のモジュールを使って中間データを生成
|
| 111 |
+
reasoning = extract_reasoning_chain(dummy_response)
|
| 112 |
+
coordinates = map_reasoning_to_medical_space(reasoning)
|
| 113 |
+
|
| 114 |
+
# Knowledge Tileを生成
|
| 115 |
+
knowledge_tile = create_knowledge_tile(dummy_response, coordinates, topic)
|
| 116 |
+
|
| 117 |
+
import json
|
| 118 |
+
print("--- 生成されたKnowledge Tile (JSON) ---")
|
| 119 |
+
print(json.dumps(knowledge_tile, indent=2, ensure_ascii=False))
|