From 4bfea2e328ace740dedcf5243d9c1e146aab9bf4 Mon Sep 17 00:00:00 2001 From: Vasilije <8619304+Vasilije1990@users.noreply.github.com> Date: Tue, 12 Sep 2023 23:26:18 +0200 Subject: [PATCH] Fixed context, added episodic event analysis --- .../level_2_pdf_vectorstore__dlt_contracts.py | 87 ++++++++----------- level_2/utils.py | 2 +- 2 files changed, 35 insertions(+), 54 deletions(-) diff --git a/level_2/level_2_pdf_vectorstore__dlt_contracts.py b/level_2/level_2_pdf_vectorstore__dlt_contracts.py index ba86dfc2e..4c042355a 100644 --- a/level_2/level_2_pdf_vectorstore__dlt_contracts.py +++ b/level_2/level_2_pdf_vectorstore__dlt_contracts.py @@ -362,9 +362,9 @@ class EpisodicBuffer(BaseMemory): """Determines what operations are available for the user to process PDFs""" return [ - "translate", - "structure", - "fetch from vector store" + "retrieve over time", + "save to personal notes", + "translate to german" # "load to semantic memory", # "load to episodic memory", # "load to buffer", @@ -594,6 +594,8 @@ class EpisodicBuffer(BaseMemory): episodic_context = await chain.arun(input=prompt_filter_chunk, verbose=True) print(cb) + print("HERE IS THE EPISODIC CONTEXT", episodic_context) + class BufferModulators(BaseModel): attention_modulators: Dict[str, float] = Field(... , description="Attention modulators") @@ -728,55 +730,6 @@ class EpisodicBuffer(BaseMemory): complete_agent_prompt= f" Document context is: {document_from_vectorstore} \n Task is : {task['task_order']} {task['task_name']} {task['operation']} " # task['vector_store_context_results']=document_context_result_parsed.dict() - class PromptWrapper(BaseModel): - observation: str = Field( - description="observation we want to fetch from vectordb" - ) - - @tool( - "convert_to_structured", args_schema=PromptWrapper, return_direct=True - ) - def convert_to_structured(observation=None, json_schema=None): - """Convert unstructured data to structured data""" - BASE_DIR = os.getcwd() - json_path = os.path.join( - BASE_DIR, "schema_registry", "ticket_schema.json" - ) - - def load_json_or_infer_schema(file_path, document_path): - """Load JSON schema from file or infer schema from text""" - - # Attempt to load the JSON file - with open(file_path, "r") as file: - json_schema = json.load(file) - return json_schema - - json_schema = load_json_or_infer_schema(json_path, None) - - def run_open_ai_mapper(observation=None, json_schema=None): - """Convert unstructured data to structured data""" - - prompt_msgs = [ - SystemMessage( - content="You are a world class algorithm converting unstructured data into structured data." - ), - HumanMessage( - content="Convert unstructured data to structured data:" - ), - HumanMessagePromptTemplate.from_template("{input}"), - HumanMessage( - content="Tips: Make sure to answer in the correct format" - ), - ] - prompt_ = ChatPromptTemplate(messages=prompt_msgs) - chain_funct = create_structured_output_chain( - json_schema, prompt=prompt_, llm=self.llm, verbose=True - ) - output = chain_funct.run(input=observation, llm=self.llm) - return output - - result = run_open_ai_mapper(observation, json_schema) - return result class FetchText(BaseModel): observation: str = Field(description="observation we want to translate") @@ -789,6 +742,34 @@ class EpisodicBuffer(BaseMemory): out = self.fetch_memories(observation['original_query'], namespace="SEMANTICMEMORY") return out + @tool("retrieve_from_memories", args_schema=FetchText, return_direct=True) + def retrieve_from_memories(observation, args_schema=FetchText): + """Retrieve from episodic memory if data doesn't exist in the context""" + + new_observations = [] + observation = self.fetch_memories(observation['original_query'], namespace="EPISODICMEMORY") + + for memory in observation: + + unix_t = memory["data"]["Get"]["EPISODICMEMORY"][0]["_additional"][ + "lastUpdateTimeUnix" + ] + + # Convert Unix timestamp to datetime + last_update_datetime = datetime.fromtimestamp(int(unix_t) / 1000) + time_difference = datetime.now() - last_update_datetime + time_difference_text = humanize.naturaltime(time_difference) + # Append the time difference to the memory + memory["time_difference"] = str(time_difference_text) + #patch the memory + #retrieve again then + + # Append the modified memory to the new list + new_observations.append(memory) + + + + class TranslateText(BaseModel): observation: str = Field(description="observation we want to translate") @@ -802,7 +783,7 @@ class EpisodicBuffer(BaseMemory): agent = initialize_agent( llm=self.llm, - tools=[fetch_from_vector_store,translate_to_de, convert_to_structured], + tools=[fetch_from_vector_store,translate_to_de], agent=AgentType.OPENAI_FUNCTIONS, verbose=True, ) diff --git a/level_2/utils.py b/level_2/utils.py index 0baa766d7..1f08fab0d 100644 --- a/level_2/utils.py +++ b/level_2/utils.py @@ -146,7 +146,7 @@ async def main(): modulator = {"relevance": 1.0, "saliency": 1.0, "frequency": 1.0, "freshness": 1.0, "repetition": 1.0} user_input = "I want to know how does Buck adapt to life in the wild" # tasks_list = """tasks": [{"task_order": "1", "task_name": "Fetch Information", "operation": "fetch from vector store", "original_query": "I want to know how does Buck adapt to life in the wild"]""" - out_tasks = """here are the result_tasks [{'task_order': '1', 'task_name': 'Fetch Information', 'operation': 'fetch from vector store', 'original_query': 'I want to know how does Buck adapt to life in the wild'}, {'docs': [{'semantic_search_term': "Buck's adaptation to wild life", 'document_content': 'THE CALL OF THE WILD 30 \nout of his desire for mastery. He was preëminently cunning, and could \nbide his time with a patience that was nothing less than primitive. \nIt was inevitable that the clash for leadership should come. Buck \nwanted it. He wanted it because it was his nature, because he had been \ngripped tight by that nameless, incomprehensible pride of the trail and \ntrace—that pride which holds dogs in the toil to the last gasp, which \nlures them to die joyfully in the harness, and breaks their hearts if they \nare cut out of the harness. This was the pride of Dave as wheel-dog, of \nSol-leks as he pulled with all his strength; the pride that laid hold of \nthem at break of camp, transforming them from sour and sullen brutes \ninto straining, eager, ambitious creatures; the pride that spurred them on \nall day and dropped them at pitch of camp at night, letting them fall back \ninto gloomy unrest and uncontent. This was the pride that bore up Spitz \nand made him thrash the sled-dogs who blundered and shirked in the \ntraces or hid away at harness-up time in the morning. Likewise it was \nthis pride that made him fear Buck as a possible lead-dog. And this was \nBuck’s pride, too. \nHe openly threatened the other’s leadership. He came between him \nand the shirks he should have punished. And he did it deliberately. One \nnight there was a heavy snowfall, and in the morning Pike, the \nmalingerer, did not appear. He was securely hidden in his nest under a \nfoot of snow. François called him and sought him in vain. Spitz was wild \nwith wrath. He raged through the camp, smelling and digging in every \nlikely place, snarling so frightfully that Pike heard and shivered in his \nhiding-place. \nBut when he was at last unearthed, and Spitz flew at him to punish \nhim, Buck flew, with equal rage, in between. So unexpected was it, and \nso shrewdly managed, that Spitz was hurled backward and off his feet. \nPike, who had been trembling abjectly, took heart at this open mutiny, \nand sprang upon his overthrown leader. Buck, to whom fairplay was a \nforgotten code, likewise sprang upon Spitz. But François, chuckling at \nthe incident while unswerving in the administration of justice, brought \nhis lash down upon Buck with all his might. This failed to drive Buck \nfrom his prostrate rival, and the butt of the whip was brought into play. \nHalf-stunned by the blow, Buck was knocked backward and the lash laid', 'document_relevance': '0.75', 'attention_modulators_list': [{'frequency': 'High', 'saliency': 'High', 'relevance': 'High'}]}], 'user_query': 'I want to know how does Buck adapt to life in the wild and then have that info translated to german'}, {'task_order': '2', 'task_name': 'Translate Information', 'operation': 'translate', 'original_query': 'then have that info translated to german'}, 'DER RUF DER WILDNIS 30\naus seinem Wunsch nach Meisterschaft. Er war überaus schlau und konnte es\nwartete seine Zeit mit einer Geduld ab, die geradezu primitiv war.\nEs war unvermeidlich, dass es zu einem Kampf um die Führung kam. Bock\nwollte es. Er wollte es, weil es in seiner Natur lag, weil er es gewesen war\nfestgehalten von diesem namenlosen, unverständlichen Stolz des Weges und\nSpur – dieser Stolz, der Hunde bis zum letzten Atemzug in der Mühsal hält, der\nlockt sie dazu, freudig im Geschirr zu sterben, und bricht ihnen das Herz, wenn sie es tun\nwerden aus dem Kabelbaum herausgeschnitten. Das war der Stolz von Dave als Radhund\nSol-leks, als er mit aller Kraft zog; der Stolz, der mich ergriff\nsie beim Abbruch des Lagers und verwandelte sie in mürrische und mürrische Bestien\nin anstrengende, eifrige, ehrgeizige Wesen; der Stolz, der sie anspornte\nden ganzen Tag und setzte sie nachts auf dem Stellplatz des Lagers ab und ließ sie zurückfallen\nin düstere Unruhe und Unzufriedenheit. Das war der Stolz, der Spitz trug\nund ließ ihn die Schlittenhunde verprügeln, die in der Gegend herumstolperten und sich scheuten\nSpuren hinterlassen oder sich morgens beim Angurten versteckt haben. Ebenso war es\nDieser Stolz ließ ihn Buck als möglichen Leithund fürchten. Und das war\nAuch Bucks Stolz.\nEr bedrohte offen die Führung des anderen. Er kam zwischen ihn\nund die Schirks hätte er bestrafen sollen. Und er hat es absichtlich getan. Eins\nNachts gab es starken Schneefall und am Morgen Pike, der\nSimulant, erschien nicht. Er war sicher in seinem Nest unter einer Decke versteckt\nFuß Schnee. François rief ihn an und suchte ihn vergeblich. Spitz war wild\nmit Zorn. Er tobte durch das Lager, schnupperte und wühlte darin herum\nWahrscheinlicher Ort, er knurrte so schrecklich, dass Pike es hörte und in seinem Kopf zitterte\nVersteck.\nAber als er endlich ausgegraben wurde, flog Spitz auf ihn zu, um ihn zu bestrafen\nihm, Buck flog mit der gleichen Wut dazwischen. So unerwartet war es, und\nEs gelang ihm so geschickt, dass Spitz nach hinten geschleudert wurde und von den Füßen fiel.\nPike, der erbärmlich gezittert hatte, fasste angesichts dieser offenen Meuterei Mut.\nund sprang auf seinen gestürzten Anführer. Buck, für den Fairplay wichtig war\nvergessener Code, sprang ebenfalls Spitz auf. Aber François kicherte\nder Vorfall, während unerschütterlich in der Rechtspflege, gebracht\nEr schlug mit aller Kraft auf Buck ein. Das gelang Buck nicht\nvon seinem am Boden liegenden Rivalen, und der Peitschenkolben wurde ins Spiel gebracht.\nVon dem Schlag halb betäubt, wurde Buck nach hinten geschleudert und mit der Peitsche niedergeschlagen']""" + out_tasks = """here are the result_tasks [{'task_order': '1', 'task_name': 'Save Information', 'operation': 'save to vector store', 'original_query': 'Add to notes who is Buck and get info saved yesterday about him'}, {'docs': [{'semantic_search_term': "Add to notes who is Buck", 'document_summary': 'Buck was a dog stolen from his home', 'document_relevance': '0.75', 'attention_modulators_list': [{'frequency': '0.33', 'saliency': '0.75', 'relevance': '0.74'}]}], 'user_query': 'I want to know who buck is and check my notes from yesterday'}, {'task_order': '2', 'task_name': 'Check historical data', 'operation': 'check historical data', 'original_query': ' check my notes from yesterday'}, ' Data saved yesterday about Buck include informaton that he was stolen from home and that he was a pretty dog ']""" await _add_to_episodic(user_input=user_input, result_tasks=out_tasks, tasks_list=None, attention_modulators=modulator, params=params) # await delete_from_episodic()