File size: 2,923 Bytes
9ff01ff
 
 
 
 
 
 
 
 
 
 
 
7352d7a
9ff01ff
 
6aa774c
9ff01ff
 
7352d7a
 
 
 
9ff01ff
 
 
 
 
 
 
7352d7a
9ff01ff
 
 
 
 
 
7352d7a
 
9ff01ff
 
 
 
7352d7a
9ff01ff
d24ee64
 
 
9ff01ff
 
d24ee64
9ff01ff
 
 
 
 
d24ee64
 
 
 
 
7352d7a
d24ee64
 
 
9ff01ff
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
class LlmAgent:

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

    def generate_paragraph(self, query: str, context: {}, histo: [(str, str)], language='fr') -> str:
        """generates the  answer"""
        template = (f"You are a conversation bot designed to answer to the query from users delimited by "
                    f"triple backticks: "
                    f"\\n ``` {query} ```\\n"
                    f"Your answer is based on the context delimited by triple backticks: "
                    f"\\n ``` {context} ```\\n"
                    f" You are consistent and avoid redundancies with the rest of the initial conversation "
                    f"delimited by triple backticks: "
                    f"\\n ``` {histo} ```\\n"
                    f"Your response shall be in {language} and shall be concise")

        p = self.llm(template)
        # print("****************")
        # print(template)
        # print("----")
        # print(p)
        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 in English\\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: str, histo: str, context: str,language : 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(s.split('.')[:-1])

        template = (f"Your task consists in translating the answer in {language}, if its not already the case, to the query "
                    f"delimited by triple backticks: ```{query}``` \\n"
                    f"You are given the answer in {language} delimited by triple backticks: ```{answer}```"
                    f"\\n You don't add new content to the answer but: "
                    f"\\n 1 You can use some vocabulary from the context delimited by triple backticks: "
                    f"```{context}```"
                    f"\\n 2 You are consistent and avoid redundancies with the rest of the initial"
                    f" conversation delimited by triple backticks: ```{histo}```"
                    )

        p = self.llm(template)
        # p = _cut_unfinished_sentence(p)
        return p
    
    def detect_language(self, text: str) -> str:
        """detects the language"""
        template = (f"Your task consists in detecting the language of the following text delimited by triple backticks: "
                    f"```{text}```"
                    f" Your answer shall be the two letters code of the language"
                    )
        p = self.llm(template)
        return p