From cc355f93fceae5bdc465ea8e16769543f31b1beb Mon Sep 17 00:00:00 2001 From: Debanjum Date: Mon, 19 May 2025 09:21:42 -0700 Subject: [PATCH] Use operator context consistently as a dict[str, str] of query, result --- .../processor/conversation/anthropic/anthropic_chat.py | 2 +- src/khoj/processor/conversation/google/gemini_chat.py | 2 +- src/khoj/processor/conversation/openai/gpt.py | 2 +- src/khoj/processor/conversation/utils.py | 4 ++-- src/khoj/processor/operator/operate_browser.py | 3 ++- src/khoj/routers/api_chat.py | 7 +++---- src/khoj/routers/helpers.py | 4 ++-- src/khoj/routers/research.py | 6 +++--- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/khoj/processor/conversation/anthropic/anthropic_chat.py b/src/khoj/processor/conversation/anthropic/anthropic_chat.py index b287b754..014fe4cc 100644 --- a/src/khoj/processor/conversation/anthropic/anthropic_chat.py +++ b/src/khoj/processor/conversation/anthropic/anthropic_chat.py @@ -144,7 +144,7 @@ async def converse_anthropic( user_query, online_results: Optional[Dict[str, Dict]] = None, code_results: Optional[Dict[str, Dict]] = None, - operator_results: Optional[List[str]] = None, + operator_results: Optional[Dict[str, str]] = None, conversation_log={}, model: Optional[str] = "claude-3-7-sonnet-latest", api_key: Optional[str] = None, diff --git a/src/khoj/processor/conversation/google/gemini_chat.py b/src/khoj/processor/conversation/google/gemini_chat.py index 54e9297e..3b1cb1c3 100644 --- a/src/khoj/processor/conversation/google/gemini_chat.py +++ b/src/khoj/processor/conversation/google/gemini_chat.py @@ -166,7 +166,7 @@ async def converse_gemini( user_query, online_results: Optional[Dict[str, Dict]] = None, code_results: Optional[Dict[str, Dict]] = None, - operator_results: Optional[List[str]] = None, + operator_results: Optional[Dict[str, str]] = None, conversation_log={}, model: Optional[str] = "gemini-2.0-flash", api_key: Optional[str] = None, diff --git a/src/khoj/processor/conversation/openai/gpt.py b/src/khoj/processor/conversation/openai/gpt.py index 05c84f01..bf5529b3 100644 --- a/src/khoj/processor/conversation/openai/gpt.py +++ b/src/khoj/processor/conversation/openai/gpt.py @@ -169,7 +169,7 @@ async def converse_openai( user_query, online_results: Optional[Dict[str, Dict]] = None, code_results: Optional[Dict[str, Dict]] = None, - operator_results: Optional[List[str]] = None, + operator_results: Optional[Dict[str, str]] = None, conversation_log={}, model: str = "gpt-4o-mini", api_key: Optional[str] = None, diff --git a/src/khoj/processor/conversation/utils.py b/src/khoj/processor/conversation/utils.py index 3b0cd348..5aef793c 100644 --- a/src/khoj/processor/conversation/utils.py +++ b/src/khoj/processor/conversation/utils.py @@ -91,7 +91,7 @@ class InformationCollectionIteration: context: list = None, onlineContext: dict = None, codeContext: dict = None, - operatorContext: str = None, + operatorContext: dict[str, str] = None, summarizedResult: str = None, warning: str = None, ): @@ -267,7 +267,7 @@ async def save_to_conversation_log( compiled_references: List[Dict[str, Any]] = [], online_results: Dict[str, Any] = {}, code_results: Dict[str, Any] = {}, - operator_results: Dict[str, Any] = {}, + operator_results: Dict[str, str] = {}, inferred_queries: List[str] = [], intent_type: str = "remember", client_application: ClientApplication = None, diff --git a/src/khoj/processor/operator/operate_browser.py b/src/khoj/processor/operator/operate_browser.py index a3944fb6..b3f9fef4 100644 --- a/src/khoj/processor/operator/operate_browser.py +++ b/src/khoj/processor/operator/operate_browser.py @@ -145,6 +145,7 @@ async def operate_browser( operator_agent.reset() yield { - "text": user_input_message or response, + "query": query, + "result": user_input_message or response, "webpages": [{"link": url, "snippet": ""} for url in environment.visited_urls], } diff --git a/src/khoj/routers/api_chat.py b/src/khoj/routers/api_chat.py index 7123998e..8f4d9f31 100644 --- a/src/khoj/routers/api_chat.py +++ b/src/khoj/routers/api_chat.py @@ -886,7 +886,7 @@ async def chat( researched_results = "" online_results: Dict = dict() code_results: Dict = dict() - operator_results: List[str] = [] + operator_results: Dict[str, str] = {} generated_asset_results: Dict = dict() ## Extract Document References compiled_references: List[Any] = [] @@ -962,8 +962,7 @@ async def chat( if research_result.context: compiled_references.extend(research_result.context) if research_result.operatorContext: - operator_results.append(research_result.operatorContext) - + operator_results.update(research_result.operatorContext) researched_results += research_result.summarizedResult else: @@ -1237,7 +1236,7 @@ async def chat( if isinstance(result, dict) and ChatEvent.STATUS in result: yield result[ChatEvent.STATUS] else: - operator_results.append(result["text"]) + operator_results = {result["query"]: result["result"]} # Add webpages visited while operating browser to references if result.get("webpages"): if not online_results.get(defiltered_query): diff --git a/src/khoj/routers/helpers.py b/src/khoj/routers/helpers.py index 25dac496..c33c3db0 100644 --- a/src/khoj/routers/helpers.py +++ b/src/khoj/routers/helpers.py @@ -1354,7 +1354,7 @@ async def agenerate_chat_response( compiled_references: List[Dict] = [], online_results: Dict[str, Dict] = {}, code_results: Dict[str, Dict] = {}, - operator_results: List[str] = [], + operator_results: Dict[str, str] = {}, inferred_queries: List[str] = [], conversation_commands: List[ConversationCommand] = [ConversationCommand.Default], user: KhojUser = None, @@ -1411,7 +1411,7 @@ async def agenerate_chat_response( compiled_references = [] online_results = {} code_results = {} - operator_results = [] + operator_results = {} deepthought = True chat_model = await ConversationAdapters.aget_valid_chat_model(user, conversation, is_subscribed) diff --git a/src/khoj/routers/research.py b/src/khoj/routers/research.py index 7d8293ff..2f8157b4 100644 --- a/src/khoj/routers/research.py +++ b/src/khoj/routers/research.py @@ -237,7 +237,7 @@ async def execute_information_collection( online_results: Dict = dict() code_results: Dict = dict() document_results: List[Dict[str, str]] = [] - operator_results: str = "" + operator_results: Dict[str, str] = {} summarize_files: str = "" this_iteration = InformationCollectionIteration(tool=None, query=query) @@ -421,7 +421,7 @@ async def execute_information_collection( if isinstance(result, dict) and ChatEvent.STATUS in result: yield result[ChatEvent.STATUS] else: - operator_results = result["text"] # type: ignore + operator_results = {result["query"]: result["result"]} this_iteration.operatorContext = operator_results # Add webpages visited while operating browser to references if result.get("webpages"): @@ -478,7 +478,7 @@ async def execute_information_collection( if code_results: results_data += f"\n\n{yaml.dump(truncate_code_context(code_results), allow_unicode=True, sort_keys=False, default_flow_style=False)}\n" if operator_results: - results_data += f"\n\n{operator_results}\n" + results_data += f"\n\n{next(iter(operator_results.values()))}\n" if summarize_files: results_data += f"\n\n{yaml.dump(summarize_files, allow_unicode=True, sort_keys=False, default_flow_style=False)}\n" if this_iteration.warning: