From 6163da3acbe0abe7801fc55c33e0bc0063da4042 Mon Sep 17 00:00:00 2001 From: Anushree Kaipa Ramanath Date: Wed, 24 Apr 2024 11:05:27 -0700 Subject: [PATCH] Proposed Changes to include Citations output from PF endpoint in Web App Endpoint (#801) --- .env.sample | 5 +++-- README.md | 5 +++-- app.py | 5 ++++- backend/utils.py | 26 ++++++++++++++++---------- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/.env.sample b/.env.sample index b8623c708c..a81d1338cf 100644 --- a/.env.sample +++ b/.env.sample @@ -106,5 +106,6 @@ USE_PROMPTFLOW=False PROMPTFLOW_ENDPOINT= PROMPTFLOW_API_KEY= PROMPTFLOW_RESPONSE_TIMEOUT=120 -PROMPTFLOW_REQUEST_FIELD_NAME=question -PROMPTFLOW_RESPONSE_FIELD_NAME=answer \ No newline at end of file +PROMPTFLOW_REQUEST_FIELD_NAME=query +PROMPTFLOW_RESPONSE_FIELD_NAME=reply +PROMPTFLOW_CITATIONS_FIELD_NAME=documents diff --git a/README.md b/README.md index 5b77b8f37f..cee0c7bfe3 100644 --- a/README.md +++ b/README.md @@ -234,8 +234,9 @@ Note: settings starting with `AZURE_SEARCH` are only needed when using Azure Ope |PROMPTFLOW_ENDPOINT||URL of the deployed Promptflow endpoint e.g. https://pf-deployment-name.region.inference.ml.azure.com/score| |PROMPTFLOW_API_KEY||Auth key for deployed Promptflow endpoint. Note: only Key-based authentication is supported.| |PROMPTFLOW_RESPONSE_TIMEOUT|120|Timeout value in seconds for the Promptflow endpoint to respond.| -|PROMPTFLOW_REQUEST_FIELD_NAME|question|Default field name to construct Promptflow request. Note: chat_history is auto constucted based on the interaction, if your API expects other mandatory field you will need to change the request parameters under `promptflow_request` function.| -|PROMPTFLOW_RESPONSE_FIELD_NAME|answer|Default field name to process the response from Promptflow request.| +|PROMPTFLOW_REQUEST_FIELD_NAME|query|Default field name to construct Promptflow request. Note: chat_history is auto constucted based on the interaction, if your API expects other mandatory field you will need to change the request parameters under `promptflow_request` function.| +|PROMPTFLOW_RESPONSE_FIELD_NAME|reply|Default field name to process the response from Promptflow request.| +|PROMPTFLOW_CITATIONS_FIELD_NAME|documents|Default field name to process the citations output from Promptflow request.| ## Contributing diff --git a/app.py b/app.py index 76b1932310..9d2d32d1fd 100644 --- a/app.py +++ b/app.py @@ -243,6 +243,9 @@ async def assets(path): PROMPTFLOW_RESPONSE_FIELD_NAME = os.environ.get( "PROMPTFLOW_RESPONSE_FIELD_NAME", "reply" ) +PROMPTFLOW_CITATIONS_FIELD_NAME = os.environ.get( + "PROMPTFLOW_CITATIONS_FIELD_NAME", "documents" +) # Frontend Settings via Environment Variables AUTH_ENABLED = os.environ.get("AUTH_ENABLED", "true").lower() == "true" CHAT_HISTORY_ENABLED = ( @@ -846,7 +849,7 @@ async def complete_chat_request(request_body): response = await promptflow_request(request_body) history_metadata = request_body.get("history_metadata", {}) return format_pf_non_streaming_response( - response, history_metadata, PROMPTFLOW_RESPONSE_FIELD_NAME + response, history_metadata, PROMPTFLOW_RESPONSE_FIELD_NAME, PROMPTFLOW_CITATIONS_FIELD_NAME ) else: response, apim_request_id = await send_chat_request(request_body) diff --git a/backend/utils.py b/backend/utils.py index a33fe6822e..09d255812b 100644 --- a/backend/utils.py +++ b/backend/utils.py @@ -142,7 +142,7 @@ def format_stream_response(chatCompletionChunk, history_metadata, apim_request_i def format_pf_non_streaming_response( - chatCompletion, history_metadata, response_field_name, message_uuid=None + chatCompletion, history_metadata, response_field_name, citations_field_name, message_uuid=None ): if chatCompletion is None: logging.error( @@ -157,22 +157,28 @@ def format_pf_non_streaming_response( logging.debug(f"chatCompletion: {chatCompletion}") try: - response_obj = { + messages = [] + if response_field_name in chatCompletion: + messages.append({ + "role": "assistant", + "content": chatCompletion[response_field_name] + }) + if citations_field_name in chatCompletion: + messages.append({ + "role": "tool", + "content": chatCompletion[citations_field_name] + }) + response_obj = { "id": chatCompletion["id"], "model": "", "created": "", "object": "", "choices": [ { - "messages": [ - { - "role": "assistant", - "content": chatCompletion[response_field_name], - } - ] + "messages": messages, + "history_metadata": history_metadata, } - ], - "history_metadata": history_metadata, + ] } return response_obj except Exception as e: