Skip to content

Commit

Permalink
made tagging logic more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
hagen-danswer committed Jan 17, 2025
1 parent 1b4c08e commit 4e6cce7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
11 changes: 8 additions & 3 deletions backend/onyx/chat/prompt_builder/answer_prompt_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from onyx.prompts.chat_prompts import CHAT_USER_CONTEXT_FREE_PROMPT
from onyx.prompts.direct_qa_prompts import HISTORY_BLOCK
from onyx.prompts.prompt_utils import drop_messages_history_overflow
from onyx.prompts.prompt_utils import handle_onyx_tags
from onyx.prompts.prompt_utils import handle_onyx_date_awareness
from onyx.tools.force import ForceUseTool
from onyx.tools.models import ToolCallFinalResult
from onyx.tools.models import ToolCallKickoff
Expand All @@ -31,7 +31,9 @@ def default_build_system_message(
prompt_config: PromptConfig,
) -> SystemMessage | None:
system_prompt = prompt_config.system_prompt.strip()
tag_handled_prompt = handle_onyx_tags(system_prompt, prompt_config.datetime_aware)
tag_handled_prompt = handle_onyx_date_awareness(
system_prompt, prompt_config, add_additional_info_if_no_tag=True
)

if not tag_handled_prompt:
return None
Expand Down Expand Up @@ -61,8 +63,11 @@ def default_build_user_message(
else user_query
)
user_prompt = user_prompt.strip()
tag_handled_prompt = handle_onyx_date_awareness(user_prompt, prompt_config)
user_msg = HumanMessage(
content=build_content_with_imgs(user_prompt, files) if files else user_prompt
content=build_content_with_imgs(tag_handled_prompt, files)
if files
else tag_handled_prompt
)
return user_msg

