Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azurechina #71

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ REDIS_ARGS=--requirepass $REDIS_PASSWORD
BLOB_ACCOUNT_NAME=YOUR_AZURE_BLOB_STORAGE_ACCOUNT_NAME
BLOB_ACCOUNT_KEY=YOUR_AZURE_BLOB_STORAGE_ACCOUNT_KEY
BLOB_CONTAINER_NAME=YOUR_AZURE_BLOB_STORAGE_CONTAINER_NAME
BLOB_ENDPOINT_SUFFIX=core.windows.net
QUEUE_NAME=doc-processing
FORM_RECOGNIZER_ENDPOINT=YOUR_AZURE_FORM_RECOGNIZER_ENDPOINT
FORM_RECOGNIZER_KEY=YOUR_AZURE_FORM_RECOGNIZER_KEY
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ A simple web application for a OpenAI-enabled document search. This repo uses Az
![Architecture](docs/architecture.png)

# IMPORTANT NOTE (OpenAI generated)
1) We did some changes to make it work in Azure china, which is also named 21v Azure. because the OpenAI service, all other related service are available in Azure China. so we can deply the Apps to Azure china, it will call the OpenAI servicec hosted in Global Azure.
2) To make it more simply, we generate the K8S file, in includes the configmap/deployment/service yaml. for the original .env file, we generated a similar configmap file to save all the config colume, then te deplyment yaml fill will use it.

We have made some changes to the data format in the latest update of this repo.
<br>The new format is more efficient and compatible with the latest standards and libraries. However, we understand that some of you may have existing applications that rely on the previous format and may not be able to migrate to the new one immediately.

