Close Menu
    Trending
    • Transform Complexity into Opportunity with Digital Engineering
    • OpenAI Is Fighting Back Against Meta Poaching AI Talent
    • Lessons Learned After 6.5 Years Of Machine Learning
    • Handling Big Git Repos in AI Development | by Rajarshi Karmakar | Jul, 2025
    • National Lab’s Machine Learning Project to Advance Seismic Monitoring Across Energy Industries
    • HP’s PCFax: Sustainability Via Re-using Used PCs
    • Mark Zuckerberg Reveals Meta Superintelligence Labs
    • Prescriptive Modeling Makes Causal Bets – Whether You Know it or Not!
    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

    Handling Big Git Repos in AI Development | by Rajarshi Karmakar | Jul, 2025

    July 1, 2025
    Machine Learning

    A Technical Overview of the Attention Mechanism in Deep Learning | by Silva.f.francis | Jun, 2025

    June 30, 2025
    Machine Learning

    Tone Awareness: Setting the Right Energy for Digital Spaces | by Fred’s Bytes | Jun, 2025

    June 30, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    Transform Complexity into Opportunity with Digital Engineering

    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

    This Industry Needs More Freelancers: Your Next Side Hustle?

    January 27, 2025

    How To Optimize Solar BOS For Value and Efficiency

    May 24, 2025

    Defeating Fraudsters at the Finish Line: The Power of AI in Gaming Transactions

    December 20, 2024
    Our Picks

    Transform Complexity into Opportunity with Digital Engineering

    July 1, 2025

    OpenAI Is Fighting Back Against Meta Poaching AI Talent

    July 1, 2025

    Lessons Learned After 6.5 Years Of Machine Learning

    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.