Expand Down
6 changes: 4 additions & 2 deletions backend/onyx/chat/prompt_builder/citations_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from onyx.prompts.direct_qa_prompts import HISTORY_BLOCK
from onyx.prompts.prompt_utils import build_complete_context_str
from onyx.prompts.prompt_utils import build_task_prompt_reminders
from onyx.prompts.prompt_utils import handle_onyx_tags
from onyx.prompts.prompt_utils import handle_onyx_date_awareness
from onyx.prompts.token_counts import ADDITIONAL_INFO_TOKEN_CNT
from onyx.prompts.token_counts import (
CHAT_USER_PROMPT_WITH_CONTEXT_OVERHEAD_TOKEN_CNT,
Expand Down Expand Up @@ -127,7 +127,9 @@ def build_citations_system_message(
system_prompt = prompt_config.system_prompt.strip()
if prompt_config.include_citations:
system_prompt += REQUIRE_CITATION_STATEMENT
tag_handled_prompt = handle_onyx_tags(system_prompt, prompt_config.datetime_aware)
tag_handled_prompt = handle_onyx_date_awareness(
system_prompt, prompt_config, add_additional_info_if_no_tag=True
)

return SystemMessage(content=tag_handled_prompt)

Expand Down
6 changes: 4 additions & 2 deletions backend/onyx/chat/prompt_builder/quotes_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from onyx.prompts.direct_qa_prompts import HISTORY_BLOCK
from onyx.prompts.direct_qa_prompts import JSON_PROMPT
from onyx.prompts.prompt_utils import build_complete_context_str
from onyx.prompts.prompt_utils import handle_onyx_tags
from onyx.prompts.prompt_utils import handle_onyx_date_awareness


def _build_strong_llm_quotes_prompt(
Expand Down Expand Up @@ -39,7 +39,9 @@ def _build_strong_llm_quotes_prompt(
language_hint_or_none=LANGUAGE_HINT.strip() if use_language_hint else "",
).strip()

tag_handled_prompt = handle_onyx_tags(full_prompt, prompt.datetime_aware)
tag_handled_prompt = handle_onyx_date_awareness(
full_prompt, prompt, add_additional_info_if_no_tag=True
)

return HumanMessage(content=tag_handled_prompt)

Expand Down
23 changes: 15 additions & 8 deletions backend/onyx/prompts/prompt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from onyx.configs.constants import DocumentSource
from onyx.context.search.models import InferenceChunk
from onyx.db.models import Prompt
from onyx.prompts.chat_prompts import ADDITIONAL_INFO
from onyx.prompts.chat_prompts import CITATION_REMINDER
from onyx.prompts.constants import CODE_BLOCK_PAT
from onyx.utils.logger import setup_logger
Expand Down Expand Up @@ -37,27 +38,33 @@ def get_current_llm_day_time(
return f"{formatted_datetime}"


def handle_onyx_tags(
def build_date_time_string() -> str:
return ADDITIONAL_INFO.format(datetime_info=get_current_llm_day_time())


def handle_onyx_date_awareness(
prompt_str: str,
datetime_aware: bool,
prompt_config: PromptConfig,
add_additional_info_if_no_tag: bool = False,
) -> str:
"""
If there is a [[CURRENT_DATETIME]] tag, replace it with the current date and time no matter what.
If the prompt is datetime aware, and there are no [[CURRENT_DATETIME]] tags, add it to the prompt.
do nothing otherwise.
This can later be expanded to support other tags.
"""

if DANSWER_DATETIME_REPLACEMENT in prompt_str:
return prompt_str.replace(
DANSWER_DATETIME_REPLACEMENT,
get_current_llm_day_time(full_sentence=False, include_day_of_week=True),
)
if datetime_aware:
return (
MOST_BASIC_PROMPT
+ " "
+ BASIC_TIME_STR.format(datetime_info=get_current_llm_day_time())
)
any_tag_present = any(
DANSWER_DATETIME_REPLACEMENT in text
for text in [prompt_str, prompt_config.system_prompt, prompt_config.task_prompt]
)
if add_additional_info_if_no_tag and not any_tag_present:
return prompt_str + build_date_time_string()
return prompt_str


Expand Down
10 changes: 5 additions & 5 deletions backend/onyx/seeding/prompts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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.
The current date is [[CURRENT_DATETIME]].
You can process and comprehend vast amounts of text and utilize this knowledge to provide
grounded, accurate, and concise answers to diverse queries.
Expand All @@ -24,7 +24,7 @@ prompts:
If there are no relevant documents, refer to the chat history and your internal 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
# If the [[CURRENT_DATETIME]] 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.
Expand All @@ -51,7 +51,7 @@ prompts:
- name: "OnlyLLM"
description: "Chat directly with the LLM!"
system: >
You are a helpful AI assistant. The current date is DANSWER_DATETIME_REPLACEMENT
You are a helpful AI assistant. The current date is [[CURRENT_DATETIME]]
You give concise responses to very simple questions, but provide more thorough responses to
Expand All @@ -69,7 +69,7 @@ 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.
The current date is [[CURRENT_DATETIME]].
You ARE NOT creative and always stick to the provided documents.
If there are no documents, refer to the conversation history.
Expand All @@ -87,7 +87,7 @@ 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.
The current date is [[CURRENT_DATETIME]].
You only provide quotes that are EXACT substrings from provided documents!
Expand Down
4 changes: 4 additions & 0 deletions backend/tests/integration/common_utils/managers/persona.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def create(
is_public: bool = True,
llm_filter_extraction: bool = True,
recency_bias: RecencyBiasSetting = RecencyBiasSetting.AUTO,
datetime_aware: bool = False,
prompt_ids: list[int] | None = None,
document_set_ids: list[int] | None = None,
tool_ids: list[int] | None = None,
Expand All @@ -46,6 +47,7 @@ def create(
description=description,
system_prompt=system_prompt,
task_prompt=task_prompt,
datetime_aware=datetime_aware,
include_citations=include_citations,
num_chunks=num_chunks,
llm_relevance_filter=llm_relevance_filter,
Expand Down Expand Up @@ -104,6 +106,7 @@ def edit(
is_public: bool | None = None,
llm_filter_extraction: bool | None = None,
recency_bias: RecencyBiasSetting | None = None,
datetime_aware: bool = False,
prompt_ids: list[int] | None = None,
document_set_ids: list[int] | None = None,
tool_ids: list[int] | None = None,
Expand All @@ -121,6 +124,7 @@ def edit(
description=description or persona.description,
system_prompt=system_prompt,
task_prompt=task_prompt,
datetime_aware=datetime_aware,
include_citations=include_citations,
num_chunks=num_chunks or persona.num_chunks,
llm_relevance_filter=llm_relevance_filter or persona.llm_relevance_filter,
Expand Down

0 comments on commit 4e6cce7

Please sign in to comment.