Skip to content

Commit

Permalink
Proposed Changes to include Citations output from PF endpoint in Web …
Browse files Browse the repository at this point in the history
…App Endpoint (microsoft#801)
  • Loading branch information
anushree1808 authored Apr 24, 2024
1 parent eb34987 commit 6163da3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
5 changes: 3 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
PROMPTFLOW_REQUEST_FIELD_NAME=query
PROMPTFLOW_RESPONSE_FIELD_NAME=reply
PROMPTFLOW_CITATIONS_FIELD_NAME=documents
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 16 additions & 10 deletions backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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:
Expand Down

0 comments on commit 6163da3

Please sign in to comment.