File size: 16,660 Bytes
440deef
028ac25
 
 
 
440deef
 
028ac25
440deef
028ac25
440deef
 
 
 
 
 
 
 
 
 
 
 
 
028ac25
 
 
 
 
 
 
 
 
 
440deef
028ac25
 
440deef
028ac25
 
 
 
 
 
 
440deef
 
 
028ac25
 
 
440deef
028ac25
440deef
028ac25
440deef
028ac25
440deef
 
 
028ac25
 
 
 
 
 
 
440deef
 
 
028ac25
 
440deef
 
 
028ac25
 
 
 
 
 
 
 
440deef
 
028ac25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440deef
028ac25
 
 
 
 
 
440deef
 
028ac25
440deef
 
028ac25
440deef
 
028ac25
440deef
 
028ac25
440deef
 
028ac25
440deef
 
 
 
 
 
 
 
 
028ac25
440deef
 
 
 
 
 
 
 
 
 
 
 
 
028ac25
 
440deef
 
 
 
 
 
 
 
 
 
 
 
 
028ac25
 
 
 
 
440deef
028ac25
440deef
028ac25
 
440deef
 
 
028ac25
 
 
 
 
 
440deef
 
 
 
 
028ac25
 
 
 
 
 
 
 
 
 
 
 
 
440deef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from langchain import FAISS
from langchain import LLMMathChain
from langchain.agents import AgentType, create_csv_agent
from langchain.chains import RetrievalQA
from langchain.chains.question_answering import load_qa_chain
from langchain.memory import ConversationBufferMemory
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
from langchain.tools import Tool

import utils


def get_chat_history(inputs) -> str:
    res = []
    for human, ai in inputs:
        res.append(f"Human:{human}\nAI:{ai}")
    return "\n".join(res)


