Skip to content

Commit

Permalink
Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
UtkarshMishra-Microsoft committed Dec 4, 2024
1 parent 4bbc335 commit 414fd22
Show file tree
Hide file tree
Showing 18 changed files with 489 additions and 440 deletions.
3 changes: 1 addition & 2 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ ignore =
E722
W503
F811
E266
W504
E266
68 changes: 34 additions & 34 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ async def add_conversation():
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]

## check request for conversation_id
# check request for conversation_id
request_json = await request.get_json()
conversation_id = request_json.get("conversation_id", None)

Expand All @@ -415,8 +415,8 @@ async def add_conversation():
history_metadata["title"] = title
history_metadata["date"] = conversation_dict["createdAt"]

## Format the incoming message object in the "chat/completions" messages format
## then write it to the conversation history in cosmos
# Format the incoming message object in the "chat/completions" messages format
# then write it to the conversation history in cosmos
messages = request_json["messages"]
if len(messages) > 0 and messages[-1]["role"] == "user":
createdMessageValue = await cosmos_conversation_client.create_message(
Expand Down Expand Up @@ -452,7 +452,7 @@ async def update_conversation():
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]

## check request for conversation_id
# check request for conversation_id
request_json = await request.get_json()
conversation_id = request_json.get("conversation_id", None)

Expand All @@ -466,8 +466,8 @@ async def update_conversation():
if not conversation_id:
raise Exception("No conversation_id found")

## Format the incoming message object in the "chat/completions" messages format
## then write it to the conversation history in cosmos
# Format the incoming message object in the "chat/completions" messages format
# then write it to the conversation history in cosmos
messages = request_json["messages"]
if len(messages) > 0 and messages[-1]["role"] == "assistant":
if len(messages) > 1 and messages[-2].get("role", None) == "tool":
Expand Down Expand Up @@ -504,7 +504,7 @@ async def update_message():
user_id = authenticated_user["user_principal_id"]
cosmos_conversation_client = init_cosmosdb_client()

## check request for message_id
# check request for message_id
request_json = await request.get_json()
message_id = request_json.get("message_id", None)
message_feedback = request_json.get("message_feedback", None)
Expand All @@ -515,7 +515,7 @@ async def update_message():
if not message_feedback:
return jsonify({"error": "message_feedback is required"}), 400

## update the message in cosmos
# update the message in cosmos
updated_message = await cosmos_conversation_client.update_message_feedback(
user_id, message_id, message_feedback
)
Expand Down Expand Up @@ -546,29 +546,29 @@ async def update_message():

@bp.route("/history/delete", methods=["DELETE"])
async def delete_conversation():
## get the user id from the request headers
# get the user id from the request headers
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]

## check request for conversation_id
# check request for conversation_id
request_json = await request.get_json()
conversation_id = request_json.get("conversation_id", None)

try:
if not conversation_id:
return jsonify({"error": "conversation_id is required"}), 400

## make sure cosmos is configured
# make sure cosmos is configured
cosmos_conversation_client = init_cosmosdb_client()
if not cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")

## delete the conversation messages from cosmos first
# delete the conversation messages from cosmos first
deleted_messages = await cosmos_conversation_client.delete_messages(
conversation_id, user_id
)

## Now delete the conversation
# Now delete the conversation
deleted_conversation = await cosmos_conversation_client.delete_conversation(
user_id, conversation_id
)
Expand All @@ -595,20 +595,20 @@ async def list_conversations():
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]

## make sure cosmos is configured
# make sure cosmos is configured
cosmos_conversation_client = init_cosmosdb_client()
if not cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")

## get the conversations from cosmos
# get the conversations from cosmos
conversations = await cosmos_conversation_client.get_conversations(
user_id, offset=offset, limit=25
)
await cosmos_conversation_client.cosmosdb_client.close()
if not isinstance(conversations, list):
return jsonify({"error": f"No conversations for {user_id} were found"}), 404

## return the conversation ids
# return the conversation ids

return jsonify(conversations), 200

Expand All @@ -618,23 +618,23 @@ async def get_conversation():
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]

## check request for conversation_id
# check request for conversation_id
request_json = await request.get_json()
conversation_id = request_json.get("conversation_id", None)

if not conversation_id:
return jsonify({"error": "conversation_id is required"}), 400

## make sure cosmos is configured
# make sure cosmos is configured
cosmos_conversation_client = init_cosmosdb_client()
if not cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")

