File size: 9,195 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import wikipedia
import json
from langchain import PromptTemplate
from langchain.vectorstores import Chroma
from langchain.text_splitter import CharacterTextSplitter

from src.tools.llms import openai_llm
from src.tools.wiki import Wiki
from src.model.document import WikiPage



def get_wikilist(task: {}) -> str:
    """
    get the titles of wiki pages interesting for solving the given task
    """

    llm = openai_llm
    template = (f"\n"
                f"    Your task consists in finding the list of wikipedia page titles which provide useful content "
                f"    for a paragraph whose description is delimited by triple backticks: ```{task['description']}```\n"
                f"    \n"
                f"    The paragraph belongs at the top level of the hierarchy to a document"
                f"    whose description is delimited by triple backticks: ``` {task['doc_description']}```\n"
                f"    Make sure that the paragraph relates the top level of the document\n"
                f"    \n"
                f"    The paragraph belongs to a higher paragraph in the hierarchy \\n"
                f"    whose description is delimited by triple backticks: ``` {task['above']}```\n"
                f"    Make sure that the paragraph relates with the paragraph in the hierarchy of the document\n"
                f"        \n"
                f"    The paragraphs comes after previous paragraphs \\n"
                f"    whose description is delimited by triple backticks: ``` {task['before']}```\n"
                f"    Make sure that the paragraph relates with previous paragraph without any repetition\n"
                f"    \n"
                f"    The paragraphs comes before next paragraphs \\n"
                f"    whose description is delimited by triple backticks: ``` {task['after']}```\n"
                f"    \n"
                f"    Format your response as a JSON list of strings separated by commas.\n"
                f"  \n"
                f"\n"
                f"    ")

    prompt = PromptTemplate(
        input_variables=[],
        template=template
    )

    #wikilist = LLMChain(llm=openai_llm, prompt=prompt).run()
    wikilist = json.loads(llm(template))

    expanded_wikilist = []

    expand_factor = 3

    for wikipage in wikilist:
        expanded_wikilist += wikipedia.search(wikipage, expand_factor)

    wikilist = list(set(expanded_wikilist))

    return wikilist


def get_public_paragraph(task: {}) -> str:
    """returns the task directly performed by chat GPT"""

    llm = openai_llm
    template = (f"\n"
                f"    Your task consists in generating a paragraph\\n"
                f"    whose description is delimited by triple backticks: ```{task['description']}```\n"
                f"\n"
                f"    The paragraph belongs at the top level of the hierarchy to a document \\n"
                f"    whose description is delimited by triple backticks: ``` {task['doc_description']}```\n"
                f"    Make sure that the paragraph relates the top level of the document\n"
                f"    \n"
                f"    The paragraph belongs to a higher paragraph in the hierarchy \\n"
                f"    whose description is delimited by triple backticks: ``` {task['above']}```\n"
                f"    Make sure that the paragraph relates with the paragraph in the hierarchy of the document\n"
                f"        \n"
                f"    The paragraphs comes after previous paragraphs \\n"
                f"    whose description is delimited by triple backticks: ``` {task['before']}```\n"
                f"    Make sure that the paragraph relates with previous paragraph without any repetition\n"
                f"    \n"
                f"    The paragraphs comes before next paragraphs \\n"
                f"    whose description is delimited by triple backticks: ``` {task['after']}```\n"
                f"    Make sure that the paragraph prepares the transition to the next paragraph without any repetition\n"
                f"    \n"
                f"  \n"
                f"\n"
                f"    ")

    p = llm(template)

    return p


