commit 62a8a9c833ce573b2b9fffc98c01f5f25a31d825 Author: KingOchoa Date: Wed May 28 00:11:38 2025 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61577d2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# Virtual environments +venv/ +env/ +ENV/ +.venv/ +.python-version + +# Pip +pip-log.txt +pip-delete-this-directory.txt + +# Distribution / packaging +build/ +dist/ +*.egg-info/ +.eggs/ + +# Logs +*.log + +# IDEs / Editors +.vscode/ +.idea/ +*.sublime-project +*.sublime-workspace + +# macOS & Windows +.DS_Store +Thumbs.db + +# Environment & credentials +.env +*.pem diff --git a/main.py b/main.py new file mode 100644 index 0000000..4ccfcff --- /dev/null +++ b/main.py @@ -0,0 +1,58 @@ +from fastapi import FastAPI, HTTPException +from pydantic import BaseModel, Field +import boto3 +import os +from fastapi.responses import StreamingResponse +import time + +app = FastAPI() + +# Define request body +class BedrockRequest(BaseModel): + question: str + requestSessionId: str = 'user' + agentId: str = "ROJCGWHSC0" + agentAliasId: str ="TQ8VDTVQII" + +# AWS Bedrock client setup +def get_bedrock_client(): + return boto3.client( + 'bedrock-agent-runtime' + ) + +@app.get("/health") +def health_check(): + return {"message": "OK"} + +@app.post("/bedrock-agent") +def call_bedrock_agent(payload: BedrockRequest): + client = get_bedrock_client() + try: + response_stream = client.invoke_agent( + sessionId=payload.requestSessionId, + agentId=payload.agentId, + agentAliasId=payload.agentAliasId, + inputText=payload.question, + ) + + def event_stream(): + for event in response_stream["completion"]: + if "chunk" in event: + # Decode bytes to string if needed + chunk_bytes = event["chunk"].get("bytes") + if chunk_bytes: + yield chunk_bytes.decode("utf-8") + else: + yield event["chunk"].get("text", "") + + return StreamingResponse(event_stream(), media_type="text/plain") + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + +@app.get("/test-stream") +def test_stream(): + def generator(): + for i in range(5): + yield f"Chunk {i}\n" + time.sleep(1) + return StreamingResponse(generator(), media_type="text/plain") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f0c5f8e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +fastapi +uvicorn +boto3 +requests \ No newline at end of file