Close Menu
    Trending
    • People are using AI to ‘sit’ with them while they trip on psychedelics
    • Reinforcement Learning in the Age of Modern AI | by @pramodchandrayan | Jul, 2025
    • How This Man Grew His Beverage Side Hustle From $1k a Month to 7 Figures
    • Finding the right tool for the job: Visual Search for 1 Million+ Products | by Elliot Ford | Kingfisher-Technology | Jul, 2025
    • How Smart Entrepreneurs Turn Mid-Year Tax Reviews Into Long-Term Financial Wins
    • Become a Better Data Scientist with These Prompt Engineering Tips and Tricks
    • Meanwhile in Europe: How We Learned to Stop Worrying and Love the AI Angst | by Andreas Maier | Jul, 2025
    • Transform Complexity into Opportunity with Digital Engineering
    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

    Reinforcement Learning in the Age of Modern AI | by @pramodchandrayan | Jul, 2025

    July 1, 2025
    Machine Learning

    Finding the right tool for the job: Visual Search for 1 Million+ Products | by Elliot Ford | Kingfisher-Technology | Jul, 2025

    July 1, 2025
    Machine Learning

    Meanwhile in Europe: How We Learned to Stop Worrying and Love the AI Angst | by Andreas Maier | Jul, 2025

    July 1, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    People are using AI to ‘sit’ with them while they trip on psychedelics

    July 1, 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

    Convex and Concave Function in Machine Learning

    May 13, 2025

    Teen With Cerebral Palsy Starts Business Making $5M a Year

    March 19, 2025

    A First-Day Trump Order: A Federal Stockpile of Bitcoin?

    January 19, 2025
    Our Picks

    People are using AI to ‘sit’ with them while they trip on psychedelics

    July 1, 2025

    Reinforcement Learning in the Age of Modern AI | by @pramodchandrayan | Jul, 2025

    July 1, 2025

    How This Man Grew His Beverage Side Hustle From $1k a Month to 7 Figures

    July 1, 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.