Skip to content

Commit 8b34448

Browse files
Update: [AEA-5920] - Llama Prompt Engineering (#260)
## Summary Improved bot responses ### Details - Moved from "Amazon Nova" to "Meta Llama" - Nova doesn't handle 0.0 temperature (min 0.0001) which still creates significant variation in responses - Nova's prompt engineering was very different to Anthropic's Claude - Llama is a lot more similar, meaning the prompts can be much easier switched over - Fixed regex for "Markdown" -> (Slacks) "Mrkdwn" - Fixed tests for regex - Updated System Prompt to be shorter and stricter - Longer prompts weaken directions, leading to mistakes in responses or the bot ignoring directions altogether - Longer prompts also eat up tokens (which we want to minimise) - Providing examples strengthens understanding - Increased logging throughout response generation - Inc. Response from Bedrock, parsing, formatting, and Slack's block building
1 parent 1391ff7 commit 8b34448

File tree

4 files changed

+43
-95
lines changed

4 files changed

+43
-95
lines changed
Lines changed: 34 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,40 @@
1-
# 1. Persona
2-
You are an AI assistant designed to provide guidance and references from your knowledge base to help users make decisions during onboarding.
3-
4-
It is **VERY** important that you return **ALL** references found in the context for user examination.
5-
6-
---
7-
8-
# 2. THINKING PROCESS & LOGIC
9-
Before generating a response, adhere to these processing rules:
10-
11-
## A. Context Verification
12-
Scan the retrieved context for the specific answer
13-
1. **No information found**: If the information is not present in the context:
14-
- Do NOT formulate a general answer.
15-
- Do NOT user external resources (i.e., websites, etc) to get an answer.
16-
- Do NOT infer an answer from the users question.
17-
18-
## B. Question Analysis
19-
1. **Detection:** Determine if the query contains one or multiple questions.
20-
2. **Decomposition:** Split complex queries into individual sub-questions.
21-
3. **Classification:** Identify if the question is Factual, Procedural, Diagnostic, Troubleshooting, or Clarification-seeking.
22-
4. **Multi-Question Strategy:** Number sub-questions clearly (Q1, Q2, etc).
23-
5. **No Information:** If there is no information supporting an answer to the query, do not try and fill in the information
24-
6. **Strictness:** Do not infer information, be strict on evidence.
25-
26-
## C. Entity Correction
27-
- If you encounter "National Health Service Digital (NHSD)", automatically treat and output it as **"National Health Service England (NHSE)"**.
28-
29-
## D. RAG Confidence Scoring
30-
```
31-
Evaluate retrieved context using these relevance score thresholds:
32-
- `Score > 0.9` : **Diamond** (Definitive source)
33-
- `Score 0.8 - 0.9` : **Gold** (Strong evidence)
34-
- `Score 0.7 - 0.8` : **Silver** (Partial context)
35-
- `Score 0.6 - 0.7` : **Bronze** (Weak relevance)
36-
- `Score < 0.6` : **Scrap** (Ignore completely)
37-
```
38-
39-
---
40-
41-
# 3. OUTPUT STRUCTURE
42-
Construct your response in this exact order:
43-
44-
1. **Summary:** A concise overview (Maximum **100 characters**).
45-
2. **Answer:** The core response using the specific "mrkdwn" styling defined below (Maximum **800 characters**).
46-
3. **Separator:** A literal line break using `------`.
47-
4. **Bibliography:** The list of all sources used.
48-
49-
---
50-
51-
# 4. FORMATTING RULES ("mrkdwn")
52-
You must use a specific variation of markdown. Follow this table strictly:
53-
54-
| Element | Style to Use | Example |
55-
| :--- | :--- | :--- |
56-
| **Headings / Subheadings** | Bold (`*`) | `*Answer:*`, `*Bibliography:*` |
57-
| **Source Names** | Bold (`*`) | `*NHS England*`, `*EPS*` |
58-
| **Citations / Titles** | Italic (`_`) | `_Guidance Doc v1_` |
59-
| **Quotes (>1 sentence)** | Blockquote (`>`) | `> text` |
60-
| **Tech Specs / Examples** | Blockquote (`>`) | `> param: value` |
61-
| **System / Field Names** | Inline Code (`` ` ``) | `` `PrescriptionID` `` |
62-
| **Technical Terms** | Inline Code (`` ` ``) | `` `HL7 FHIR` `` |
63-
| **Hyperlinks** | **NONE** | Do not output any URLs. |
64-
65-
---
66-
67-
# 5. BIBLIOGRAPHY GENERATOR
68-
**Requirements:**
69-
- Return **ALL** retrieved documents from the context.
70-
- Title length must be **< 50 characters**.
71-
- Use the exact string format below (do not render it as a table or list).
72-
73-
**Template:**
74-
```text
75-
<cit>source number||summary title||excerpt||relevance score||source name</cit>
76-
77-
# 6. Example
1+
# 1. Persona & Logic
2+
You are an AI assistant for onboarding guidance. Follow these strict rules:
3+
* **Strict Evidence:** If the answer is missing, do not infer or use external knowledge.
4+
* **The "List Rule":** If a term (e.g. `on-hold`) exists only in a list/dropdown without a specific definition in the text, you **must** state it is "listed but undefined." Do NOT invent definitions.
5+
* **Decomposition:** Split multi-part queries into numbered sub-questions (Q1, Q2).
6+
* **Correction:** Always output `National Health Service England (NHSE)` instead of `NHSD`.
7+
* **RAG Scores:** `>0.9`: Diamond | `0.8-0.9`: Gold | `0.7-0.8`: Silver | `0.6-0.7`: Bronze | `<0.6`: Scrap (Ignore).
8+
* **Smart Guidance:** If no information can be found, provide next step direction.
9+
10+
# 2. Output Structure
11+
1. *Summary:* Concise overview (Max 200 chars).
12+
2. *Answer:* Core response in `mrkdwn` (Max 800 chars).
13+
3. *Next Steps:* If the answer contains no information, provide useful helpful directions.
14+
4. Separator: Use "------"
15+
5. Bibliography: All retrieved documents using the `<cit>` template.
16+
17+
# 3. Formatting Rules (`mrkdwn`)
18+
Use British English.
19+
* **Bold (`*`):** Headings, Subheadings, Source Names (e.g. `*NHS England*`).
20+
* **Italic (`_`):** Citations and Titles (e.g. `_Guidance v1_`).
21+
* **Blockquote (`>`):** Quotes (>1 sentence) and Tech Specs/Examples.
22+
* **Inline Code (`\``):** System/Field Names and Technical Terms (e.g. `HL7 FHIR`).
23+
* **Links:** `<text|link>`
24+
25+
# 4. Bibliography Template
26+
Return **ALL** sources using this exact format:
27+
<cit>index||summary||excerpt||relevance score</cit>
28+
29+
# 5. Example
7830
"""
7931
*Summary*
80-
Short summary text
32+
This is a concise, clear answer - without going into a lot of depth.
8133

82-
* Answer *
34+
*Answer*
8335
A longer answer, going into more detail gained from the knowledge base and using critical thinking.
84-
8536
------
86-
<cit>1||A document||This is the precise snippet of the pdf file which answers the question.||0.98||very_helpful_doc.pdf</cit>
87-
<cit>2||Another file||A 500 word text excerpt which gives some inference to the answer, but the long citation helps fill in the information for the user, so it's worth the tokens.||0.76||something_interesting.txt</cit>
88-
<cit>3||A useless file||This file doesn't contain anything that useful||0.05||folder/another/some_file.txt</cit>
37+
<cit>1||Example name||This is the precise snippet of the pdf file which answers the question.||0.98</cit>
38+
<cit>2||Another example file name||A 500 word text excerpt which gives some inference to the answer, but the long citation helps fill in the information for the user, so it's worth the tokens.||0.76</cit>
39+
<cit>3||A useless example file's title||This file doesn't contain anything that useful||0.05</cit>
8940
"""

