Skip to content

Commit

Permalink
Merge pull request #204 from microsoft/vNext-Dev
Browse files Browse the repository at this point in the history
0.3-Gamma Release Candidate
  • Loading branch information
dayland authored Sep 13, 2023
2 parents 61b5e1c + 439f428 commit d16ef65
Show file tree
Hide file tree
Showing 87 changed files with 2,067 additions and 552 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ARG USER_GID=$USER_UID

# Set up non-root user
COPY ./scripts/non-root-user.sh /tmp/
RUN bash /tmp/non-root-user.sh "${USERNAME}" "${USER_UID}" "${USER_GID}"
RUN bash "/tmp/non-root-user.sh" "${USERNAME}" "${USER_UID}" "${USER_GID}"

# Set env for tracking that we're running in a devcontainer
ENV DEVCONTAINER=true
Expand Down
5 changes: 2 additions & 3 deletions .devcontainer/scripts/non-root-user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ if [ "$(id -u)" -ne 0 ]; then
exit 1
fi


# If in automatic mode, determine if a user already exists, if not use vscode
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
USERNAME=""
Expand All @@ -39,11 +38,11 @@ fi
# Create or update a non-root user to match UID/GID.
if id -u ${USERNAME} > /dev/null 2>&1; then
# User exists, update if needed
if [ "${USER_GID}" != "automatic" ] && [ "$USER_GID" != "$(id -G $USERNAME)" ]; then
if [ "${USER_GID}" != "automatic" ] && [ "${USER_GID}" != "999" ] && [ "$USER_GID" != "$(id -G $USERNAME)" ]; then
groupmod --gid $USER_GID $USERNAME
usermod --gid $USER_GID $USERNAME
fi
if [ "${USER_UID}" != "automatic" ] && [ "$USER_UID" != "$(id -u $USERNAME)" ]; then
if [ "${USER_UID}" != "automatic" ] && [ "${USER_GID}" != "999" ] && [ "$USER_UID" != "$(id -u $USERNAME)" ]; then
usermod --uid $USER_UID $USERNAME
fi
else
Expand Down
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@
"request": "attach",
"port": 9091,
"preLaunchTask": "func host start"
},
{
"name": "Vite: Debug",
"type": "msedge",
"request": "launch",
"url": "http://localhost:5000",
"webRoot": "${workspaceFolder}/app/backend/static",
"sourceMapPathOverrides": {
"webpack:///src/*": "${webRoot}/*"
},
"skipFiles": ["<node_internals>/**", "**/node_modules/**"]
}
]
}
177 changes: 147 additions & 30 deletions README.md

Large diffs are not rendered by default.

78 changes: 45 additions & 33 deletions app/backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import logging
import mimetypes
import os
import json
import urllib.parse
from datetime import datetime, timedelta

import openai
from approaches.chatreadretrieveread import ChatReadRetrieveReadApproach
from azure.core.credentials import AzureKeyCredential
from azure.identity import DefaultAzureCredential
from azure.mgmt.cognitiveservices import CognitiveServicesManagementClient
from azure.search.documents import SearchClient
from azure.storage.blob import (
AccountSasPermissions,
Expand All @@ -33,10 +35,12 @@
AZURE_SEARCH_SERVICE_KEY = os.environ.get("AZURE_SEARCH_SERVICE_KEY")
AZURE_SEARCH_INDEX = os.environ.get("AZURE_SEARCH_INDEX") or "gptkbindex"
AZURE_OPENAI_SERVICE = os.environ.get("AZURE_OPENAI_SERVICE") or "myopenai"
AZURE_OPENAI_RESOURCE_GROUP = os.environ.get("AZURE_OPENAI_RESOURCE_GROUP") or ""
AZURE_OPENAI_CHATGPT_DEPLOYMENT = (
os.environ.get("AZURE_OPENAI_CHATGPT_DEPLOYMENT") or "chat"
)
AZURE_OPENAI_SERVICE_KEY = os.environ.get("AZURE_OPENAI_SERVICE_KEY")
AZURE_SUBSCRIPTION_ID = os.environ.get("AZURE_SUBSCRIPTION_ID")

KB_FIELDS_CONTENT = os.environ.get("KB_FIELDS_CONTENT") or "merged_content"
KB_FIELDS_CATEGORY = os.environ.get("KB_FIELDS_CATEGORY") or "category"
Expand Down Expand Up @@ -83,6 +87,15 @@
)
blob_container = blob_client.get_container_client(AZURE_BLOB_STORAGE_CONTAINER)

