adrien.aribaut-gaudin commited on
Commit
3ca15d8
1 Parent(s): f343031

feat: requirement part fonctionnal

Browse files
src/control/controller.py CHANGED
@@ -7,15 +7,18 @@ import random
7
  import datetime
8
  import string
9
  import docx
 
 
10
  from src.tools.doc_tools import get_title
11
  from src.domain.doc import Doc
12
  from src.domain.wikidoc import WikiPage
13
  from src.view.log_msg import create_msg_from
14
  import src.tools.semantic_db as semantic_db
15
  from src.tools.wiki import Wiki
 
16
  from src.llm.llm_tools import get_wikilist, get_public_paragraph, get_private_paragraph
17
  from src.tools.semantic_db import add_texts_to_collection, query_collection
18
- from src.tools.excel_tools import excel_to_json
19
  import gradio as gr
20
  from src.retriever.retriever import Retriever
21
 
@@ -290,20 +293,52 @@ class Controller:
290
  """
291
  Retriever(doc=doc, collection=collection)
292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  def generate_response_to_requirements(self):
294
- excel_content = self.get_requirements_from_csv()
295
- excel_content = json.loads(excel_content)
296
- print(excel_content)
 
 
 
 
 
 
 
 
 
 
 
297
  excel_name = self.input_csv
298
  if '/' in excel_name:
299
  excel_name = excel_name.split('/')[-1]
300
  elif '\\' in excel_name:
301
  excel_name = excel_name.split('\\')[-1]
302
- #copy the document and generate the new one
303
- shutil.copy(self.input_csv, self.excel_doc_path)
304
-
 
305
 
306
 
307
  def get_requirements_from_csv(self):
308
- excel_content = excel_to_json(self.input_csv)
309
  return excel_content
 
7
  import datetime
8
  import string
9
  import docx
10
+ import pandas as pd
11
+ from src.domain.block import Block
12
  from src.tools.doc_tools import get_title
13
  from src.domain.doc import Doc
14
  from src.domain.wikidoc import WikiPage
15
  from src.view.log_msg import create_msg_from
16
  import src.tools.semantic_db as semantic_db
17
  from src.tools.wiki import Wiki
18
+ from src.llm.llm_tools import generate_response_to_exigence
19
  from src.llm.llm_tools import get_wikilist, get_public_paragraph, get_private_paragraph
20
  from src.tools.semantic_db import add_texts_to_collection, query_collection
21
+ from src.tools.excel_tools import excel_to_dict
22
  import gradio as gr
23
  from src.retriever.retriever import Retriever
24
 
 
293
  """
294
  Retriever(doc=doc, collection=collection)
295
 
296
+
297
+ @staticmethod
298
+ def _select_best_sources(sources: [Block], delta_1_2=0.15, delta_1_n=0.3, absolute=1.2, alpha=0.9) -> [Block]:
299
+ """
300
+ Select the best sources: not far from the very best, not far from the last selected, and not too bad per se
301
+ """
302
+ best_sources = []
303
+ for idx, s in enumerate(sources):
304
+ if idx == 0 \
305
+ or (s.distance - sources[idx - 1].distance < delta_1_2
306
+ and s.distance - sources[0].distance < delta_1_n) \
307
+ or s.distance < absolute:
308
+ best_sources.append(s)
309
+ delta_1_2 *= alpha
310
+ delta_1_n *= alpha
311
+ absolute *= alpha
312
+ else:
313
+ break
314
+ return best_sources
315
+
316
  def generate_response_to_requirements(self):
317
+ dict_of_excel_content = self.get_requirements_from_csv()
318
+ for exigence in dict_of_excel_content:
319
+ blocks_sources = self.retriever.similarity_search(queries = exigence["Exigence"])
320
+ best_sources = self._select_best_sources(blocks_sources)
321
+ sources_contents = [f"Paragraph title : {s.title}\n-----\n{s.content}" if s.title else f"Paragraph {s.index}\n-----\n{s.content}" for s in best_sources]
322
+ context = '\n'.join(sources_contents)
323
+ i = 1
324
+ while (len(context) > 15000) and i < len(sources_contents):
325
+ context = "\n".join(sources_contents[:-i])
326
+ i += 1
327
+ reponse_exigence = generate_response_to_exigence(exigence = exigence["Exigence"], titre_exigence = exigence["Titre"], context = context)
328
+ dict_of_excel_content[dict_of_excel_content.index(exigence)]["Conformité"] = reponse_exigence
329
+ dict_of_excel_content[dict_of_excel_content.index(exigence)]["Document"] = best_sources[0].doc
330
+ dict_of_excel_content[dict_of_excel_content.index(exigence)]["Paragraphes"] = "; ".join([block.index for block in best_sources])
331
  excel_name = self.input_csv
