Spaces:
Runtime error
Runtime error
adrien.aribaut-gaudin
commited on
Commit
•
597976f
1
Parent(s):
4de2d8b
deleted 1 call to llama2 but doesnt work 100%
Browse files- src/control/control.py +1 -5
- src/tools/llm.py +13 -12
src/control/control.py
CHANGED
@@ -10,11 +10,7 @@ class Chatbot:
|
|
10 |
|
11 |
def get_response(self, query, histo):
|
12 |
histo_conversation, histo_queries = self._get_histo(histo)
|
13 |
-
langage_of_query = self.llm.
|
14 |
-
if langage_of_query != "en":
|
15 |
-
queries = self.llm.translate(text=histo_queries)
|
16 |
-
else:
|
17 |
-
queries = histo_queries
|
18 |
block_sources = self.retriever.similarity_search(query=queries)
|
19 |
block_sources = self._select_best_sources(block_sources)
|
20 |
sources_contents = [s.content for s in block_sources]
|
|
|
10 |
|
11 |
def get_response(self, query, histo):
|
12 |
histo_conversation, histo_queries = self._get_histo(histo)
|
13 |
+
langage_of_query, queries = self.llm.detect_language_and_translate(histo_queries)
|
|
|
|
|
|
|
|
|
14 |
block_sources = self.retriever.similarity_search(query=queries)
|
15 |
block_sources = self._select_best_sources(block_sources)
|
16 |
sources_contents = [s.content for s in block_sources]
|
src/tools/llm.py
CHANGED
@@ -29,8 +29,8 @@ class LlmAgent:
|
|
29 |
"Your response shall be in {language} and shall be concise"
|
30 |
You should respect the following format: "
|
31 |
<response>"
|
32 |
-
"
|
33 |
-
"
|
34 |
pipe = self.pipe(template)
|
35 |
# print("****************")
|
36 |
# print(template)
|
@@ -39,7 +39,7 @@ class LlmAgent:
|
|
39 |
print(res)
|
40 |
return res
|
41 |
|
42 |
-
def translate(self, text: str
|
43 |
torch.cuda.empty_cache()
|
44 |
"""translates"""
|
45 |
|
@@ -125,22 +125,23 @@ class LlmAgent:
|
|
125 |
print(res)
|
126 |
return res
|
127 |
|
128 |
-
def
|
129 |
torch.cuda.empty_cache()
|
130 |
"""detects the language"""
|
131 |
template = (f'''[INST] <<SYS>>
|
132 |
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
133 |
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
|
134 |
-
Your task consists in detecting the language of the user query"
|
135 |
-
|
136 |
-
|
137 |
-
<code>"
|
138 |
-
|
139 |
-
|
140 |
)
|
141 |
pipe = self.pipe(template)
|
142 |
# print("****************")
|
143 |
# print(template)
|
144 |
# print("----")
|
145 |
-
|
146 |
-
|
|
|
|
29 |
"Your response shall be in {language} and shall be concise"
|
30 |
You should respect the following format: "
|
31 |
<response>"
|
32 |
+
"<</SYS>>"
|
33 |
+
"{query}[/INST]''')
|
34 |
pipe = self.pipe(template)
|
35 |
# print("****************")
|
36 |
# print(template)
|
|
|
39 |
print(res)
|
40 |
return res
|
41 |
|
42 |
+
def translate(self, text: str) -> str:
|
43 |
torch.cuda.empty_cache()
|
44 |
"""translates"""
|
45 |
|
|
|
125 |
print(res)
|
126 |
return res
|
127 |
|
128 |
+
def detect_language_and_translate(self, text: str) -> str:
|
129 |
torch.cuda.empty_cache()
|
130 |
"""detects the language"""
|
131 |
template = (f'''[INST] <<SYS>>
|
132 |
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
133 |
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
|
134 |
+
Your task consists in detecting the language of the text, giving the two letters of the language detected <code>, and if the user query is not English, translate it in English and return the original text translated : <text>"
|
135 |
+
If the text is already in English, you don't need to translate it and just return the original text after the two letters of the language detected. <text>"
|
136 |
+
Your response should respect the following format: "
|
137 |
+
The language detected is: <code> : <text>"
|
138 |
+
<</SYS>>"
|
139 |
+
{text}[/INST]'''
|
140 |
)
|
141 |
pipe = self.pipe(template)
|
142 |
# print("****************")
|
143 |
# print(template)
|
144 |
# print("----")
|
145 |
+
pipe_res = pipe[0]["generated_text"]
|
146 |
+
res = pipe_res.split(":")
|
147 |
+
return str(res[1]).strip(), str(res[2]).strip()
|