Updates to the api + refactor

This commit is contained in:
Vasilije 2023-08-25 11:48:43 +02:00
parent 32d3bd026a
commit fd9d4952ee
3 changed files with 71 additions and 29 deletions

View file

@ -24,21 +24,43 @@ Initial code lets you do three operations:
## Usage ## Usage
The fast API endpoint accepts prompts and PDF files and returns a JSON object with the generated text. The fast API endpoint accepts prompts and stores data with the help of the Memory Manager
The types of memory are: Episodic, Semantic, Buffer
Endpoint Overview
The Memory API provides the following endpoints:
- /[memory_type]/add-memory (POST)
- /[memory_type]/fetch-memory (POST)
- /[memory_type]/delete-memory (POST)
- /available-buffer-actions (GET)
- /run-buffer (POST)
- /buffer/create-context (POST)
Here is a payload example:
```curl
-X POST
-F "prompt=The quick brown fox"
-F "file=@/path/to/file.pdf"
http://localhost:8000/upload/
``` ```
{ {
"payload": { "payload": {
"user_id": "681", "user_id": "681",
"session_id": "471", "session_id": "471",
"model_speed": "slow", "model_speed": "slow",
"prompt": "Temperature=Cold;Food Type=Ice Cream", "prompt": "I want ",
"pdf_url": "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" "pdf_url": "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
"params": {
"version": "1.0",
"agreement_id": "AG123456",
"privacy_policy": "https://example.com/privacy",
"terms_of_service": "https://example.com/terms",
"format": "json",
"schema_version": "1.1",
"checksum": "a1b2c3d4e5f6",
"owner": "John Doe",
"license": "MIT",
"validity_start": "2023-08-01",
"validity_end": "2024-07-31"
}
} }
} }
```

View file

@ -242,7 +242,7 @@ async def available_buffer_actions(
await Memory_.async_init() await Memory_.async_init()
# memory_class = getattr(Memory_, f"_delete_{memory_type}_memory", None) # memory_class = getattr(Memory_, f"_delete_{memory_type}_memory", None)
output = Memory_._available_operations() output = await Memory_._available_operations()
return JSONResponse(content={"response": output}, status_code=200) return JSONResponse(content={"response": output}, status_code=200)
except Exception as e: except Exception as e:
@ -263,13 +263,35 @@ async def available_buffer_actions(
await Memory_.async_init() await Memory_.async_init()
# memory_class = getattr(Memory_, f"_delete_{memory_type}_memory", None) # memory_class = getattr(Memory_, f"_delete_{memory_type}_memory", None)
output = Memory_._run_buffer(user_input=decoded_payload['prompt'], params=decoded_payload['params']) output = await Memory_._run_buffer(user_input=decoded_payload['prompt'], params=decoded_payload['params'])
return JSONResponse(content={"response": output}, status_code=200) return JSONResponse(content={"response": output}, status_code=200)
except Exception as e: except Exception as e:
return JSONResponse(content={"response": {"error": str(e)}}, status_code=503) return JSONResponse(content={"response": {"error": str(e)}}, status_code=503)
@app.post("/buffer/create-context", response_model=dict)
async def available_buffer_actions(
payload: Payload,
# files: List[UploadFile] = File(...),
):
try:
decoded_payload = payload.payload
Memory_ = Memory(user_id=decoded_payload['user_id'])
await Memory_.async_init()
# memory_class = getattr(Memory_, f"_delete_{memory_type}_memory", None)
output = await Memory_._create_buffer_context(user_input=decoded_payload['prompt'], params=decoded_payload['params'])
return JSONResponse(content={"response": output}, status_code=200)
except Exception as e:
return JSONResponse(content={"response": {"error": str(e)}}, status_code=503)
# #
# # Process each uploaded PDF file # # Process each uploaded PDF file
# results = [] # results = []

View file

@ -490,17 +490,12 @@ class EpisodicBuffer(BaseMemory):
return ["translate", "structure", "load to database", "load to semantic memory", "load to episodic memory", return ["translate", "structure", "load to database", "load to semantic memory", "load to episodic memory",
"load to buffer"] "load to buffer"]
async def main_buffer(self, user_input=None, content=None, params=None): async def buffer_context(self, user_input=None, content=None, params=None):
"""Generates the context to be used for the buffer and passed to the agent"""
"""AI buffer to understand user PDF query, prioritize memory info and process it based on available operations"""
# we get a list of available operations for our buffer to consider # we get a list of available operations for our buffer to consider
# these operations are what we can do with the data, in the context of PDFs (load, translate, structure, etc) # these operations are what we can do with the data, in the context of PDFs (load, translate, structure, etc)
list_of_operations = await self.available_operations() list_of_operations = await self.available_operations()
memory = Memory(user_id=self.user_id)
await memory.async_init()
try: try:
# we delete all memories in the episodic buffer, so we can start fresh # we delete all memories in the episodic buffer, so we can start fresh
await self.delete_memories() await self.delete_memories()
@ -530,8 +525,6 @@ class EpisodicBuffer(BaseMemory):
context.append(frequency) context.append(frequency)
# fix this so it actually filters # fix this so it actually filters
else: else:
# defaults to semantic search if we don't want to apply algorithms on the vectordb data # defaults to semantic search if we don't want to apply algorithms on the vectordb data
memory = Memory(user_id=self.user_id) memory = Memory(user_id=self.user_id)
@ -616,7 +609,15 @@ class EpisodicBuffer(BaseMemory):
# Extract the list of tasks # Extract the list of tasks
tasks_list = data["tasks"] tasks_list = data["tasks"]
return tasks_list
async def main_buffer(self, user_input=None, content=None, params=None):
"""AI buffer to understand user PDF query, prioritize memory info and process it based on available operations"""
memory = Memory(user_id=self.user_id)
await memory.async_init()
tasks_list = await self.buffer_context(user_input=user_input, content=content, params=params)
result_tasks = [] result_tasks = []
for task in tasks_list: for task in tasks_list:
@ -716,20 +717,13 @@ class EpisodicBuffer(BaseMemory):
) )
_input = prompt.format_prompt(query=user_input, steps=str(tasks_list), buffer=buffer_result) _input = prompt.format_prompt(query=user_input, steps=str(tasks_list), buffer=buffer_result)
#
# print("a few things to do like load episodic memory in a structured format")
#
# return "a few things to do like load episodic memory in a structured format" # return "a few things to do like load episodic memory in a structured format"
output = self.llm_base(_input.to_string()) output = self.llm_base(_input.to_string())
result_parsing = parser.parse(output) result_parsing = parser.parse(output)
print("here is the parsing result", result_parsing) print("here is the parsing result", result_parsing)
#
lookup_value = await memory._add_episodic_memory(observation=str(output), params=params) lookup_value = await memory._add_episodic_memory(observation=str(output), params=params)
# now we clean up buffer memory
await self.delete_memories() await self.delete_memories()
return lookup_value return lookup_value
@ -857,6 +851,10 @@ class Memory:
return await self.short_term_memory.episodic_buffer.delete_memories( return await self.short_term_memory.episodic_buffer.delete_memories(
params=params params=params
) )
async def _create_buffer_context(self, user_input: str, content: str = None, params:str=None):
return await self.short_term_memory.episodic_buffer.buffer_context(
user_input=user_input, content=content, params=params
)
async def _available_operations(self): async def _available_operations(self):
return await self.long_term_memory.episodic_buffer.available_operations() return await self.long_term_memory.episodic_buffer.available_operations()