File size: 1,898 Bytes
7fea1f4
2e02639
7fea1f4
 
2e02639
7fea1f4
 
 
 
 
 
 
2e02639
7fea1f4
 
2e02639
7fea1f4
 
2e02639
7fea1f4
2e02639
7fea1f4
 
 
2e02639
7fea1f4
 
2e02639
7fea1f4
 
2e02639
7fea1f4
 
2e02639
7fea1f4
 
 
 
 
 
 
 
 
 
2e02639
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
class LlmAgent:

    def __init__(self, llm):
        self.llm = llm

    def generate_paragraph(self, query: str, context: {}, language='fr') -> str:
        """generates the final answer"""
        template = (f" You are an agent designed to answer to the {query} based on the context delimited by triple backticks:\n"
                 f"``` {context}```\n"
                 f"    The response shall be in {language} and shall be concise and based on the context provided\n"
                 f"    In case the provided context is not relevant to answer to the question, just return that you "
                 f"    don't know the answer ")

        p = self.llm(template)
        return p

    def translate(self, text: str, language="en") -> str:
        """translates"""

        languages = "french to english" if language == "en" else "english to french"

        template = (f"    Your task consists in translating {languages}\\n"
                    f"    the following text delimited by by triple backticks: ```{text}```\n"
                    )

        p = self.llm(template)
        return p

    def generate_answer(self, query: str, answer_en: str, context_fr: str) -> str:
        """provides the final answer in french based on the initial query and the answer in english"""

        def _cut_unfinished_sentence(s: str):
            return '.'.join(p.split('.')[:-1])

        template = (f"Your task consists in providing the answer in french to the query "
                    f"delimited by triple backticks: ```{query}``` given the informations here delimited "
                    f"by triple backticks: ```{context_fr}``` and the answer in english delimited by triple "
                    f"backticks: ```{answer_en}```"
                    )
        print(template)
        p = self.llm(template)
        p = _cut_unfinished_sentence(p)
        print(p)
        return p