def create_index(wikilist: [str]):
    """
    useful for creating the index of wikipages
    """
    fetch = Wiki().fetch

    pages = [(title, fetch(title)) for title in wikilist if type(fetch(title)) != str]
    texts = []
    chunk = 800
    for title, page in pages:
        texts.append(WikiPage(title=title, fulltext=page.page_content))

    doc_splitter = CharacterTextSplitter(
        separator=".",
        chunk_size=chunk,
        chunk_overlap=100,
        length_function=len,
    )

    paragraphs = texts[0].get_paragraphs(chunk=800)

    split_texts = []
    for p in paragraphs:
        split_texts += doc_splitter.split_text(p)

    for split_text in split_texts:
        assert type(split_text) == str
        assert 0 < len(split_text) < 2 * 500

    wiki_index = Chroma.from_texts(split_texts)

    return wiki_index


def get_wiki_paragraph(wiki_index, task: {}) -> str:
    """useful to get a summary in one line from wiki index"""

    task_description = get_public_paragraph(task)
    wiki_paragraphs = semantic_search(wiki_index, task_description)
    text_content = ""
    for p in wiki_paragraphs:
        text_content += p.page_content + "/n/n"

    template = (f"\n"
                f"    Your task consists in generating a paragraph\\n"
                f"    whose description is delimited by triple backticks: ```{task['description']}```\n"
                f"\n"
                f"    The text generation is based in the documents provided in these sections \n"
                f"    delimited by by triple backticks: ``` {text_content}``` \n"
                f"    The paragraph belongs at the top level of the hierarchy to a document \\n"
                f"    whose description is delimited by triple backticks: ``` {task['doc_description']}```\n"
                f"    Make sure that the paragraph relates the top level of the document\n"
                f"    \n"
                f"    The paragraph belongs to a higher paragraph in the hierarchy \\n"
                f"    whose description is delimited by triple backticks: ``` {task['above']}```\n"
                f"    Make sure that the paragraph relates with the paragraph in the hierarchy of the document\n"
                f"        \n"
                f"    The paragraphs comes after previous paragraphs \\n"
                f"    whose description is delimited by triple backticks: ``` {task['before']}```\n"
                f"    Make sure that the paragraph relates with previous paragraph without any repetition\n"
                f"    \n"
                f"    The paragraphs comes before next paragraphs \\n"
                f"    whose description is delimited by triple backticks: ``` {task['after']}```\n"
                f"    Make sure that the paragraph prepares the transition to the next paragraph without any repetition\n"
                f"    \n"
                f"  \n"
                f"\n"
                f"    ")

    llm = openai_llm
    p = llm(template)

    return p


def get_private_paragraph(texts, task: {}) -> str:
    """useful to get a summary in one line from wiki index"""

    text_content = ""
    for t in texts:
        text_content += t + "/n/n"

    template = (f"\n"
                f"    Your task consists in generating a paragraph\\n"
                f"    whose description is delimited by triple backticks: ```{task['description']}```\n"
                f"\n"
                f"    The text generation is based in the documents provided in these sections \n"
                f"    delimited by by triple backticks: ``` {text_content}``` \n"
                f"    The paragraph belongs at the top level of the hierarchy to a document \\n"
                f"    whose description is delimited by triple backticks: ``` {task['doc_description']}```\n"
                f"    Make sure that the paragraph relates the top level of the document\n"
                f"    \n"
                f"    The paragraph belongs to a higher paragraph in the hierarchy \\n"
                f"    whose description is delimited by triple backticks: ``` {task['above']}```\n"
                f"    Make sure that the paragraph relates with the paragraph in the hierarchy of the document\n"
                f"        \n"
                f"    The paragraphs comes after previous paragraphs \\n"
                f"    whose description is delimited by triple backticks: ``` {task['before']}```\n"
                f"    Make sure that the paragraph relates with previous paragraph without any repetition\n"
                f"    \n"
                f"    The paragraphs comes before next paragraphs \\n"
                f"    whose description is delimited by triple backticks: ``` {task['after']}```\n"
                f"    Make sure that the paragraph prepares the transition to the next paragraph without any repetition\n"
                f"    \n"
                f"  \n"
                f"\n"
                f"    ")

    llm = openai_llm
    p = llm(template)

    return p