Spaces:
Running
Running
File size: 2,561 Bytes
408c946 29dc222 408c946 ceb70c7 408c946 ceb70c7 408c946 ceb70c7 408c946 ceb70c7 408c946 cedce85 408c946 cedce85 408c946 cedce85 408c946 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#
# 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 |