File size: 2,155 Bytes
c7e94cf
b8b0b89
 
a164c8b
b8b0b89
7330cbd
 
 
 
 
 
003c5fb
b2b25fd
b8b0b89
 
 
76d85d9
b2b25fd
b8b0b89
a164c8b
b8b0b89
d920a9f
7330cbd
 
 
a164c8b
2c92edf
b8b0b89
7330cbd
b8b0b89
43cda03
b8b0b89
 
003c5fb
d920a9f
7330cbd
3e43065
7330cbd
3e43065
7330cbd
3e43065
7330cbd
b8b0b89
a164c8b
7330cbd
 
b8b0b89
 
55a5dbd
b8b0b89
 
 
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
import os
from app_agent_config import AgentConfig
from utils.logger import log_response
from model.custom_agent import CustomHfAgent
from model.conversation_chain_singleton import ConversationChainSingleton

def cut_text_after_keyword(text, keyword):
    index = text.find(keyword)
    if index != -1:
        return text[:index].strip()
    return text


class Controller:
    def __init__(self):
        self.agent_config = AgentConfig()
        #self.app_config = AppConfig()

    image = []
    def handle_submission(self, user_message ):        
        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},
        )


        angent_respone = 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(angent_respone))

        return angent_respone

    def handle_submission_chat(self, user_message, angent_respone):
        # os.environ['HUGGINGFACEHUB_API_TOKEN'] = os.environ['HF_token']
        agent_chat_bot = ConversationChainSingleton().get_conversation_chain()
        if angent_respone is not None:
            text = agent_chat_bot.predict(input=user_message + angent_respone)
        else:
            text = agent_chat_bot.predict(input=user_message)

        result = cut_text_after_keyword(text, "Human:")
        print(result)
        return result