Skip to content

Commit

Permalink
Merge pull request #264 from AndreasKratzig/fix/missingparam
Browse files Browse the repository at this point in the history
fix for milligram startup failure due to missing params
  • Loading branch information
waeltken authored May 7, 2024
2 parents a95a187 + e712e78 commit 3ac71e8
Show file tree
Hide file tree
Showing 7 changed files with 1,066 additions and 363 deletions.
4 changes: 2 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ name = "pypi"
[packages]
uvicorn = "*"
azure-storage-blob = "*"
uvloop = "*"
fastapi = "*"
httptools = "*"
python-multipart = "*"
nest-asyncio = "*"
openai = "*"

[dev-packages]
black = "*"

[requires]
python_version = "3.9"
python_version = "3.12"

[pipenv]
allow_prereleases = true
1,293 changes: 975 additions & 318 deletions Pipfile.lock

Large diffs are not rendered by default.

55 changes: 41 additions & 14 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,14 @@
from fastapi.responses import RedirectResponse, StreamingResponse, JSONResponse
from pydantic import BaseModel
import openai
import os



app = FastAPI()
cache_header = {"Cache-Control": "max-age=31556952"}

shared_container_client = None

client = openai.AzureOpenAI(
api_key=os.getenv("CHAT_API_KEY"),
api_version="2023-12-01-preview",
azure_endpoint = os.getenv("CHAT_API_ENDPOINT"),
azure_deployment=os.getenv("AZURE_OPENAI_MODEL_NAME"),

)

shared_openai_client = None

async def get_container_client():
"""Get a client to interact with the blob storage container."""
Expand All @@ -45,17 +36,54 @@ async def get_container_client():
shared_container_client = service.get_container_client("images")
return shared_container_client

async def get_openai_client():
"""Get a client to interact with the Azure OpenAI chat API."""
global shared_openai_client
if not shared_openai_client:
chat_api_key = os.environ["CHAT_API_KEY"]
chat_azure_endpoint = os.environ["CHAT_API_ENDPOINT"]
chat_azure_deployment = os.environ["AZURE_OPENAI_MODEL_NAME"]

shared_openai_client = openai.AzureOpenAI(
api_key=chat_api_key,
api_version="2023-12-01-preview",
azure_endpoint=chat_azure_endpoint,
azure_deployment=chat_azure_deployment
)
return shared_openai_client

@app.exception_handler(KeyError)
async def unicorn_exception_handler(request: Request, exc: KeyError):
"""Handle missing environment variables."""
if exc.args[0] == "CUSTOMCONNSTR_STORAGE":
match exc.args[0]:
case "CUSTOMCONNSTR_STORAGE":
return JSONResponse(
status_code=500,
content={
"message": f"Oops! You forgot to set the STORAGE connection string for your Azure Storage Account. You can test locally by setting CUSTOMCONNSTR_STORAGE. 🤓"
},
)
case "CHAT_API_KEY":
return JSONResponse(
status_code=500,
content={
"message": f"Oops! You forgot to set the CHAT_API_KEY environment variable. 🤓"
},
)
case "CHAT_API_ENDPOINT":
return JSONResponse(
status_code=500,
content={
"message": f"Oops! You forgot to set the CHAT_API_ENDPOINT environment variable. 🤓"
},
)
case "AZURE_OPENAI_MODEL_NAME":
return JSONResponse(
status_code=500,
content={
"message": f"Oops! You forgot to set the AZURE_OPENAI_MODEL_NAME environment variable. 🤓"
},
)
raise exc


Expand All @@ -70,7 +98,6 @@ async def unicorn_exception_handler(request: Request, exc: KeyError):
)
raise exc


class Image(BaseModel):
created_at: datetime = None
image_url: str
Expand Down Expand Up @@ -143,13 +170,13 @@ async def upload(


@app.post("/chat")
async def chat(prompt: Prompt):
async def chat(prompt: Prompt, azOpenAIClient=Depends(get_openai_client)):
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt.message}
]

response = client.chat.completions.create(
response = azOpenAIClient.chat.completions.create(
model="gpt-35-turbo",
messages=messages,
)
Expand Down
4 changes: 2 additions & 2 deletions instructions/day1/ApplicationPart2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Our [Azure Web App](https://learn.microsoft.com/en-us/azure/static-web-apps/) is
- Make sure to adjust the settings according to the image below:
- Name: `<pick your own unique name>`
- Publish: `Code`
- Runtime stack: `Python 3.8`
- Runtime stack: `Python 3.12`
- Operating System: `Linux`
- Region: `West Europe`
![backend 0](./images/light/BackendApp0.png)
Expand Down Expand Up @@ -212,7 +212,7 @@ Now your storage account and web app are successfully connected and can communic
There is still a small configuration missing. Our app uses a ready-made module so that users can interact with their content. But this module is not installed yet. In order for it to be installed, we provide the web app with a configuration that is executed when the app is launched, allowing users to interact with our app's data.

- Navigate to **_Configuration_** under _Settings_.
- Under the tab **_General settings_** you should find the _Stack settings_. For our backend we are working with the programming language Python - more specifically Python 3.8.
- Under the tab **_General settings_** you should find the _Stack settings_. For our backend we are working with the programming language Python - more specifically Python 3.12.
- Behind **_Startup Command_** enter `gunicorn -k uvicorn.workers.UvicornWorker` and hit _Save_.
![How to configure the Startup Command of the Web application](./images/light/AppServiceStartupCommand.png)

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified instructions/day1/ApplicationPart2/images/light/BackendApp0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 46 additions & 27 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
anyio==3.6.2
asgiref==3.4.1
azure-core==1.26.1
azure-storage-blob==12.14.1
certifi==2022.12.7
cffi==1.15.1
charset-normalizer==2.1.1
click==8.1.3
cryptography==38.0.4
fastapi==0.88.0
annotated-types==0.6.0
anyio==4.3.0
azure-core==1.30.1
azure-storage-blob==12.20.0b1
certifi==2024.2.2
cffi==1.16.0
charset-normalizer==3.3.2
click==8.1.7
cryptography==42.0.5
distro==1.9.0
dnspython==2.6.1
email_validator==2.1.1
fastapi==0.111.0
fastapi-cli==0.0.2
h11==0.14.0
httptools==0.5.0
idna==3.4
httpcore==1.0.5
httptools==0.6.1
httpx==0.27.0
idna==3.7
isodate==0.6.1
msrest==0.7.1
nest-asyncio==1.5.6
oauthlib==3.2.2
pycparser==2.21
pydantic==1.10.2
python-multipart==0.0.5
requests==2.28.1
requests-oauthlib==1.3.1
Jinja2==3.1.3
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
nest-asyncio==1.6.0
openai==1.25.1
orjson==3.10.2
pycparser==2.22
pydantic==2.7.1
pydantic_core==2.18.2
Pygments==2.17.2
python-dotenv==1.0.1
python-multipart==0.0.9
PyYAML==6.0.1
requests==2.31.0
rich==13.7.1
shellingham==1.5.4
six==1.16.0
sniffio==1.3.0
starlette==0.22.0
typing_extensions==4.10.0
urllib3==1.26.13
uvicorn==0.20.0
uvloop==0.17.0
openai==1.12.0
sniffio==1.3.1
starlette==0.37.2
tqdm==4.66.4
typer==0.12.3
typing_extensions==4.11.0
ujson==5.9.0
urllib3==2.2.1
uvicorn==0.29.0
uvloop==0.19.0
watchfiles==0.21.0
websockets==12.0

0 comments on commit 3ac71e8

Please sign in to comment.