updated version with chatbot
Browse files- data/~$siness_trips_content_until_end_en.docx +0 -0
- src/control/control.py +11 -3
- src/model/container.py +1 -1
- src/tools/llm.py +12 -9
- src/view/view.py +37 -33
data/~$siness_trips_content_until_end_en.docx
DELETED
|
Binary file (162 Bytes)
|
|
|
src/control/control.py
CHANGED
|
@@ -14,7 +14,7 @@ class Controller:
|
|
| 14 |
self.specials = specials
|
| 15 |
self.llm = llm
|
| 16 |
|
| 17 |
-
def get_response(self, query_fr: str) -> (str, [Block]):
|
| 18 |
query = self.llm.translate(text=query_fr) if self.plan_language == 'en' else query_fr
|
| 19 |
block_sources = self.retriever.similarity_search(query=query)
|
| 20 |
block_sources = self._select_best_sources(block_sources)
|
|
@@ -22,7 +22,8 @@ class Controller:
|
|
| 22 |
self._expand_block_with_specials(block, query_fr)
|
| 23 |
sources_contents = [s.content for s in block_sources]
|
| 24 |
context = '\n'.join(sources_contents)
|
| 25 |
-
|
|
|
|
| 26 |
sources_contents_fr = [s.content_fr for s in block_sources[:2]]
|
| 27 |
context_fr = '\n'.join(sources_contents_fr)
|
| 28 |
if self.content_language == 'en':
|
|
@@ -31,7 +32,14 @@ class Controller:
|
|
| 31 |
return answer, block_sources
|
| 32 |
|
| 33 |
@staticmethod
|
| 34 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
"""
|
| 36 |
Select the best sources: not far from the very best, not far from the last selected, and not too bad per se
|
| 37 |
"""
|
|
|
|
| 14 |
self.specials = specials
|
| 15 |
self.llm = llm
|
| 16 |
|
| 17 |
+
def get_response(self, query_fr: str, histo_fr: [(str, str)]) -> (str, [Block]):
|
| 18 |
query = self.llm.translate(text=query_fr) if self.plan_language == 'en' else query_fr
|
| 19 |
block_sources = self.retriever.similarity_search(query=query)
|
| 20 |
block_sources = self._select_best_sources(block_sources)
|
|
|
|
| 22 |
self._expand_block_with_specials(block, query_fr)
|
| 23 |
sources_contents = [s.content for s in block_sources]
|
| 24 |
context = '\n'.join(sources_contents)
|
| 25 |
+
histo = self._get_histo_str(histo_fr)
|
| 26 |
+
answer = self.llm.generate_paragraph(query=query, histo=histo, context=context, language=self.content_language)
|
| 27 |
sources_contents_fr = [s.content_fr for s in block_sources[:2]]
|
| 28 |
context_fr = '\n'.join(sources_contents_fr)
|
| 29 |
if self.content_language == 'en':
|
|
|
|
| 32 |
return answer, block_sources
|
| 33 |
|
| 34 |
@staticmethod
|
| 35 |
+
def _get_histo_str(histo: [(str, str)]) -> str:
|
| 36 |
+
histo_str = ""
|
| 37 |
+
for (query, answer) in histo[1:5]:
|
| 38 |
+
histo_str += f'user: {query} \n botagent: {answer}\n'
|
| 39 |
+
return histo_str
|
| 40 |
+
|
| 41 |
+
@staticmethod
|
| 42 |
+
def _select_best_sources(sources: [Block], delta_1_2=0.15, delta_1_n=0.3, absolute=1.2, alpha=0.9) -> [Block]:
|
| 43 |
"""
|
| 44 |
Select the best sources: not far from the very best, not far from the last selected, and not too bad per se
|
| 45 |
"""
|
src/model/container.py
CHANGED
|
@@ -58,7 +58,7 @@ class Container:
|
|
| 58 |
special_action = p.text.lstrip('##### ')
|
| 59 |
block.specials.append(special_action)
|
| 60 |
else:
|
| 61 |
-
block.content += p.text
|
| 62 |
blocks = [block] if block.content or block.specials else []
|
| 63 |
for child in self.children:
|
| 64 |
blocks += child.blocks
|
|
|
|
| 58 |
special_action = p.text.lstrip('##### ')
|
| 59 |
block.specials.append(special_action)
|
| 60 |
else:
|
| 61 |
+
block.content += p.text
|
| 62 |
blocks = [block] if block.content or block.specials else []
|
| 63 |
for child in self.children:
|
| 64 |
blocks += child.blocks
|
src/tools/llm.py
CHANGED
|
@@ -3,17 +3,19 @@ class LlmAgent:
|
|
| 3 |
def __init__(self, llm):
|
| 4 |
self.llm = llm
|
| 5 |
|
| 6 |
-
def generate_paragraph(self, query: str, context: {}, language='fr') -> str:
|
| 7 |
-
"""generates the
|
| 8 |
-
template = (f"
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
p = self.llm(template)
|
| 15 |
return p
|
| 16 |
|
|
|
|
| 17 |
def translate(self, text: str, language="en") -> str:
|
| 18 |
"""translates"""
|
| 19 |
|
|
@@ -37,9 +39,10 @@ class LlmAgent:
|
|
| 37 |
f"by triple backticks: ```{context_fr}``` and the answer in english delimited by triple "
|
| 38 |
f"backticks: ```{answer_en}```"
|
| 39 |
)
|
| 40 |
-
|
| 41 |
p = self.llm(template)
|
| 42 |
p = _cut_unfinished_sentence(p)
|
| 43 |
-
print(p)
|
| 44 |
return p
|
| 45 |
|
|
|
|
|
|
|
|
|
| 3 |
def __init__(self, llm):
|
| 4 |
self.llm = llm
|
| 5 |
|
| 6 |
+
def generate_paragraph(self, query: str, context: {}, histo: [(str, str)], language='fr') -> str:
|
| 7 |
+
"""generates the answer"""
|
| 8 |
+
template = (f"You are a botagent designed to answer to the {query} from users based on the context "
|
| 9 |
+
f"delimited by triple backticks: ``` {context}``` and on the previous part of the conversation in"
|
| 10 |
+
f"french and delimited by triple backticks ``` {histo} ```"
|
| 11 |
+
f" The response shall be in {language} and shall be concise and based on the context provided. "
|
| 12 |
+
f"In case the provided context is not relevant to answer to the question, just return that you "
|
| 13 |
+
f"don't know the answer ")
|
| 14 |
|
| 15 |
p = self.llm(template)
|
| 16 |
return p
|
| 17 |
|
| 18 |
+
|
| 19 |
def translate(self, text: str, language="en") -> str:
|
| 20 |
"""translates"""
|
| 21 |
|
|
|
|
| 39 |
f"by triple backticks: ```{context_fr}``` and the answer in english delimited by triple "
|
| 40 |
f"backticks: ```{answer_en}```"
|
| 41 |
)
|
| 42 |
+
|
| 43 |
p = self.llm(template)
|
| 44 |
p = _cut_unfinished_sentence(p)
|
|
|
|
| 45 |
return p
|
| 46 |
|
| 47 |
+
|
| 48 |
+
|
src/view/view.py
CHANGED
|
@@ -12,6 +12,10 @@ def run(ctrl: Controller, config: {}):
|
|
| 12 |
|
| 13 |
gr.Markdown(config['title'])
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
input_text_comp = gr.Textbox(
|
| 16 |
label="",
|
| 17 |
lines=1,
|
|
@@ -19,22 +23,14 @@ def run(ctrl: Controller, config: {}):
|
|
| 19 |
interactive=True,
|
| 20 |
placeholder="Posez votre question ici",
|
| 21 |
)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
label="La réponse automatique",
|
| 28 |
-
lines=12,
|
| 29 |
-
max_lines=12,
|
| 30 |
-
interactive=False,
|
| 31 |
-
visible=False,
|
| 32 |
-
)
|
| 33 |
-
sources_title_comp = gr.CheckboxGroup(
|
| 34 |
-
label="Documents sources",
|
| 35 |
-
visible=False,
|
| 36 |
-
interactive=False,
|
| 37 |
)
|
|
|
|
|
|
|
| 38 |
source_text_comp = []
|
| 39 |
for i in range(4):
|
| 40 |
source_text_comp.append(gr.Textbox(
|
|
@@ -47,42 +43,50 @@ def run(ctrl: Controller, config: {}):
|
|
| 47 |
with gr.Column():
|
| 48 |
pass
|
| 49 |
|
| 50 |
-
def input_text_fn1():
|
|
|
|
| 51 |
update_ = {
|
| 52 |
-
|
| 53 |
}
|
|
|
|
|
|
|
| 54 |
return update_
|
| 55 |
|
| 56 |
-
def input_text_fn2(input_text_):
|
| 57 |
-
answer, sources = ctrl.get_response(query_fr=input_text_)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
update_ = {
|
| 61 |
-
|
| 62 |
-
|
| 63 |
}
|
| 64 |
-
for i in range(min(len(sources),
|
| 65 |
s = sources[i]
|
| 66 |
source_label = f'{s.index} {s.title_fr} score = {s.distance_str}'
|
| 67 |
source_text = s.content_fr
|
| 68 |
update_[source_text_comp[i]] = gr.update(visible=True, value=source_text, label=source_label)
|
| 69 |
return update_
|
| 70 |
|
| 71 |
-
def
|
| 72 |
update_ = {
|
| 73 |
-
input_text_comp: gr.update(value=
|
| 74 |
-
|
| 75 |
}
|
|
|
|
|
|
|
| 76 |
return update_
|
| 77 |
|
| 78 |
input_text_comp \
|
| 79 |
-
.submit(input_text_fn1,
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
.then(input_text_fn2,
|
| 84 |
-
inputs=[input_text_comp],
|
| 85 |
-
outputs=[
|
| 86 |
source_text_comp[0], source_text_comp[1], source_text_comp[2], source_text_comp[3]])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
return qna
|
|
|
|
| 12 |
|
| 13 |
gr.Markdown(config['title'])
|
| 14 |
|
| 15 |
+
histo_text_comp = gr.Chatbot(
|
| 16 |
+
visible=False,
|
| 17 |
+
value=[],
|
| 18 |
+
)
|
| 19 |
input_text_comp = gr.Textbox(
|
| 20 |
label="",
|
| 21 |
lines=1,
|
|
|
|
| 23 |
interactive=True,
|
| 24 |
placeholder="Posez votre question ici",
|
| 25 |
)
|
| 26 |
+
gr.Examples(
|
| 27 |
+
list(config['examples'].values()),
|
| 28 |
+
input_text_comp,
|
| 29 |
+
None,
|
| 30 |
+
lambda: None,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
+
|
| 33 |
+
clear_btn = gr.Button("Clear")
|
| 34 |
source_text_comp = []
|
| 35 |
for i in range(4):
|
| 36 |
source_text_comp.append(gr.Textbox(
|
|
|
|
| 43 |
with gr.Column():
|
| 44 |
pass
|
| 45 |
|
| 46 |
+
def input_text_fn1(input_text_, histo_text_):
|
| 47 |
+
histo_text_.append((input_text_, None))
|
| 48 |
update_ = {
|
| 49 |
+
histo_text_comp: gr.update(visible=True, value=histo_text_),
|
| 50 |
}
|
| 51 |
+
for i in range(4):
|
| 52 |
+
update_[source_text_comp[i]] = gr.update(visible=False)
|
| 53 |
return update_
|
| 54 |
|
| 55 |
+
def input_text_fn2(input_text_, histo_text_):
|
| 56 |
+
answer, sources = ctrl.get_response(query_fr=input_text_, histo_fr=histo_text_)
|
| 57 |
+
histo_text_[-1] = (input_text_, answer)
|
|
|
|
| 58 |
update_ = {
|
| 59 |
+
histo_text_comp: gr.update(value=histo_text_),
|
| 60 |
+
input_text_comp: gr.update(value=''),
|
| 61 |
}
|
| 62 |
+
for i in range(min(len(sources), 3)):
|
| 63 |
s = sources[i]
|
| 64 |
source_label = f'{s.index} {s.title_fr} score = {s.distance_str}'
|
| 65 |
source_text = s.content_fr
|
| 66 |
update_[source_text_comp[i]] = gr.update(visible=True, value=source_text, label=source_label)
|
| 67 |
return update_
|
| 68 |
|
| 69 |
+
def clear_fn():
|
| 70 |
update_ = {
|
| 71 |
+
input_text_comp: gr.update(value=''),
|
| 72 |
+
histo_text_comp: gr.update(value='', visible=False),
|
| 73 |
}
|
| 74 |
+
for i in range(4):
|
| 75 |
+
update_[source_text_comp[i]] = gr.update(visible=False, value='hello')
|
| 76 |
return update_
|
| 77 |
|
| 78 |
input_text_comp \
|
| 79 |
+
.submit(input_text_fn1,
|
| 80 |
+
inputs=[input_text_comp, histo_text_comp],
|
| 81 |
+
outputs=[histo_text_comp,
|
| 82 |
+
source_text_comp[0], source_text_comp[1], source_text_comp[2], source_text_comp[3]])\
|
| 83 |
.then(input_text_fn2,
|
| 84 |
+
inputs=[input_text_comp, histo_text_comp],
|
| 85 |
+
outputs=[input_text_comp, histo_text_comp,
|
| 86 |
source_text_comp[0], source_text_comp[1], source_text_comp[2], source_text_comp[3]])
|
| 87 |
+
clear_btn.click(clear_fn,
|
| 88 |
+
inputs=None,
|
| 89 |
+
outputs=[input_text_comp, histo_text_comp,
|
| 90 |
+
source_text_comp[0], source_text_comp[1], source_text_comp[2], source_text_comp[3]])
|
| 91 |
|
| 92 |
return qna
|