Expand All @@ -25,6 +28,7 @@ You have multiple options to run the code:
- [Run everything locally in Python with Conda (WebApp only)](#run-everything-locally-in-python-with-conda-webapp-only)
- [Run everything locally in Python with venv](#run-everything-locally-in-python-with-venv)
- [Run WebApp locally in Docker against an existing Redis deployment](#run-webapp-locally-in-docker-against-an-existing-redis-deployment)
- [Run WebApp locally in K8S against an existing Redis deployment](#run-webapp-locally-in-K8S-against-an-existing-redis-deployment)

## Deploy on Azure (WebApp + Azure Cache for Redis Enterprise + Batch Processing)
[![Deploy to Azure](https://aka.ms/deploytoazurebutton)](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fruoccofabrizio%2Fazure-open-ai-embeddings-qna%2Fmain%2Finfrastructure%2FdeploymentACRE.json)
Expand Down Expand Up @@ -169,7 +173,27 @@ docker run --env-file .env -p 8080:80 your_docker_registry/your_docker_image:you
Note: You can use
- WebApp.Dockerfile to build the Web Application
- BatchProcess.Dockerfile to build the Azure Function for Batch Processing
-
## Run WebApp locally in K8S against an existing Redis deployment

### needed - Build the Docker image yourself, since we upate the code to cordinate with the Azure China blob endpoinsts. (only needed when you use Azure China)

```code (already updated for this Azure china branch)
1) For WebApp.Dockerfile, update the exposed port from 80 to 8088. since 80 is already used by the batch service. since batch and web containers will be in the same pod. they can't use the same exposed port. (in my example, they can't, and I am some douting for the difference between pod and host when using docker-compose, which docker-compose can support since it can map container port to different host port)
2) code\utilities\azureblobstorage.py, go to line 13, update the EndpointSuffix=core.chinacloudapi.cn
```

```console
docker build . -f WebApp.Dockerfile -t your_docker_registry/your_docker_image:your_tag
docker push your_docker_registry/your_docker_image:your_tag
```

```K8S part
generate a configmap file based on the `.env` as described in as described in [Environment variables](#environment-variables)
kubectl apply -f myconfigmap.yml
#update the all-in-one.yml file to use own docker containers.
kubectl apply -f all-in-one.yml
```
## Environment variables

Here is the explanation of the parameters:
Expand Down
48 changes: 48 additions & 0 deletions all-in-one.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
apiVersion: apps/v1
kind: Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: azure-openai-front-01
spec:
replicas: 1
selector:
matchLabels:
app: azure-openai-front-01
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
minReadySeconds: 5
template:
metadata:
labels:
app: azure-openai-front-01
spec:
containers:
- name: web
image: openai.azurecr.cn/oai-embeddings:latest
ports:
- containerPort: 8088
envFrom:
- configMapRef:
name: env-configmap
- name: batch
image: fruocco/oai-batch:latest
ports:
- containerPort: 80
envFrom:
- configMapRef:
name: env-configmap
---
apiVersion: v1
kind: Service
metadata:
name: azure-openai-front-01
spec:
type: LoadBalancer
ports:
- port: 8088
targetPort: 8088
selector:
app: azure-openai-front-01
8 changes: 6 additions & 2 deletions code/utilities/azureblobstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
from dotenv import load_dotenv

class AzureBlobStorageClient:
def __init__(self, account_name: str = None, account_key: str = None, container_name: str = None):
def __init__(self, account_name: str = None, account_key: str = None, blob_endPoint_suffix: str = None, container_name: str = None):

load_dotenv()

self.account_name : str = account_name if account_name else os.getenv('BLOB_ACCOUNT_NAME')
self.account_key : str = account_key if account_key else os.getenv('BLOB_ACCOUNT_KEY')
self.connect_str : str = f"DefaultEndpointsProtocol=https;AccountName={self.account_name};AccountKey={self.account_key};EndpointSuffix=core.windows.net"
self.blob_endPoint_suffix : str = blob_endPoint_suffix if blob_endPoint_suffix else os.getenv('BLOB_ENDPOINT_SUFFIX')
# comment and update it to support both global azure and Azure China, they are of different blob end point suffix.
# self.connect_str : str = f"DefaultEndpointsProtocol=https;AccountName={self.account_name};AccountKey={self.account_key};EndpointSuffix=core.chinacloudapi.cn"
self.connect_str : str = f"DefaultEndpointsProtocol=https;AccountName={self.account_name};AccountKey={self.account_key};EndpointSuffix={blob_endPoint_suffix}"

self.container_name : str = container_name if container_name else os.getenv('BLOB_CONTAINER_NAME')
self.blob_service_client : BlobServiceClient = BlobServiceClient.from_connection_string(self.connect_str)

Expand Down
34 changes: 34 additions & 0 deletions config-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: env-configmap
data:
OPENAI_ENGINE: davinci
OPENAI_DEPLOYMENT_TYPE: Text
OPENAI_EMBEDDINGS_ENGINE: text-embedding-ada-002
OPENAI_EMBEDDINGS_ENGINE_DOC: text-embedding-ada-002
OPENAI_EMBEDDINGS_ENGINE_QUERY: text-embedding-ada-002
OPENAI_API_BASE: "https://cog-pfj4y3k2moyjw.openai.azure.com/"
OPENAI_API_KEY: "[update to your key]"
OPENAI_TEMPERATURE: "0.7"
OPENAI_MAX_TOKENS: "-1"
REDIS_ADDRESS: "[update to your address]"
REDIS_PORT: "6379"
REDIS_PASSWORD: redis-stack-password
BLOB_ACCOUNT_NAME: storage4openai
BLOB_ACCOUNT_KEY: "[update to your key]"
BLOB_ENDPOINT_SUFFIX: "core.windows.net"
BLOB_CONTAINER_NAME: embedding
QUEUE_NAME: doc-processing
FORM_RECOGNIZER_ENDPOINT: "https://formreader.cognitiveservices.azure.cn/"
FORM_RECOGNIZER_KEY: "[update to your key]"
CHUNK_SIZE: "500"
CHUNK_OVERLAP: "100"
TRANSLATE_ENDPOINT: "https://api.translator.azure.cn/"
TRANSLATE_KEY: "[update to your key]"
TRANSLATE_REGION: chinanorth3
VNET_DEPLOYMENT: "false"
NUMBER_OF_EMBEDDINGS_FOR_QNA: "3"
CONVERT_ADD_EMBEDDINGS_URL: "http://batch/api/BatchStartProcessing"
AzureWebJobsStorage: "DefaultEndpointsProtocol=https;AccountName=storage4openai;AccountKey=[update to your key];EndpointSuffix=core.chinacloudapi.cn"
QUESTION_PROMPT: "Please reply to the question using only the information present in the text above. If you can't find it, reply 'Not in the text'.\nQuestion: _QUESTION_\nAnswer:"