From af4c81ae059d2ac021330d5cd43853bbb4c1fc9d Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Tue, 21 Jan 2025 14:08:49 -0800 Subject: [PATCH 1/5] resolve mystery --- backend/onyx/chat/answer.py | 2 ++ backend/onyx/chat/process_message.py | 8 ++++++++ .../context/search/retrieval/search_runner.py | 3 +++ backend/onyx/db/slack_channel_config.py | 4 ++++ .../slack/handlers/handle_regular_answer.py | 3 +++ backend/onyx/tools/built_in_tools.py | 20 ++++++++++++++----- backend/onyx/tools/tool_constructor.py | 2 ++ 7 files changed, 37 insertions(+), 5 deletions(-) diff --git a/backend/onyx/chat/answer.py b/backend/onyx/chat/answer.py index ff211cbf307..da8a6549d89 100644 --- a/backend/onyx/chat/answer.py +++ b/backend/onyx/chat/answer.py @@ -263,6 +263,8 @@ def processed_streamed_output(self) -> AnswerStream: prompt_builder.update_system_prompt( default_build_system_message(self.prompt_config) ) + print("TOOL LIST") + print(self._get_tools_list()) llm_call = LLMCall( prompt_builder=prompt_builder, tools=self._get_tools_list(), diff --git a/backend/onyx/chat/process_message.py b/backend/onyx/chat/process_message.py index b874ea7ef60..5fd82b0946d 100644 --- a/backend/onyx/chat/process_message.py +++ b/backend/onyx/chat/process_message.py @@ -227,12 +227,15 @@ def _handle_internet_search_tool_response_summary( def _get_force_search_settings( new_msg_req: CreateChatMessageRequest, tools: list[Tool] ) -> ForceUseTool: + print(new_msg_req.__dict__) internet_search_available = any( isinstance(tool, InternetSearchTool) for tool in tools ) search_tool_available = any(isinstance(tool, SearchTool) for tool in tools) if not internet_search_available and not search_tool_available: + print("NO TOOLS AVAILABLE") + # Does not matter much which tool is set here as force is false and neither tool is available return ForceUseTool(force_use=False, tool_name=SearchTool._NAME) @@ -683,11 +686,16 @@ def stream_chat_message_objects( additional_headers=custom_tool_additional_headers, ), ) + print("TOOL DICT") + print(tool_dict) tools: list[Tool] = [] for tool_list in tool_dict.values(): tools.extend(tool_list) + print("SHOULD FORCE SEARCH?") + print(_get_force_search_settings(new_msg_req, tools)) + # LLM prompt building, response capturing, etc. answer = Answer( is_connected=is_connected, diff --git a/backend/onyx/context/search/retrieval/search_runner.py b/backend/onyx/context/search/retrieval/search_runner.py index 64491a20a02..ba089ad9b66 100644 --- a/backend/onyx/context/search/retrieval/search_runner.py +++ b/backend/onyx/context/search/retrieval/search_runner.py @@ -142,6 +142,9 @@ def doc_index_retrieval( num_to_retrieve=query.num_hits, offset=query.offset, ) + print("THE TOP CHUNKS") + print(top_chunks) + print("\n\n\n\n\n") retrieval_requests: list[VespaChunkRequest] = [] normal_chunks: list[InferenceChunkUncleaned] = [] diff --git a/backend/onyx/db/slack_channel_config.py b/backend/onyx/db/slack_channel_config.py index a3b84533e6a..7c093a700a1 100644 --- a/backend/onyx/db/slack_channel_config.py +++ b/backend/onyx/db/slack_channel_config.py @@ -15,6 +15,7 @@ from onyx.db.persona import mark_persona_as_deleted from onyx.db.persona import upsert_persona from onyx.db.prompts import get_default_prompt +from onyx.tools.built_in_tools import get_search_tool from onyx.utils.errors import EERequiredError from onyx.utils.variable_functionality import ( fetch_versioned_implementation_with_fallback, @@ -47,6 +48,8 @@ def create_slack_channel_persona( ) -> Persona: """NOTE: does not commit changes""" + search_tool = get_search_tool(db_session) + # create/update persona associated with the Slack channel persona_name = _build_persona_name(channel_name) default_prompt = get_default_prompt(db_session) @@ -60,6 +63,7 @@ def create_slack_channel_persona( llm_filter_extraction=enable_auto_filters, recency_bias=RecencyBiasSetting.AUTO, prompt_ids=[default_prompt.id], + tool_ids=[search_tool.id], document_set_ids=document_set_ids, llm_model_provider_override=None, llm_model_version_override=None, diff --git a/backend/onyx/onyxbot/slack/handlers/handle_regular_answer.py b/backend/onyx/onyxbot/slack/handlers/handle_regular_answer.py index cad549a7644..67104b2ad4e 100644 --- a/backend/onyx/onyxbot/slack/handlers/handle_regular_answer.py +++ b/backend/onyx/onyxbot/slack/handlers/handle_regular_answer.py @@ -159,6 +159,9 @@ def _get_slack_answer( new_message_request: CreateChatMessageRequest, onyx_user: User | None ) -> ChatOnyxBotResponse: with get_session_with_tenant(tenant_id) as db_session: + print(new_message_request.__dict__) + print(bypass_acl) + packets = stream_chat_message_objects( new_msg_req=new_message_request, user=onyx_user, diff --git a/backend/onyx/tools/built_in_tools.py b/backend/onyx/tools/built_in_tools.py index dd67f5b16f7..9d11fe48e63 100644 --- a/backend/onyx/tools/built_in_tools.py +++ b/backend/onyx/tools/built_in_tools.py @@ -104,13 +104,10 @@ def load_builtin_tools(db_session: Session) -> None: logger.notice("All built-in tools are loaded/verified.") -def auto_add_search_tool_to_personas(db_session: Session) -> None: +def get_search_tool(db_session: Session) -> InCodeToolInfo | None: """ - Automatically adds the SearchTool to all Persona objects in the database that have - `num_chunks` either unset or set to a value that isn't 0. This is done to migrate - Persona objects that were created before the concept of Tools were added. + Retrieves for the SearchTool from the BUILT_IN_TOOLS list. """ - # Fetch the SearchTool from the database based on in_code_tool_id from BUILT_IN_TOOLS search_tool_id = next( ( tool["in_code_tool_id"] @@ -119,6 +116,7 @@ def auto_add_search_tool_to_personas(db_session: Session) -> None: ), None, ) + if not search_tool_id: raise RuntimeError("SearchTool not found in the BUILT_IN_TOOLS list.") @@ -126,6 +124,18 @@ def auto_add_search_tool_to_personas(db_session: Session) -> None: select(ToolDBModel).where(ToolDBModel.in_code_tool_id == search_tool_id) ).scalar_one_or_none() + return search_tool + + +def auto_add_search_tool_to_personas(db_session: Session) -> None: + """ + Automatically adds the SearchTool to all Persona objects in the database that have + `num_chunks` either unset or set to a value that isn't 0. This is done to migrate + Persona objects that were created before the concept of Tools were added. + """ + # Fetch the SearchTool from the database based on in_code_tool_id from BUILT_IN_TOOLS + search_tool = get_search_tool(db_session) + if not search_tool: raise RuntimeError("SearchTool not found in the database.") diff --git a/backend/onyx/tools/tool_constructor.py b/backend/onyx/tools/tool_constructor.py index 4650dd3b0ee..9001f6d43b4 100644 --- a/backend/onyx/tools/tool_constructor.py +++ b/backend/onyx/tools/tool_constructor.py @@ -145,6 +145,7 @@ def construct_tools( ) -> dict[int, list[Tool]]: """Constructs tools based on persona configuration and available APIs""" tool_dict: dict[int, list[Tool]] = {} + print(persona.__dict__) # Get user's OAuth token if available user_oauth_token = None @@ -152,6 +153,7 @@ def construct_tools( user_oauth_token = user.oauth_accounts[0].access_token for db_tool_model in persona.tools: + print(db_tool_model.__dict__) if db_tool_model.in_code_tool_id: tool_cls = get_built_in_tool_by_id( db_tool_model.in_code_tool_id, db_session From 2f50d5a0adc4d044afca9beec839c6123a6dcc75 Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Tue, 21 Jan 2025 14:09:22 -0800 Subject: [PATCH 2/5] remove some logs --- backend/onyx/chat/process_message.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/backend/onyx/chat/process_message.py b/backend/onyx/chat/process_message.py index 5fd82b0946d..b874ea7ef60 100644 --- a/backend/onyx/chat/process_message.py +++ b/backend/onyx/chat/process_message.py @@ -227,15 +227,12 @@ def _handle_internet_search_tool_response_summary( def _get_force_search_settings( new_msg_req: CreateChatMessageRequest, tools: list[Tool] ) -> ForceUseTool: - print(new_msg_req.__dict__) internet_search_available = any( isinstance(tool, InternetSearchTool) for tool in tools ) search_tool_available = any(isinstance(tool, SearchTool) for tool in tools) if not internet_search_available and not search_tool_available: - print("NO TOOLS AVAILABLE") - # Does not matter much which tool is set here as force is false and neither tool is available return ForceUseTool(force_use=False, tool_name=SearchTool._NAME) @@ -686,16 +683,11 @@ def stream_chat_message_objects( additional_headers=custom_tool_additional_headers, ), ) - print("TOOL DICT") - print(tool_dict) tools: list[Tool] = [] for tool_list in tool_dict.values(): tools.extend(tool_list) - print("SHOULD FORCE SEARCH?") - print(_get_force_search_settings(new_msg_req, tools)) - # LLM prompt building, response capturing, etc. answer = Answer( is_connected=is_connected, From c5594604b88279548d7d04aa30405746e1dba830 Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Tue, 21 Jan 2025 14:10:41 -0800 Subject: [PATCH 3/5] remove more logs --- backend/onyx/chat/answer.py | 2 -- backend/onyx/context/search/retrieval/search_runner.py | 3 --- backend/onyx/onyxbot/slack/handlers/handle_regular_answer.py | 3 --- 3 files changed, 8 deletions(-) diff --git a/backend/onyx/chat/answer.py b/backend/onyx/chat/answer.py index da8a6549d89..ff211cbf307 100644 --- a/backend/onyx/chat/answer.py +++ b/backend/onyx/chat/answer.py @@ -263,8 +263,6 @@ def processed_streamed_output(self) -> AnswerStream: prompt_builder.update_system_prompt( default_build_system_message(self.prompt_config) ) - print("TOOL LIST") - print(self._get_tools_list()) llm_call = LLMCall( prompt_builder=prompt_builder, tools=self._get_tools_list(), diff --git a/backend/onyx/context/search/retrieval/search_runner.py b/backend/onyx/context/search/retrieval/search_runner.py index ba089ad9b66..64491a20a02 100644 --- a/backend/onyx/context/search/retrieval/search_runner.py +++ b/backend/onyx/context/search/retrieval/search_runner.py @@ -142,9 +142,6 @@ def doc_index_retrieval( num_to_retrieve=query.num_hits, offset=query.offset, ) - print("THE TOP CHUNKS") - print(top_chunks) - print("\n\n\n\n\n") retrieval_requests: list[VespaChunkRequest] = [] normal_chunks: list[InferenceChunkUncleaned] = [] diff --git a/backend/onyx/onyxbot/slack/handlers/handle_regular_answer.py b/backend/onyx/onyxbot/slack/handlers/handle_regular_answer.py index 67104b2ad4e..cad549a7644 100644 --- a/backend/onyx/onyxbot/slack/handlers/handle_regular_answer.py +++ b/backend/onyx/onyxbot/slack/handlers/handle_regular_answer.py @@ -159,9 +159,6 @@ def _get_slack_answer( new_message_request: CreateChatMessageRequest, onyx_user: User | None ) -> ChatOnyxBotResponse: with get_session_with_tenant(tenant_id) as db_session: - print(new_message_request.__dict__) - print(bypass_acl) - packets = stream_chat_message_objects( new_msg_req=new_message_request, user=onyx_user, From 43d535eaa1a33060552558d2bee44a734c638a16 Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Tue, 21 Jan 2025 14:11:10 -0800 Subject: [PATCH 4/5] remove logs --- backend/onyx/tools/tool_constructor.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/onyx/tools/tool_constructor.py b/backend/onyx/tools/tool_constructor.py index 9001f6d43b4..4650dd3b0ee 100644 --- a/backend/onyx/tools/tool_constructor.py +++ b/backend/onyx/tools/tool_constructor.py @@ -145,7 +145,6 @@ def construct_tools( ) -> dict[int, list[Tool]]: """Constructs tools based on persona configuration and available APIs""" tool_dict: dict[int, list[Tool]] = {} - print(persona.__dict__) # Get user's OAuth token if available user_oauth_token = None @@ -153,7 +152,6 @@ def construct_tools( user_oauth_token = user.oauth_accounts[0].access_token for db_tool_model in persona.tools: - print(db_tool_model.__dict__) if db_tool_model.in_code_tool_id: tool_cls = get_built_in_tool_by_id( db_tool_model.in_code_tool_id, db_session From 7b812a6e0964ccbb2ed1ee3735c9fd074de2c99b Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Tue, 21 Jan 2025 15:13:10 -0800 Subject: [PATCH 5/5] nit --- backend/onyx/db/slack_channel_config.py | 2 ++ backend/onyx/tools/built_in_tools.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/onyx/db/slack_channel_config.py b/backend/onyx/db/slack_channel_config.py index 7c093a700a1..523909f22b9 100644 --- a/backend/onyx/db/slack_channel_config.py +++ b/backend/onyx/db/slack_channel_config.py @@ -49,6 +49,8 @@ def create_slack_channel_persona( """NOTE: does not commit changes""" search_tool = get_search_tool(db_session) + if search_tool is None: + raise ValueError("Search tool not found") # create/update persona associated with the Slack channel persona_name = _build_persona_name(channel_name) diff --git a/backend/onyx/tools/built_in_tools.py b/backend/onyx/tools/built_in_tools.py index 9d11fe48e63..31adab6c418 100644 --- a/backend/onyx/tools/built_in_tools.py +++ b/backend/onyx/tools/built_in_tools.py @@ -104,7 +104,7 @@ def load_builtin_tools(db_session: Session) -> None: logger.notice("All built-in tools are loaded/verified.") -def get_search_tool(db_session: Session) -> InCodeToolInfo | None: +def get_search_tool(db_session: Session) -> ToolDBModel | None: """ Retrieves for the SearchTool from the BUILT_IN_TOOLS list. """