## get the conversation object and the related messages from cosmos
# get the conversation object and the related messages from cosmos
conversation = await cosmos_conversation_client.get_conversation(
user_id, conversation_id
)
## return the conversation id and the messages in the bot frontend format
# return the conversation id and the messages in the bot frontend format
if not conversation:
return (
jsonify(
Expand All @@ -650,7 +650,7 @@ async def get_conversation():
user_id, conversation_id
)

## format the messages in the bot frontend format
# format the messages in the bot frontend format
messages = [
{
"id": msg["id"],
Expand All @@ -671,19 +671,19 @@ async def rename_conversation():
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]

## check request for conversation_id
# check request for conversation_id
request_json = await request.get_json()
conversation_id = request_json.get("conversation_id", None)

if not conversation_id:
return jsonify({"error": "conversation_id is required"}), 400

## make sure cosmos is configured
# make sure cosmos is configured
cosmos_conversation_client = init_cosmosdb_client()
if not cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")

## get the conversation from cosmos
# get the conversation from cosmos
conversation = await cosmos_conversation_client.get_conversation(
user_id, conversation_id
)
Expand All @@ -697,7 +697,7 @@ async def rename_conversation():
404,
)

## update the title
# update the title
title = request_json.get("title", None)
if not title:
return jsonify({"error": "title is required"}), 400
Expand All @@ -712,13 +712,13 @@ async def rename_conversation():

@bp.route("/history/delete_all", methods=["DELETE"])
async def delete_all_conversations():
## get the user id from the request headers
# get the user id from the request headers
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]

# get conversations for user
try:
## make sure cosmos is configured
# make sure cosmos is configured
cosmos_conversation_client = init_cosmosdb_client()
if not cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")
Expand All @@ -731,12 +731,12 @@ async def delete_all_conversations():

# delete each conversation
for conversation in conversations:
## delete the conversation messages from cosmos first
# delete the conversation messages from cosmos first
deleted_messages = await cosmos_conversation_client.delete_messages(
conversation["id"], user_id
)

## Now delete the conversation
# Now delete the conversation
deleted_conversation = await cosmos_conversation_client.delete_conversation(
user_id, conversation["id"]
)
Expand All @@ -757,24 +757,24 @@ async def delete_all_conversations():

@bp.route("/history/clear", methods=["POST"])
async def clear_messages():
## get the user id from the request headers
# get the user id from the request headers
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]

## check request for conversation_id
# check request for conversation_id
request_json = await request.get_json()
conversation_id = request_json.get("conversation_id", None)

try:
if not conversation_id:
return jsonify({"error": "conversation_id is required"}), 400

## make sure cosmos is configured
# make sure cosmos is configured
cosmos_conversation_client = init_cosmosdb_client()
if not cosmos_conversation_client:
raise Exception("CosmosDB is not configured or not working")

## delete the conversation messages from cosmos
# delete the conversation messages from cosmos
deleted_messages = await cosmos_conversation_client.delete_messages(
conversation_id, user_id
)
Expand Down Expand Up @@ -864,7 +864,7 @@ async def get_document(filepath):


async def generate_title(conversation_messages):
## make sure the messages are sorted by _ts descending
# make sure the messages are sorted by _ts descending
title_prompt = app_settings.azure_openai.title_prompt

