Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/danielroe/_productivity_app.
Something went wrong. Try again.
_productivity_app main.py
2.5 kB · 76 lines
Python
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677from fastapi import FastAPI, Request, Formfrom fastapi.staticfiles import StaticFilesfrom fastapi.responses import HTMLResponse, RedirectResponsefrom azure.identity import DefaultAzureCredential, ManagedIdentityCredentialfrom azure.identity import get_bearer_token_providerfrom fastapi.templating import Jinja2Templatesfrom openai import AzureOpenAI
from fastapi import Formfrom dotenv import load_dotenv
import uvicornimport os
load_dotenv()
client_args ={}
# Authenticate using an API key (not recommended for production)if os.getenv("AZURE_OPENAI_KEY"): client_args["api_key"] = os.getenv("AZURE_OPENAI_KEY")else: if client_id := os.getenv("AZURE_OPENAI_CLIENT_ID"): # Authenticate using a user-assigned managed identity on Azure azure_credential = ManagedIdentityCredential( client_id=client_id) else: # Authenticate using the default Azure credential chain azure_credential = DefaultAzureCredential() client_args["azure_ad_token_provider"] = get_bearer_token_provider( azure_credential, "https://cognitiveservices.azure.com/.default")
# Initialize the AzureOpenAI clientclient = AzureOpenAI( api_version=os.getenv('AZURE_OPENAI_API_VERSION') or "2024-02-15-preview", azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com", **client_args,)
app = FastAPI()
# Mount static filesapp.mount("/static", StaticFiles(directory="static"), name="static")templates = Jinja2Templates(directory="static")
@app.get("/", response_class=HTMLResponse)async def read_index(request: Request): return templates.TemplateResponse("index.html", context={"request":request})
@app.get("/health")async def health_check(): return {"status": "OK"}
@app.post("/", response_class=RedirectResponse)async def create_plan(request: Request, goal_input: str = Form(...)):
goal = goal_input
response = client.chat.completions.create( model=os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"), messages=[ {"role": "user", "content": f"I want to achieve the following goal: {goal}. Can you create a simple plan to achieve this? Return the results in HTML format, but do not include the html and body tags."} ], temperature=0, )
plan = response.choices[0].message.content print(plan) return templates.TemplateResponse("index.html", context={"request":request, "plan": f"{response.choices[0].message.content}", "goal":goal})
if __name__ == "__main__": uvicorn.run(app, host="localhost", port=8080)