File size: 3,877 Bytes
273c375 c7e94cf 273c375 7330cbd 273c375 7330cbd 003c5fb b8b0b89 273c375 b8b0b89 273c375 b2b25fd 273c375 b8b0b89 d920a9f 7330cbd 273c375 2c92edf 273c375 b8b0b89 7330cbd b8b0b89 43cda03 b8b0b89 003c5fb 273c375 d920a9f 273c375 3e43065 273c375 3e43065 273c375 3e43065 273c375 a164c8b 273c375 b8b0b89 55a5dbd b8b0b89 4fe10de |
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
"""
Module: controller
This module provides a Controller class for handling user submissions and managing conversations.
Dependencies:
- app_agent_config: Module providing the AgentConfig class for configuring agents.
- utils.logger: Module providing logging functionalities.
- model.custom_agent: Module providing the CustomHfAgent class for interacting with Hugging Face models.
- model.conversation_chain_singleton: Module providing the ConversationChainSingleton class for managing conversation chains.
Classes:
- Controller: A class for handling user submissions and managing conversations.
"""
import os
from app_agent_config import AgentConfig # Importing AgentConfig class from app_agent_config module
from utils.logger import log_response # Importing log_response function from utils.logger module
from model.custom_agent import CustomHfAgent # Importing CustomHfAgent class from model.custom_agent module
from model.conversation_chain_singleton import ConversationChainSingleton # Importing ConversationChainSingleton class from model.conversation_chain_singleton module
def cut_text_after_keyword(text, keyword):
"""
Cuts text after the occurrence of a keyword.
Args:
- text (str): The text to be processed.
- keyword (str): The keyword to search for in the text.
Returns:
- str: The processed text.
"""
index = text.find(keyword)
if index != -1:
return text[:index].strip()
return text
class Controller:
"""
Controller class for handling user submissions and managing conversations.
"""
def __init__(self):
self.agent_config = AgentConfig() # Initialize AgentConfig instance
image = [] # Class attribute for storing image data
def handle_submission(self, user_message):
"""
Handles user submission and interaction with the Hugging Face model.
Args:
- user_message (str): The message submitted by the user.
Returns:
- str: The response from the Hugging Face model.
"""
log_response("User input \n {}".format(user_message))
log_response("selected_tools \n {}".format(self.agent_config.s_tool_checkboxes))
log_response("url_endpoint \n {}".format(self.agent_config.url_endpoint))
log_response("document \n {}".format(self.agent_config.document))
log_response("image \n {}".format(self.agent_config.image))
log_response("context \n {}".format(self.agent_config.context))
selected_tools = [self.agent_config.tool_loader.tools[idx] for idx, checkbox in enumerate(self.agent_config.s_tool_checkboxes) if checkbox]
agent = CustomHfAgent(
url_endpoint=self.agent_config.url_endpoint,
token=os.environ['HF_token'],
additional_tools=selected_tools,
input_params={"max_new_tokens": 192},
)
agent_response = agent.chat(user_message, document=self.agent_config.document, image=self.agent_config.image, context=self.agent_config.context)
log_response("Agent Response\n {}".format(agent_response))
return agent_response
def handle_submission_chat(self, user_message, agent_response):
"""
Handles user messages and responses in a conversation chain.
Args:
- user_message (str): The message submitted by the user.
- agent_response (str): The response from the agent.
Returns:
- str: The response from the conversation chain.
"""
agent_chat_bot = ConversationChainSingleton().get_conversation_chain()
if agent_response is not None:
text = agent_chat_bot.predict(input=user_message + agent_response)
else:
text = agent_chat_bot.predict(input=user_message)
result = cut_text_after_keyword(text, "Human:")
print(result)
return result
|