From edd49b065d8299a306072b5c0ea9400c12aa6945 Mon Sep 17 00:00:00 2001 From: Pierre Malarme Date: Fri, 17 May 2024 14:40:04 +0200 Subject: [PATCH] Enable dynamic update of Azure OpenAI model deployment name --- backend/README.md | 31 ++++++++++++++++++++++++++++++- backend/api.py | 21 ++++++++++++++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/backend/README.md b/backend/README.md index cbb85bc..457a773 100644 --- a/backend/README.md +++ b/backend/README.md @@ -13,5 +13,34 @@ To run the backend, you need to: ``` 2. Install the dependencies using `pip install -r requirements.txt` -3. Setup the parameters in api.py (do not forget to setup Azure OpenAI parameters at the beginign of the file) +3. Setup the parameters in api.py (do not forget to setup Azure OpenAI parameters at the beginning of the file) 4. Run the backend using `python api.py` or using `F5` in Visual Studio Code + +## Setting the Azure OpenAI Deployment Name + +To dynamically update the model deployment name for Azure OpenAI, set the `AZURE_OPENAI_DEPLOYMENT_NAME` environment variable. This allows the backend to fetch the deployment name at runtime. + +For example, on Windows, you can set the environment variable like this: + +```bash +set AZURE_OPENAI_DEPLOYMENT_NAME=your_model_deployment_name +``` + +And on Linux or macOS: + +```bash +export AZURE_OPENAI_DEPLOYMENT_NAME=your_model_deployment_name +``` + +## Model Deployment Name Endpoints + +The backend provides two REST endpoints to get and set the model deployment name dynamically: + +- `GET /model-deployment-name`: Returns the current model deployment name. +- `POST /model-deployment-name`: Accepts a JSON payload with the `model_deployment_name` field to update the model deployment name. Example payload: + +```json +{ + "model_deployment_name": "new_model_deployment_name" +} +``` diff --git a/backend/api.py b/backend/api.py index 2e27ce6..dddcbd2 100644 --- a/backend/api.py +++ b/backend/api.py @@ -43,8 +43,8 @@ # Set the number of results to be returned by the retriever retriever_k = 20 -# Set the name of the GPT model to be used for the chatbot -gpt_model_name = '' +# Fetch the model deployment name dynamically from environment variable +gpt_model_name = os.getenv('AZURE_OPENAI_DEPLOYMENT_NAME', '') # Set the name of the embeddings model to be used for the retriever embeddings_model_name = 'text-embedding-ada-002' @@ -105,5 +105,20 @@ def ask(): error = 'Invalid request' return make_response(error, 400) +@app.route('/model-deployment-name', methods=['GET', 'POST']) +@cross_origin() +def model_deployment_name(): + global gpt_model_name + if request.method == 'POST': + data = request.json + if 'model_deployment_name' in data: + gpt_model_name = data['model_deployment_name'] + os.environ['AZURE_OPENAI_DEPLOYMENT_NAME'] = gpt_model_name + return make_response({'message': 'Model deployment name updated successfully.'}, 200) + else: + return make_response({'error': 'Model deployment name not provided.'}, 400) + elif request.method == 'GET': + return make_response({'model_deployment_name': gpt_model_name}, 200) + if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000) \ No newline at end of file + app.run(host='0.0.0.0', port=5000)