messages = [
Expand Down
10 changes: 5 additions & 5 deletions backend/auth/auth_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
def get_authenticated_user_details(request_headers):
user_object = {}

## check the headers for the Principal-Id (the guid of the signed in user)
# check the headers for the Principal-Id (the guid of the signed in user)
if "X-Ms-Client-Principal-Id" not in request_headers.keys():
## if it's not, assume we're in development mode and return a default user
# if it's not, assume we're in development mode and return a default user
from . import sample_user
raw_user_object = sample_user.sample_user
else:
## if it is, get the user details from the EasyAuth headers
raw_user_object = {k:v for k,v in request_headers.items()}
# if it is, get the user details from the EasyAuth headers
raw_user_object = {k: v for k, v in request_headers.items()}

user_object['user_principal_id'] = raw_user_object.get('X-Ms-Client-Principal-Id')
user_object['user_name'] = raw_user_object.get('X-Ms-Client-Principal-Name')
Expand All @@ -17,4 +17,4 @@ def get_authenticated_user_details(request_headers):
user_object['client_principal_b64'] = raw_user_object.get('X-Ms-Client-Principal')
user_object['aad_id_token'] = raw_user_object.get('X-Ms-Token-Aad-Id-Token')

return user_object
return user_object
74 changes: 37 additions & 37 deletions backend/auth/sample_user.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
sample_user = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en",
"Client-Ip": "22.222.222.2222:64379",
"Content-Length": "192",
"Content-Type": "application/json",
"Cookie": "AppServiceAuthSession=/AuR5ENU+pmpoN3jnymP8fzpmVBgphx9uPQrYLEWGcxjIITIeh8NZW7r3ePkG8yBcMaItlh1pX4nzg5TFD9o2mxC/5BNDRe/uuu0iDlLEdKecROZcVRY7QsFdHLjn9KB90Z3d9ZeLwfVIf0sZowWJt03BO5zKGB7vZgL+ofv3QY3AaYn1k1GtxSE9HQWJpWar7mOA64b7Lsy62eY3nxwg3AWDsP3/rAta+MnDCzpdlZMFXcJLj+rsCppW+w9OqGhKQ7uCs03BPeon3qZOdmE8cOJW3+i96iYlhneNQDItHyQqEi1CHbBTSkqwpeOwWP4vcwGM22ynxPp7YFyiRw/X361DGYy+YkgYBkXq1AEIDZ44BCBz9EEaEi0NU+m6yUOpNjEaUtrJKhQywcM2odojdT4XAY+HfTEfSqp0WiAkgAuE/ueCu2JDOfvxGjCgJ4DGWCoYdOdXAN1c+MenT4OSvkMO41YuPeah9qk9ixkJI5s80lv8rUu1J26QF6pstdDkYkAJAEra3RQiiO1eAH7UEb3xHXn0HW5lX8ZDX3LWiAFGOt5DIKxBKFymBKJGzbPFPYjfczegu0FD8/NQPLl2exAX3mI9oy/tFnATSyLO2E8DxwP5wnYVminZOQMjB/I4g3Go14betm0MlNXlUbU1fyS6Q6JxoCNLDZywCoU9Y65UzimWZbseKsXlOwYukCEpuQ5QPT55LuEAWhtYier8LSh+fvVUsrkqKS+bg0hzuoX53X6aqUr7YB31t0Z2zt5TT/V3qXpdyD8Xyd884PqysSkJYa553sYx93ETDKSsfDguanVfn2si9nvDpvUWf6/R02FmQgXiaaaykMgYyIuEmE77ptsivjH3hj/MN4VlePFWokcchF4ciqqzonmICmjEHEx5zpjU2Kwa+0y7J5ROzVVygcnO1jH6ZKDy9bGGYL547bXx/iiYBYqSIQzleOAkCeULrGN2KEHwckX5MpuRaqTpoxdZH9RJv0mIWxbDA0kwGsbMICQd0ZODBkPUnE84qhzvXInC+TL7MbutPEnGbzgxBAS1c2Ct4vxkkjykOeOxTPxqAhxoefwUfIwZZax6A9LbeYX2bsBpay0lScHcA==",
"Disguised-Host": "your_app_service.azurewebsites.net",
"Host": "your_app_service.azurewebsites.net",
"Max-Forwards": "10",
"Origin": "https://your_app_service.azurewebsites.net",
"Referer": "https://your_app_service.azurewebsites.net/",
"Sec-Ch-Ua": "\"Microsoft Edge\";v=\"113\", \"Chromium\";v=\"113\", \"Not-A.Brand\";v=\"24\"",
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": "\"Windows\"",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Traceparent": "00-24e9a8d1b06f233a3f1714845ef971a9-3fac69f81ca5175c-00",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.42",
"Was-Default-Hostname": "your_app_service.azurewebsites.net",
"X-Appservice-Proto": "https",
"X-Arr-Log-Id": "4102b832-6c88-4c7c-8996-0edad9e4358f",
"X-Arr-Ssl": "2048|256|CN=Microsoft Azure TLS Issuing CA 02, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US",
"X-Client-Ip": "22.222.222.222",
"X-Client-Port": "64379",
"X-Forwarded-For": "22.222.222.22:64379",
"X-Forwarded-Proto": "https",
"X-Forwarded-Tlsversion": "1.2",
"X-Ms-Client-Principal": "your_base_64_encoded_token",
"X-Ms-Client-Principal-Id": "00000000-0000-0000-0000-000000000000",
"X-Ms-Client-Principal-Idp": "aad",
"X-Ms-Client-Principal-Name": "[email protected]",
"X-Ms-Token-Aad-Id-Token": "your_aad_id_token",
"X-Original-Url": "/chatgpt",
"X-Site-Deployment-Id": "your_app_service",
"X-Waws-Unencoded-Url": "/chatgpt"
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en",
"Client-Ip": "22.222.222.2222:64379",
"Content-Length": "192",
"Content-Type": "application/json",
"Cookie": "AppServiceAuthSession=/AuR5ENU+pmpoN3jnymP8fzpmVBgphx9uPQrYLEWGcxjIITIeh8NZW7r3ePkG8yBcMaItlh1pX4nzg5TFD9o2mxC/5BNDRe/uuu0iDlLEdKecROZcVRY7QsFdHLjn9KB90Z3d9ZeLwfVIf0sZowWJt03BO5zKGB7vZgL+ofv3QY3AaYn1k1GtxSE9HQWJpWar7mOA64b7Lsy62eY3nxwg3AWDsP3/rAta+MnDCzpdlZMFXcJLj+rsCppW+w9OqGhKQ7uCs03BPeon3qZOdmE8cOJW3+i96iYlhneNQDItHyQqEi1CHbBTSkqwpeOwWP4vcwGM22ynxPp7YFyiRw/X361DGYy+YkgYBkXq1AEIDZ44BCBz9EEaEi0NU+m6yUOpNjEaUtrJKhQywcM2odojdT4XAY+HfTEfSqp0WiAkgAuE/ueCu2JDOfvxGjCgJ4DGWCoYdOdXAN1c+MenT4OSvkMO41YuPeah9qk9ixkJI5s80lv8rUu1J26QF6pstdDkYkAJAEra3RQiiO1eAH7UEb3xHXn0HW5lX8ZDX3LWiAFGOt5DIKxBKFymBKJGzbPFPYjfczegu0FD8/NQPLl2exAX3mI9oy/tFnATSyLO2E8DxwP5wnYVminZOQMjB/I4g3Go14betm0MlNXlUbU1fyS6Q6JxoCNLDZywCoU9Y65UzimWZbseKsXlOwYukCEpuQ5QPT55LuEAWhtYier8LSh+fvVUsrkqKS+bg0hzuoX53X6aqUr7YB31t0Z2zt5TT/V3qXpdyD8Xyd884PqysSkJYa553sYx93ETDKSsfDguanVfn2si9nvDpvUWf6/R02FmQgXiaaaykMgYyIuEmE77ptsivjH3hj/MN4VlePFWokcchF4ciqqzonmICmjEHEx5zpjU2Kwa+0y7J5ROzVVygcnO1jH6ZKDy9bGGYL547bXx/iiYBYqSIQzleOAkCeULrGN2KEHwckX5MpuRaqTpoxdZH9RJv0mIWxbDA0kwGsbMICQd0ZODBkPUnE84qhzvXInC+TL7MbutPEnGbzgxBAS1c2Ct4vxkkjykOeOxTPxqAhxoefwUfIwZZax6A9LbeYX2bsBpay0lScHcA==",
"Disguised-Host": "your_app_service.azurewebsites.net",
"Host": "your_app_service.azurewebsites.net",
"Max-Forwards": "10",
"Origin": "https://your_app_service.azurewebsites.net",
"Referer": "https://your_app_service.azurewebsites.net/",
"Sec-Ch-Ua": "\"Microsoft Edge\";v=\"113\", \"Chromium\";v=\"113\", \"Not-A.Brand\";v=\"24\"",
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": "\"Windows\"",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Traceparent": "00-24e9a8d1b06f233a3f1714845ef971a9-3fac69f81ca5175c-00",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.42",
"Was-Default-Hostname": "your_app_service.azurewebsites.net",
"X-Appservice-Proto": "https",
"X-Arr-Log-Id": "4102b832-6c88-4c7c-8996-0edad9e4358f",
"X-Arr-Ssl": "2048|256|CN=Microsoft Azure TLS Issuing CA 02, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US",
"X-Client-Ip": "22.222.222.222",
"X-Client-Port": "64379",
"X-Forwarded-For": "22.222.222.22:64379",
"X-Forwarded-Proto": "https",
"X-Forwarded-Tlsversion": "1.2",
"X-Ms-Client-Principal": "your_base_64_encoded_token",
"X-Ms-Client-Principal-Id": "00000000-0000-0000-0000-000000000000",
"X-Ms-Client-Principal-Idp": "aad",
"X-Ms-Client-Principal-Name": "[email protected]",
"X-Ms-Token-Aad-Id-Token": "your_aad_id_token",
"X-Original-Url": "/chatgpt",
"X-Site-Deployment-Id": "your_app_service",
"X-Waws-Unencoded-Url": "/chatgpt"
}
Loading

0 comments on commit 414fd22

Please sign in to comment.