Skip to content

Commit f0c1ecc

Browse files
author
adesousa_microsoft
committed
update system prompt switching for template and browse
1 parent aca47a2 commit f0c1ecc

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

app.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
format_non_streaming_response,
3535
convert_to_pf_format,
3636
format_pf_non_streaming_response,
37+
ChatType
3738
)
3839

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

201202

202203
def prepare_model_args(request_body, request_headers):
203-
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.' +
204-
'Respond with a json object in the format containing a list of section information {{"template": [{"section_title": string, "section_description": string}]}}.' +
205-
'Example: {"template": [{"section_title": "Introduction", "section_description": "This section introduces the document."}, {"section_title": "Section 2", "section_description": "This is section 2."}]}.' +
206-
'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.')
204+
chat_type = ChatType.BROWSE if not (request_body["chat_type"] and request_body["chat_type"] == "template") else ChatType.TEMPLATE
207205
request_messages = request_body.get("messages", [])
208206

209-
# template chat should only respond to messages for template modification
210-
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
211-
212207
messages = []
213208
if not app_settings.datasource:
214209
messages = [
215210
{
216211
"role": "system",
217-
"content": system_message
212+
"content": app_settings.azure_openai.system_message if chat_type == ChatType.BROWSE else app_settings.azure_openai.template_system_message
218213
}
219214
]
220-
221-
if ("chat_type" in request_body and request_body["chat_type"] == "template"):
222-
messages.append(
223-
{
224-
"role": "assistant",
225-
"content": system_message
226-
}
227-
)
228215

229216
for message in request_messages:
230217
if message:
@@ -246,7 +233,7 @@ def prepare_model_args(request_body, request_headers):
246233
"max_tokens": app_settings.azure_openai.max_tokens,
247234
"top_p": app_settings.azure_openai.top_p,
248235
"stop": app_settings.azure_openai.stop_sequence,
249-
"stream": app_settings.azure_openai.stream,
236+
"stream": app_settings.azure_openai.stream if chat_type == ChatType.BROWSE else False,
250237
"model": app_settings.azure_openai.model,
251238
"user": user_json
252239
}
@@ -260,6 +247,11 @@ def prepare_model_args(request_body, request_headers):
260247
]
261248
}
262249

250+
# change role information if template chat
251+
if chat_type == ChatType.TEMPLATE:
252+
model_args["extra_body"]["data_sources"][0]["parameters"]["role_information"] = app_settings.azure_openai.template_system_message
253+
254+
263255
model_args_clean = copy.deepcopy(model_args)
264256
if model_args_clean.get("extra_body"):
265257
secret_params = [
@@ -383,7 +375,8 @@ async def generate():
383375

384376
async def conversation_internal(request_body, request_headers):
385377
try:
386-
if app_settings.azure_openai.stream:
378+
chat_type = ChatType.BROWSE if not (request_body["chat_type"] and request_body["chat_type"] == "template") else ChatType.TEMPLATE
379+
if app_settings.azure_openai.stream and chat_type == ChatType.BROWSE:
387380
result = await stream_chat_request(request_body, request_headers)
388381
response = await make_response(format_as_ndjson(result))
389382
response.timeout = None

backend/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ class _AzureOpenAISettings(BaseSettings):
122122
embedding_endpoint: Optional[str] = None
123123
embedding_key: Optional[str] = None
124124
embedding_name: Optional[str] = None
125+
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.' +
126+
'Respond with a json object in the format containing a list of section information {{"template": [{"section_title": string, "section_description": string}]}}.' +
127+
'Example: {"template": [{"section_title": "Introduction", "section_description": "This section introduces the document."}, {"section_title": "Section 2", "section_description": "This is section 2."}]}.' +
128+
'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.')
125129

126130
@field_validator('tools', mode='before')
127131
@classmethod

backend/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import requests
55
import dataclasses
6+
from enum import Enum
67

78
from typing import List
89

@@ -15,6 +16,11 @@
1516
)
1617

1718

19+
class ChatType(Enum):
20+
TEMPLATE = "template"
21+
BROWSE = "browse"
22+
23+
1824
class JSONEncoder(json.JSONEncoder):
1925
def default(self, o):
2026
if dataclasses.is_dataclass(o):

0 commit comments

Comments
 (0)