minor improvements in llm prompts
Browse files- app.py +1 -1
- config.py +6 -6
- src/control/control.py +15 -3
- src/tools/llm.py +18 -13
- src/view/view.py +3 -3
app.py
CHANGED
@@ -23,7 +23,7 @@ doc_content_fr = Doc(content_fr_path)
|
|
23 |
client_db = chromadb.Client()
|
24 |
retriever = Retriever(client_db, doc_plan, doc_content, doc_content_fr, collection_name)
|
25 |
|
26 |
-
llm_model = OpenAI(temperature=0)
|
27 |
llm = LlmAgent(llm_model)
|
28 |
|
29 |
specials['remote_rate_df'] = pd.read_csv(specials['remote_rate_path'])
|
|
|
23 |
client_db = chromadb.Client()
|
24 |
retriever = Retriever(client_db, doc_plan, doc_content, doc_content_fr, collection_name)
|
25 |
|
26 |
+
llm_model = OpenAI(temperature=0, model_name="gpt-3.5-turbo")
|
27 |
llm = LlmAgent(llm_model)
|
28 |
|
29 |
specials['remote_rate_df'] = pd.read_csv(specials['remote_rate_path'])
|
config.py
CHANGED
@@ -25,19 +25,19 @@ view_config = {
|
|
25 |
}
|
26 |
|
27 |
countries_extensions = {
|
28 |
-
'
|
29 |
-
|
|
|
30 |
'E.A.U': ["EAU", "Emirats", "Emirats Arabes Unis", "Emirates", "UAE", "United Arab Emirates"],
|
31 |
'Pays-Bas': ['Les Pays-Bas', 'Hollande', 'Holland']
|
32 |
}
|
33 |
specials = {'remote_rate_path': 'data/remote_rates.csv',
|
34 |
'remote_rate_known': "the scale rate of remoteness for the ",
|
35 |
-
|
36 |
-
'remote_rate_unknown': "the scale rate of remoteness for the country mentionned is unknown. Allowances "
|
37 |
"apply though",
|
38 |
'accommodation_meal_path': 'data/accommodation_meal_rates.csv',
|
39 |
'accommodation_meal_known': 'the rates for accommodation and meals are the following: ',
|
40 |
-
'accommodation_meal_unknown':
|
41 |
-
|
42 |
'countries_extensions': countries_extensions,
|
43 |
}
|
|
|
25 |
}
|
26 |
|
27 |
countries_extensions = {
|
28 |
+
'Grande Bretagne': ['UK', 'U.K.', 'RU', 'R.U.', 'Angleterre', 'Grande-Bretagne', 'Royaume-Uni', 'Royaume Uni',
|
29 |
+
"l'Angleterre", 'Grande Bretagne'],
|
30 |
+
'Etats-Unis': ['Etats-unis', 'Etats Unis', 'Etats unis', 'ETATS-UNIS', 'USA', 'U.S.A', 'U.S.A.'],
|
31 |
'E.A.U': ["EAU", "Emirats", "Emirats Arabes Unis", "Emirates", "UAE", "United Arab Emirates"],
|
32 |
'Pays-Bas': ['Les Pays-Bas', 'Hollande', 'Holland']
|
33 |
}
|
34 |
specials = {'remote_rate_path': 'data/remote_rates.csv',
|
35 |
'remote_rate_known': "the scale rate of remoteness for the ",
|
36 |
+
'remote_rate_unknown': "the scale rate of remoteness for the mentioned country is unknown. Allowances "
|
|
|
37 |
"apply though",
|
38 |
'accommodation_meal_path': 'data/accommodation_meal_rates.csv',
|
39 |
'accommodation_meal_known': 'the rates for accommodation and meals are the following: ',
|
40 |
+
'accommodation_meal_unknown':
|
41 |
+
'the rates for accommodation and meals are not defined for the mentioned country ',
|
42 |
'countries_extensions': countries_extensions,
|
43 |
}
|
src/control/control.py
CHANGED
@@ -28,12 +28,13 @@ class Controller:
|
|
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':
|
31 |
-
answer = self.llm.generate_answer(answer_en=answer, query=query_fr,
|
32 |
-
|
|
|
33 |
return answer, block_sources
|
34 |
|
35 |
@staticmethod
|
36 |
-
def _get_histo(histo: [(str, str)]) -> str:
|
37 |
histo_conversation = ""
|
38 |
histo_queries = ""
|
39 |
|
@@ -42,6 +43,17 @@ class Controller:
|
|
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]:
|
|
|
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':
|
31 |
+
answer = self.llm.generate_answer(answer_en=answer, query=query_fr,
|
32 |
+
histo_fr=histo_conversation, context_fr=context_fr)
|
33 |
+
answer = self._clean_answer(answer)
|
34 |
return answer, block_sources
|
35 |
|
36 |
@staticmethod
|
37 |
+
def _get_histo(histo: [(str, str)]) -> (str, str):
|
38 |
histo_conversation = ""
|
39 |
histo_queries = ""
|
40 |
|
|
|
43 |
histo_queries += query + '\n'
|
44 |
return histo_conversation[:-1], histo_queries
|
45 |
|
46 |
+
@staticmethod
|
47 |
+
def _clean_answer(answer: str) -> str:
|
48 |
+
answer = answer.strip('bot:')
|
49 |
+
while answer and answer[-1] in {"'", '"', " ", "`"}:
|
50 |
+
answer = answer[:-1]
|
51 |
+
while answer and answer[0] in {"'", '"', " ", "`"}:
|
52 |
+
answer = answer[1:]
|
53 |
+
answer = answer.strip('bot:')
|
54 |
+
if answer and answer[-1] != ".":
|
55 |
+
answer += "."
|
56 |
+
return answer
|
57 |
|
58 |
@staticmethod
|
59 |
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,15 @@ 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 bot designed to answer to the query from users delimited by
|
9 |
-
f"
|
10 |
-
f"
|
11 |
-
f"
|
12 |
-
f"
|
13 |
-
f"
|
14 |
-
f"
|
15 |
-
f"
|
|
|
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 |
|
@@ -36,16 +37,20 @@ class LlmAgent:
|
|
36 |
p = self.llm(template)
|
37 |
return p
|
38 |
|
39 |
-
def generate_answer(self, query: str, answer_en: str, context_fr: str) -> str:
|
40 |
"""provides the final answer in french based on the initial query and the answer in english"""
|
41 |
|
42 |
def _cut_unfinished_sentence(s: str):
|
43 |
return '.'.join(p.split('.')[:-1])
|
44 |
|
45 |
-
template = (f"Your task consists in
|
46 |
-
f"delimited by triple backticks: ```{query}```
|
47 |
-
f"
|
48 |
-
f"
|
|
|
|
|
|
|
|
|
49 |
)
|
50 |
|
51 |
p = self.llm(template)
|
|
|
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 conversation bot designed to answer to the query from users delimited by "
|
9 |
+
f"triple backticks: "
|
10 |
+
f"\\n ``` {query} ```\\n"
|
11 |
+
f"Your answer is based on the context delimited by triple backticks: "
|
12 |
+
f"\\n ``` {context} ```\\n"
|
13 |
+
f"You are consistent and avoid redundancies with the rest of the initial conversation in french"
|
14 |
+
f"delimited by triple backticks: "
|
15 |
+
f"\\n ``` {histo} ```\\n"
|
16 |
+
f"Your response shall be in {language} and shall be concise"
|
17 |
f"In case the provided context is not relevant to answer to the question, just return that you "
|
18 |
f"don't know the answer ")
|
19 |
|
|
|
37 |
p = self.llm(template)
|
38 |
return p
|
39 |
|
40 |
+
def generate_answer(self, query: str, answer_en: str, histo_fr: str, context_fr: str) -> str:
|
41 |
"""provides the final answer in french based on the initial query and the answer in english"""
|
42 |
|
43 |
def _cut_unfinished_sentence(s: str):
|
44 |
return '.'.join(p.split('.')[:-1])
|
45 |
|
46 |
+
template = (f"Your task consists in translating the answer in french to the query "
|
47 |
+
f"delimited by triple backticks: ```{query}``` \\n"
|
48 |
+
f"You are given the answer in english delimited by triple backticks: ```{answer_en}```"
|
49 |
+
f"\\n You don't add new content to the answer in english but: "
|
50 |
+
f"\\n 1 You can use some vocabulary from the context in french delimited by triple backticks: "
|
51 |
+
f"```{context_fr}```"
|
52 |
+
f"\\n 2 You are consistent and avoid redundancies with the rest of the initial"
|
53 |
+
f" conversation in french delimited by triple backticks: ```{histo_fr}```"
|
54 |
)
|
55 |
|
56 |
p = self.llm(template)
|
src/view/view.py
CHANGED
@@ -45,7 +45,7 @@ def run(ctrl: Controller, config: {}):
|
|
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,
|
49 |
}
|
50 |
for i in range(4):
|
51 |
update_[source_text_comp[i]] = gr.update(visible=False)
|
@@ -70,7 +70,7 @@ def run(ctrl: Controller, config: {}):
|
|
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,
|
74 |
}
|
75 |
for i in range(4):
|
76 |
update_[source_text_comp[i]] = gr.update(visible=False)
|
@@ -80,7 +80,7 @@ def run(ctrl: Controller, config: {}):
|
|
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
|
84 |
}
|
85 |
for i in range(4):
|
86 |
update_[source_text_comp[i]] = gr.update(visible=False, value='hello')
|
|
|
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,),
|
49 |
}
|
50 |
for i in range(4):
|
51 |
update_[source_text_comp[i]] = gr.update(visible=False)
|
|
|
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, value=''),
|
74 |
}
|
75 |
for i in range(4):
|
76 |
update_[source_text_comp[i]] = gr.update(visible=False)
|
|
|
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),
|
84 |
}
|
85 |
for i in range(4):
|
86 |
update_[source_text_comp[i]] = gr.update(visible=False, value='hello')
|