Spaces:
Sleeping
Sleeping
Commit
·
d532a01
1
Parent(s):
a51583a
update the prompt
Browse files
backend/api/services/agent_orchestrator.py
CHANGED
|
@@ -754,6 +754,18 @@ Response:"""
|
|
| 754 |
"error": str(build_err),
|
| 755 |
}
|
| 756 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 757 |
|
| 758 |
llm_start = time.time()
|
| 759 |
llm_out = await self.llm.simple_call(prompt_for_llm, temperature=req.temperature)
|
|
@@ -886,18 +898,55 @@ Response:"""
|
|
| 886 |
|
| 887 |
def _build_prompt_with_rag(self, req: AgentRequest, rag_resp: Dict[str, Any]) -> str:
|
| 888 |
snippets = []
|
|
|
|
| 889 |
if isinstance(rag_resp, dict):
|
| 890 |
hits = rag_resp.get("results") or rag_resp.get("hits") or []
|
| 891 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 892 |
txt = h.get("text") or h.get("content") or str(h)
|
| 893 |
-
|
|
|
|
|
|
|
| 894 |
|
| 895 |
-
snippet_text = "\n
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
| 900 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 901 |
return prompt
|
| 902 |
|
| 903 |
async def _execute_multi_step(self, req: AgentRequest, steps: List[Dict[str, Any]],
|
|
@@ -1260,21 +1309,39 @@ Response:"""
|
|
| 1260 |
# Otherwise, build the normal multi-step synthesis prompt.
|
| 1261 |
if data_section:
|
| 1262 |
prompt = (
|
| 1263 |
-
f"You are an assistant helping tenant {req.tenant_id}
|
|
|
|
| 1264 |
f"## Information Collected\n"
|
| 1265 |
-
f"The following details have been gathered from multiple reliable sources:\n"
|
| 1266 |
f"{data_section}\n\n"
|
| 1267 |
-
f"## User Request\n"
|
| 1268 |
-
f"{req.message}\n\n"
|
| 1269 |
f"## Your Task\n"
|
| 1270 |
-
f"Use the information above to directly address the user's request
|
| 1271 |
-
f"
|
| 1272 |
-
f"
|
| 1273 |
-
f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1274 |
)
|
| 1275 |
else:
|
| 1276 |
-
# No data collected,
|
| 1277 |
-
prompt =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1278 |
|
| 1279 |
# Final LLM synthesis
|
| 1280 |
try:
|
|
@@ -1761,27 +1828,47 @@ Rewritten message:"""
|
|
| 1761 |
snippets = []
|
| 1762 |
if isinstance(web_resp, dict):
|
| 1763 |
hits = web_resp.get("results") or web_resp.get("items") or []
|
| 1764 |
-
for h in hits[:5]:
|
| 1765 |
title = h.get("title") or h.get("headline") or ""
|
| 1766 |
snippet = h.get("snippet") or h.get("summary") or h.get("text") or ""
|
| 1767 |
url = h.get("url") or h.get("link") or ""
|
| 1768 |
-
|
| 1769 |
-
|
| 1770 |
-
|
| 1771 |
-
|
| 1772 |
-
|
| 1773 |
-
|
| 1774 |
-
|
| 1775 |
-
|
| 1776 |
-
|
| 1777 |
-
|
| 1778 |
-
|
| 1779 |
-
|
| 1780 |
-
|
| 1781 |
-
|
| 1782 |
-
|
| 1783 |
-
|
| 1784 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1785 |
|
| 1786 |
return prompt
|
| 1787 |
|
|
|
|
| 754 |
"error": str(build_err),
|
| 755 |
}
|
| 756 |
)
|
| 757 |
+
elif not use_rag_for_admin:
|
| 758 |
+
# No RAG results available - enhance the prompt to still provide best answer
|
| 759 |
+
prompt_for_llm = (
|
| 760 |
+
f"You are an assistant helping tenant {req.tenant_id}.\n\n"
|
| 761 |
+
f"## User Question\n{req.message}\n\n"
|
| 762 |
+
f"## Your Task\n"
|
| 763 |
+
f"Provide the best possible answer to the user's question. "
|
| 764 |
+
f"Be clear, accurate, comprehensive, and helpful. "
|
| 765 |
+
f"Focus on giving the user exactly what they need—clear guidance, accurate facts, "
|
| 766 |
+
f"and practical steps whenever possible. "
|
| 767 |
+
f"If you're uncertain about tenant-specific details, acknowledge that and provide general guidance."
|
| 768 |
+
)
|
| 769 |
|
| 770 |
llm_start = time.time()
|
| 771 |
llm_out = await self.llm.simple_call(prompt_for_llm, temperature=req.temperature)
|
|
|
|
| 898 |
|
| 899 |
def _build_prompt_with_rag(self, req: AgentRequest, rag_resp: Dict[str, Any]) -> str:
|
| 900 |
snippets = []
|
| 901 |
+
scores_info = []
|
| 902 |
if isinstance(rag_resp, dict):
|
| 903 |
hits = rag_resp.get("results") or rag_resp.get("hits") or []
|
| 904 |
+
# Sort by score if available, take top 5
|
| 905 |
+
sorted_hits = sorted(
|
| 906 |
+
hits,
|
| 907 |
+
key=lambda h: float(h.get("score", h.get("similarity", 0.0))),
|
| 908 |
+
reverse=True
|
| 909 |
+
)[:5]
|
| 910 |
+
|
| 911 |
+
for i, h in enumerate(sorted_hits, 1):
|
| 912 |
txt = h.get("text") or h.get("content") or str(h)
|
| 913 |
+
score = h.get("score") or h.get("similarity", 0.0)
|
| 914 |
+
snippets.append(f"[Source {i}] {txt}")
|
| 915 |
+
scores_info.append(f"Source {i}: relevance score {score:.3f}")
|
| 916 |
|
| 917 |
+
snippet_text = "\n\n".join(snippets) if snippets else ""
|
| 918 |
+
scores_text = "\n".join(scores_info) if scores_info else ""
|
| 919 |
+
|
| 920 |
+
if not snippet_text:
|
| 921 |
+
prompt = (
|
| 922 |
+
f"You are an assistant helping tenant {req.tenant_id}.\n\n"
|
| 923 |
+
f"## User Question\n{req.message}\n\n"
|
| 924 |
+
f"## Context\n"
|
| 925 |
+
f"No relevant documents were found in the knowledge base for this question.\n\n"
|
| 926 |
+
f"## Your Task\n"
|
| 927 |
+
f"Provide the best possible answer based on your general knowledge. "
|
| 928 |
+
f"Be clear, accurate, and helpful. If you're uncertain about tenant-specific details, "
|
| 929 |
+
f"acknowledge that and provide general guidance."
|
| 930 |
+
)
|
| 931 |
+
else:
|
| 932 |
+
prompt = (
|
| 933 |
+
f"You are an assistant helping tenant {req.tenant_id}. "
|
| 934 |
+
f"Your goal is to provide the most accurate, comprehensive, and helpful answer possible.\n\n"
|
| 935 |
+
f"## Knowledge Base Documents\n"
|
| 936 |
+
f"The following documents were retrieved from the tenant's knowledge base as relevant to the user's question:\n\n"
|
| 937 |
+
f"{snippet_text}\n\n"
|
| 938 |
+
f"{'## Relevance Scores\n' + scores_text + '\n\n' if scores_text else ''}"
|
| 939 |
+
f"## User Question\n{req.message}\n\n"
|
| 940 |
+
f"## Your Task\n"
|
| 941 |
+
f"1. **Primary Goal**: Answer the user's question using the information from the knowledge base documents above.\n"
|
| 942 |
+
f"2. **Accuracy**: Base your answer primarily on the highest-scoring sources (most relevant documents).\n"
|
| 943 |
+
f"3. **Comprehensiveness**: If multiple sources provide complementary information, synthesize them into a complete answer.\n"
|
| 944 |
+
f"4. **Citation**: When referencing specific information, indicate which source(s) you used (e.g., 'According to Source 1...' or 'Sources 1 and 2 indicate...').\n"
|
| 945 |
+
f"5. **Completeness**: If the documents don't fully answer the question, clearly state what information is available and what is missing.\n"
|
| 946 |
+
f"6. **Clarity**: Write in a clear, professional, and easy-to-understand manner.\n"
|
| 947 |
+
f"7. **Directness**: Get straight to the point - provide the answer the user needs without unnecessary preamble.\n\n"
|
| 948 |
+
f"Provide your answer now:"
|
| 949 |
+
)
|
| 950 |
return prompt
|
| 951 |
|
| 952 |
async def _execute_multi_step(self, req: AgentRequest, steps: List[Dict[str, Any]],
|
|
|
|
| 1309 |
# Otherwise, build the normal multi-step synthesis prompt.
|
| 1310 |
if data_section:
|
| 1311 |
prompt = (
|
| 1312 |
+
f"You are an assistant helping tenant {req.tenant_id}. "
|
| 1313 |
+
f"Your goal is to provide the most accurate, comprehensive, and helpful answer possible.\n\n"
|
| 1314 |
f"## Information Collected\n"
|
| 1315 |
+
f"The following details have been gathered from multiple reliable sources (knowledge base, web search, etc.):\n\n"
|
| 1316 |
f"{data_section}\n\n"
|
| 1317 |
+
f"## User Request\n{req.message}\n\n"
|
|
|
|
| 1318 |
f"## Your Task\n"
|
| 1319 |
+
f"1. **Primary Goal**: Use the information above to directly and completely address the user's request.\n"
|
| 1320 |
+
f"2. **Synthesis**: Combine information from different sources when they provide complementary details.\n"
|
| 1321 |
+
f"3. **Prioritization**: If sources conflict, prioritize the most authoritative or recent information.\n"
|
| 1322 |
+
f"4. **Completeness**: Provide a comprehensive answer that covers all aspects of the user's question.\n"
|
| 1323 |
+
f"5. **Accuracy**: Base your answer on the provided information. If information is missing or uncertain, clearly state that.\n"
|
| 1324 |
+
f"6. **Clarity**: Write in a clear, professional, and easy-to-understand manner.\n"
|
| 1325 |
+
f"7. **Directness**: Get straight to the point - provide the answer the user needs without unnecessary preamble.\n"
|
| 1326 |
+
f"8. **Actionability**: If the question requires steps or actions, provide clear, actionable guidance.\n"
|
| 1327 |
+
f"9. **Citation**: When referencing specific sources, indicate which source(s) you used (e.g., '[RAG]', '[WEB]').\n\n"
|
| 1328 |
+
f"If the information is incomplete, explain what can and cannot be concluded from the available data. "
|
| 1329 |
+
f"Focus on giving the user exactly what they need—clear guidance, accurate facts, and practical steps whenever possible.\n\n"
|
| 1330 |
+
f"Provide your comprehensive answer now:"
|
| 1331 |
)
|
| 1332 |
else:
|
| 1333 |
+
# No data collected, provide best answer from general knowledge
|
| 1334 |
+
prompt = (
|
| 1335 |
+
f"You are an assistant helping tenant {req.tenant_id}.\n\n"
|
| 1336 |
+
f"## User Question\n{req.message}\n\n"
|
| 1337 |
+
f"## Context\n"
|
| 1338 |
+
f"No specific information was found in the knowledge base or web search for this question.\n\n"
|
| 1339 |
+
f"## Your Task\n"
|
| 1340 |
+
f"Provide the best possible answer based on your general knowledge. "
|
| 1341 |
+
f"Be clear, accurate, comprehensive, and helpful. "
|
| 1342 |
+
f"If you're uncertain about tenant-specific details, acknowledge that and provide general guidance. "
|
| 1343 |
+
f"Focus on giving the user exactly what they need—clear guidance, accurate facts, and practical steps whenever possible."
|
| 1344 |
+
)
|
| 1345 |
|
| 1346 |
# Final LLM synthesis
|
| 1347 |
try:
|
|
|
|
| 1828 |
snippets = []
|
| 1829 |
if isinstance(web_resp, dict):
|
| 1830 |
hits = web_resp.get("results") or web_resp.get("items") or []
|
| 1831 |
+
for i, h in enumerate(hits[:5], 1):
|
| 1832 |
title = h.get("title") or h.get("headline") or ""
|
| 1833 |
snippet = h.get("snippet") or h.get("summary") or h.get("text") or ""
|
| 1834 |
url = h.get("url") or h.get("link") or ""
|
| 1835 |
+
display_link = h.get("displayLink") or h.get("display_link") or ""
|
| 1836 |
+
source_info = display_link if display_link else url.split('/')[2] if url else "Unknown source"
|
| 1837 |
+
snippets.append(f"[Result {i}] {title}\n{snippet}\nSource: {source_info} ({url})")
|
| 1838 |
+
|
| 1839 |
+
snippet_text = "\n\n".join(snippets) if snippets else ""
|
| 1840 |
+
|
| 1841 |
+
if not snippet_text:
|
| 1842 |
+
prompt = (
|
| 1843 |
+
f"You are an assistant helping tenant {req.tenant_id}.\n\n"
|
| 1844 |
+
f"## User Question\n{req.message}\n\n"
|
| 1845 |
+
f"## Context\n"
|
| 1846 |
+
f"No relevant web search results were found for this question.\n\n"
|
| 1847 |
+
f"## Your Task\n"
|
| 1848 |
+
f"Provide the best possible answer based on your general knowledge. "
|
| 1849 |
+
f"Be clear, accurate, and helpful. If you're uncertain about specific details, "
|
| 1850 |
+
f"acknowledge that and provide general guidance."
|
| 1851 |
+
)
|
| 1852 |
+
else:
|
| 1853 |
+
prompt = (
|
| 1854 |
+
f"You are an assistant helping tenant {req.tenant_id} with access to recent web search results. "
|
| 1855 |
+
f"Your goal is to provide the most accurate, comprehensive, and helpful answer possible.\n\n"
|
| 1856 |
+
f"## Web Search Results\n"
|
| 1857 |
+
f"The following search results were found for the user's question:\n\n"
|
| 1858 |
+
f"{snippet_text}\n\n"
|
| 1859 |
+
f"## User Question\n{req.message}\n\n"
|
| 1860 |
+
f"## Your Task\n"
|
| 1861 |
+
f"1. **Primary Goal**: Answer the user's question using the information from the web search results above.\n"
|
| 1862 |
+
f"2. **Accuracy**: Prioritize information from authoritative sources (recognized websites, official sources, etc.).\n"
|
| 1863 |
+
f"3. **Synthesis**: If multiple results provide different perspectives or complementary information, synthesize them into a comprehensive answer.\n"
|
| 1864 |
+
f"4. **Verification**: If results conflict, mention the discrepancy and provide the most reliable information.\n"
|
| 1865 |
+
f"5. **Citation**: When referencing specific information, indicate which result(s) you used (e.g., 'According to Result 1...' or 'Results 1 and 2 indicate...').\n"
|
| 1866 |
+
f"6. **Completeness**: If the search results don't fully answer the question, clearly state what information is available and what might be missing.\n"
|
| 1867 |
+
f"7. **Clarity**: Write in a clear, professional, and easy-to-understand manner.\n"
|
| 1868 |
+
f"8. **Directness**: Get straight to the point - provide the answer the user needs without unnecessary preamble.\n"
|
| 1869 |
+
f"9. **Relevance**: Focus on information that directly addresses the user's question.\n\n"
|
| 1870 |
+
f"Provide your answer now:"
|
| 1871 |
+
)
|
| 1872 |
|
| 1873 |
return prompt
|
| 1874 |
|