{"id":23623304,"url":"https://github.com/gbaeke/aca-openai","last_synced_at":"2025-07-04T03:37:28.540Z","repository":{"id":96506840,"uuid":"608362091","full_name":"gbaeke/aca-openai","owner":"gbaeke","description":"OpenAI Completion Demo","archived":false,"fork":false,"pushed_at":"2023-10-21T14:28:43.000Z","size":55,"stargazers_count":5,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2023-10-21T15:35:45.218Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gbaeke.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-03-01T21:29:05.000Z","updated_at":"2023-05-10T12:40:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"42087cb7-a2dd-42ff-8a4c-9688f47f953b","html_url":"https://github.com/gbaeke/aca-openai","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbaeke%2Faca-openai","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbaeke%2Faca-openai/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbaeke%2Faca-openai/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbaeke%2Faca-openai/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gbaeke","download_url":"https://codeload.github.com/gbaeke/aca-openai/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231539720,"owners_count":18392333,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-27T20:38:36.156Z","updated_at":"2024-12-27T20:38:36.866Z","avatar_url":"https://github.com/gbaeke.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Azure Container Apps Demo: OpenAI Tweet Generator\n\n## Introduction\n\nThis demo shows how to deploy a simple web app that uses the [OpenAI API](https://platform.openai.com/) to generate tweets based on a given prompt. The app is built using [Flask](https://flask.palletsprojects.com/en/1.1.x/) and [Bootstrap](https://getbootstrap.com/).\n\nBecause this is mainly an Azure Container Apps demo, the app is not very sophisticated. The call to the OpenAI model could have been implemented in the Flask UI app. For demonstration purposes, a separate Container App is used to call the OpenAI API. The UI app calls the API app with the help of Dapr. Find more information about Dapr here: [https://dapr.io/](https://dapr.io/).\n\n## Prerequisites\n\n- Azure subscription\n- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli)\n- [Docker](https://docs.docker.com/get-docker/)\n- OpenAI API key or Azure OpenAI API key\n- Github account to fork this repo and run the workflows\n\n## Create container images for first deployment\n\nBefore deploying the infrastructure with Bicep, build the Docker images and push them to Docker Hub. Use the following commands from the root of this repo:\n\n```bash\n# login to Docker Hub\ndocker login\n\ncd ./webui\ndocker build -t \u003cdockerhub-username\u003e/openaiui:latest .\ndocker push \u003cdockerhub-username\u003e/openaiui:latest\n\ncd ../openai\ndocker build -t \u003cdockerhub-username\u003e/openaiapi:latest .\ndocker push \u003cdockerhub-username\u003e/openaiapi:latest\n```\n\nIn `main.bicep`, find the references to the container images and replace them with the Docker Hub images. For example, for the UI, replace `${acr.properties.loginServer}/openaiui:latest` with `\u003cdockerhub-username\u003e/openaiui:latest`. Do the same for the API.\n\n## Deploy Azure OpenAI\n\nTo use Azure OpenAI, you have to request access. In the portal, try to create an Azure OpenAI resource. If you don't have access, you will see a link to a page where you can request access. After you have access, create an Azure OpenAI resource.\n\nOnce you have access, deploy the `text-davinci-003` model and call it `tweeter`. Grab the value of KEY 1 and the endpoint URL from the `Keys and Endpoint` tab of the Azure OpenAI resource. You will need these values in the next steps.\n\nIn `./openai/main.py`, find the following lines:\n\n```python\ntry:\n    response = requests.post(\n        \"https://oa-geba.openai.azure.com/openai/deployments/tweeter/completions?api-version=2022-12-01\",\n        json=payload,\n        headers={\n            \"api-key\": azure_api_key,\n            \"Content-Type\": \"application/json\"   \n        })\n    response = response.json()\n```\n\nReplace `oa-geba.openai.azure.com` with the endpoint URL of your Azure OpenAI resource.\n\n\nYou will need the key in the following section where you deploy the infrastructure. The key will be saved as a secret in Key Vault.\n\n## Optional: Use the OpenAI API\n\nIf you cannot get access to Azure OpenAI, get an account at OpenAI and create your own API key. Use that key in the following section where you deploy the infrastructure. The key will be saved as a secret in Key Vault.\n\n## Deploy infrastructure\n\nUse Bicep to deploy the infrastructure. From the root of this repo, run the following commands:\n\n```bash\nRG=rg-aca-openai\nLOCATION=westeurope\nPREFIX=ai\nAZURE_OPENAI_KEY=\"Your Azure OpenAI API key\"\nOPENAI_KEY=\"Your OpenAI API key\"\n\naz group create --name $RG --location $LOCATION\n\naz deployment group create -g $RG -f ./deploy/main.bicep \\\n    --parameters parPrefix=$PREFIX \\\n    --parameters parLocation=$LOCATION \\\n    --parameters parAzureApiKey=$AZURE_OPENAI_KEY \\\n    --parameters parOpenAiApiKey=$OPENAI_KEY\n```\n\nNote that the API can use either the OpenAI API or the Azure OpenAI API. In the deployment of the API, the `TYPE` environment variable can be set to `Azure` to use Azure OpenAI. In that case you need the Azure OpenAI API key. The other key can be left empty. If you set `TYPE` to `OpenAI`, you need the OpenAI API key.\n\nNow that the infrastructure is deployed, check the name of the `Azure Container Registry` that was deployed. It will be something like `aiacr\u003crandom-string\u003e`. You will need this name in the next step.\n\n## Modify the GitHub Workflows\n\nIn `.github/workflows`, there are two workflows: `build-api.yml` and `build-ui.yml`. The first one builds the API container image and pushes it to the ACR. The second one builds the UI container image and pushes it to ACR. You need to modify the workflows to use the ACR name that was deployed in the previous step.\n\nIn both yml files, find the following line:\n\n```yaml\nenv:\n  PREFIX: \u003cMATCH TO YOUR PREFIX\u003e\n  RG: \u003cMATCH TO YOUR RESOURCE GROUP\u003e\n  ACR: \u003cMATCH TO YOUR ACR SHORT NAME.azurecr.io\u003e\n```\n\nNow run both workflows from the GitHub Actions tab in your forked repo. They can be run manually because they use the `workflow_dispatch` trigger. The workflows will build the container images and push them to the ACR. After that, `az containerapp update` is used to update the container images in the container apps.\n\nIf you want, you can also update `main.bicep` to use the ACR that was deployed.\n\n## Setup the GitHub Secrets\n\nSetup the following secrets in your forked repo:\n- ACR_USERNAME: the short name of the ACR\n- ACR_PASSWORD: the password of the ACR\n- AZURE_SUBSCRIPTION_ID: the subscription ID of the subscription where the infrastructure was deployed\n- AZURE_TENANT_ID: the Azure tenant ID\n- AZURE_CLIENT_ID: the Azure client ID of the app registration used for OIDC authentication\n\nTo setup GitHub for OIDC authentication to Azure, follow the instructions here: https://learn.microsoft.com/en-us/azure/developer/github/connect-from-azure?tabs=azure-portal\n\nTo retrieve the subscription ID and tenant ID, run the following commands:\n\n```bash\naz account show --query id --output tsv\naz account show --query tenantId --output tsv\n```\n\nTo get the name of your ACR in your resource group, run the following command:\n\n```bash\naz acr list --resource-group \u003cresource-group-name\u003e --query \"[].name\" --output tsv\n```\n\nTo get the ACR password, run the following command:\n\n```bash\naz acr credential show --name \u003cacr-name\u003e --query passwords[0].value --output tsv\n```\n\n\n## Test the app\n\nIn the `ai-webui` container app (assuming your prefix is `ai`), find the Application Url. Open the URL in a browser and test the app.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgbaeke%2Faca-openai","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgbaeke%2Faca-openai","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgbaeke%2Faca-openai/lists"}