ponshu-room-lite/tools/synology/ai-proxy/README.md

105 lines
2.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Ponshu Room AI Proxy Server
## 概要
Ponshu Room Liteアプリのバックエンドプロキシサーバー。Gemini APIへのリクエストを中継し、以下の機能を提供します
### 主な機能
1. **APIキー保護**
- アプリにAPIキーを埋め込まない
- サーバー側で環境変数として管理
2. **デバイスID認証**
- SHA256ハッシュ化されたデバイスIDで認証
- 未登録デバイスのアクセスを制限可能(将来実装)
3. **レート制限**
- 1デバイスあたり1日10回までLite版
- Pro版では無制限に拡張可能
4. **使用状況ログ**
- JSON形式で使用履歴を記録
- 30日以上前のデータは自動削除
## 技術スタック
- **言語**: Python 3.11
- **フレームワーク**: FastAPI
- **HTTPクライアント**: httpx
- **デプロイ**: Docker
## エンドポイント
### `GET /`
ヘルスチェック
### `GET /health`
詳細なヘルスチェックAPIキー設定状況など
### `POST /analyze`
日本酒ラベル画像を解析
**リクエスト:**
```json
{
"device_id": "sha256ハッシュ64文字",
"images": ["base64エンコードされた画像1", "画像2", ...],
"prompt": "カスタムプロンプト(オプション)"
}
```
**レスポンス:**
```json
{
"success": true,
"data": {
"name": "獺祭 純米大吟醸",
"brand": "旭酒造",
"prefecture": "山口県",
...
},
"usage": {
"today": 3,
"limit": 10
}
}
```
### `GET /usage/{device_id}`
デバイスの使用状況を取得
## 環境変数
| 変数名 | 説明 | デフォルト |
|--------|------|-----------|
| `GEMINI_API_KEY` | Gemini API Key | (必須) |
| `GEMINI_MODEL` | 使用するモデル | `gemini-1.5-flash` |
| `RATE_LIMIT_PER_DAY` | 1日あたりの制限 | `10` |
## データ保存
- 使用状況: `/app/data/usage.json`
- 30日以上前のデータは自動削除
## セキュリティ
- デバイスIDはSHA256ハッシュ化されたもののみ受け入れ
- CORS設定でオリジン制限可能
- APIキーは環境変数で管理コードに埋め込まない
## ローカルでのテスト
```bash
# 依存関係インストール
pip install -r requirements.txt
# 環境変数設定
export GEMINI_API_KEY="YOUR_API_KEY"
# サーバー起動
python server.py
```
http://localhost:8080 でアクセス可能