mirror of
https://github.com/hoshikawa2/agent_platform_oci.git
synced 2026-07-09 14:04:19 +00:00
First commit
This commit is contained in:
7
apps/ai_gateway/Dockerfile
Normal file
7
apps/ai_gateway/Dockerfile
Normal file
@@ -0,0 +1,7 @@
|
||||
FROM python:3.11-slim
|
||||
WORKDIR /app
|
||||
COPY apps/ai_gateway/requirements.txt /app/requirements.txt
|
||||
RUN pip install --no-cache-dir -r /app/requirements.txt
|
||||
COPY apps/ai_gateway /app
|
||||
EXPOSE 9100
|
||||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "9100"]
|
||||
18
apps/ai_gateway/README.md
Normal file
18
apps/ai_gateway/README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# AI Gateway
|
||||
|
||||
Camada desacoplada para abstração, roteamento, controle e governança de chamadas LLM.
|
||||
|
||||
Este componente não substitui o Agent Runtime. Ele centraliza políticas de modelo, seleção de provider, fallback, telemetria e controles corporativos.
|
||||
|
||||
## Rotas
|
||||
|
||||
- `GET /health`
|
||||
- `GET /models`
|
||||
- `POST /v1/chat/completions`
|
||||
|
||||
## Execução local
|
||||
|
||||
```bash
|
||||
cd apps/ai_gateway
|
||||
uvicorn app.main:app --host 0.0.0.0 --port 9100 --reload
|
||||
```
|
||||
0
apps/ai_gateway/app/__init__.py
Normal file
0
apps/ai_gateway/app/__init__.py
Normal file
81
apps/ai_gateway/app/main.py
Normal file
81
apps/ai_gateway/app/main.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from typing import Any, Literal
|
||||
|
||||
import httpx
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class Message(BaseModel):
|
||||
role: Literal["system", "user", "assistant", "tool"]
|
||||
content: str | list[Any] | None = None
|
||||
|
||||
|
||||
class ChatCompletionRequest(BaseModel):
|
||||
model: str | None = None
|
||||
messages: list[Message]
|
||||
temperature: float | None = None
|
||||
max_tokens: int | None = None
|
||||
metadata: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
class ModelRoute(BaseModel):
|
||||
provider: str
|
||||
model: str
|
||||
base_url: str | None = None
|
||||
|
||||
|
||||
app = FastAPI(title="Agent Framework OCI - AI Gateway", version="0.1.0")
|
||||
|
||||
|
||||
def resolve_route(requested_model: str | None, metadata: dict[str, Any]) -> ModelRoute:
|
||||
provider = metadata.get("provider") or os.getenv("AI_GATEWAY_DEFAULT_PROVIDER", "oci_openai")
|
||||
model = requested_model or metadata.get("profile_model") or os.getenv("AI_GATEWAY_DEFAULT_MODEL", "openai.gpt-4.1")
|
||||
base_url = os.getenv("AI_GATEWAY_OPENAI_COMPAT_BASE_URL")
|
||||
return ModelRoute(provider=provider, model=model, base_url=base_url)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
def health() -> dict[str, Any]:
|
||||
return {"status": "ok", "component": "ai_gateway"}
|
||||
|
||||
|
||||
@app.get("/models")
|
||||
def models() -> dict[str, Any]:
|
||||
return {
|
||||
"default_provider": os.getenv("AI_GATEWAY_DEFAULT_PROVIDER", "oci_openai"),
|
||||
"default_model": os.getenv("AI_GATEWAY_DEFAULT_MODEL", "openai.gpt-4.1"),
|
||||
"purpose": "model routing, policy control and LLM abstraction",
|
||||
}
|
||||
|
||||
|
||||
@app.post("/v1/chat/completions")
|
||||
async def chat_completions(payload: ChatCompletionRequest) -> dict[str, Any]:
|
||||
route = resolve_route(payload.model, payload.metadata)
|
||||
# Safe default: when no upstream is configured, return route decision only.
|
||||
# Production deployments should configure AI_GATEWAY_OPENAI_COMPAT_BASE_URL and credentials.
|
||||
if not route.base_url:
|
||||
return {
|
||||
"gateway": "ai_gateway",
|
||||
"mode": "dry_run",
|
||||
"route": route.model_dump(),
|
||||
"message": "No upstream base URL configured. Set AI_GATEWAY_OPENAI_COMPAT_BASE_URL to proxy requests.",
|
||||
}
|
||||
|
||||
api_key = os.getenv("AI_GATEWAY_API_KEY") or os.getenv("OCI_GENAI_API_KEY") or os.getenv("OPENAI_API_KEY")
|
||||
headers = {"Authorization": f"Bearer {api_key}"} if api_key else {}
|
||||
body = payload.model_dump(exclude_none=True)
|
||||
body["model"] = route.model
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=float(os.getenv("AI_GATEWAY_TIMEOUT_SECONDS", "60"))) as client:
|
||||
resp = await client.post(f"{route.base_url.rstrip('/')}/chat/completions", json=body, headers=headers)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
if isinstance(data, dict):
|
||||
data.setdefault("gateway", "ai_gateway")
|
||||
data.setdefault("route", route.model_dump())
|
||||
return data
|
||||
except httpx.HTTPError as exc:
|
||||
raise HTTPException(status_code=502, detail=f"AI upstream request failed: {exc}") from exc
|
||||
5
apps/ai_gateway/requirements.txt
Normal file
5
apps/ai_gateway/requirements.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
fastapi>=0.111
|
||||
uvicorn[standard]>=0.30
|
||||
pydantic>=2
|
||||
httpx>=0.27
|
||||
PyYAML>=6
|
||||
Reference in New Issue
Block a user