@@ -20,7 +20,7 @@ def add(self, item: str):
2020 self .items .append (item )
2121 self .vectors = None # Reset vectors to force recalculation
2222
23- def get_relevant (self , query : str , n : int = 5 ) -> List [str ]:
23+ def get_relevant (self , query : str , n : int = 10 ) -> List [str ]:
2424 if not self .items :
2525 return []
2626
@@ -49,26 +49,46 @@ def extract_query(text: str) -> Tuple[str, str]:
4949 query = "What is the main point of this text?"
5050 return query , context
5151
52- def extract_key_information (text : str , client , model : str ) -> List [str ]:
53- # print(f"Prompt : {text}")
54- prompt = f"""Extract key information from the following text. Provide a list of important facts or concepts, each on a new line:
52+ def classify_margin (margin ):
53+ return margin .startswith ("YES#" )
5554
55+ def extract_key_information (system_message , text : str , query : str , client , model : str ) -> List [str ]:
56+ # print(f"Prompt : {text}")
57+ messages = [
58+ {"role" : "system" , "content" : system_message },
59+ {"role" : "user" , "content" : f"""
60+ '''text
5661{ text }
57-
58- Key information:"""
62+ '''
63+ Copy over all context relevant to the query: { query }
64+ Provide the answer in the format: <YES/NO>#<Relevant context>.
65+ Here are rules:
66+ - If you don't know how to answer the query - start your answer with NO#
67+ - If the text is not related to the query - start your answer with NO#
68+ - If you can extract relevant information - start your answer with YES#
69+ - If the text does not mention the person by name - start your answer with NO#
70+ Example answers:
71+ - YES#Western philosophy originated in Ancient Greece in the 6th century BCE with the pre-Socratics.
72+ - NO#No relevant context.
73+ """ }
74+ ]
5975
6076 try :
6177 response = client .chat .completions .create (
6278 model = model ,
63- messages = [{ "role" : "user" , "content" : prompt }] ,
79+ messages = messages ,
6480 max_tokens = 1000
6581 )
66- key_info = response .choices [0 ].message .content .strip (). split ( ' \n ' )
82+ key_info = response .choices [0 ].message .content .strip ()
6783 except Exception as e :
6884 print (f"Error parsing content: { str (e )} " )
6985 return [],0
86+ margins = []
87+
88+ if classify_margin (key_info ):
89+ margins .append (key_info .split ("#" , 1 )[1 ])
7090
71- return [ info . strip ( '- ' ) for info in key_info if info . strip ()] , response .usage .completion_tokens
91+ return margins , response .usage .completion_tokens
7292
7393def run (system_prompt : str , initial_query : str , client , model : str ) -> Tuple [str , int ]:
7494 memory = Memory ()
@@ -80,7 +100,7 @@ def run(system_prompt: str, initial_query: str, client, model: str) -> Tuple[str
80100 for i in range (0 , len (context ), chunk_size ):
81101 chunk = context [i :i + chunk_size ]
82102 # print(f"chunk: {chunk}")
83- key_info , tokens = extract_key_information (chunk , client , model )
103+ key_info , tokens = extract_key_information (system_prompt , chunk , query , client , model )
84104 #print(f"key info: {key_info}")
85105 completion_tokens += tokens
86106 for info in key_info :
@@ -90,16 +110,22 @@ def run(system_prompt: str, initial_query: str, client, model: str) -> Tuple[str
90110 relevant_info = memory .get_relevant (query )
91111 # print(f"relevant_info : {relevant_info}")
92112 # Generate response using relevant information
93- prompt = f"""System: { system_prompt }
94-
95- Context: { ' ' .join (relevant_info )}
96-
113+ messages = [
114+ {"role" : "system" , "content" : system_prompt },
115+ {"role" : "user" , "content" : f"""
116+
117+ I asked my assistant to read and analyse the above content page by page to help you complete this task. These are margin notes left on each page:
118+ '''text
119+ { relevant_info }
120+ '''
121+ Read again the note(s), take a deep breath and answer the query.
97122{ query }
98- """
123+ """ }
124+ ]
99125
100126 response = client .chat .completions .create (
101127 model = model ,
102- messages = [{ "role" : "user" , "content" : prompt }] ,
128+ messages = messages ,
103129 )
104130 # print(f"response : {response}")
105131 final_response = response .choices [0 ].message .content .strip ()
0 commit comments