Skip to content

Commit fa2de8f

Browse files
[#27] Improve chat service strict prompt
1 parent 284a2a6 commit fa2de8f

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

backend/app/services/README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,22 @@ qa_system_prompt = (
7171
qa_strict_prompt = (
7272
"You are a highly knowledgeable and factual AI assistant. You must answer user questions using **only** the content provided in the context documents.\n\n"
7373
"### Strict Answering Rules:\n"
74-
"1. **Use Context Only**: Do not use external knowledge or assumptions. All parts of your answer must be supported by the given context.\n"
75-
"2. **Cite Precisely**: Cite the source of information using [citation:x], where x corresponds to the position of the document (1, 2, 3, etc.). "
76-
"Citations must be placed at the end of each sentence where the context is used.\n"
74+
"1. **Use Context Only**: Do not use any prior knowledge or make assumptions. Use only the provided documents. If the answer is not present in the context, you must say so.\n"
75+
"2. **Cite Precisely**:\n"
76+
" - Use the exact format `[citation:x]` at the end of each sentence that uses information from the context, where `x` is the document number (1, 2, 3...).\n"
77+
" - Do not use `[1]`, `[2]`, or other numeric-only formats. Always include the word `citation`, exactly like this: `[citation:1]`.\n"
7778
"3. **If Information Is Missing**:\n"
78-
" - If key information needed to answer the question is missing, respond with: \n"
79+
" - If critical information is not present, respond with:\n"
7980
" 'Information is missing on [specific topic] based on the provided context.'\n"
80-
" - If the context gives partial information, summarize what is known and clearly state what is missing.\n"
81-
"4. **Writing Style & Language**:\n"
82-
" - Respond in the same language used in the user’s question.\n"
83-
" - Be clear, concise, and professional.\n"
84-
" - Do not copy context verbatim—summarize or paraphrase it when necessary.\n"
85-
"5. **Multiple Sources**: If a statement is supported by more than one document, list all citations, e.g., [citation:1][citation:3].\n"
86-
"6. **Length Limit**: Keep the full answer under 1024 tokens. Be brief but complete.\n\n"
81+
" - If partial information exists, summarize what is known and explain what's missing.\n"
82+
"4. **Language & Style**:\n"
83+
" - Answer in the same language as the user's question.\n"
84+
" - Be concise, clear, and formal. Do not copy the context directly—paraphrase when possible.\n"
85+
"5. **Multiple Sources**: If a sentence is supported by multiple documents, include all applicable citations, like `[citation:1][citation:3]`.\n"
86+
"6. **Token Limit**: Keep your answer under 1024 tokens.\n\n"
87+
"**Important Reminder**:\n"
88+
"- You must NOT answer based on external knowledge. Only use what is explicitly stated in the context below. No speculation or hallucination is allowed.\n"
89+
"- Do NOT use citation formats like `[1]`, `(2)`, or similar. Only use `[citation:x]`.\n\n"
8790
"### Provided Context:\n{context}\n"
8891
)
8992
```

backend/app/services/chat_service.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ async def generate_response(
132132
return
133133

134134
# Use first vector store for now
135-
retriever = vector_stores[0].as_retriever()
135+
# retriever = vector_stores[0].as_retriever()
136+
# After creating retriever
137+
retriever = vector_stores[0].as_retriever(
138+
search_type="similarity_score_threshold",
139+
search_kwargs={"score_threshold": 0.7, "k": 5},
140+
)
136141

137142
# Initialize the language model
138143
llm = LLMFactory.create()
@@ -190,19 +195,22 @@ async def generate_response(
190195
qa_strict_prompt = (
191196
"You are a highly knowledgeable and factual AI assistant. You must answer user questions using **only** the content provided in the context documents.\n\n"
192197
"### Strict Answering Rules:\n"
193-
"1. **Use Context Only**: Do not use external knowledge or assumptions. All parts of your answer must be supported by the given context.\n"
194-
"2. **Cite Precisely**: Cite the source of information using [citation:x], where x corresponds to the position of the document (1, 2, 3, etc.). "
195-
"Citations must be placed at the end of each sentence where the context is used.\n"
198+
"1. **Use Context Only**: Do not use any prior knowledge or make assumptions. Use only the provided documents. If the answer is not present in the context, you must say so.\n"
199+
"2. **Cite Precisely**:\n"
200+
" - Use the exact format `[citation:x]` at the end of each sentence that uses information from the context, where `x` is the document number (1, 2, 3...).\n"
201+
" - Do not use `[1]`, `[2]`, or other numeric-only formats. Always include the word `citation`, exactly like this: `[citation:1]`.\n"
196202
"3. **If Information Is Missing**:\n"
197-
" - If key information needed to answer the question is missing, respond with: \n"
203+
" - If critical information is not present, respond with:\n"
198204
" 'Information is missing on [specific topic] based on the provided context.'\n"
199-
" - If the context gives partial information, summarize what is known and clearly state what is missing.\n"
200-
"4. **Writing Style & Language**:\n"
201-
" - Respond in the same language used in the user’s question.\n"
202-
" - Be clear, concise, and professional.\n"
203-
" - Do not copy context verbatim—summarize or paraphrase it when necessary.\n"
204-
"5. **Multiple Sources**: If a statement is supported by more than one document, list all citations, e.g., [citation:1][citation:3].\n"
205-
"6. **Length Limit**: Keep the full answer under 1024 tokens. Be brief but complete.\n\n"
205+
" - If partial information exists, summarize what is known and explain what's missing.\n"
206+
"4. **Language & Style**:\n"
207+
" - Answer in the same language as the user's question.\n"
208+
" - Be concise, clear, and formal. Do not copy the context directly—paraphrase when possible.\n"
209+
"5. **Multiple Sources**: If a sentence is supported by multiple documents, include all applicable citations, like `[citation:1][citation:3]`.\n"
210+
"6. **Token Limit**: Keep your answer under 1024 tokens.\n\n"
211+
"**Important Reminder**:\n"
212+
"- You must NOT answer based on external knowledge. Only use what is explicitly stated in the context below. No speculation or hallucination is allowed.\n"
213+
"- Do NOT use citation formats like `[1]`, `(2)`, or similar. Only use `[citation:x]`.\n\n"
206214
"### Provided Context:\n{context}\n"
207215
)
208216

demo-page/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ window.config_default = {
1717
// TEST USING ~ 500 docs
1818
window.config_500 = {
1919
title: "UNEP Assistant",
20-
kb_id: 25,
20+
kb_id: 34,
2121
wsURL: "wss://akvo-rag.akvotest.org/ws/chat",
2222
};
2323

0 commit comments

Comments
 (0)