improvement in the past context management with chatbot
Browse files- data/business_trips_plan_until_end_en.docx +0 -0
- src/control/control.py +15 -10
- src/tools/llm.py +12 -4
- src/view/view.py +29 -9
data/business_trips_plan_until_end_en.docx
CHANGED
Binary files a/data/business_trips_plan_until_end_en.docx and b/data/business_trips_plan_until_end_en.docx differ
|
|
src/control/control.py
CHANGED
@@ -15,15 +15,16 @@ class Controller:
|
|
15 |
self.llm = llm
|
16 |
|
17 |
def get_response(self, query_fr: str, histo_fr: [(str, str)]) -> (str, [Block]):
|
18 |
-
|
19 |
-
|
|
|
20 |
block_sources = self._select_best_sources(block_sources)
|
21 |
for block in block_sources:
|
22 |
-
self._expand_block_with_specials(block,
|
23 |
sources_contents = [s.content for s in block_sources]
|
24 |
context = '\n'.join(sources_contents)
|
25 |
-
|
26 |
-
|
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,11 +33,15 @@ class Controller:
|
|
32 |
return answer, block_sources
|
33 |
|
34 |
@staticmethod
|
35 |
-
def
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
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]:
|
|
|
15 |
self.llm = llm
|
16 |
|
17 |
def get_response(self, query_fr: str, histo_fr: [(str, str)]) -> (str, [Block]):
|
18 |
+
histo_conversation, histo_queries = self._get_histo(histo_fr)
|
19 |
+
queries = self.llm.translate(text=histo_queries) if self.plan_language == 'en' else histo_queries
|
20 |
+
block_sources = self.retriever.similarity_search(query=queries)
|
21 |
block_sources = self._select_best_sources(block_sources)
|
22 |
for block in block_sources:
|
23 |
+
self._expand_block_with_specials(block, histo_queries)
|
24 |
sources_contents = [s.content for s in block_sources]
|
25 |
context = '\n'.join(sources_contents)
|
26 |
+
answer = self.llm.generate_paragraph(query=queries, histo=histo_conversation, context=context,
|
27 |
+
language=self.content_language)
|
28 |
sources_contents_fr = [s.content_fr for s in block_sources[:2]]
|
29 |
context_fr = '\n'.join(sources_contents_fr)
|
30 |
if self.content_language == 'en':
|
|
|
33 |
return answer, block_sources
|
34 |
|
35 |
@staticmethod
|
36 |
+
def _get_histo(histo: [(str, str)]) -> str:
|
37 |
+
histo_conversation = ""
|
38 |
+
histo_queries = ""
|
39 |
+
|
40 |
+
for (query, answer) in histo[-5:]:
|
41 |
+
histo_conversation += f'user: {query} \n bot: {answer}\n'
|
42 |
+
histo_queries += query + '\n'
|
43 |
+
return histo_conversation[:-1], histo_queries
|
44 |
+
|
45 |
|
46 |
@staticmethod
|
47 |
def _select_best_sources(sources: [Block], delta_1_2=0.15, delta_1_n=0.3, absolute=1.2, alpha=0.9) -> [Block]:
|
src/tools/llm.py
CHANGED
@@ -5,14 +5,22 @@ class LlmAgent:
|
|
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
|
9 |
-
f"
|
10 |
-
f"
|
11 |
-
f"
|
|
|
|
|
|
|
|
|
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 |
|
|
|
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 bot designed to answer to the query from users delimited by triple backticks: \\n"
|
9 |
+
f"``` {query} ```\\n"
|
10 |
+
f"Your answer is based on the context delimited by triple backticks: \\n"
|
11 |
+
f"``` {context} ```\\n"
|
12 |
+
f"Your answer takes into account previous part of the conversation in french delimited by triple "
|
13 |
+
f"backticks and shall avoid redundancies in the answers\\n "
|
14 |
+
f"``` {histo} ```\\n"
|
15 |
+
f"Your response shall be in {language} and shall be concise/"
|
16 |
f"In case the provided context is not relevant to answer to the question, just return that you "
|
17 |
f"don't know the answer ")
|
18 |
|
19 |
p = self.llm(template)
|
20 |
+
print("****************")
|
21 |
+
print(template)
|
22 |
+
print("----")
|
23 |
+
print(p)
|
24 |
return p
|
25 |
|
26 |
|
src/view/view.py
CHANGED
@@ -23,14 +23,12 @@ def run(ctrl: Controller, config: {}):
|
|
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(
|
@@ -47,6 +45,7 @@ def run(ctrl: Controller, config: {}):
|
|
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)
|
@@ -66,10 +65,22 @@ def run(ctrl: Controller, config: {}):
|
|
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')
|
@@ -78,15 +89,24 @@ def run(ctrl: Controller, config: {}):
|
|
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
|
|
|
23 |
interactive=True,
|
24 |
placeholder="Posez votre question ici",
|
25 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
clear_btn = gr.Button("Clear")
|
27 |
+
input_example_comp = gr.Radio(
|
28 |
+
label="Examples",
|
29 |
+
choices=list(config['examples'].values()),
|
30 |
+
value="",
|
31 |
+
)
|
32 |
source_text_comp = []
|
33 |
for i in range(4):
|
34 |
source_text_comp.append(gr.Textbox(
|
|
|
45 |
histo_text_.append((input_text_, None))
|
46 |
update_ = {
|
47 |
histo_text_comp: gr.update(visible=True, value=histo_text_),
|
48 |
+
input_example_comp: gr.update(visible=False, interactive=False),
|
49 |
}
|
50 |
for i in range(4):
|
51 |
update_[source_text_comp[i]] = gr.update(visible=False)
|
|
|
65 |
update_[source_text_comp[i]] = gr.update(visible=True, value=source_text, label=source_label)
|
66 |
return update_
|
67 |
|
68 |
+
def input_example_fn(input_example_, histo_text_):
|
69 |
+
histo_text_.append((input_example_, None))
|
70 |
+
update_ = {
|
71 |
+
input_text_comp: gr.update(value=input_example_),
|
72 |
+
histo_text_comp: gr.update(visible=True, value=histo_text_),
|
73 |
+
input_example_comp: gr.update(visible=False, interactive=False, value=''),
|
74 |
+
}
|
75 |
+
for i in range(4):
|
76 |
+
update_[source_text_comp[i]] = gr.update(visible=False)
|
77 |
+
return update_
|
78 |
+
|
79 |
def clear_fn():
|
80 |
update_ = {
|
81 |
input_text_comp: gr.update(value=''),
|
82 |
histo_text_comp: gr.update(value='', visible=False),
|
83 |
+
input_example_comp: gr.update(value='', visible=True, interactive=True),
|
84 |
}
|
85 |
for i in range(4):
|
86 |
update_[source_text_comp[i]] = gr.update(visible=False, value='hello')
|
|
|
89 |
input_text_comp \
|
90 |
.submit(input_text_fn1,
|
91 |
inputs=[input_text_comp, histo_text_comp],
|
92 |
+
outputs=[histo_text_comp, input_example_comp,
|
93 |
source_text_comp[0], source_text_comp[1], source_text_comp[2], source_text_comp[3]])\
|
94 |
.then(input_text_fn2,
|
95 |
inputs=[input_text_comp, histo_text_comp],
|
96 |
outputs=[input_text_comp, histo_text_comp,
|
97 |
source_text_comp[0], source_text_comp[1], source_text_comp[2], source_text_comp[3]])
|
98 |
+
input_example_comp \
|
99 |
+
.input(input_example_fn,
|
100 |
+
inputs=[input_example_comp, histo_text_comp],
|
101 |
+
outputs=[input_text_comp, histo_text_comp, input_example_comp,
|
102 |
+
source_text_comp[0], source_text_comp[1], source_text_comp[2], source_text_comp[3]])\
|
103 |
+
.then(input_text_fn2,
|
104 |
+
inputs=[input_text_comp, histo_text_comp],
|
105 |
+
outputs=[input_text_comp, histo_text_comp,
|
106 |
+
source_text_comp[0], source_text_comp[1], source_text_comp[2], source_text_comp[3]])
|
107 |
clear_btn.click(clear_fn,
|
108 |
inputs=None,
|
109 |
+
outputs=[input_text_comp, histo_text_comp, input_example_comp,
|
110 |
source_text_comp[0], source_text_comp[1], source_text_comp[2], source_text_comp[3]])
|
111 |
|
112 |
return qna
|