Upload create_tile_from_topic.py with huggingface_hub
Browse files- create_tile_from_topic.py +115 -0
create_tile_from_topic.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import asyncio
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
# プロジェクトルートをPythonパスに追加
|
| 7 |
+
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
|
| 8 |
+
|
| 9 |
+
# Phase 0 & 1で作成した各モジュールをインポート
|
| 10 |
+
from backend.deepseek_local_client import DeepSeekLocalClient, DeepSeekConfig
|
| 11 |
+
from deepseek_prompt_templates import MEDICAL_KNOWLEDGE_GENERATION_PROMPT
|
| 12 |
+
from reasoning_chain_extractor import extract_reasoning_chain
|
| 13 |
+
from knowledge_tile_generator import create_knowledge_tile
|
| 14 |
+
from iath_encoder import IathEncoder
|
| 15 |
+
# 修正:新しいパスからCoordinateMapperとDomainManagerをインポート
|
| 16 |
+
from ilm_athens_engine.domain.manager import DomainManager
|
| 17 |
+
from coordinate_mapper import CoordinateMapper
|
| 18 |
+
|
| 19 |
+
# --- グローバルオブジェクトの初期化 ---
|
| 20 |
+
# DomainManagerとCoordinateMapperは一度だけ初期化する
|
| 21 |
+
domain_manager = DomainManager()
|
| 22 |
+
# このスクリプトは現在、医療ドメイン専用
|
| 23 |
+
medical_schema = domain_manager.get_schema("medical")
|
| 24 |
+
if not medical_schema:
|
| 25 |
+
raise RuntimeError("医療ドメインのスキーマを 'domain_schemas.json' から読み込めませんでした。")
|
| 26 |
+
mapper = CoordinateMapper(medical_schema)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
async def create_knowledge_tile_pipeline(
|
| 30 |
+
topic: str,
|
| 31 |
+
domain_id: str = "medical", # ドメインIDを引数に追加
|
| 32 |
+
audience_level: str = "intermediate",
|
| 33 |
+
output_filename: str = None,
|
| 34 |
+
save_json: bool = True
|
| 35 |
+
):
|
| 36 |
+
"""
|
| 37 |
+
単一のトピックからDeepSeekで知識を生成し、.iathファイルとして保存するまでの
|
| 38 |
+
完全なパイプラインを実行します。
|
| 39 |
+
"""
|
| 40 |
+
print(f"--- パイプライン開始: トピック「{topic}」, ドメイン「{domain_id}」 ---")
|
| 41 |
+
|
| 42 |
+
# 1. DeepSeekで知識を生成
|
| 43 |
+
print("ステップ1: DeepSeekによる知識生成...")
|
| 44 |
+
api = DeepSeekLocalClient(config=DeepSeekConfig(
|
| 45 |
+
api_url="http://localhost:11434",
|
| 46 |
+
model_name="deepseek-r1:32b"
|
| 47 |
+
))
|
| 48 |
+
# ドメインに応じたプロンプトを取得
|
| 49 |
+
from ilm_athens_engine.deepseek_integration.deepseek_runner import DeepSeekR1Engine
|
| 50 |
+
domain_instructions = DeepSeekR1Engine()._get_domain_instructions(domain_id)
|
| 51 |
+
|
| 52 |
+
prompt = f"{domain_instructions}\n\n【トピック】\n{topic}"
|
| 53 |
+
|
| 54 |
+
deepseek_response = await api.generate_async(prompt)
|
| 55 |
+
if not deepseek_response or not deepseek_response.get("success"):
|
| 56 |
+
print(f"エラー: DeepSeekモデルからの応答に失敗しました - {deepseek_response.get('error', '不明なエラー')}")
|
| 57 |
+
return None
|
| 58 |
+
|
| 59 |
+
# 2. テキストの解析と座標へのマッピング
|
| 60 |
+
print("ステップ2: テキストの解析と座標へのマッピング...")
|
| 61 |
+
reasoning_steps = extract_reasoning_chain(deepseek_response)
|
| 62 |
+
if not reasoning_steps:
|
| 63 |
+
reasoning_steps = [{'sequence': 0, 'text': deepseek_response['response'], 'confidence': 0.7, 'concepts': [], 'depth_level': 2}]
|
| 64 |
+
|
| 65 |
+
# ドメインスキーマをロードしてマッパーを初期化
|
| 66 |
+
schema = domain_manager.get_schema(domain_id)
|
| 67 |
+
if not schema:
|
| 68 |
+
print(f"エラー: ドメイン '{domain_id}' のスキーマが見つかりません。")
|
| 69 |
+
return None
|
| 70 |
+
mapper = CoordinateMapper(schema)
|
| 71 |
+
coordinates = mapper.map_reasoning_to_domain_space(reasoning_steps)
|
| 72 |
+
print(f" -> {len(coordinates)}個の推論ステップを座標にマッピングしました。")
|
| 73 |
+
|
| 74 |
+
# 3. Knowledge Tileの構造化
|
| 75 |
+
print("ステップ3: Knowledge Tileの構造化...")
|
| 76 |
+
knowledge_tile = create_knowledge_tile(deepseek_response, coordinates, topic)
|
| 77 |
+
print(f" -> Knowledge Tile ID: {knowledge_tile['metadata']['knowledge_id']}")
|
| 78 |
+
|
| 79 |
+
# 4. エンコードとファイルへの保存
|
| 80 |
+
print("ステップ4: エンコードと.iathファイルへの保存...")
|
| 81 |
+
encoder = IathEncoder()
|
| 82 |
+
compressed_binary = encoder.encode_tile(knowledge_tile)
|
| 83 |
+
|
| 84 |
+
if not output_filename:
|
| 85 |
+
safe_filename = topic.replace(" ", "_").replace("/", "_").replace("(", "").replace(")", "")[:30]
|
| 86 |
+
output_filename = f"{safe_filename}.iath"
|
| 87 |
+
|
| 88 |
+
try:
|
| 89 |
+
with open(output_filename, "wb") as f:
|
| 90 |
+
f.write(compressed_binary)
|
| 91 |
+
print(f" -> 成功: 知識タイルを {output_filename} ({len(compressed_binary)} bytes) に保存しました。")
|
| 92 |
+
except IOError as e:
|
| 93 |
+
print(f" -> エラー: ファイルの保存に失敗しました - {e}")
|
| 94 |
+
return None
|
| 95 |
+
|
| 96 |
+
if save_json:
|
| 97 |
+
json_filename = output_filename.replace(".iath", ".json")
|
| 98 |
+
with open(json_filename, "w", encoding="utf-8") as f:
|
| 99 |
+
json.dump(knowledge_tile, f, indent=2, ensure_ascii=False)
|
| 100 |
+
print(f" -> 検証用の {json_filename} も保存しました。")
|
| 101 |
+
|
| 102 |
+
print("--- パイプライン完了 ---")
|
| 103 |
+
return output_filename
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
if __name__ == '__main__':
|
| 107 |
+
# --- 実行 ---
|
| 108 |
+
# DBに追加したいトピックを指定してください
|
| 109 |
+
target_topic = "心筋梗塞の急性期診断アルゴリズム"
|
| 110 |
+
|
| 111 |
+
# パイプ��インを実行
|
| 112 |
+
# このスクリプトを直接実行する場合、トップレベルで `await` は使えないため、
|
| 113 |
+
# asyncio.run() を使用します。
|
| 114 |
+
import asyncio
|
| 115 |
+
asyncio.run(create_knowledge_tile_pipeline(topic=target_topic))
|