# Set up OpenAI management client
openai_mgmt_client = CognitiveServicesManagementClient(
credential=azure_credential,
subscription_id=AZURE_SUBSCRIPTION_ID)

deployment = openai_mgmt_client.deployments.get(
resource_group_name=AZURE_OPENAI_RESOURCE_GROUP,
account_name=AZURE_OPENAI_SERVICE,
deployment_name=AZURE_OPENAI_CHATGPT_DEPLOYMENT)

chat_approaches = {
"rrr": ChatReadRetrieveReadApproach(
Expand All @@ -94,6 +107,8 @@
KB_FIELDS_CONTENT,
blob_client,
QUERY_TERM_LANGUAGE,
deployment.properties.model.name,
deployment.properties.model.version
)
}

Expand All @@ -105,33 +120,6 @@
def static_file(path):
return app.send_static_file(path)


# Return blob path with SAS token for citation access
@app.route("/content/<path:path>")
def content_file(path):
blob = blob_container.get_blob_client(path).download_blob()
mime_type = blob.properties["content_settings"]["content_type"]
file_extension = blob.properties["name"].split(".")[-1:]
if mime_type == "application/octet-stream":
mime_type = mimetypes.guess_type(path)[0] or "application/octet-stream"
if mime_type == "text/plain" and file_extension[0] in ["htm", "html"]:
mime_type = "text/html"
print(
"Using mime type: "
+ mime_type
+ "for file with extension: "
+ file_extension[0]
)
return (
blob.readall(),
200,
{
"Content-Type": mime_type,
"Content-Disposition": f"inline; filename={urllib.parse.quote(path, safe='')}",
},
)


@app.route("/chat", methods=["POST"])
def chat():
approach = request.json["approach"]
Expand All @@ -156,7 +144,6 @@ def chat():
logging.exception("Exception in /chat")
return jsonify({"error": str(e)}), 500


@app.route("/getblobclienturl")
def get_blob_client_url():
sas_token = generate_account_sas(
Expand All @@ -177,11 +164,6 @@ def get_blob_client_url():
)
return jsonify({"url": f"{blob_client.url}?{sas_token}"})


if __name__ == "__main__":
app.run()


@app.route("/getalluploadstatus", methods=["POST"])
def get_all_upload_status():
timeframe = request.json["timeframe"]
Expand All @@ -192,3 +174,33 @@ def get_all_upload_status():
logging.exception("Exception in /getalluploadstatus")
return jsonify({"error": str(e)}), 500
return jsonify(results)

# Return AZURE_OPENAI_CHATGPT_DEPLOYMENT
@app.route("/getInfoData")
def get_info_data():
response = jsonify(
{
"AZURE_OPENAI_CHATGPT_DEPLOYMENT": f"{AZURE_OPENAI_CHATGPT_DEPLOYMENT}",
"AZURE_OPENAI_MODEL_NAME": f"{deployment.properties.model.name}",
"AZURE_OPENAI_MODEL_VERSION": f"{deployment.properties.model.version}",
"AZURE_OPENAI_SERVICE": f"{AZURE_OPENAI_SERVICE}",
"AZURE_SEARCH_SERVICE": f"{AZURE_SEARCH_SERVICE}",
"AZURE_SEARCH_INDEX": f"{AZURE_SEARCH_INDEX}",
"TARGET_LANGUAGE": f"{QUERY_TERM_LANGUAGE}"
})
return response

@app.route("/getcitation", methods=["POST"])
def get_citation():
citation = urllib.parse.unquote(request.json["citation"])
try:
blob = blob_container.get_blob_client(citation).download_blob()
decoded_text = blob.readall().decode()
results = jsonify(json.loads(decoded_text))
except Exception as e:
logging.exception("Exception in /getalluploadstatus")
return jsonify({"error": str(e)}), 500
return jsonify(results.json)

if __name__ == "__main__":
app.run()
Loading

0 comments on commit d16ef65

Please sign in to comment.