CoolFace
Apppublic

Marusan030225/Press-RAG

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
App README

press-rag

プレス加工技術 RAG(Retrieval-Augmented Generation)API サーバー。

自動車ボディのプレス加工技術(冷間プレス・熱間プレス・ホットスタンプ・ヘミング加工・ブランキング)に関する公開文献を検索し、Gemini が回答を生成します。


技術スタック

コンポーネント詳細
API サーバーFastAPI 0.115 + uvicorn
ベクトル DBQdrant Cloud (Free Tier, Frankfurt)
埋め込みモデルintfloat/multilingual-e5-large (1024次元)
LLMGemini 2.5 Flash
言語Python 3.11+

前提条件

  • —Python 3.11 以上
  • —Qdrant Cloud アカウント(無料枠で動作)
  • —Google AI Studio の Gemini API キー

セットアップ

1. 依存ライブラリのインストール

bash
pip install -e .

または uv を使用する場合:

bash
uv sync

2. 環境変数の設定

.env.example をコピーして .env を作成:

bash
cp .env.example .env

.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:3000

3. サーバー起動

Docker を使う場合(推奨・別PCへの移行時):

bash
docker compose up

初回起動時は埋め込みモデル(約1.2GB)のダウンロードがあります。2回目以降はキャッシュが使われます。

Python 直接起動の場合:

bash
python -m uvicorn src.api.main:app --host 0.0.0.0 --port 8000

Windows の場合は start.bat をダブルクリックでも起動できます。

開発時(ホットリロード有効):

bash
python -m uvicorn src.api.main:app --host 0.0.0.0 --port 8000 --reload

API エンドポイント

GET /api/health

サーバーとQdrant接続の確認。

bash
curl http://localhost:8000/api/health

レスポンス例:

json
{
  "status": "ok",
  "qdrant": "connected",
  "document_count": 981,
  "embedding_model": "intfloat/multilingual-e5-large"
}

GET /api/scopes

対応工法スコープの一覧。

bash
curl http://localhost:8000/api/scopes

レスポンス例:

json
[
  {
    "id": "hot_stamping",
    "label": "ホットスタンプ",
    "description": "ホットスタンプ・熱間プレス焼入れ",
    "document_count": 329,
    "collection": "press_rag_documents"
  }
]

スコープID一覧:

ID工法
press_formingプレス加工全般
cold_pressing冷間プレス
hot_pressing熱間プレス
hot_stampingホットスタンプ
hemmingヘミング加工
blankingブランキング

POST /api/rag/search

RAG ベクトル検索(チャンク取得)。

bash
curl -X POST http://localhost:8000/api/rag/search \
  -H "Content-Type: application/json" \
  -d '{
    "query": "ホットスタンプの加熱温度",
    "scopeId": "hot_stamping",
    "top_k": 3,
    "lang": "both"
  }'

パラメータ:

パラメータ型必須説明
querystring✓質問テキスト(1〜500文字)
scopeIdstring—工法フィルター(上記スコープID参照)
top_kinteger—取得件数(1〜20、デフォルト: 5)
langstring—ja / en / both(デフォルト: both)

レスポンス例:

json
{
  "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 ストリーミング)。

bash
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 からの論文収集

bash
# メタデータ収集(実行例)
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

テスト

bash
# ユニットテスト(全件)
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キーを設定:

bash
copy .env.example .env

.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. 起動

bash
docker compose up

http://localhost:8000/api/health でアクセス確認。


Buchoai-remake からの接続

press-rag は Buchoai-remake BFF から以下の3エンドポイントで利用されます:

エンドポイント用途
GET /api/scopes工法スコープ一覧取得
POST /api/rag/searchベクトル検索(チャンク取得)
POST /api/llm/generateLLM回答生成(SSEストリーミング)

接続設定 (Buchoai-remake 側):

env
PRESS_RAG_URL=http://<press-rag-host>:8000

CORS の許可追加:

.env の CORS_ORIGINS に Buchoai-remake のオリジンを追加:

env
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