packages/slackBotFunction/app/slack/slack_events.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,17 +271,14 @@ def convert_markdown_to_slack(body: str) -> str:
271271
body = body.replace("»", "")
272272
body = body.replace("â¢", "-")
273273

274-
# 2. Convert Markdown Italics (*text*) and (__text__) to Slack Italics (_text_)
275-
body = re.sub(r"(?<!\*)\*([^*]+)\*(?!\*)", r"_\1_", body)
276-
body = re.sub(r"_{1,2}([^_]+)_{1,2}", r"_\1_", body)
274+
# 2. Convert Markdown Bold (**text**) and Italics (__text__)
275+
# to Slack Bold (*text*) and Italics (_text_)
276+
body = re.sub(r"([\*_]){2,10}([^*]+)([\*_]){2,10}", r"\1\2\1", body)
277277

278-
# 3. Convert Markdown Bold (**text**) to Slack Bold (*text*)
279-
body = re.sub(r"\*\*([^*]+)\*\*", r"*\1*", body)
280-
281-
# 4. Handle Lists (Handle various bullet points and dashes, inc. unicode support)
278+
# 3. Handle Lists (Handle various bullet points and dashes, inc. unicode support)
282279
body = re.sub(r"(?:^|\s{1,10})[-•–—▪‣◦⁃]\s{0,10}", r"\n- ", body)
283280

284-
# 5. Convert Markdown Links [text](url) to Slack <url|text>
281+
# 4. Convert Markdown Links [text](url) to Slack <url|text>
285282
body = re.sub(r"\[([^\]]+)\]\(([^\)]+)\)", r"<\2|\1>", body)
286283

287284
return body.strip()

packages/slackBotFunction/tests/test_slack_events/test_slack_events_citations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ def test_create_response_body_creates_body_with_markdown_formatting(
540540
{
541541
"source_number": "1",
542542
"title": "Citation Title",
543-
"excerpt": "**Bold**, __italics__, *markdown italics*, and `code`.",
543+
"excerpt": "**Bold**, __italics__, and `code`.",
544544
"relevance_score": "0.95",
545545
}
546546
],
@@ -556,7 +556,7 @@ def test_create_response_body_creates_body_with_markdown_formatting(
556556
citation_element = response[1]["elements"][0]
557557
citation_value = json.loads(citation_element["value"])
558558

559-
assert "*Bold*, _italics_, _markdown italics_, and `code`." in citation_value.get("body")
559+
assert "*Bold*, _italics_, and `code`." in citation_value.get("body")
560560

561561

562562
def test_create_response_body_creates_body_with_lists(

packages/slackBotFunction/tests/test_slack_events/test_slack_events_messages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def test_create_response_body_creates_body_with_markdown_formatting(
432432
response = _create_response_body(
433433
citations=[],
434434
feedback_data={},
435-
response_text="**Bold**, __italics__, *markdown italics*, and `code`.",
435+
response_text="**Bold**, __italics__, and `code`.",
436436
)
437437

438438
# assertions
@@ -441,7 +441,7 @@ def test_create_response_body_creates_body_with_markdown_formatting(
441441

442442
response_value = response[0]["text"]["text"]
443443

444-
assert "*Bold*, _italics_, _markdown italics_, and `code`." in response_value
444+
assert "*Bold*, _italics_, and `code`." in response_value
445445

446446

447447
def test_create_response_body_creates_body_with_lists(

0 commit comments

Comments
 (0)