Skip to content

Commit

Permalink
Merge pull request #35 from microsoft/adesousa_microsoft/refine-prompts
Browse files Browse the repository at this point in the history
update system prompt switching for template and browse
  • Loading branch information
andrewldesousa authored Aug 5, 2024
2 parents aca47a2 + f0c1ecc commit 3916461
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
29 changes: 11 additions & 18 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
format_non_streaming_response,
convert_to_pf_format,
format_pf_non_streaming_response,
ChatType
)

bp = Blueprint("routes", __name__, static_folder="static", template_folder="static")
Expand Down Expand Up @@ -200,31 +201,17 @@ def init_cosmosdb_client():


def prepare_model_args(request_body, request_headers):
template_chat_system_prompt = ('Generate a template for a document given a user description of the template. Do not include any other commentary or description.' +
'Respond with a json object in the format containing a list of section information {{"template": [{"section_title": string, "section_description": string}]}}.' +
'Example: {"template": [{"section_title": "Introduction", "section_description": "This section introduces the document."}, {"section_title": "Section 2", "section_description": "This is section 2."}]}.' +
'If the user provides a message that is not related to modifying the template, respond asking the user to go to the Browse tab to chat with documents.')
chat_type = ChatType.BROWSE if not (request_body["chat_type"] and request_body["chat_type"] == "template") else ChatType.TEMPLATE
request_messages = request_body.get("messages", [])

# template chat should only respond to messages for template modification
system_message = app_settings.azure_openai.system_message if not ("chat_type" in request_body and request_body["chat_type"] == "template") else template_chat_system_prompt

messages = []
if not app_settings.datasource:
messages = [
{
"role": "system",
"content": system_message
"content": app_settings.azure_openai.system_message if chat_type == ChatType.BROWSE else app_settings.azure_openai.template_system_message
}
]

if ("chat_type" in request_body and request_body["chat_type"] == "template"):
messages.append(
{
"role": "assistant",
"content": system_message
}
)

for message in request_messages:
if message:
Expand All @@ -246,7 +233,7 @@ def prepare_model_args(request_body, request_headers):
"max_tokens": app_settings.azure_openai.max_tokens,
"top_p": app_settings.azure_openai.top_p,
"stop": app_settings.azure_openai.stop_sequence,
"stream": app_settings.azure_openai.stream,
"stream": app_settings.azure_openai.stream if chat_type == ChatType.BROWSE else False,
"model": app_settings.azure_openai.model,
"user": user_json
}
Expand All @@ -260,6 +247,11 @@ def prepare_model_args(request_body, request_headers):
]
}

# change role information if template chat
if chat_type == ChatType.TEMPLATE:
model_args["extra_body"]["data_sources"][0]["parameters"]["role_information"] = app_settings.azure_openai.template_system_message


model_args_clean = copy.deepcopy(model_args)
if model_args_clean.get("extra_body"):
secret_params = [
Expand Down Expand Up @@ -383,7 +375,8 @@ async def generate():

async def conversation_internal(request_body, request_headers):
try:
if app_settings.azure_openai.stream:
chat_type = ChatType.BROWSE if not (request_body["chat_type"] and request_body["chat_type"] == "template") else ChatType.TEMPLATE
if app_settings.azure_openai.stream and chat_type == ChatType.BROWSE:
result = await stream_chat_request(request_body, request_headers)
response = await make_response(format_as_ndjson(result))
response.timeout = None
Expand Down
4 changes: 4 additions & 0 deletions backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ class _AzureOpenAISettings(BaseSettings):
embedding_endpoint: Optional[str] = None
embedding_key: Optional[str] = None
embedding_name: Optional[str] = None
template_system_message: str = ('Generate a template for a document given a user description of the template. Do not include any other commentary or description.' +
'Respond with a json object in the format containing a list of section information {{"template": [{"section_title": string, "section_description": string}]}}.' +
'Example: {"template": [{"section_title": "Introduction", "section_description": "This section introduces the document."}, {"section_title": "Section 2", "section_description": "This is section 2."}]}.' +
'If the user provides a message that is not related to modifying the template, respond asking the user to go to the Browse tab to chat with documents.')

@field_validator('tools', mode='before')
@classmethod
Expand Down
6 changes: 6 additions & 0 deletions backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import requests
import dataclasses
from enum import Enum

from typing import List

Expand All @@ -15,6 +16,11 @@
)


class ChatType(Enum):
TEMPLATE = "template"
BROWSE = "browse"


class JSONEncoder(json.JSONEncoder):
def default(self, o):
if dataclasses.is_dataclass(o):
Expand Down

0 comments on commit 3916461

Please sign in to comment.