File size: 5,910 Bytes
24405fe
f90028c
34688e7
24405fe
 
e2e688a
34688e7
 
24405fe
34688e7
 
 
 
 
 
 
24405fe
 
2269229
 
 
24405fe
 
2269229
24405fe
2269229
71f729a
2269229
 
2696e88
 
 
34688e7
5e47346
34688e7
5e47346
f90028c
 
 
5e47346
2696e88
 
 
 
 
34688e7
5e47346
34688e7
5e47346
f90028c
 
 
 
 
 
 
 
 
 
5e47346
0c797c9
 
2269229
 
 
 
34688e7
5e47346
34688e7
5e47346
f90028c
 
 
 
 
 
 
 
 
 
 
 
 
 
5e47346
2696e88
 
 
 
 
34688e7
5e47346
34688e7
5e47346
f90028c
 
 
 
 
 
 
 
 
 
 
 
2696e88
aaab899
24405fe
2269229
1c95026
2269229
 
 
 
 
 
 
 
 
 
 
 
24405fe
2269229
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
import logging
from textwrap import dedent
from agno.models.openai import OpenAILike
from agno.tools.arxiv import ArxivTools
from agno.tools.pubmed import PubmedTools
from agno.agent import Agent
import os
import random

API_KEYS = [
    os.getenv("SK1"),
    os.getenv("SK2"),
    os.getenv("SK3"),
    os.getenv("SK4"),
    os.getenv("SK5")
]
class ModelHandler:
    def __init__(self):
        """Initialize the model handler"""
        self.model = None
        self.tokenizer = None
        self.translator = None
        self.researcher = None
        self.summarizer = None
        self.presenter = None
        self._initialize_model()
    
    def _initialize_model(self):
        """Initialize model and tokenizer"""
        self.translator = Agent(
            name="Translator",
            role="You will translate the query to English",
            model=OpenAILike(
                api_key=str(random.choice(API_KEYS)),
                base_url="https://api.moonshot.cn/v1",
                id="moonshot-v1-8k",
                instructions=dedent("""\
                    Translate the query to English
                """)
            )
        ) 
        
        self.researcher = Agent(
            name="Researcher",
            role="You are a research scholar who specializes in autism research.",
            model=OpenAILike(
                api_key=str(random.choice(API_KEYS)),
                base_url="https://api.moonshot.cn/v1",
                id="moonshot-v1-8k",
                instructions=dedent("""\
                    - You have ArxivTools and PubmedTools at your disposal. Use them to find relevant papers for the question.
                    - You need to understand the context of the question to provide the best answer based on your tools.
                    - Be precise and provide just enough information to be useful.
                    - You must cite the sources used in your answer.
                    - You must create an accessible summary.
                    - The content must be for people without autism knowledge.
                    - Focus in the main findings of the paper taking in consideration the question.
                    - The answer must be brief.
                """),
                show_tool_calls=True
            ),
            tools=[ArxivTools(), PubmedTools()]
        )
        self.summarizer = Agent(
            name="Summarizer",
            role="You are a specialist in summarizing research papers for people without autism knowledge.",
            model=OpenAILike(
                api_key=str(random.choice(API_KEYS)),
                base_url="https://api.moonshot.cn/v1",
                id="moonshot-v1-8k",
                instructions=dedent("""\
                    - You must provide just enough information to be useful
                    - You must cite the sources used in your answer.
                    - You must be clear and concise.
                    - You must create an accessible summary.
                    - The content must be for people without autism knowledge.
                    - Focus in the main findings of the paper taking in consideration the question.
                    - The answer must be brief.
                    - Remove everything related to the run itself like: 'Running: transfer_' just use plain text
                    - You must use the language provided by the user to present the results.
                    - Add references to the sources used in the answer.
                    - Add emojis to make the presentation more interactive.
                    - Translate the answer to Portuguese.
                """),
            )
        )
        
        self.presenter = Agent(
            name="Presenter",
            role="You are a professional researcher who presents the results of the research.",
            model=OpenAILike(
                api_key=str(random.choice(API_KEYS)),
                base_url="https://api.moonshot.cn/v1",
                id="moonshot-v1-8k",
                instructions=dedent("""\
                    - You are multilingual
                    - You must present the results in a clear and concise manner.
                    - Clean up the presentation to make it more readable.
                    - Remove unnecessary information.
                    - Remove everything related to the run itself like: 'Running: transfer_', just use plain text
                    - You must use the language provided by the user to present the results.
                    - Add references to the sources used in the answer.
                    - Add emojis to make the presentation more interactive.
                    - Translate the answer to Portuguese.
                """),
            )
        )
        
    
    def generate_answer(self, query: str) -> str:
        try:
            translator = self.translator.run(query, stream=False)
            logging.info(f"Translated query")
            research = self.researcher.run(translator.content, stream=False)
            logging.info(f"Generated research")
            summary = self.summarizer.run(research.content, stream=False)
            logging.info(f"Generated summary")
            presentation = self.presenter.run(summary.content, stream=False)
            logging.info(f"Generated presentation")
            
            if not presentation.content:
                return self._get_fallback_response()
            return presentation.content
        except Exception as e:
            logging.error(f"Error generating answer: {str(e)}")
            return self._get_fallback_response()
    
    @staticmethod
    def _get_fallback_response() -> str:
        """Provide a friendly, helpful fallback response"""
        return """
            Peço descula, mas encontrei um erro ao gerar a resposta. Tente novamente ou refaça a sua pergunta.
        """