Close Menu
    Trending
    • TikTok to lay off hundreds of UK content moderators
    • People Really Only Care About These 3 Things at Work — Do You Offer Them?
    • Can Machines Really Recreate “You”?
    • Meet the researcher hosting a scientific conference by and for AI
    • Current Landscape of Artificial Intelligence Threats | by Kosiyae Yussuf | CodeToDeploy : The Tech Digest | Aug, 2025
    • Data Protection vs. Data Privacy: What’s the Real Difference?
    • Elon Musk and X reach settlement with axed Twitter workers
    • Labubu Could Reach $1B in Sales, According to Pop Mart CEO
    AIBS News
    • Home
    • Artificial Intelligence
    • Machine Learning
    • AI Technology
    • Data Science
    • More
      • Technology
      • Business
    AIBS News
    Home»Machine Learning»Deploying Your AI Agent with FastAPI, Docker, and AWS ECS | by Kushal Banda | Jun, 2025
    Machine Learning

    Deploying Your AI Agent with FastAPI, Docker, and AWS ECS | by Kushal Banda | Jun, 2025

    Team_AIBS NewsBy Team_AIBS NewsJune 17, 2025No Comments1 Min Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    from fastapi import FastAPI, HTTPException
    from pydantic import BaseModel
    from openai import OpenAI
    import os
    from typing import Elective
    from dotenv import load_dotenv

    # Load atmosphere variables from .env file
    load_dotenv()

    app = FastAPI(title=”OpenAI Agent API”, description=”Easy FastAPI software for AI Brokers”)

    def get_openai_client():
    “””Get OpenAI consumer with correct error dealing with”””
    api_key = os.getenv(“OPENAI_API_KEY”)
    if not api_key:
    elevate HTTPException(
    status_code=500,
    element=”OpenAI API key not configured. Please set the OPENAI_API_KEY atmosphere variable.”
    )
    return OpenAI(api_key=api_key)

    class QueryRequest(BaseModel):
    question: str
    mannequin: Elective[str] = “gpt-4.1-mini”
    max_tokens: Elective[int] = 1000

    class QueryResponse(BaseModel):
    response: str
    model_used: str

    @app.get(“/”)
    async def root():
    return {“message”: “Welcome to OpenAI Agent API”}

    @app.get(“/well being”)
    async def health_check():
    # Verify if API secret’s configured
    api_key_status = “configured” if os.getenv(“OPENAI_API_KEY”) else “not configured”
    return {
    “standing”: “wholesome”,
    “openai_api_key”: api_key_status
    }

    @app.put up(“/chat”, response_model=QueryResponse)
    async def chat_with_agent(request: QueryRequest):
    “””
    Ship a question to the OpenAI agent and get a response
    “””
    attempt:
    # Get consumer with error dealing with
    consumer = get_openai_client()

    response = consumer.chat.completions.create(
    mannequin=request.mannequin,
    messages=[
    {“role”: “system”, “content”: “You are a helpful assistant.”},
    {“role”: “user”, “content”: request.query}
    ],
    max_tokens=request.max_tokens
    )

    return QueryResponse(
    response=response.selections[0].message.content material,
    model_used=request.mannequin
    )

    besides HTTPException:
    # Re-raise HTTP exceptions (like lacking API key)
    elevate
    besides Exception as e:
    elevate HTTPException(status_code=500, element=f”Error processing request: {str(e)}”)

    if __name__ == “__main__”:
    import uvicorn
    uvicorn.run(app, host=”0.0.0.0″, port=8090)



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleUK watchdog fines 23andMe for ‘profoundly damaging’ data breach
    Next Article The CEO’s Guide to Thriving as a First-Time Parent
    Team_AIBS News
    • Website

    Related Posts

    Machine Learning

    Current Landscape of Artificial Intelligence Threats | by Kosiyae Yussuf | CodeToDeploy : The Tech Digest | Aug, 2025

    August 22, 2025
    Machine Learning

    Optimizing ML Costs with Azure Machine Learning | by Joshua Fox | Aug, 2025

    August 22, 2025
    Machine Learning

    Top Tools and Skills for AI/ML Engineers in 2025 | by Raviishankargarapti | Aug, 2025

    August 22, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    TikTok to lay off hundreds of UK content moderators

    August 22, 2025

    I Tried Buying a Car Through Amazon: Here Are the Pros, Cons

    December 10, 2024

    Amazon and eBay to pay ‘fair share’ for e-waste recycling

    December 10, 2024

    Artificial Intelligence Concerns & Predictions For 2025

    December 10, 2024

    Barbara Corcoran: Entrepreneurs Must ‘Embrace Change’

    December 10, 2024
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    Most Popular

    What AI’s Gaming Origins Can Teach Us About Business Breakthroughs

    March 6, 2025

    Why Skipping This One PR Move Could Stall Your Startup’s Growth Before It Even Begins

    July 16, 2025

    9 AI Hentai Chatbots No Sign Up

    June 6, 2025
    Our Picks

    TikTok to lay off hundreds of UK content moderators

    August 22, 2025

    People Really Only Care About These 3 Things at Work — Do You Offer Them?

    August 22, 2025

    Can Machines Really Recreate “You”?

    August 22, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2024 Aibsnews.comAll Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.