Update Prompt (#1526)

This commit is contained in:
Yuhong Sun
2024-05-28 01:08:29 -07:00
committed by GitHub
parent 008a91bff0
commit 32c37f8b17
6 changed files with 54 additions and 34 deletions

View File

@@ -8,6 +8,7 @@ prompts:
# System Prompt (as shown in UI)
system: >
You are a question answering system that is constantly learning and improving.
The current date is DANSWER_DATETIME_REPLACEMENT.
You can process and comprehend vast amounts of text and utilize this knowledge to provide
grounded, accurate, and concise answers to diverse queries.
@@ -23,6 +24,7 @@ prompts:
If there are no relevant documents, refer to the chat history and existing knowledge.
# Inject a statement at the end of system prompt to inform the LLM of the current date/time
# If the DANSWER_DATETIME_REPLACEMENT is set, the date/time is inserted there instead
# Format looks like: "October 16, 2023 14:30"
datetime_aware: true
# Prompts the LLM to include citations in the for [1], [2] etc.
@@ -32,7 +34,16 @@ prompts:
- name: "OnlyLLM"
description: "Chat directly with the LLM!"
system: "You are a helpful assistant."
system: >
You are a helpful AI assistant. The current date is DANSWER_DATETIME_REPLACEMENT
You give concise responses to very simple questions, but provide more thorough responses to
more complex and open-ended questions.
You are happy to help with writing, analysis, question answering, math, coding and all sorts
of other tasks. You use markdown where reasonable and also for coding.
task: ""
datetime_aware: true
include_citations: true
@@ -43,10 +54,11 @@ prompts:
system: >
You are a text summarizing assistant that highlights the most important knowledge from the
context provided, prioritizing the information that relates to the user query.
The current date is DANSWER_DATETIME_REPLACEMENT.
You ARE NOT creative and always stick to the provided documents.
If there are no documents, refer to the conversation history.
IMPORTANT: YOU ONLY SUMMARIZE THE IMPORTANT INFORMATION FROM THE PROVIDED DOCUMENTS,
NEVER USE YOUR OWN KNOWLEDGE.
task: >
@@ -61,7 +73,8 @@ prompts:
description: "Recites information from retrieved context! Least creative but most safe!"
system: >
Quote and cite relevant information from provided context based on the user query.
The current date is DANSWER_DATETIME_REPLACEMENT.
You only provide quotes that are EXACT substrings from provided documents!
If there are no documents provided,

View File

@@ -14,10 +14,9 @@ from danswer.llm.utils import build_content_with_imgs
from danswer.llm.utils import check_message_tokens
from danswer.llm.utils import get_default_llm_tokenizer
from danswer.llm.utils import translate_history_to_basemessages
from danswer.prompts.chat_prompts import ADDITIONAL_INFO
from danswer.prompts.chat_prompts import CHAT_USER_CONTEXT_FREE_PROMPT
from danswer.prompts.prompt_utils import add_time_to_system_prompt
from danswer.prompts.prompt_utils import drop_messages_history_overflow
from danswer.prompts.prompt_utils import get_current_llm_day_time
from danswer.tools.message import ToolCallSummary
@@ -26,12 +25,7 @@ def default_build_system_message(
) -> SystemMessage | None:
system_prompt = prompt_config.system_prompt.strip()
if prompt_config.datetime_aware:
if system_prompt:
system_prompt += ADDITIONAL_INFO.format(
datetime_info=get_current_llm_day_time()
)
else:
system_prompt = get_current_llm_day_time()
system_prompt = add_time_to_system_prompt(system_prompt=system_prompt)
if not system_prompt:
return None

View File

@@ -13,14 +13,13 @@ from danswer.llm.interfaces import LLMConfig
from danswer.llm.utils import build_content_with_imgs
from danswer.llm.utils import check_number_of_tokens
from danswer.llm.utils import get_max_input_tokens
from danswer.prompts.chat_prompts import ADDITIONAL_INFO
from danswer.prompts.chat_prompts import REQUIRE_CITATION_STATEMENT
from danswer.prompts.constants import DEFAULT_IGNORE_STATEMENT
from danswer.prompts.direct_qa_prompts import CITATIONS_PROMPT
from danswer.prompts.direct_qa_prompts import CITATIONS_PROMPT_FOR_TOOL_CALLING
from danswer.prompts.prompt_utils import add_time_to_system_prompt
from danswer.prompts.prompt_utils import build_complete_context_str
from danswer.prompts.prompt_utils import build_task_prompt_reminders
from danswer.prompts.prompt_utils import get_current_llm_day_time
from danswer.prompts.token_counts import ADDITIONAL_INFO_TOKEN_CNT
from danswer.prompts.token_counts import (
CHAT_USER_PROMPT_WITH_CONTEXT_OVERHEAD_TOKEN_CNT,
@@ -122,12 +121,7 @@ def build_citations_system_message(
if prompt_config.include_citations:
system_prompt += REQUIRE_CITATION_STATEMENT
if prompt_config.datetime_aware:
if system_prompt:
system_prompt += ADDITIONAL_INFO.format(
datetime_info=get_current_llm_day_time()
)
else:
system_prompt = get_current_llm_day_time()
system_prompt = add_time_to_system_prompt(system_prompt=system_prompt)
return SystemMessage(content=system_prompt)

View File

@@ -14,16 +14,3 @@ INVALID_PAT = "Invalid:"
SOURCES_KEY = "sources"
DEFAULT_IGNORE_STATEMENT = " Ignore any context documents that are not relevant."
REQUIRE_CITATION_STATEMENT = """
Cite relevant statements INLINE using the format [1], [2], [3], etc to reference the document number, \
DO NOT provide a reference section at the end and DO NOT provide any links following the citations.
""".rstrip()
NO_CITATION_STATEMENT = """
Do not provide any citations even if there are examples in the chat history.
""".rstrip()
CITATION_REMINDER = """
Remember to provide inline citations in the format [1], [2], [3], etc.
"""

View File

@@ -9,18 +9,49 @@ from danswer.configs.chat_configs import MULTILINGUAL_QUERY_EXPANSION
from danswer.configs.constants import DocumentSource
from danswer.db.models import Prompt
from danswer.llm.answering.models import PromptConfig
from danswer.prompts.chat_prompts import ADDITIONAL_INFO
from danswer.prompts.chat_prompts import CITATION_REMINDER
from danswer.prompts.constants import CODE_BLOCK_PAT
from danswer.prompts.direct_qa_prompts import LANGUAGE_HINT
from danswer.search.models import InferenceChunk
def get_current_llm_day_time() -> str:
MOST_BASIC_PROMPT = "You are a helpful AI assistant."
DANSWER_DATETIME_REPLACEMENT = "DANSWER_DATETIME_REPLACEMENT"
BASIC_TIME_STR = "The current date is {datetime_info}."
def get_current_llm_day_time(
include_day_of_week: bool = True, full_sentence: bool = True
) -> str:
current_datetime = datetime.now()
# Format looks like: "October 16, 2023 14:30"
formatted_datetime = current_datetime.strftime("%B %d, %Y %H:%M")
day_of_week = current_datetime.strftime("%A")
return f"The current day and time is {day_of_week} {formatted_datetime}"
if full_sentence:
return f"The current day and time is {day_of_week} {formatted_datetime}"
if include_day_of_week:
return f"{day_of_week} {formatted_datetime}"
return f"{formatted_datetime}"
def add_time_to_system_prompt(system_prompt: str) -> str:
if DANSWER_DATETIME_REPLACEMENT in system_prompt:
return system_prompt.replace(
DANSWER_DATETIME_REPLACEMENT,
get_current_llm_day_time(full_sentence=False, include_day_of_week=False),
)
if system_prompt:
return system_prompt + ADDITIONAL_INFO.format(
datetime_info=get_current_llm_day_time()
)
else:
return (
MOST_BASIC_PROMPT
+ " "
+ BASIC_TIME_STR.format(datetime_info=get_current_llm_day_time())
)
def build_task_prompt_reminders(

View File

@@ -24,6 +24,7 @@ CITATION_REMINDER_TOKEN_CNT = check_number_of_tokens(CITATION_REMINDER)
LANGUAGE_HINT_TOKEN_CNT = check_number_of_tokens(LANGUAGE_HINT)
# If the date/time is inserted directly as a replacement in the prompt, this is a slight over count
ADDITIONAL_INFO_TOKEN_CNT = check_number_of_tokens(
ADDITIONAL_INFO.format(datetime_info=get_current_llm_day_time())
)