class GraderQA:
    def __init__(self, grader, embeddings):
        self.grader = grader
        self.llm = self.grader.llm
        self.folder_path = "vector_stores/"
        self.summary_index_name = "canvas-discussions-summary"
        self.summary_index_file = "vector_stores/canvas-discussions-summary.faiss"
        self.summary_pickle_file = "vector_stores/canvas-discussions-summary.pkl"
        self.qa_index_name = "canvas-discussions-qa"
        self.qa_index_file = "vector_stores/canvas-discussions-qa.faiss"
        self.qa_pickle_file = "vector_stores/canvas-discussions-qa.pkl"
        self.summary_docs = utils.get_csv_files(self.grader.csv, source_column='student_name')
        self.qa_docs = utils.get_csv_files(self.grader.csv, source_column='student_name',
                                           field_names=['student_name', 'total_score', 'score_breakdown'])
        self.rubric_text = grader.rubric_text
        self.summary_index = self.get_search_index(embeddings)
        self.qa_index = self.get_qa_index(embeddings)
        self.chain = self.create_chain(embeddings)
        self.qa_chain = self.create_qa_chain()
        self.math_chain = self.create_math_chain()
        self.tools = self.get_tools()
        self.memory = ConversationBufferMemory(memory_key='chat_history',
                                               return_messages=True,
                                               output_key='answer')
        self.agent = self.create_agent()
        self.tokens = None
        self.question = None

    def load_all_indexes(self, embeddings):
        return self.get_search_index(embeddings), self.get_qa_index(embeddings)

    def get_search_index(self, embeddings):
        if utils.index_exists(self.summary_pickle_file, self.summary_index_file):
            # Load index from pickle file
            search_index = utils.load_index(self.folder_path, self.summary_index_name, embeddings)
        else:
            search_index = utils.create_index(self.folder_path, self.summary_index_name, embeddings, self.summary_docs)
            print("Created index")
        return search_index

    def get_qa_index(self, embeddings):
        if utils.index_exists(self.qa_pickle_file, self.qa_index_file):
            # Load index from pickle file
            search_index = utils.load_index(self.folder_path, self.qa_index_name, embeddings)
        else:
            search_index = utils.create_index(self.folder_path, self.qa_index_name, embeddings, self.qa_docs)
            print("Created index")
        return search_index

    def create_chain(self, embeddings):
        if not self.summary_index:
            self.summary_index = self.get_search_index(embeddings)

        question_prompt, combine_prompt = self.create_map_reduce_prompt()
        # create agent, 1 chain for summary based question, 2nd chain for semantic retrieval based question
        qa_chain = load_qa_chain(self.llm, chain_type="map_reduce", question_prompt=question_prompt,
                                 combine_prompt=combine_prompt, verbose=True)

        chain = RetrievalQA(combine_documents_chain=qa_chain,
                            retriever=self.summary_index.as_retriever(search_type='mmr',
                                                                      search_kwargs={'lambda_mult': 1, 'fetch_k': 50,
                                                                                     'k': 30}),
                            return_source_documents=True, verbose=True, )
        return chain

    def create_qa_chain(self):
        qa = RetrievalQA.from_chain_type(llm=self.llm, chain_type="stuff",
                                         retriever=self.qa_index.as_retriever(search_type='mmr',
                                                                              search_kwargs={'lambda_mult': 1,
                                                                                             'fetch_k': 50,
                                                                                             'k': 30}), verbose=True)
        return qa

    def create_math_chain(self):
        return LLMMathChain.from_llm(llm=self.llm, verbose=True)

    def get_tools(self):
        tools = [
            Tool(
                name="Grading Score Results",
                func=self.run_qa_chain,
                description="useful when you need to answer questions related to GRADES, SCORING or SCORE BREAKDOWN(INDIVIDUAL OR OVERALL) based questions from the grading results of the canvas discussion. Use this more often because this has a higher accuracy about the SCORING and GRADES of the students."
            ),
            Tool(
                name="Summary",
                func=self.run_summary_chain,
                description="useful when you need to answer summary based questions for all students' grading results for the canvas discussion where the question is complicated and ONLY WHEN the answer is not directly available in the grading score results"
            ),
            Tool(
                name="Calculator",
                func=self.run_math_chain,
                description="Useful for when you need to compute mathematical expressions"
            )
        ]
        return tools

    def create_agent(self):
        # Initialize a Conversational Agent with the existing chain as a tool
        # planner = load_chat_planner(self.llm)
        #
        # # agent = initialize_agent(self.tools, self.llm, agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=self.memory)
        # executor = load_agent_executor(self.llm,self.tools, verbose=True)
        #
        #
        # agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)
        # agent = initialize_agent(
        #     self.tools, self.llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
        # )

        agent = create_csv_agent(
            self.llm,
            self.grader.csv,
            verbose=True,
            agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
        )
        return agent

    def create_map_reduce_prompt(self):
        system_template = f"""Use the following student's grading result document to answer a summary based question. The question will always be related to the overall grading results, feedback, score, summary of student responses for the discussion. But the answer will ALWAYS be specific to the student based on the question. There are examples to help you understand how to answer the question.
        ______________________
        Grading Result For:
        {{context}}
        ______________________
        Use the following examples to take guidance on how to answer the question.
        Examples:
        Question: How many students participated in the discussion?
        Rephrased question: Did this student participate in the discussion?
        Answer: This student participated in the discussion./This student did not participate in the discussion.
        Question: What was the average score for the discussion?
        Rephrased question: What was the score for this student for the discussion?
        Answer: This student received a score of 10/10 for the discussion.
        Question: How many students received a full score?/How many students did not receive a full score?
        Rephrased question: Did this student receive a full score?
        Answer: This student received a full score./This student did not receive a full score.
        Question: How many students lost marks in X category of the rubric?
        Rephrased question: Did this student lose marks in X category of the rubric?
        Answer: This student lost marks in X category of the rubric./This student did not lose marks in X category of the rubric.
        Question: Give me 3 best responses received for the discussion.
        Rephrased question: What were the 3 best responses received for the discussion?
        Answer: This student gave the following responses for the discussion and received a score of 10/10.
        ______________________
        """
        messages = [
            SystemMessagePromptTemplate.from_template(system_template),
            HumanMessagePromptTemplate.from_template("{question}"),
        ]
        CHAT_QUESTION_PROMPT = ChatPromptTemplate.from_messages(messages)
        system_template = """You are Canvas Discussions Grading + Feedback QA Bot. Have a conversation with a human, answering the questions about the grading results, feedback, answers as accurately as possible.
        Use the following answers for each student to answer the users question as accurately as possible.
        You are an expert at basic calculations and answering questions on grading results and can answer the following questions with ease.
        If you don't know the answer, just say that you don't know. Don't try to make up an answer.
        ______________________
        {summaries}"""
        messages = [
            SystemMessagePromptTemplate.from_template(system_template),
            HumanMessagePromptTemplate.from_template("{question}"),
        ]
        CHAT_COMBINE_PROMPT = ChatPromptTemplate.from_messages(messages)
        return CHAT_QUESTION_PROMPT, CHAT_COMBINE_PROMPT

    def create_prompt(self):
        system_template = f"""You are Canvas Discussions Grading + Feedback QA Bot. Have a conversation with a human, answering the questions about the grading results, feedback, answers as accurately as possible.
        You are a grading assistant who graded the canvas discussions to create the following grading results and feedback.
        Use the following instruction, rubric of the discussion which were used to grade the discussions and refine the answer if needed.
        ----------------
        {self.rubric_text}
        ----------------
        Use the following pieces of the grading results, score, feedback and summary of student responses to answer the users question as accurately as possible.
        {{context}}"""
        messages = [
            SystemMessagePromptTemplate.from_template(system_template),
            HumanMessagePromptTemplate.from_template("{question}"),
        ]
        return ChatPromptTemplate.from_messages(messages)

    def get_tokens(self):
        total_tokens = 0
        # for doc in self.docs:
        #     chat_prompt = self.prompt.format(context=doc, question=self.question)
        #
        #     num_tokens = self.llm.get_num_tokens(chat_prompt)
        #     total_tokens += num_tokens

        # summary = self.llm(summary_prompt)

        # print (f"Summary: {summary.strip()}")
        # print ("\n")
        return total_tokens

    def run_qa_chain(self, question):
        self.question = question
        self.get_tokens()
        answer = self.qa_chain.run(question)
        return answer

    def run_summary_chain(self, question):
        self.question = question
        self.get_tokens()
        answer = self.chain(question)
        return answer

    def run_math_chain(self, question):
        self.question = question
        self.get_tokens()
        answer = self.math_chain.run(question)
        return answer


