File size: 2,756 Bytes
65642c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

import asyncio  # on va en avoir besoin :)
import string
import random
from datetime import datetime


from src.tools.semantic_db import get_or_create_collection, reset_collection
from src.tools.wiki import Wiki
from src.model.document import InputDoc, WikiPage
from src.tools.llm_tools import get_wikilist, get_public_paragraph, get_private_paragraph
from src.tools.semantic_db import add_texts_to_collection, query_collection

"""
Tools
"""


def get_long_id(id_):
    if id_ != -1:
        return id_
    else:
        now = datetime.now().strftime("%m%d%H%M")
        letters = string.ascii_lowercase + string.digits
        long_id = now+'-'+''.join(random.choice(letters) for _ in range(10))
    return long_id


"""
Input control
"""



"""
Source Control
"""

def wiki_fetch(input_text: str) -> [str]:
    """
    returns the title of the wikipages corresponding to the tasks described in the input text
    """
    tasks = InputDoc(input_text).tasks
    wiki_lists = [get_wikilist(t) for t in tasks]
    flatten_wiki_list = list(set().union(*[set(w) for w in wiki_lists]))
    return flatten_wiki_list


async def wiki_upload_and_store(wiki_title: str, collection_name: str):
    """
    uploads one wikipage and stores them into the right collection
    """
    wikipage = Wiki().fetch(wiki_title)
    wiki_title = wiki_title
    if type(wikipage) != str:
        texts = WikiPage(wikipage.page_content).get_paragraphs()
        add_texts_to_collection(coll_name=collection_name, texts=texts, file=wiki_title, source='wiki')
    else:
        print(wikipage)


async def my_files_upload_and_store(title: str, collection_name: str):
    doc = title
    title = title
    texts = InputDoc(doc).get_paragraphs()
    add_texts_to_collection(coll_name=collection_name, texts=texts, file=title, source='my_files')


"""
Generate Control
"""


def generate_doc_from_gpt(input_txt: str) -> str:
    input_doc = InputDoc(input_txt)
    tasks = input_doc.tasks
    task_resolutions = [get_public_paragraph(t) for t in tasks]
    # task_resolutions = ["ça c'est de la réso"]
    generated_doc = input_doc.replace_tasks(task_resolutions)
    return generated_doc


def generate_doc_from_db(input_txt: str, collection_name: str, from_files: [str]) -> str:

    def query_from_task(task):
        return get_public_paragraph(task)
    input_doc = InputDoc(input_txt)
    tasks = input_doc.tasks
    queries = [query_from_task(t) for t in tasks]
    texts_list = [query_collection(coll_name=collection_name, query=q, from_files=from_files) for q in queries]
    task_resolutions = [get_private_paragraph(task=task, texts=texts) for task, texts in zip(tasks, texts_list)]
    generated_doc = input_doc.replace_tasks(task_resolutions)
    return generated_doc