File size: 5,165 Bytes
273c375
 
 
 
 
 
 
 
 
 
 
 
 
 
c7e94cf
273c375
 
 
 
7330cbd
 
273c375
 
 
 
 
 
 
 
 
 
7330cbd
 
 
 
003c5fb
636abac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
 
c7588c6
22a3f30
dfec423
22a3f30
dfec423
273c375
a8dafaa
 
b8b0b89
a8dafaa
 
 
 
81db6d0
 
dfec423
636abac
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
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

def get_text_after_last_occurrence(text, delimiter):
    """
    Retrieves the text after the last occurrence of the specified delimiter.

    Args:
    - text (str): The input text.
    - delimiter (str): The delimiter to search for.

    Returns:
    - str: The text after the last occurrence of the delimiter, or an empty string if the delimiter is not found.
    """
    last_index = text.rfind(delimiter)
    if last_index != -1:
        return text[last_index + len(delimiter):].strip()
    return ""


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().conversation_chain("tmp")
        print(agent_chat_bot)
        print("------------ msg -----------------------")
        print(user_message + " ---- " )
        print("------------ /msg -----------------------")
        if agent_response is not None:
            msg = "[INST] You are a friendly chatbot who always responds to the user input in the style of a pirate. USER_INPUT:  "+user_message+" HINT: In a previous step the following was generated. use this to answer the user. AGENT_RESPONSE: "+ agent_response+" [/INST]"
            text = agent_chat_bot.predict(input=msg)
        else:
            msg = "[INST] You are a friendly chatbot who always responds to the user input in the style of a pirate. USER_INPUT:  "+user_message+"[/INST]"
            text = agent_chat_bot.predict(input=msg)
        print("----- msg----")
        print(msg)
        print("------------ text -----------------------")
        print(text)
        print("------------ /result -----------------------")
        result = get_text_after_last_occurrence(text, "AI: ")
        print(result)
        return result