|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from langgraph.types import StreamWriter |
| 5 | + |
| 6 | +from onyx.agents.agent_search.basic.utils import process_llm_stream |
| 7 | +from onyx.chat.models import PromptConfig |
| 8 | +from onyx.chat.prompt_builder.answer_prompt_builder import AnswerPromptBuilder |
| 9 | +from onyx.chat.prompt_builder.answer_prompt_builder import default_build_system_message |
| 10 | +from onyx.chat.prompt_builder.answer_prompt_builder import default_build_user_message |
| 11 | +from onyx.configs.app_configs import POSTGRES_API_SERVER_POOL_OVERFLOW |
| 12 | +from onyx.configs.app_configs import POSTGRES_API_SERVER_POOL_SIZE |
| 13 | +from onyx.configs.constants import DEFAULT_PERSONA_ID |
| 14 | +from onyx.db.engine import get_session_with_current_tenant |
| 15 | +from onyx.db.engine import SqlEngine |
| 16 | +from onyx.db.persona import get_persona_by_id |
| 17 | +from onyx.llm.factory import get_llms_for_persona |
| 18 | +from onyx.llm.interfaces import LLM |
| 19 | +from onyx.tools.tool_implementations.search.search_tool import SearchTool |
| 20 | +from onyx.tools.utils import explicit_tool_calling_supported |
| 21 | +from onyx.utils.logger import setup_logger |
| 22 | + |
| 23 | +logger = setup_logger() |
| 24 | + |
| 25 | + |
| 26 | +def _load_queries() -> list[str]: |
| 27 | + current_dir = Path(__file__).parent |
| 28 | + with open(current_dir / "search_queries.json", "r") as file: |
| 29 | + return json.load(file) |
| 30 | + |
| 31 | + |
| 32 | +def _modify_one_query( |
| 33 | + query: str, |
| 34 | + llm: LLM, |
| 35 | + prompt_config: PromptConfig, |
| 36 | + tool_definition: dict, |
| 37 | + writer: StreamWriter = lambda _: None, |
| 38 | +) -> str: |
| 39 | + prompt_builder = AnswerPromptBuilder( |
| 40 | + user_message=default_build_user_message( |
| 41 | + user_query=query, |
| 42 | + prompt_config=prompt_config, |
| 43 | + files=[], |
| 44 | + single_message_history=None, |
| 45 | + ), |
| 46 | + system_message=default_build_system_message(prompt_config, llm.config), |
| 47 | + message_history=[], |
| 48 | + llm_config=llm.config, |
| 49 | + raw_user_query=query, |
| 50 | + raw_user_uploaded_files=[], |
| 51 | + single_message_history=None, |
| 52 | + ) |
| 53 | + prompt = prompt_builder.build() |
| 54 | + |
| 55 | + stream = llm.stream( |
| 56 | + prompt=prompt, |
| 57 | + tools=[tool_definition], |
| 58 | + tool_choice="required", |
| 59 | + structured_response_format=None, |
| 60 | + ) |
| 61 | + tool_message = process_llm_stream( |
| 62 | + messages=stream, |
| 63 | + should_stream_answer=False, |
| 64 | + writer=writer, |
| 65 | + ) |
| 66 | + return ( |
| 67 | + tool_message.tool_calls[0]["args"]["query"] |
| 68 | + if tool_message.tool_calls |
| 69 | + else query |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +class SearchToolOverride(SearchTool): |
| 74 | + def __init__(self) -> None: |
| 75 | + # do nothing, the tool_definition function doesn't require variables to be initialized |
| 76 | + pass |
| 77 | + |
| 78 | + |
| 79 | +def generate_search_queries() -> None: |
| 80 | + SqlEngine.init_engine( |
| 81 | + pool_size=POSTGRES_API_SERVER_POOL_SIZE, |
| 82 | + max_overflow=POSTGRES_API_SERVER_POOL_OVERFLOW, |
| 83 | + ) |
| 84 | + |
| 85 | + queries = _load_queries() |
| 86 | + |
| 87 | + with get_session_with_current_tenant() as db_session: |
| 88 | + persona = get_persona_by_id(DEFAULT_PERSONA_ID, None, db_session) |
| 89 | + llm, _ = get_llms_for_persona(persona) |
| 90 | + prompt_config = PromptConfig.from_model(persona.prompts[0]) |
| 91 | + tool_definition = SearchToolOverride().tool_definition() |
| 92 | + |
| 93 | + tool_call_supported = explicit_tool_calling_supported( |
| 94 | + llm.config.model_provider, llm.config.model_name |
| 95 | + ) |
| 96 | + |
| 97 | + if tool_call_supported: |
| 98 | + logger.info( |
| 99 | + "Tool calling is supported for the current model. Modifying queries." |
| 100 | + ) |
| 101 | + modified_queries = [ |
| 102 | + _modify_one_query( |
| 103 | + query=query, |
| 104 | + llm=llm, |
| 105 | + prompt_config=prompt_config, |
| 106 | + tool_definition=tool_definition, |
| 107 | + ) |
| 108 | + for query in queries |
| 109 | + ] |
| 110 | + else: |
| 111 | + logger.warning( |
| 112 | + "Tool calling is not supported for the current model. " |
| 113 | + "Using the original queries." |
| 114 | + ) |
| 115 | + modified_queries = queries |
| 116 | + |
| 117 | + with open("search_queries_modified.json", "w") as file: |
| 118 | + json.dump(modified_queries, file, indent=4) |
| 119 | + |
| 120 | + logger.info("Exported modified queries to search_queries_modified.json") |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + generate_search_queries() |
0 commit comments