searchgpt / src /processor /message_processor.py
hadadrjt's picture
SearchGPT: Allow LLM to access local time.
29dc222
#
# SPDX-FileCopyrightText: Hadad <hadad@linuxmail.org>
# SPDX-License-Identifier: Apache-2.0
#
import traceback
from config import MODEL
from src.core.web_configuration import WebConfiguration
from src.engine.browser_engine import BrowserEngine
from src.tools.tool_manager import construct_tool_definitions
from src.client.openai_client import initialize_client
from .response.setup import setup_response
from .response.generator import generate_response
from .tools.interaction import process_tool_interactions
def process_user_request(user_message, chat_history):
if not isinstance(user_message, str) or not user_message.strip():
yield []
return
output_content = ""
try:
server, client_initialization_error = initialize_client()
if client_initialization_error:
output_content = client_initialization_error
yield output_content
return
search_configuration = WebConfiguration()
search_engine_instance = BrowserEngine(search_configuration)
available_tools = construct_tool_definitions()
conversation_messages = setup_response(
chat_history,
user_message
)
tool_response = ""
tools_done = False
for tool_update in process_tool_interactions(
server=server,
model_name=MODEL,
conversation_messages=conversation_messages,
tool_definitions=available_tools,
search_engine=search_engine_instance
):
if isinstance(tool_update, str):
tool_response = tool_update
yield tool_response
else:
conversation_messages = tool_update[0]
tool_response = tool_update[1]
tools_done = tool_update[2]
if tool_response:
yield tool_response + "\n\n"
final_response_generator = generate_response(
server=server,
model_name=MODEL,
conversation_messages=conversation_messages,
tool_definitions=available_tools,
tools_done=tools_done
)
for final_response in final_response_generator:
if tool_response:
yield tool_response + "\n\n" + final_response
else:
yield final_response
except Exception as processing_error:
output_content += f"\nError: {str(processing_error)}\n"
output_content += traceback.format_exc()
yield output_content