生成AI(LLM)の実務活用が普及する中で、情報システム部門や開発チームは「セキュリティ・個人情報保護」「ハルシネーション(事実誤認)の抑制」「APIの障害・スロットリング対策」という3つの構造的課題に直面しています。

本記事では、これらを精神論や手動のルール運用ではなく、インフラと構造(Policy as Code + ゲートウェイアーキテクチャ)によって自律的に解決するためのリファレンス実装を公開します。


セキュアAIゲートウェイの構成と機能

本アーキテクチャは、FastAPI(非同期ゲートウェイ)、Amazon Bedrock Guardrails、Redis Stack(Vector Search)を組み合わせた構成となっています。

  • Policy as Code ガバナンス: ガードレール定義を YAML コードとしてリポジトリ管理し、GitHub Actions(OIDC認証)で AWS Bedrock へ自動デプロイします。
  • 無停止耐障害性(サーキットブレーカー + セマンティック・キャッシュ): AWS API の混雑や障害時には、Redis の HNSW ベクトル検索を活用し、過去の類似回答を数ミリ秒で自動フォールバック表示します。
  • グラウンディング判定: 事実誤認や根拠のない出力を API レベルで自動ブロックします。

オープンソース・リポジトリ

FastAPI 本体コード、Docker Compose 設定、Terraform(IaC)コード、CI/CD ワークフローを含むすべてのソースコードは、以下の GitHub パブリックリポジトリにて公開しています。

👉 GitHub リポジトリ: https://github.com/Koyano693/open-safe-ai-gateway


人間とAIが互いの境界を尊重し、安全かつ対当に協調するための基盤として、本実装が多くの開発現場に貢献できれば幸いです。

> A Production-Ready Reference Implementation for High-Availability & Self-Governing AI Infrastructure via Policy as Code and Semantic Caching.

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
[![FastAPI](https://img.shields.io/badge/FastAPI-0.110+-green.svg)](https://fastapi.tiangolo.com/)
[![AWS Bedrock](https://img.shields.io/badge/AWS-Amazon%20Bedrock-orange.svg)](https://aws.amazon.com/bedrock/)

---

## 💡 Overview

As enterprise adoption of Generative AI (LLMs) accelerates, software engineering teams face three critical structural challenges:
1. **Security & Privacy Risks**: PII leakage, prompt injection, and unauthorized data access.
2. **Hallucination & Governance**: Unreliable responses and rigid, manual compliance management.
3. **Resilience & Availability**: Cloud API throttling (HTTP 429) and upstream service outages.

**Open Safe-AI Gateway** addresses these challenges through **infrastructure, facts, and structure** rather than speculative assumptions. It provides a multi-layered, zero-trust defense gateway integrating FastAPI, Amazon Bedrock Guardrails, and Redis Stack (Vector Search).

---

## 🚀 Quick Start (Docker Compose)

### 1. Clone the Repository & Setup Environment

```bash
git clone [https://github.com/Koyano693/open-safe-ai-gateway.git](https://github.com/Koyano693/open-safe-ai-gateway.git)
cd open-safe-ai-gateway
cp .env.example .env

Set your AWS credentials and Guardrail parameters inside .env.

2. Launch the Stack

Bash

docker-compose up --build -d

📜 License

MIT License – Feel free to use, modify, and distribute.