332
  if '/' in excel_name:
333
  excel_name = excel_name.split('/')[-1]
334
  elif '\\' in excel_name:
335
  excel_name = excel_name.split('\\')[-1]
336
+
337
+ df = pd.DataFrame(data=dict_of_excel_content)
338
+ df.to_excel(f"{self.excel_doc_path}/{excel_name}", index=False)
339
+ return f"{self.excel_doc_path}/{excel_name}"
340
 
341
 
342
  def get_requirements_from_csv(self):
343
+ excel_content = excel_to_dict(self.input_csv)
344
  return excel_content
src/llm/llm_tools.py CHANGED
@@ -11,6 +11,7 @@ import wikipedia
11
  from langchain.text_splitter import CharacterTextSplitter
12
  from langchain.prompts import PromptTemplate
13
  from langchain.chains import LLMChain
 
14
  from src.llm.llms import openai_llm
15
  from src.tools.wiki import Wiki
16
 
@@ -334,4 +335,17 @@ def summarize_paragraph_v2(prompt : str, title_doc : str = '', title_para : str
334
  print("****************")
335
  print(res)
336
  print("----")
337
- return str(res).strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  from langchain.text_splitter import CharacterTextSplitter
12
  from langchain.prompts import PromptTemplate
13
  from langchain.chains import LLMChain
14
+ from src.domain.block import Block
15
  from src.llm.llms import openai_llm
16
  from src.tools.wiki import Wiki
17
 
 
335
  print("****************")
336
  print(res)
337
  print("----")
338
+ return str(res).strip()
339
+
340
+ def generate_response_to_exigence(exigence : str, titre_exigence : str, content : str):
341
+ """
342
+ Generates a response to an exigence depending on the context of the exigence and the blocks of the document.
343
+ """
344
+ task = (f"Your task consists in generating a response to a requirement in a tender for Orange, a telecommunication operator."
345
+ f"The requirement dealing with {titre_exigence} is expressed below between triple backquotes:"
346
+ f"```{exigence}```"
347
+ f"Your answer should be precise, consistent and as concise as possible with no politeness formulas and strictly be based on the following text delimited by triple backquotes : ```{content}```"
348
+ )
349
+ llm = openai_llm
350
+ generation = llm.invoke(task)
351
+ return generation
src/tools/excel_tools.py CHANGED
@@ -1,5 +1,5 @@
1
  import pandas as pd
2
 
3
- def excel_to_json(file_path):
4
  df = pd.read_excel(file_path)
5
- return df.to_json(orient='records', force_ascii=False)
 
1
  import pandas as pd
2
 
3
+ def excel_to_dict(file_path):
4
  df = pd.read_excel(file_path)
5
+ return df.to_dict(orient='records')
src/view/view.py CHANGED
@@ -192,7 +192,7 @@ def run(config: Dict, controller: Controller):
192
  input_csv_comp.upload(input_csv_fn,
193
  inputs=[input_csv_comp],
194
  outputs=[verif_btn],
195
- )
196
 
197
  def input_csv_clear_fn():
198
  controller.clear_input_csv()
@@ -217,8 +217,7 @@ def run(config: Dict, controller: Controller):
217
 
218
  verif_btn.click(generate_requirements_excel,
219
  inputs=[],
220
- outputs=[output_csv_comp],
221
- )
222
 
223
  def input_files_upload_fn(input_files_):
224
  for files in input_files_:
 
192
  input_csv_comp.upload(input_csv_fn,
193
  inputs=[input_csv_comp],
194
  outputs=[verif_btn],
195
+ show_progress="full")
196
 
197
  def input_csv_clear_fn():
198
  controller.clear_input_csv()
 
217
 
218
  verif_btn.click(generate_requirements_excel,
219
  inputs=[],
220
+ outputs=[output_csv_comp],show_progress="full")
 
221
 
222
  def input_files_upload_fn(input_files_):
223
  for files in input_files_: