Marusan030225/Press-RAG
press-rag
プレス加工技術 RAG(Retrieval-Augmented Generation)API サーバー。
自動車ボディのプレス加工技術(冷間プレス・熱間プレス・ホットスタンプ・ヘミング加工・ブランキング)に関する公開文献を検索し、Gemini が回答を生成します。
技術スタック
前提条件
- Python 3.11 以上
- Qdrant Cloud アカウント(無料枠で動作)
- Google AI Studio の Gemini API キー
セットアップ
1. 依存ライブラリのインストール
pip install -e .または uv を使用する場合:
uv sync2. 環境変数の設定
.env.example をコピーして .env を作成:
cp .env.example .env.env を編集:
# Qdrant Cloud
QDRANT_URL=https://xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.eu-central-1-0.aws.cloud.qdrant.io:6333
QDRANT_API_KEY=your_qdrant_api_key_here
QDRANT_COLLECTION=press_rag_documents
# Google Gemini
GEMINI_API_KEY=your_gemini_api_key_here
GEMINI_MODEL=gemini-2.5-flash
# RAG設定(任意)
TOP_K_DEFAULT=5
MIN_SCORE_THRESHOLD=0.60
# サーバー設定(任意)
HOST=0.0.0.0
PORT=8000
CORS_ORIGINS=http://localhost:5173,http://localhost:30003. サーバー起動
Docker を使う場合(推奨・別PCへの移行時):
docker compose up初回起動時は埋め込みモデル(約1.2GB)のダウンロードがあります。2回目以降はキャッシュが使われます。
Python 直接起動の場合:
python -m uvicorn src.api.main:app --host 0.0.0.0 --port 8000Windows の場合は start.bat をダブルクリックでも起動できます。
開発時(ホットリロード有効):
python -m uvicorn src.api.main:app --host 0.0.0.0 --port 8000 --reloadAPI エンドポイント
GET /api/health
サーバーとQdrant接続の確認。
curl http://localhost:8000/api/healthレスポンス例:
{
"status": "ok",
"qdrant": "connected",
"document_count": 981,
"embedding_model": "intfloat/multilingual-e5-large"
}GET /api/scopes
対応工法スコープの一覧。
curl http://localhost:8000/api/scopesレスポンス例:
[
{
"id": "hot_stamping",
"label": "ホットスタンプ",
"description": "ホットスタンプ・熱間プレス焼入れ",
"document_count": 329,
"collection": "press_rag_documents"
}
]スコープID一覧:
POST /api/rag/search
RAG ベクトル検索(チャンク取得)。
curl -X POST http://localhost:8000/api/rag/search \
-H "Content-Type: application/json" \
-d '{
"query": "ホットスタンプの加熱温度",
"scopeId": "hot_stamping",
"top_k": 3,
"lang": "both"
}'パラメータ:
レスポンス例:
{
"results": [
{
"chunk_id": "paper_xxx_chunk_00",
"text": "ホットスタンプでは鋼板を900℃以上に加熱し...",
"score": 0.87,
"source": {
"title": "熱間プレス成形技術の動向",
"source_type": "paper",
"journal": "塑性と加工",
"year": 2022,
"url": "https://...",
"page": 0
}
}
],
"query_id": "uuid-string",
"scope_id": "hot_stamping",
"total_retrieved": 3
}POST /api/llm/generate
LLM 回答生成(SSE ストリーミング)。
curl -X POST http://localhost:8000/api/llm/generate \
-H "Content-Type: application/json" \
-d '{
"query": "ホットスタンプの加熱温度を教えてください",
"context": [
{
"chunk_id": "paper_xxx_chunk_00",
"text": "ホットスタンプでは鋼板を900℃以上に加熱し...",
"score": 0.87,
"source": {"title": "...", "source_type": "paper"}
}
],
"scopeId": "hot_stamping"
}'SSE レスポンス形式:
data: {"text": "ホットスタンプでは、"}
data: {"text": "通常 900〜950℃ に加熱します [1]。"}
data: [DONE]データ収集・取り込み
J-STAGE からの論文収集
# メタデータ収集(実行例)
python -m src.collector.collect_jstage
# PDF一括取り込み(data/raw/ 以下のPDFを処理)
python -m src.ingest.runner
# 対象ディレクトリ指定
python -m src.ingest.runner --dir data/raw/jstage
# 動作確認のみ(Qdrant投入なし)
python -m src.ingest.runner --dry-run対応フォルダ構成
data/raw/
├── jstage/ → source_type: paper
├── jplatpat/ → source_type: patent
└── reports/ → source_type: reportテスト
# ユニットテスト(全件)
python -m pytest tests/ --ignore=tests/test_e2e -v
# E2Eテスト(実Qdrant Cloud接続必須)
python -m pytest tests/test_e2e/ -v --no-cov
# カバレッジ付きで実行
python -m pytest tests/ --ignore=tests/test_e2e --cov=src別PCへの移行手順
press-rag を別の PC に移して使う場合の手順です。
1. フォルダをコピー
press-rag フォルダごと別PCに移す(data/raw/ の PDF も含む)。
2. Docker Desktop をインストール
Docker Desktop for Windows をインストール。
3. .env を設定
.env.example をコピーして .env を作成し、APIキーを設定:
copy .env.example .env.env を編集して以下を設定:
QDRANT_URL=https://xxxxxxxx.qdrant.io
QDRANT_API_KEY=your-qdrant-api-key
GEMINI_API_KEY=your-gemini-api-key
# Buchoai-remake のオリジンを追加
CORS_ORIGINS=http://localhost:5173,http://localhost:3000,http://<buchoai-host>:<port>4. 起動
docker compose uphttp://localhost:8000/api/health でアクセス確認。
Buchoai-remake からの接続
press-rag は Buchoai-remake BFF から以下の3エンドポイントで利用されます:
接続設定 (Buchoai-remake 側):
PRESS_RAG_URL=http://<press-rag-host>:8000CORS の許可追加:
.env の CORS_ORIGINS に Buchoai-remake のオリジンを追加:
CORS_ORIGINS=http://localhost:5173,http://localhost:3000,http://<buchoai-host>:<port>プロジェクト構成
press-rag/
├── src/
│ ├── api/
│ │ ├── main.py # FastAPI アプリ
│ │ ├── routes/
│ │ │ ├── scopes.py # GET /api/scopes
│ │ │ ├── rag.py # POST /api/rag/search
│ │ │ └── llm.py # POST /api/llm/generate
│ │ └── schemas/
│ │ └── request.py # Pydantic リクエストモデル
│ ├── rag/
│ │ ├── embedder.py # multilingual-e5-large 埋め込み
│ │ ├── retriever.py # Qdrant ベクトル検索
│ │ ├── generator.py # Gemini LLM 生成
│ │ └── scope_registry.py # スコープ管理
│ ├── ingest/
│ │ ├── extractor.py # PDF テキスト抽出 (PyMuPDF)
│ │ ├── chunker.py # テキストチャンキング (tiktoken)
│ │ ├── tagger.py # 工法タグ付け
│ │ ├── uploader.py # Qdrant アップロード
│ │ └── runner.py # 一括処理スクリプト
│ ├── collector/
│ │ ├── collect_jstage.py # J-STAGE 論文収集
│ │ ├── collect_patents.py # Espacenet 特許収集
│ │ └── collect_semantic.py # Semantic Scholar 収集
│ └── config.py # 設定管理 (pydantic-settings)
├── tests/
│ ├── test_api/ # API エンドポイントテスト
│ ├── test_rag/ # RAG ロジックテスト
│ ├── test_ingest/ # 取り込み処理テスト
│ ├── test_collector/ # 収集スクリプトテスト
│ └── test_e2e/ # E2E テスト(実Qdrant接続)
├── data/
│ └── raw/ # PDF 格納場所
│ └── jstage/ # J-STAGE 論文PDF
├── docs/ # 仕様書
├── .env.example # 環境変数テンプレート
├── pyproject.toml # プロジェクト設定
└── README.md