forked from NirDiamant/Controllable-RAG-Agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions_for_pipeline.py
1183 lines (893 loc) · 47.7 KB
/
functions_for_pipeline.py
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from langchain_openai import ChatOpenAI
# from langchain_groq import ChatGroq
from langchain.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain.prompts import PromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.output_parsers import JsonOutputParser
from langgraph.graph import END, StateGraph
from dotenv import load_dotenv
from pprint import pprint
import os
from typing_extensions import TypedDict
from typing import List, TypedDict
### Helper functions for the notebook
from helper_functions import escape_quotes, text_wrap
"""
Set the environment variables for the API keys.
"""
load_dotenv()
os.environ["PYDEVD_WARN_EVALUATION_TIMEOUT"] = "100000"
os.environ["OPENAI_API_KEY"] = os.getenv('OPENAI_API_KEY')
# groq_api_key = os.getenv('GROQ_API_KEY')
def create_retrievers():
embeddings = OpenAIEmbeddings()
chunks_vector_store = FAISS.load_local("chunks_vector_store", embeddings, allow_dangerous_deserialization=True)
chapter_summaries_vector_store = FAISS.load_local("chapter_summaries_vector_store", embeddings, allow_dangerous_deserialization=True)
book_quotes_vectorstore = FAISS.load_local("book_quotes_vectorstore", embeddings, allow_dangerous_deserialization=True)
chunks_query_retriever = chunks_vector_store.as_retriever(search_kwargs={"k": 1})
chapter_summaries_query_retriever = chapter_summaries_vector_store.as_retriever(search_kwargs={"k": 1})
book_quotes_query_retriever = book_quotes_vectorstore.as_retriever(search_kwargs={"k": 10})
return chunks_query_retriever, chapter_summaries_query_retriever, book_quotes_query_retriever
chunks_query_retriever, chapter_summaries_query_retriever, book_quotes_query_retriever = create_retrievers()
def retrieve_context_per_question(state):
"""
Retrieves relevant context for a given question. The context is retrieved from the book chunks and chapter summaries.
Args:
state: A dictionary containing the question to answer.
"""
# Retrieve relevant documents
print("Retrieving relevant chunks...")
question = state["question"]
docs = chunks_query_retriever.get_relevant_documents(question)
# Concatenate document content
context = " ".join(doc.page_content for doc in docs)
print("Retrieving relevant chapter summaries...")
docs_summaries = chapter_summaries_query_retriever.get_relevant_documents(state["question"])
# Concatenate chapter summaries with citation information
context_summaries = " ".join(
f"{doc.page_content} (Chapter {doc.metadata['chapter']})" for doc in docs_summaries
)
print("Retrieving relevant book quotes...")
docs_book_quotes = book_quotes_query_retriever.get_relevant_documents(state["question"])
book_qoutes = " ".join(doc.page_content for doc in docs_book_quotes)
all_contexts = context + context_summaries + book_qoutes
all_contexts = escape_quotes(all_contexts)
return {"context": all_contexts, "question": question}
def create_keep_only_relevant_content_chain():
keep_only_relevant_content_prompt_template = """you receive a query: {query} and retrieved docuemnts: {retrieved_documents} from a
vector store.
You need to filter out all the non relevant information that don't supply important information regarding the {query}.
your goal is just to filter out the non relevant information.
you can remove parts of sentences that are not relevant to the query or remove whole sentences that are not relevant to the query.
DO NOT ADD ANY NEW INFORMATION THAT IS NOT IN THE RETRIEVED DOCUMENTS.
output the filtered relevant content.
"""
class KeepRelevantContent(BaseModel):
relevant_content: str = Field(description="The relevant content from the retrieved documents that is relevant to the query.")
keep_only_relevant_content_prompt = PromptTemplate(
template=keep_only_relevant_content_prompt_template,
input_variables=["query", "retrieved_documents"],
)
keep_only_relevant_content_llm = ChatOpenAI(temperature=0, model_name="gpt-4o", max_tokens=2000)
keep_only_relevant_content_chain = keep_only_relevant_content_prompt | keep_only_relevant_content_llm.with_structured_output(KeepRelevantContent)
return keep_only_relevant_content_chain
keep_only_relevant_content_chain = create_keep_only_relevant_content_chain()
def keep_only_relevant_content(state):
"""
Keeps only the relevant content from the retrieved documents that is relevant to the query.
Args:
question: The query question.
context: The retrieved documents.
chain: The LLMChain instance.
Returns:
The relevant content from the retrieved documents that is relevant to the query.
"""
question = state["question"]
context = state["context"]
input_data = {
"query": question,
"retrieved_documents": context
}
print("keeping only the relevant content...")
pprint("--------------------")
output = keep_only_relevant_content_chain.invoke(input_data)
relevant_content = output.relevant_content
relevant_content = "".join(relevant_content)
relevant_content = escape_quotes(relevant_content)
return {"relevant_context": relevant_content, "context": context, "question": question}
def create_question_answer_from_context_cot_chain():
class QuestionAnswerFromContext(BaseModel):
answer_based_on_content: str = Field(description="generates an answer to a query based on a given context.")
question_answer_from_context_llm = ChatOpenAI(temperature=0, model_name="gpt-4o", max_tokens=2000)
question_answer_cot_prompt_template = """
Examples of Chain-of-Thought Reasoning
Example 1
Context: Mary is taller than Jane. Jane is shorter than Tom. Tom is the same height as David.
Question: Who is the tallest person?
Reasoning Chain:
The context tells us Mary is taller than Jane
It also says Jane is shorter than Tom
And Tom is the same height as David
So the order from tallest to shortest is: Mary, Tom/David, Jane
Therefore, Mary must be the tallest person
Example 2
Context: Harry was reading a book about magic spells. One spell allowed the caster to turn a person into an animal for a short time. Another spell could levitate objects.
A third spell created a bright light at the end of the caster's wand.
Question: Based on the context, if Harry cast these spells, what could he do?
Reasoning Chain:
The context describes three different magic spells
The first spell allows turning a person into an animal temporarily
The second spell can levitate or float objects
The third spell creates a bright light
If Harry cast these spells, he could turn someone into an animal for a while, make objects float, and create a bright light source
So based on the context, if Harry cast these spells he could transform people, levitate things, and illuminate an area
Instructions.
Example 3
Context: Harry Potter woke up on his birthday to find a present at the end of his bed. He excitedly opened it to reveal a Nimbus 2000 broomstick.
Question: Why did Harry receive a broomstick for his birthday?
Reasoning Chain:
The context states that Harry Potter woke up on his birthday and received a present - a Nimbus 2000 broomstick.
However, the context does not provide any information about why he received that specific present or who gave it to him.
There are no details about Harry's interests, hobbies, or the person who gifted him the broomstick.
Without any additional context about Harry's background or the gift-giver's motivations, there is no way to determine the reason he received a broomstick as a birthday present.
For the question below, provide your answer by first showing your step-by-step reasoning process, breaking down the problem into a chain of thought before arriving at the final answer,
just like in the previous examples.
Context
{context}
Question
{question}
"""
question_answer_from_context_cot_prompt = PromptTemplate(
template=question_answer_cot_prompt_template,
input_variables=["context", "question"],
)
question_answer_from_context_cot_chain = question_answer_from_context_cot_prompt | question_answer_from_context_llm.with_structured_output(QuestionAnswerFromContext)
return question_answer_from_context_cot_chain
question_answer_from_context_cot_chain = create_question_answer_from_context_cot_chain()
def answer_question_from_context(state):
"""
Answers a question from a given context.
Args:
question: The query question.
context: The context to answer the question from.
chain: The LLMChain instance.
Returns:
The answer to the question from the context.
"""
question = state["question"]
context = state["aggregated_context"] if "aggregated_context" in state else state["context"]
input_data = {
"question": question,
"context": context
}
print("Answering the question from the retrieved context...")
output = question_answer_from_context_cot_chain.invoke(input_data)
answer = output.answer_based_on_content
print(f'answer before checking hallucination: {answer}')
return {"answer": answer, "context": context, "question": question}
def create_is_relevant_content_chain():
is_relevant_content_prompt_template = """you receive a query: {query} and a context: {context} retrieved from a vector store.
You need to determine if the document is relevant to the query. """
class Relevance(BaseModel):
is_relevant: bool = Field(description="Whether the document is relevant to the query.")
explanation: str = Field(description="An explanation of why the document is relevant or not.")
# is_relevant_json_parser = JsonOutputParser(pydantic_object=Relevance)
# is_relevant_llm = ChatGroq(temperature=0, model_name="llama3-70b-8192", groq_api_key=groq_api_key, max_tokens=4000)
is_relevant_llm = ChatOpenAI(temperature=0, model_name="gpt-4o", max_tokens=2000)
is_relevant_content_prompt = PromptTemplate(
template=is_relevant_content_prompt_template,
input_variables=["query", "context"],
# partial_variables={"format_instructions": is_relevant_json_parser.get_format_instructions()},
)
is_relevant_content_chain = is_relevant_content_prompt | is_relevant_llm.with_structured_output(Relevance)
return is_relevant_content_chain
is_relevant_content_chain = create_is_relevant_content_chain()
def is_relevant_content(state):
"""
Determines if the document is relevant to the query.
Args:
question: The query question.
context: The context to determine relevance.
"""
question = state["question"]
context = state["context"]
input_data = {
"query": question,
"context": context
}
# Invoke the chain to determine if the document is relevant
output = is_relevant_content_chain.invoke(input_data)
print("Determining if the document is relevant...")
if output["is_relevant"] == True:
print("The document is relevant.")
return "relevant"
else:
print("The document is not relevant.")
return "not relevant"
def create_is_grounded_on_facts_chain():
class is_grounded_on_facts(BaseModel):
"""
Output schema for the rewritten question.
"""
grounded_on_facts: bool = Field(description="Answer is grounded in the facts, 'yes' or 'no'")
is_grounded_on_facts_llm = ChatOpenAI(temperature=0, model_name="gpt-4o", max_tokens=2000)
is_grounded_on_facts_prompt_template = """You are a fact-checker that determines if the given answer {answer} is grounded in the given context {context}
you don't mind if it doesn't make sense, as long as it is grounded in the context.
output a json containing the answer to the question, and appart from the json format don't output any additional text.
"""
is_grounded_on_facts_prompt = PromptTemplate(
template=is_grounded_on_facts_prompt_template,
input_variables=["context", "answer"],
)
is_grounded_on_facts_chain = is_grounded_on_facts_prompt | is_grounded_on_facts_llm.with_structured_output(is_grounded_on_facts)
return is_grounded_on_facts_chain
def create_can_be_answered_chain():
can_be_answered_prompt_template = """You receive a query: {question} and a context: {context}.
You need to determine if the question can be fully answered based on the context."""
class QuestionAnswer(BaseModel):
can_be_answered: bool = Field(description="binary result of whether the question can be fully answered or not")
explanation: str = Field(description="An explanation of why the question can be fully answered or not.")
# can_be_answered_json_parser = JsonOutputParser(pydantic_object=QuestionAnswer)
answer_question_prompt = PromptTemplate(
template=can_be_answered_prompt_template,
input_variables=["question","context"],
# partial_variables={"format_instructions": can_be_answered_json_parser.get_format_instructions()},
)
# can_be_answered_llm = ChatGroq(temperature=0, model_name="llama3-70b-8192", groq_api_key=groq_api_key, max_tokens=4000)
can_be_answered_llm = ChatOpenAI(temperature=0, model_name="gpt-4o", max_tokens=2000)
can_be_answered_chain = answer_question_prompt | can_be_answered_llm.with_structured_output(QuestionAnswer)
return can_be_answered_chain
def create_is_distilled_content_grounded_on_content_chain():
is_distilled_content_grounded_on_content_prompt_template = """you receive some distilled content: {distilled_content} and the original context: {original_context}.
you need to determine if the distilled content is grounded on the original context.
if the distilled content is grounded on the original context, set the grounded field to true.
if the distilled content is not grounded on the original context, set the grounded field to false."""
class IsDistilledContentGroundedOnContent(BaseModel):
grounded: bool = Field(description="Whether the distilled content is grounded on the original context.")
explanation: str = Field(description="An explanation of why the distilled content is or is not grounded on the original context.")
# is_distilled_content_grounded_on_content_json_parser = JsonOutputParser(pydantic_object=IsDistilledContentGroundedOnContent)
is_distilled_content_grounded_on_content_prompt = PromptTemplate(
template=is_distilled_content_grounded_on_content_prompt_template,
input_variables=["distilled_content", "original_context"],
# partial_variables={"format_instructions": is_distilled_content_grounded_on_content_json_parser.get_format_instructions()},
)
# is_distilled_content_grounded_on_content_llm = ChatGroq(temperature=0, model_name="llama3-70b-8192", groq_api_key=groq_api_key, max_tokens=4000)
is_distilled_content_grounded_on_content_llm =ChatOpenAI(temperature=0, model_name="gpt-4o", max_tokens=2000)
is_distilled_content_grounded_on_content_chain = is_distilled_content_grounded_on_content_prompt | is_distilled_content_grounded_on_content_llm.with_structured_output(IsDistilledContentGroundedOnContent)
return is_distilled_content_grounded_on_content_chain
is_distilled_content_grounded_on_content_chain = create_is_distilled_content_grounded_on_content_chain()
def is_distilled_content_grounded_on_content(state):
pprint("--------------------")
"""
Determines if the distilled content is grounded on the original context.
Args:
distilled_content: The distilled content.
original_context: The original context.
Returns:
Whether the distilled content is grounded on the original context.
"""
print("Determining if the distilled content is grounded on the original context...")
distilled_content = state["relevant_context"]
original_context = state["context"]
input_data = {
"distilled_content": distilled_content,
"original_context": original_context
}
output = is_distilled_content_grounded_on_content_chain.invoke(input_data)
grounded = output.grounded
if grounded:
print("The distilled content is grounded on the original context.")
return "grounded on the original context"
else:
print("The distilled content is not grounded on the original context.")
return "not grounded on the original context"
def retrieve_chunks_context_per_question(state):
"""
Retrieves relevant context for a given question. The context is retrieved from the book chunks and chapter summaries.
Args:
state: A dictionary containing the question to answer.
"""
# Retrieve relevant documents
print("Retrieving relevant chunks...")
question = state["question"]
docs = chunks_query_retriever.get_relevant_documents(question)
# Concatenate document content
context = " ".join(doc.page_content for doc in docs)
context = escape_quotes(context)
return {"context": context, "question": question}
def retrieve_summaries_context_per_question(state):
print("Retrieving relevant chapter summaries...")
question = state["question"]
docs_summaries = chapter_summaries_query_retriever.get_relevant_documents(state["question"])
# Concatenate chapter summaries with citation information
context_summaries = " ".join(
f"{doc.page_content} (Chapter {doc.metadata['chapter']})" for doc in docs_summaries
)
context_summaries = escape_quotes(context_summaries)
return {"context": context_summaries, "question": question}
def retrieve_book_quotes_context_per_question(state):
question = state["question"]
print("Retrieving relevant book quotes...")
docs_book_quotes = book_quotes_query_retriever.get_relevant_documents(state["question"])
book_qoutes = " ".join(doc.page_content for doc in docs_book_quotes)
book_qoutes_context = escape_quotes(book_qoutes)
return {"context": book_qoutes_context, "question": question}
class QualitativeRetrievalGraphState(TypedDict):
"""
Represents the state of our graph.
"""
question: str
context: str
relevant_context: str
def create_qualitative_retrieval_book_chunks_workflow_app():
qualitative_chunks_retrieval_workflow = StateGraph(QualitativeRetrievalGraphState)
# Define the nodes
qualitative_chunks_retrieval_workflow.add_node("retrieve_chunks_context_per_question",retrieve_chunks_context_per_question)
qualitative_chunks_retrieval_workflow.add_node("keep_only_relevant_content",keep_only_relevant_content)
# Build the graph
qualitative_chunks_retrieval_workflow.set_entry_point("retrieve_chunks_context_per_question")
qualitative_chunks_retrieval_workflow.add_edge("retrieve_chunks_context_per_question", "keep_only_relevant_content")
qualitative_chunks_retrieval_workflow.add_conditional_edges(
"keep_only_relevant_content",
is_distilled_content_grounded_on_content,
{"grounded on the original context":END,
"not grounded on the original context":"keep_only_relevant_content"},
)
qualitative_chunks_retrieval_workflow_app = qualitative_chunks_retrieval_workflow.compile()
return qualitative_chunks_retrieval_workflow_app
def create_qualitative_retrieval_chapter_summaries_workflow_app():
qualitative_summaries_retrieval_workflow = StateGraph(QualitativeRetrievalGraphState)
# Define the nodes
qualitative_summaries_retrieval_workflow.add_node("retrieve_summaries_context_per_question",retrieve_summaries_context_per_question)
qualitative_summaries_retrieval_workflow.add_node("keep_only_relevant_content",keep_only_relevant_content)
# Build the graph
qualitative_summaries_retrieval_workflow.set_entry_point("retrieve_summaries_context_per_question")
qualitative_summaries_retrieval_workflow.add_edge("retrieve_summaries_context_per_question", "keep_only_relevant_content")
qualitative_summaries_retrieval_workflow.add_conditional_edges(
"keep_only_relevant_content",
is_distilled_content_grounded_on_content,
{"grounded on the original context":END,
"not grounded on the original context":"keep_only_relevant_content"},
)
qualitative_summaries_retrieval_workflow_app = qualitative_summaries_retrieval_workflow.compile()
return qualitative_summaries_retrieval_workflow_app
def create_qualitative_book_quotes_retrieval_workflow_app():
qualitative_book_quotes_retrieval_workflow = StateGraph(QualitativeRetrievalGraphState)
# Define the nodes
qualitative_book_quotes_retrieval_workflow.add_node("retrieve_book_quotes_context_per_question",retrieve_book_quotes_context_per_question)
qualitative_book_quotes_retrieval_workflow.add_node("keep_only_relevant_content",keep_only_relevant_content)
# Build the graph
qualitative_book_quotes_retrieval_workflow.set_entry_point("retrieve_book_quotes_context_per_question")
qualitative_book_quotes_retrieval_workflow.add_edge("retrieve_book_quotes_context_per_question", "keep_only_relevant_content")
qualitative_book_quotes_retrieval_workflow.add_conditional_edges(
"keep_only_relevant_content",
is_distilled_content_grounded_on_content,
{"grounded on the original context":END,
"not grounded on the original context":"keep_only_relevant_content"},
)
qualitative_book_quotes_retrieval_workflow_app = qualitative_book_quotes_retrieval_workflow.compile()
return qualitative_book_quotes_retrieval_workflow_app
is_grounded_on_facts_chain = create_is_grounded_on_facts_chain()
def is_answer_grounded_on_context(state):
"""Determines if the answer to the question is grounded in the facts.
Args:
state: A dictionary containing the context and answer.
"""
print("Checking if the answer is grounded in the facts...")
context = state["context"]
answer = state["answer"]
result = is_grounded_on_facts_chain.invoke({"context": context, "answer": answer})
grounded_on_facts = result.grounded_on_facts
if not grounded_on_facts:
print("The answer is hallucination.")
return "hallucination"
else:
print("The answer is grounded in the facts.")
return "grounded on context"
def create_qualitative_answer_workflow_app():
class QualitativeAnswerGraphState(TypedDict):
"""
Represents the state of our graph.
"""
question: str
context: str
answer: str
qualitative_answer_workflow = StateGraph(QualitativeAnswerGraphState)
# Define the nodes
qualitative_answer_workflow.add_node("answer_question_from_context",answer_question_from_context)
# Build the graph
qualitative_answer_workflow.set_entry_point("answer_question_from_context")
qualitative_answer_workflow.add_conditional_edges(
"answer_question_from_context",is_answer_grounded_on_context ,{"hallucination":"answer_question_from_context", "grounded on context":END}
)
qualitative_answer_workflow_app = qualitative_answer_workflow.compile()
return qualitative_answer_workflow_app
class PlanExecute(TypedDict):
curr_state: str
question: str
anonymized_question: str
query_to_retrieve_or_answer: str
plan: List[str]
past_steps: List[str]
mapping: dict
curr_context: str
aggregated_context: str
tool: str
response: str
class Plan(BaseModel):
"""Plan to follow in future"""
steps: List[str] = Field(
description="different steps to follow, should be in sorted order"
)
def create_plan_chain():
planner_prompt =""" For the given query {question}, come up with a simple step by step plan of how to figure out the answer.
This plan should involve individual tasks, that if executed correctly will yield the correct answer. Do not add any superfluous steps.
The result of the final step should be the final answer. Make sure that each step has all the information needed - do not skip steps.
"""
planner_prompt = PromptTemplate(
template=planner_prompt,
input_variables=["question"],
)
planner_llm = ChatOpenAI(temperature=0, model_name="gpt-4o", max_tokens=2000)
planner = planner_prompt | planner_llm.with_structured_output(Plan)
return planner
def create_break_down_plan_chain():
break_down_plan_prompt_template = """You receive a plan {plan} which contains a series of steps to follow in order to answer a query.
you need to go through the plan refine it according to this:
1. every step has to be able to be executed by either:
i. retrieving relevant information from a vector store of book chunks
ii. retrieving relevant information from a vector store of chapter summaries
iii. retrieving relevant information from a vector store of book quotes
iv. answering a question from a given context.
2. every step should contain all the information needed to execute it.
output the refined plan
"""
break_down_plan_prompt = PromptTemplate(
template=break_down_plan_prompt_template,
input_variables=["plan"],
)
break_down_plan_llm = ChatOpenAI(temperature=0, model_name="gpt-4o", max_tokens=2000)
break_down_plan_chain = break_down_plan_prompt | break_down_plan_llm.with_structured_output(Plan)
return break_down_plan_chain
def create_replanner_chain():
# class ActPossibleResults(BaseModel):
# """Possible results of the action."""
# plan: Plan = Field(description="Plan to follow in future.")
# explanation: str = Field(description="Explanation of the action.")
# act_possible_results_parser = JsonOutputParser(pydantic_object=ActPossibleResults)
replanner_prompt_template =""" For the given objective, come up with a simple step by step plan of how to figure out the answer.
This plan should involve individual tasks, that if executed correctly will yield the correct answer. Do not add any superfluous steps.
The result of the final step should be the final answer. Make sure that each step has all the information needed - do not skip steps.
assume that the answer was not found yet and you need to update the plan accordingly, so the plan should never be empty.
Your objective was this:
{question}
Your original plan was this:
{plan}
You have currently done the follow steps:
{past_steps}
You already have the following context:
{aggregated_context}
Update your plan accordingly. If further steps are needed, fill out the plan with only those steps.
Do not return previously done steps as part of the plan.
the format is json so escape quotes and new lines.
"""
replanner_prompt = PromptTemplate(
template=replanner_prompt_template,
input_variables=["question", "plan", "past_steps", "aggregated_context"],
# partial_variables={"format_instructions": act_possible_results_parser.get_format_instructions()},
)
replanner_llm = ChatOpenAI(temperature=0, model_name="gpt-4o", max_tokens=2000)
replanner = replanner_prompt | replanner_llm.with_structured_output(Plan)
return replanner
def create_task_handler_chain():
tasks_handler_prompt_template = """You are a task handler that receives a task {curr_task} and have to decide with tool to use to execute the task.
You have the following tools at your disposal:
Tool A: a tool that retrieves relevant information from a vector store of book chunks based on a given query.
- use Tool A when you think the current task should search for information in the book chunks.
Took B: a tool that retrieves relevant information from a vector store of chapter summaries based on a given query.
- use Tool B when you think the current task should search for information in the chapter summaries.
Tool C: a tool that retrieves relevant information from a vector store of quotes from the book based on a given query.
- use Tool C when you think the current task should search for information in the book quotes.
Tool D: a tool that answers a question from a given context.
- use Tool D ONLY when you the current task can be answered by the aggregated context {aggregated_context}
you also receive the last tool used {last_tool}
if {last_tool} was retrieve_chunks, use other tools than Tool A.
You also have the past steps {past_steps} that you can use to make decisions and understand the context of the task.
You also have the initial user's question {question} that you can use to make decisions and understand the context of the task.
if you decide to use Tools A,B or C, output the query to be used for the tool and also output the relevant tool.
if you decide to use Tool D, output the question to be used for the tool, the context, and also that the tool to be used is Tool D.
"""
class TaskHandlerOutput(BaseModel):
"""Output schema for the task handler."""
query: str = Field(description="The query to be either retrieved from the vector store, or the question that should be answered from context.")
curr_context: str = Field(description="The context to be based on in order to answer the query.")
tool: str = Field(description="The tool to be used should be either retrieve_chunks, retrieve_summaries, retrieve_quotes, or answer_from_context.")
task_handler_prompt = PromptTemplate(
template=tasks_handler_prompt_template,
input_variables=["curr_task", "aggregated_context", "last_tool" "past_steps", "question"],
)
task_handler_llm = ChatOpenAI(temperature=0, model_name="gpt-4o", max_tokens=2000)
task_handler_chain = task_handler_prompt | task_handler_llm.with_structured_output(TaskHandlerOutput)
return task_handler_chain
def create_anonymize_question_chain():
class AnonymizeQuestion(BaseModel):
"""Anonymized question and mapping."""
anonymized_question : str = Field(description="Anonymized question.")
mapping: dict = Field(description="Mapping of original name entities to variables.")
explanation: str = Field(description="Explanation of the action.")
anonymize_question_parser = JsonOutputParser(pydantic_object=AnonymizeQuestion)
anonymize_question_prompt_template = """ You are a question anonymizer. The input You receive is a string containing several words that
construct a question {question}. Your goal is to changes all name entities in the input to variables, and remember the mapping of the original name entities to the variables.
```example1:
if the input is \"who is harry potter?\" the output should be \"who is X?\" and the mapping should be {{\"X\": \"harry potter\"}} ```
```example2:
if the input is \"how did the bad guy played with the alex and rony?\"
the output should be \"how did the X played with the Y and Z?\" and the mapping should be {{\"X\": \"bad guy\", \"Y\": \"alex\", \"Z\": \"rony\"}}```
you must replace all name entities in the input with variables, and remember the mapping of the original name entities to the variables.
output the anonymized question and the mapping as two separate fields in a json format as described here, without any additional text apart from the json format.
"""
anonymize_question_prompt = PromptTemplate(
template=anonymize_question_prompt_template,
input_variables=["question"],
partial_variables={"format_instructions": anonymize_question_parser.get_format_instructions()},
)
anonymize_question_llm = ChatOpenAI(temperature=0, model_name="gpt-4o", max_tokens=2000)
anonymize_question_chain = anonymize_question_prompt | anonymize_question_llm | anonymize_question_parser
return anonymize_question_chain
def create_deanonymize_plan_chain():
class DeAnonymizePlan(BaseModel):
"""Possible results of the action."""
plan: List = Field(description="Plan to follow in future. with all the variables replaced with the mapped words.")
de_anonymize_plan_prompt_template = """ you receive a list of tasks: {plan}, where some of the words are replaced with mapped variables. you also receive
the mapping for those variables to words {mapping}. replace all the variables in the list of tasks with the mapped words. if no variables are present,
return the original list of tasks. in any case, just output the updated list of tasks in a json format as described here, without any additional text apart from the
"""
de_anonymize_plan_prompt = PromptTemplate(
template=de_anonymize_plan_prompt_template,
input_variables=["plan", "mapping"],
)
de_anonymize_plan_llm = ChatOpenAI(temperature=0, model_name="gpt-4o", max_tokens=2000)
de_anonymize_plan_chain = de_anonymize_plan_prompt | de_anonymize_plan_llm.with_structured_output(DeAnonymizePlan)
return de_anonymize_plan_chain
def create_can_be_answered_already_chain():
class CanBeAnsweredAlready(BaseModel):
"""Possible results of the action."""
can_be_answered: bool = Field(description="Whether the question can be fully answered or not based on the given context.")
can_be_answered_already_prompt_template = """You receive a query: {question} and a context: {context}.
You need to determine if the question can be fully answered relying only the given context.
The only infomation you have and can rely on is the context you received.
you have no prior knowledge of the question or the context.
if you think the question can be answered based on the context, output 'true', otherwise output 'false'.
"""
can_be_answered_already_prompt = PromptTemplate(
template=can_be_answered_already_prompt_template,
input_variables=["question","context"],
)
can_be_answered_already_llm = ChatOpenAI(temperature=0, model_name="gpt-4o", max_tokens=2000)
can_be_answered_already_chain = can_be_answered_already_prompt | can_be_answered_already_llm.with_structured_output(CanBeAnsweredAlready)
return can_be_answered_already_chain
task_handler_chain = create_task_handler_chain()
qualitative_chunks_retrieval_workflow_app = create_qualitative_retrieval_book_chunks_workflow_app()
qualitative_summaries_retrieval_workflow_app = create_qualitative_retrieval_chapter_summaries_workflow_app()
qualitative_book_quotes_retrieval_workflow_app = create_qualitative_book_quotes_retrieval_workflow_app()
qualitative_answer_workflow_app = create_qualitative_answer_workflow_app()
de_anonymize_plan_chain = create_deanonymize_plan_chain()
planner = create_plan_chain()
break_down_plan_chain = create_break_down_plan_chain()
replanner = create_replanner_chain()
anonymize_question_chain = create_anonymize_question_chain()
can_be_answered_already_chain = create_can_be_answered_already_chain()
def run_task_handler_chain(state: PlanExecute):
""" Run the task handler chain to decide which tool to use to execute the task.
Args:
state: The current state of the plan execution.
Returns:
The updated state of the plan execution.
"""
state["curr_state"] = "task_handler"
print("the current plan is:")
print(state["plan"])
pprint("--------------------")
if not state['past_steps']:
state["past_steps"] = []
curr_task = state["plan"][0]
inputs = {"curr_task": curr_task,
"aggregated_context": state["aggregated_context"],
"last_tool": state["tool"],
"past_steps": state["past_steps"],
"question": state["question"]}
output = task_handler_chain.invoke(inputs)
state["past_steps"].append(curr_task)
state["plan"].pop(0)
if output.tool == "retrieve_chunks":
state["query_to_retrieve_or_answer"] = output.query
state["tool"]="retrieve_chunks"
elif output.tool == "retrieve_summaries":
state["query_to_retrieve_or_answer"] = output.query
state["tool"]="retrieve_summaries"
elif output.tool == "retrieve_quotes":
state["query_to_retrieve_or_answer"] = output.query
state["tool"]="retrieve_quotes"
elif output.tool == "answer_from_context":
state["query_to_retrieve_or_answer"] = output.query
state["curr_context"] = output.curr_context
state["tool"]="answer"
else:
raise ValueError("Invalid tool was outputed. Must be either 'retrieve' or 'answer_from_context'")
return state
def retrieve_or_answer(state: PlanExecute):
"""Decide whether to retrieve or answer the question based on the current state.
Args:
state: The current state of the plan execution.
Returns:
updates the tool to use .
"""
state["curr_state"] = "decide_tool"
print("deciding whether to retrieve or answer")
if state["tool"] == "retrieve_chunks":
return "chosen_tool_is_retrieve_chunks"
elif state["tool"] == "retrieve_summaries":
return "chosen_tool_is_retrieve_summaries"
elif state["tool"] == "retrieve_quotes":
return "chosen_tool_is_retrieve_quotes"
elif state["tool"] == "answer":
return "chosen_tool_is_answer"
else:
raise ValueError("Invalid tool was outputed. Must be either 'retrieve' or 'answer_from_context'")
def run_qualitative_chunks_retrieval_workflow(state):
"""
Run the qualitative chunks retrieval workflow.
Args:
state: The current state of the plan execution.
Returns:
The state with the updated aggregated context.
"""
state["curr_state"] = "retrieve_chunks"
print("Running the qualitative chunks retrieval workflow...")
question = state["query_to_retrieve_or_answer"]
inputs = {"question": question}
for output in qualitative_chunks_retrieval_workflow_app.stream(inputs):
for _, _ in output.items():
pass
pprint("--------------------")
if not state["aggregated_context"]:
state["aggregated_context"] = ""
state["aggregated_context"] += output['relevant_context']
return state
def run_qualitative_summaries_retrieval_workflow(state):
"""
Run the qualitative summaries retrieval workflow.
Args:
state: The current state of the plan execution.
Returns:
The state with the updated aggregated context.
"""
state["curr_state"] = "retrieve_summaries"
print("Running the qualitative summaries retrieval workflow...")
question = state["query_to_retrieve_or_answer"]
inputs = {"question": question}
for output in qualitative_summaries_retrieval_workflow_app.stream(inputs):
for _, _ in output.items():
pass
pprint("--------------------")
if not state["aggregated_context"]:
state["aggregated_context"] = ""
state["aggregated_context"] += output['relevant_context']
return state
def run_qualitative_book_quotes_retrieval_workflow(state):
"""
Run the qualitative book quotes retrieval workflow.
Args:
state: The current state of the plan execution.
Returns:
The state with the updated aggregated context.
"""
state["curr_state"] = "retrieve_book_quotes"
print("Running the qualitative book quotes retrieval workflow...")
question = state["query_to_retrieve_or_answer"]
inputs = {"question": question}
for output in qualitative_book_quotes_retrieval_workflow_app.stream(inputs):
for _, _ in output.items():
pass
pprint("--------------------")
if not state["aggregated_context"]:
state["aggregated_context"] = ""
state["aggregated_context"] += output['relevant_context']
return state
def run_qualtative_answer_workflow(state):
"""
Run the qualitative answer workflow.
Args:
state: The current state of the plan execution.
Returns:
The state with the updated aggregated context.
"""
state["curr_state"] = "answer"
print("Running the qualitative answer workflow...")
question = state["query_to_retrieve_or_answer"]
context = state["curr_context"]
inputs = {"question": question, "context": context}
for output in qualitative_answer_workflow_app.stream(inputs):
for _, _ in output.items():
pass
pprint("--------------------")
if not state["aggregated_context"]:
state["aggregated_context"] = ""
state["aggregated_context"] += output["answer"]
return state
def run_qualtative_answer_workflow_for_final_answer(state):
"""
Run the qualitative answer workflow for the final answer.
Args:
state: The current state of the plan execution.
Returns:
The state with the updated response.
"""
state["curr_state"] = "get_final_answer"
print("Running the qualitative answer workflow for final answer...")
question = state["question"]
context = state["aggregated_context"]
inputs = {"question": question, "context": context}
for output in qualitative_answer_workflow_app.stream(inputs):
for _, value in output.items():
pass
pprint("--------------------")
state["response"] = value
return state
def anonymize_queries(state: PlanExecute):
"""
Anonymizes the question.
Args:
state: The current state of the plan execution.
Returns:
The updated state with the anonymized question and mapping.
"""
state["curr_state"] = "anonymize_question"
print("state['question']: ", state['question'])
print("Anonymizing question")
pprint("--------------------")
input_values = {"question": state['question']}
anonymized_question_output = anonymize_question_chain.invoke(input_values)
print(f'anonymized_question_output: {anonymized_question_output}')
anonymized_question = anonymized_question_output["anonymized_question"]
print(f'anonimized_querry: {anonymized_question}')