def search_index_from_docs(source_chunks, embeddings):
    # print("source chunks: " + str(len(source_chunks)))
    # print("embeddings: " + str(embeddings))
    search_index = FAISS.from_documents(source_chunks, embeddings)
    return search_index

# system_template = """You are Canvas Discussions Grading + Feedback QA Bot. Have a conversation with a human, answering the following questions as best you can.
# You are a grading assistant who graded the canvas discussions to create the following grading results and feedback. Use the following pieces of the grading results and feedback to answer the users question.
# Use the following pieces of context to answer the users question.
# ----------------
# {context}"""
#
# messages = [
#     SystemMessagePromptTemplate.from_template(system_template),
#     HumanMessagePromptTemplate.from_template("{question}"),
# ]
# CHAT_PROMPT = ChatPromptTemplate.from_messages(messages)
#
#
# def get_search_index(embeddings):
#     global vectorstore_index
#     if os.path.isfile(pickle_file) and os.path.isfile(index_file) and os.path.getsize(pickle_file) > 0:
#         # Load index from pickle file
#         search_index = load_index(embeddings)
#     else:
#         search_index = create_index(model)
#         print("Created index")
#
#     vectorstore_index = search_index
#     return search_index
#
#
# def create_index(embeddings):
#     source_chunks = create_chunk_documents()
#     search_index = search_index_from_docs(source_chunks, embeddings)
#     # search_index.persist()
#     FAISS.save_local(search_index, folder_path="vector_stores/", index_name="canvas-discussions")
#     # Save index to pickle file
#     # with open(pickle_file, "wb") as f:
#     #     pickle.dump(search_index, f)
#     return search_index
#
#
# def search_index_from_docs(source_chunks, embeddings):
#     # print("source chunks: " + str(len(source_chunks)))
#     # print("embeddings: " + str(embeddings))
#     search_index = FAISS.from_documents(source_chunks, embeddings)
#     return search_index
#
#
# def get_html_files():
#     loader = DirectoryLoader('docs', glob="**/*.html", loader_cls=UnstructuredHTMLLoader, recursive=True)
#     document_list = loader.load()
#     for document in document_list:
#         document.metadata["name"] = document.metadata["source"].split("/")[-1].split(".")[0]
#     return document_list
#
#
# def get_text_files():
#     loader = DirectoryLoader('docs', glob="**/*.txt", loader_cls=TextLoader, recursive=True)
#     document_list = loader.load()
#     return document_list
#
#
# def create_chunk_documents():
#     sources = fetch_data_for_embeddings()
#
#     splitter = RecursiveCharacterTextSplitter.from_language(
#         language=Language.HTML, chunk_size=500, chunk_overlap=0
#     )
#
#     source_chunks = splitter.split_documents(sources)
#
#     print("chunks: " + str(len(source_chunks)))
#     print("sources: " + str(len(sources)))
#
#     return source_chunks
#
#
# def create_chain(question, llm, embeddings):
#     db = load_index(embeddings)
#
#     # Create chain
#     chain = ConversationalRetrievalChain.from_llm(llm, db.as_retriever(search_type='mmr',
#                                                                        search_kwargs={'lambda_mult': 1, 'fetch_k': 50,
#                                                                                       'k': 30}),
#                                                   return_source_documents=True,
#                                                   verbose=True,
#                                                   memory=ConversationSummaryBufferMemory(memory_key='chat_history',
#                                                                                          llm=llm, max_token_limit=40,
#                                                                                          return_messages=True,
#                                                                                          output_key='answer'),
#                                                   get_chat_history=get_chat_history,
#                                                   combine_docs_chain_kwargs={"prompt": CHAT_PROMPT})
#
#     result = chain({"question": question})
#
#     sources = []
#     print(result)
#
#     for document in result['source_documents']:
#         sources.append("\n" + str(document.metadata))
#         print(sources)
#
#     source = ',\n'.join(set(sources))
#     return result['answer'] + '\nSOURCES: ' + source
#
#
# def load_index(embeddings):
#     # Load index
#     db = FAISS.load_local(
#         folder_path="vector_stores/",
#         index_name="canvas-discussions", embeddings=embeddings,
#     )
#     return db
#
#
# def get_chat_history(inputs) -> str:
#     res = []
#     for human, ai in inputs:
#         res.append(f"Human:{human}\nAI:{ai}")
#     return "\n".join(res)