[READ-ONLY] Mirror of https://github.com/danielroe/_productivity_app.
0

Configure Feed

Select the types of activity you want to include in your feed.

_productivity_app / main.py
2.5 kB 76 lines
1from fastapi import FastAPI, Request, Form 2from fastapi.staticfiles import StaticFiles 3from fastapi.responses import HTMLResponse, RedirectResponse 4from azure.identity import DefaultAzureCredential, ManagedIdentityCredential 5from azure.identity import get_bearer_token_provider 6from fastapi.templating import Jinja2Templates 7from openai import AzureOpenAI 8 9from fastapi import Form 10from dotenv import load_dotenv 11 12import uvicorn 13import os 14 15load_dotenv() 16 17 18client_args ={} 19 20# Authenticate using an API key (not recommended for production) 21if os.getenv("AZURE_OPENAI_KEY"): 22 client_args["api_key"] = os.getenv("AZURE_OPENAI_KEY") 23else: 24 if client_id := os.getenv("AZURE_OPENAI_CLIENT_ID"): 25 # Authenticate using a user-assigned managed identity on Azure 26 azure_credential = ManagedIdentityCredential( 27 client_id=client_id) 28 else: 29 # Authenticate using the default Azure credential chain 30 azure_credential = DefaultAzureCredential() 31 client_args["azure_ad_token_provider"] = get_bearer_token_provider( 32 azure_credential, "https://cognitiveservices.azure.com/.default") 33 34# Initialize the AzureOpenAI client 35client = AzureOpenAI( 36 api_version=os.getenv('AZURE_OPENAI_API_VERSION') or "2024-02-15-preview", 37 azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com", 38 **client_args, 39) 40 41app = FastAPI() 42 43# Mount static files 44app.mount("/static", StaticFiles(directory="static"), name="static") 45templates = Jinja2Templates(directory="static") 46 47@app.get("/", response_class=HTMLResponse) 48async def read_index(request: Request): 49 return templates.TemplateResponse("index.html", context={"request":request}) 50 51@app.get("/health") 52async def health_check(): 53 return {"status": "OK"} 54 55@app.post("/", response_class=RedirectResponse) 56async def create_plan(request: Request, goal_input: str = Form(...)): 57 58 goal = goal_input 59 60 response = client.chat.completions.create( 61 62 model=os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"), 63 messages=[ 64 {"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."} 65 ], 66 temperature=0, 67 ) 68 69 plan = response.choices[0].message.content 70 print(plan) 71 return templates.TemplateResponse("index.html", context={"request":request, "plan": f"{response.choices[0].message.content}", "goal":goal}) 72 73 74 75if __name__ == "__main__": 76 uvicorn.run(app, host="localhost", port=8080)