This is an example of basic api service using FastAPI and OpenAI. It simply returns answers in Polish language to the provided questions using gpt-4 model knowledge and 'short-term' memory. Short-term memory was implemented to cover course's task needs that is to store conversation data untill question is asked.
- python3
- Azure Subscription
- az cli
- terraform
- Azure Key Vault with a secret called 'openai-api-key'
- Azure Key Vault with a secret called 'openai-api-key'
- Linux Web App with python3 runtime and configured access to the Key Vault
- (Optional) Vnet and service/private endpoints for security.
To deploy required infrastructure, go to iac
folder and execute the following commands:
az login
cd iac
terraform init
terraform apply -var="{IP_ADDRESS_OF_YOUR_MACHINE}" -var="openai_api_key={OPENAI_API_KEY}" -auto-approve
Terraform will deploy all the required infra and you'll need only to deploy the app.
To destroy the infra run the following command:
terraform destroy -var="{IP_ADDRESS_OF_YOUR_MACHINE}" -var="openai_api_key={OPENAI_API_KEY}" -auto-approve
NOTE: Current configuration exposes Key Vault only to web app subnet and to the provided local machine IP address.
To run service locally execute the following commands:
az login # Required to get access to the key vault
pip3 install -r requirements.txt
python3 main.py 'KEY_VAULT_URL'
Add the command above to the Web App startup command box to run service in a Web App.
Questions should be sent to /api/question endpoint, with the following format:
{
"question": "What's the capital of Poland"
}
Service returns following response:
{
"reply": "Stolicą Polski jest Warszawa."
}
In case a piece of an iformation is given:
{
"question": "My name is Alex"
}
Service saves the info utill a question is asked and returns the following response:
{
"reply": "Ok. Got it."
}