Skip to content

Commit a0cbcf5

Browse files
authored
Fix ask prompts to allow prompt override/injection (#2277)
* Configure Azure Developer Pipeline * Add prompt override/inject to Ask tab, add to tests
1 parent 3629df8 commit a0cbcf5

File tree

13 files changed

+379
-11
lines changed

13 files changed

+379
-11
lines changed

app/backend/approaches/approach.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ async def compute_image_embedding(self, q: str):
274274
image_query_vector = json["vector"]
275275
return VectorizedQuery(vector=image_query_vector, k_nearest_neighbors=50, fields="imageEmbedding")
276276

277+
def get_system_prompt_variables(self, override_prompt: Optional[str]) -> dict[str, str]:
278+
# Allows client to replace the entire prompt, or to inject into the existing prompt using >>>
279+
if override_prompt is None:
280+
return {}
281+
elif override_prompt.startswith(">>>"):
282+
return {"injected_prompt": override_prompt[3:]}
283+
else:
284+
return {"override_prompt": override_prompt}
285+
277286
async def run(
278287
self,
279288
messages: list[ChatCompletionMessageParam],

app/backend/approaches/chatapproach.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ class ChatApproach(Approach, ABC):
1616
async def run_until_final_call(self, messages, overrides, auth_claims, should_stream) -> tuple:
1717
pass
1818

19-
def get_system_prompt_variables(self, override_prompt: Optional[str]) -> dict[str, str]:
20-
# Allows client to replace the entire prompt, or to inject into the existing prompt using >>>
21-
if override_prompt is None:
22-
return {}
23-
elif override_prompt.startswith(">>>"):
24-
return {"injected_prompt": override_prompt[3:]}
25-
else:
26-
return {"override_prompt": override_prompt}
27-
2819
def get_search_query(self, chat_completion: ChatCompletion, user_query: str):
2920
response_message = chat_completion.choices[0].message
3021

app/backend/approaches/prompts/ask_answer_question.prompty

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ sample:
1010
- "role_library.pdf#page=23: Company: Contoso Electronics Location: Anywhere Job Type: Full-Time Salary: Competitive, commensurate with experience Job Summary: The Senior Manager of Product Management will be responsible for leading the product management team at Contoso Electronics. This role includes developing strategies, plans and objectives for the product management team and managing the day-to-day operations. The Senior Manager of Product Management will be responsible for the successful launch of new products and the optimization of existing products. Responsibilities: · Develop and implement product management strategies, plans and objectives to maximize team performance. · Analyze competitive landscape and market trends to develop product strategies. · Lead the product management team in the development of product plans, roadmaps and launch plans. · Monitor the performance of product management team, analyze results and implement corrective action as needed. · Manage the product lifecycle, including product development, launch, and end of life. · Ensure product features and benefits meet customer requirements. · Establish and maintain relationships with key customers, partners, and vendors."
1111
- "role_library.pdf#page=28: · 7+ years of experience in research and development in the electronics sector. · Proven track record of successfully designing, testing, and optimizing products. · Experience leading a team of researchers and engineers. · Excellent problem-solving and analytical skills. · Ability to work in a fast-paced environment and meet tight deadlines.· Knowledge of industry trends, technologies, and regulations. · Excellent communication and presentation skills. Manager of Product Management Job Title: Manager of Product Management, Contoso Electronics Job Summary: The Manager of Product Management is responsible for overseeing the product management team, driving product development and marketing strategy for Contoso Electronics. This individual will be accountable for the successful launch of new products and the implementation of product life-cycle management processes. The Manager of Product Management will collaborate with internal teams, such as engineering, sales, marketing, and finance, as well as external partners, suppliers, and customers to ensure successful product execution."
1212
---
13+
system:
14+
{% if override_prompt %}
15+
{{ override_prompt }}
16+
{% else %}
1317
You are an intelligent assistant helping Contoso Inc employees with their healthcare plan questions and employee handbook questions.
1418
Use 'you' to refer to the individual asking the questions even if they ask with 'I'.
1519
Answer the following question using only the data provided in the sources below.
1620
Each source has a name followed by colon and the actual information, always include the source name for each fact you use in the response.
1721
If you cannot answer using the sources below, say you don't know. Use below example to answer
22+
{{ injected_prompt }}
23+
{% endif %}
1824

1925
user:
2026
(EXAMPLE) What is the deductible for the employee plan for a visit to Overlake in Bellevue?

app/backend/approaches/prompts/ask_answer_question_vision.prompty

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ description: Answer a single question (with no chat history) using both text and
44
model:
55
api: chat
66
---
7+
system:
8+
{% if override_prompt %}
9+
{{ override_prompt }}
10+
{% else %}
711
You are an intelligent assistant helping analyze the Annual Financial Report of Contoso Ltd., The documents contain text, graphs, tables and images.
812
Each image source has the file name in the top left corner of the image with coordinates (10,10) pixels and is in the format SourceFileName:<file_name>.
913
Each text source starts in a new line and has the file name followed by colon and the actual information.
1014
Always include the source name from the image or text for each fact you use in the response in the format: [filename].
1115
Answer the following question using only the data provided in the sources below.
1216
The text and image source can be the same file name, don't use the image title when citing the image source, only use the file name as mentioned.
1317
If you cannot answer using the sources below, say you don't know. Return just the answer without any input texts.
18+
{{ injected_prompt }}
19+
{% endif %}
1420

1521
user:
1622
{{ user_query }}

app/backend/approaches/prompts/chat_answer_question.prompty

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ sample:
1616
- "role_library.pdf#page=23: Company: Contoso Electronics Location: Anywhere Job Type: Full-Time Salary: Competitive, commensurate with experience Job Summary: The Senior Manager of Product Management will be responsible for leading the product management team at Contoso Electronics. This role includes developing strategies, plans and objectives for the product management team and managing the day-to-day operations. The Senior Manager of Product Management will be responsible for the successful launch of new products and the optimization of existing products. Responsibilities: · Develop and implement product management strategies, plans and objectives to maximize team performance. · Analyze competitive landscape and market trends to develop product strategies. · Lead the product management team in the development of product plans, roadmaps and launch plans. · Monitor the performance of product management team, analyze results and implement corrective action as needed. · Manage the product lifecycle, including product development, launch, and end of life. · Ensure product features and benefits meet customer requirements. · Establish and maintain relationships with key customers, partners, and vendors."
1717
- "role_library.pdf#page=28: · 7+ years of experience in research and development in the electronics sector. · Proven track record of successfully designing, testing, and optimizing products. · Experience leading a team of researchers and engineers. · Excellent problem-solving and analytical skills. · Ability to work in a fast-paced environment and meet tight deadlines.· Knowledge of industry trends, technologies, and regulations. · Excellent communication and presentation skills. Manager of Product Management Job Title: Manager of Product Management, Contoso Electronics Job Summary: The Manager of Product Management is responsible for overseeing the product management team, driving product development and marketing strategy for Contoso Electronics. This individual will be accountable for the successful launch of new products and the implementation of product life-cycle management processes. The Manager of Product Management will collaborate with internal teams, such as engineering, sales, marketing, and finance, as well as external partners, suppliers, and customers to ensure successful product execution."
1818
---
19+
system:
1920
{% if override_prompt %}
2021
{{ override_prompt }}
2122
{% else %}

app/backend/approaches/prompts/chat_answer_question_vision.prompty

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ description: Answer a question (with chat history) using both text and image sou
44
model:
55
api: chat
66
---
7+
system:
78
{% if override_prompt %}
89
{{ override_prompt }}
910
{% else %}

app/backend/approaches/retrievethenread.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ async def run(
9494
# Process results
9595
text_sources = self.get_sources_content(results, use_semantic_captions, use_image_citation=False)
9696
rendered_answer_prompt = self.prompt_manager.render_prompt(
97-
self.answer_prompt, {"user_query": q, "text_sources": text_sources}
97+
self.answer_prompt,
98+
self.get_system_prompt_variables(overrides.get("prompt_template"))
99+
| {"user_query": q, "text_sources": text_sources},
98100
)
99101

100102
chat_completion = await self.openai_client.chat.completions.create(

app/backend/approaches/retrievethenreadvision.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ async def run(
122122
image_sources.append(url)
123123

124124
rendered_answer_prompt = self.prompt_manager.render_prompt(
125-
self.answer_prompt, {"user_query": q, "text_sources": text_sources, "image_sources": image_sources}
125+
self.answer_prompt,
126+
self.get_system_prompt_variables(overrides.get("prompt_template"))
127+
| {"user_query": q, "text_sources": text_sources, "image_sources": image_sources},
126128
)
127129

128130
chat_completion = await self.openai_client.chat.completions.create(
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"context": {
3+
"data_points": {
4+
"text": [
5+
"Benefit_Options-2.pdf: There is a whistleblower policy."
6+
]
7+
},
8+
"thoughts": [
9+
{
10+
"description": "What is the capital of France?",
11+
"props": {
12+
"filter": null,
13+
"top": 3,
14+
"use_semantic_captions": false,
15+
"use_semantic_ranker": false,
16+
"use_text_search": true,
17+
"use_vector_search": false
18+
},
19+
"title": "Search using user query"
20+
},
21+
{
22+
"description": [
23+
{
24+
"captions": [
25+
{
26+
"additional_properties": {},
27+
"highlights": [],
28+
"text": "Caption: A whistleblower policy."
29+
}
30+
],
31+
"category": null,
32+
"content": "There is a whistleblower policy.",
33+
"embedding": null,
34+
"groups": null,
35+
"id": "file-Benefit_Options_pdf-42656E656669745F4F7074696F6E732E706466-page-2",
36+
"imageEmbedding": null,
37+
"oids": null,
38+
"reranker_score": 3.4577205181121826,
39+
"score": 0.03279569745063782,
40+
"sourcefile": "Benefit_Options.pdf",
41+
"sourcepage": "Benefit_Options-2.pdf"
42+
}
43+
],
44+
"props": null,
45+
"title": "Search results"
46+
},
47+
{
48+
"description": [
49+
{
50+
"content": "You are a cat.",
51+
"role": "system"
52+
},
53+
{
54+
"content": "What is the deductible for the employee plan for a visit to Overlake in Bellevue?\n\nSources:\ninfo1.txt: deductibles depend on whether you are in-network or out-of-network. In-network deductibles are $500 for employee and $1000 for family. Out-of-network deductibles are $1000 for employee and $2000 for family.\ninfo2.pdf: Overlake is in-network for the employee plan.\ninfo3.pdf: Overlake is the name of the area that includes a park and ride near Bellevue.\ninfo4.pdf: In-network institutions include Overlake, Swedish and others in the region.",
55+
"role": "user"
56+
},
57+
{
58+
"content": "In-network deductibles are $500 for employee and $1000 for family [info1.txt] and Overlake is in-network for the employee plan [info2.pdf][info4.pdf].",
59+
"role": "assistant"
60+
},
61+
{
62+
"content": "What is the capital of France?\nSources:\n\nBenefit_Options-2.pdf: There is a whistleblower policy.",
63+
"role": "user"
64+
}
65+
],
66+
"props": {
67+
"model": "gpt-35-turbo"
68+
},
69+
"title": "Prompt to generate answer"
70+
}
71+
]
72+
},
73+
"message": {
74+
"content": "The capital of France is Paris. [Benefit_Options-2.pdf].",
75+
"role": "assistant"
76+
},
77+
"session_state": null
78+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"context": {
3+
"data_points": {
4+
"text": [
5+
"Benefit_Options-2.pdf: There is a whistleblower policy."
6+
]
7+
},
8+
"thoughts": [
9+
{
10+
"description": "What is the capital of France?",
11+
"props": {
12+
"filter": null,
13+
"top": 3,
14+
"use_semantic_captions": false,
15+
"use_semantic_ranker": false,
16+
"use_text_search": true,
17+
"use_vector_search": false
18+
},
19+
"title": "Search using user query"
20+
},
21+
{
22+
"description": [
23+
{
24+
"captions": [
25+
{
26+
"additional_properties": {},
27+
"highlights": [],
28+
"text": "Caption: A whistleblower policy."
29+
}
30+
],
31+
"category": null,
32+
"content": "There is a whistleblower policy.",
33+
"embedding": null,
34+
"groups": null,
35+
"id": "file-Benefit_Options_pdf-42656E656669745F4F7074696F6E732E706466-page-2",
36+
"imageEmbedding": null,
37+
"oids": null,
38+
"reranker_score": 3.4577205181121826,
39+
"score": 0.03279569745063782,
40+
"sourcefile": "Benefit_Options.pdf",
41+
"sourcepage": "Benefit_Options-2.pdf"
42+
}
43+
],
44+
"props": null,
45+
"title": "Search results"
46+
},
47+
{
48+
"description": [
49+
{
50+
"content": "You are a cat.",
51+
"role": "system"
52+
},
53+
{
54+
"content": "What is the deductible for the employee plan for a visit to Overlake in Bellevue?\n\nSources:\ninfo1.txt: deductibles depend on whether you are in-network or out-of-network. In-network deductibles are $500 for employee and $1000 for family. Out-of-network deductibles are $1000 for employee and $2000 for family.\ninfo2.pdf: Overlake is in-network for the employee plan.\ninfo3.pdf: Overlake is the name of the area that includes a park and ride near Bellevue.\ninfo4.pdf: In-network institutions include Overlake, Swedish and others in the region.",
55+
"role": "user"
56+
},
57+
{
58+
"content": "In-network deductibles are $500 for employee and $1000 for family [info1.txt] and Overlake is in-network for the employee plan [info2.pdf][info4.pdf].",
59+
"role": "assistant"
60+
},
61+
{
62+
"content": "What is the capital of France?\nSources:\n\nBenefit_Options-2.pdf: There is a whistleblower policy.",
63+
"role": "user"
64+
}
65+
],
66+
"props": {
67+
"deployment": "test-chatgpt",
68+
"model": "gpt-35-turbo"
69+
},
70+
"title": "Prompt to generate answer"
71+
}
72+
]
73+
},
74+
"message": {
75+
"content": "The capital of France is Paris. [Benefit_Options-2.pdf].",
76+
"role": "assistant"
77+
},
78+
"session_state": null
79+
}

0 commit comments

Comments
 (0)