mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-09-27 12:29:41 +02:00
Fix Prompt for Non Function Calling LLMs (#3241)
This commit is contained in:
@@ -233,6 +233,8 @@ class Answer:
|
|||||||
|
|
||||||
# DEBUG: good breakpoint
|
# DEBUG: good breakpoint
|
||||||
stream = self.llm.stream(
|
stream = self.llm.stream(
|
||||||
|
# For tool calling LLMs, we want to insert the task prompt as part of this flow, this is because the LLM
|
||||||
|
# may choose to not call any tools and just generate the answer, in which case the task prompt is needed.
|
||||||
prompt=current_llm_call.prompt_builder.build(),
|
prompt=current_llm_call.prompt_builder.build(),
|
||||||
tools=[tool.tool_definition() for tool in current_llm_call.tools] or None,
|
tools=[tool.tool_definition() for tool in current_llm_call.tools] or None,
|
||||||
tool_choice=(
|
tool_choice=(
|
||||||
|
@@ -58,8 +58,8 @@ class AnswerPromptBuilder:
|
|||||||
user_message: HumanMessage,
|
user_message: HumanMessage,
|
||||||
message_history: list[PreviousMessage],
|
message_history: list[PreviousMessage],
|
||||||
llm_config: LLMConfig,
|
llm_config: LLMConfig,
|
||||||
|
raw_user_text: str,
|
||||||
single_message_history: str | None = None,
|
single_message_history: str | None = None,
|
||||||
raw_user_text: str | None = None,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
self.max_tokens = compute_max_llm_input_tokens(llm_config)
|
self.max_tokens = compute_max_llm_input_tokens(llm_config)
|
||||||
|
|
||||||
@@ -89,11 +89,7 @@ class AnswerPromptBuilder:
|
|||||||
|
|
||||||
self.new_messages_and_token_cnts: list[tuple[BaseMessage, int]] = []
|
self.new_messages_and_token_cnts: list[tuple[BaseMessage, int]] = []
|
||||||
|
|
||||||
self.raw_user_message = (
|
self.raw_user_message = raw_user_text
|
||||||
HumanMessage(content=raw_user_text)
|
|
||||||
if raw_user_text is not None
|
|
||||||
else user_message
|
|
||||||
)
|
|
||||||
|
|
||||||
def update_system_prompt(self, system_message: SystemMessage | None) -> None:
|
def update_system_prompt(self, system_message: SystemMessage | None) -> None:
|
||||||
if not system_message:
|
if not system_message:
|
||||||
|
@@ -62,7 +62,7 @@ class ToolResponseHandler:
|
|||||||
llm_call.force_use_tool.args
|
llm_call.force_use_tool.args
|
||||||
if llm_call.force_use_tool.args is not None
|
if llm_call.force_use_tool.args is not None
|
||||||
else tool.get_args_for_non_tool_calling_llm(
|
else tool.get_args_for_non_tool_calling_llm(
|
||||||
query=llm_call.prompt_builder.get_user_message_content(),
|
query=llm_call.prompt_builder.raw_user_message,
|
||||||
history=llm_call.prompt_builder.raw_message_history,
|
history=llm_call.prompt_builder.raw_message_history,
|
||||||
llm=llm,
|
llm=llm,
|
||||||
force_run=True,
|
force_run=True,
|
||||||
@@ -76,7 +76,7 @@ class ToolResponseHandler:
|
|||||||
else:
|
else:
|
||||||
tool_options = check_which_tools_should_run_for_non_tool_calling_llm(
|
tool_options = check_which_tools_should_run_for_non_tool_calling_llm(
|
||||||
tools=llm_call.tools,
|
tools=llm_call.tools,
|
||||||
query=llm_call.prompt_builder.get_user_message_content(),
|
query=llm_call.prompt_builder.raw_user_message,
|
||||||
history=llm_call.prompt_builder.raw_message_history,
|
history=llm_call.prompt_builder.raw_message_history,
|
||||||
llm=llm,
|
llm=llm,
|
||||||
)
|
)
|
||||||
@@ -95,7 +95,7 @@ class ToolResponseHandler:
|
|||||||
select_single_tool_for_non_tool_calling_llm(
|
select_single_tool_for_non_tool_calling_llm(
|
||||||
tools_and_args=available_tools_and_args,
|
tools_and_args=available_tools_and_args,
|
||||||
history=llm_call.prompt_builder.raw_message_history,
|
history=llm_call.prompt_builder.raw_message_history,
|
||||||
query=llm_call.prompt_builder.get_user_message_content(),
|
query=llm_call.prompt_builder.raw_user_message,
|
||||||
llm=llm,
|
llm=llm,
|
||||||
)
|
)
|
||||||
if available_tools_and_args
|
if available_tools_and_args
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
from typing import cast
|
from typing import cast
|
||||||
|
|
||||||
|
from langchain_core.messages import HumanMessage
|
||||||
|
|
||||||
from danswer.chat.models import LlmDoc
|
from danswer.chat.models import LlmDoc
|
||||||
from danswer.llm.answering.models import AnswerStyleConfig
|
from danswer.llm.answering.models import AnswerStyleConfig
|
||||||
from danswer.llm.answering.models import PromptConfig
|
from danswer.llm.answering.models import PromptConfig
|
||||||
@@ -58,9 +60,11 @@ def build_next_prompt_for_search_like_tool(
|
|||||||
# For Quotes, the system prompt is included in the user prompt
|
# For Quotes, the system prompt is included in the user prompt
|
||||||
prompt_builder.update_system_prompt(None)
|
prompt_builder.update_system_prompt(None)
|
||||||
|
|
||||||
|
human_message = HumanMessage(content=prompt_builder.raw_user_message)
|
||||||
|
|
||||||
prompt_builder.update_user_prompt(
|
prompt_builder.update_user_prompt(
|
||||||
build_quotes_user_message(
|
build_quotes_user_message(
|
||||||
message=prompt_builder.raw_user_message,
|
message=human_message,
|
||||||
context_docs=final_context_documents,
|
context_docs=final_context_documents,
|
||||||
history_str=prompt_builder.single_message_history or "",
|
history_str=prompt_builder.single_message_history or "",
|
||||||
prompt=prompt_config,
|
prompt=prompt_config,
|
||||||
|
Reference in New Issue
Block a user