Skip to content

Commit cbaa264

Browse files
committed
testing vector db and rag
1 parent e8ca757 commit cbaa264

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

hissmed_literature_assistant.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,27 @@ def hisscat_download_literature(user_message, cat):
3232
return user_message
3333

3434

35-
@tool
36-
def rag_tool(query, cat) -> str:
37-
"""
38-
run this tools whenever message starts with Please tell me about
39-
40-
Process a query using Retrieval Augmented Generation (RAG) and cite documents from memory.
41-
"""
42-
# Initialize the Agent and Memory
43-
agent = Agent(cat)
44-
memory = Memory(cat)
45-
settings = cat.mad_hatter.get_plugin().load_settings()
46-
top_references = settings['top_references']
47-
# Retrieve relevant documents based on the query
48-
relevant_documents = memory.retrieve_documents(query, max_results=top_references)
35+
@hook
36+
def before_cat_sends_message(msg, cat):
37+
# Extract the user query from the message
38+
user_query = msg['content']
39+
40+
# Retrieve the relevant context documents from memory
41+
context_documents = cat.memory.get('relevant_documents', '') # Adjust this to your memory retrieval method
4942

50-
# Prepare the prompt for the LLM
51-
prompt = f"You are a research assistant that, based on the following documents, answer the query.
52-
If the information required is not in the documents answer 'I am sorry, I need more information'.
53-
The query: '{query}'.\n\n"
54-
for doc in relevant_documents:
55-
prompt += f"Document: {doc.title}\nContent: {doc.content}\n\n"
43+
# Create the final prompt
44+
prompt = f"""You are a research assistant who wants to give accurate answers considering only the provided context. Write a paragraph with the answer and cite the documents you used. Don't invent anything, if you can't answer just say 'I am sorry I don't have enough information'.
5645
57-
# Generate the response using the LLM
58-
response = agent.generate_response(prompt)
46+
Documents:
47+
{context_documents}
5948
60-
# Add citations to the response
61-
citations = [f"[{i+1}] {doc.title}" for i, doc in enumerate(relevant_documents)]
62-
response += "\n\nCitations:\n" + "\n".join(citations)
49+
Question:
50+
{user_query}
51+
"""
6352

64-
return response
53+
# Update the message content with the new prompt
54+
msg['content'] = prompt
55+
return msg
6556

6657

6758
@tool
@@ -80,6 +71,7 @@ def delete_declarative_memory(cat):
8071
return "Failed to delete entries in declarative memory."
8172

8273

74+
8375
def load_files_to_memory(cat, directory='./literature'):
8476
"""
8577
Load supported files from the specified directory into the Cat's declarative memory.
@@ -90,12 +82,20 @@ def load_files_to_memory(cat, directory='./literature'):
9082
"""
9183
# Initialize the Rabbit Hole
9284
rabbit_hole = RabbitHole(cat)
93-
85+
settings = cat.mad_hatter.get_plugin().load_settings()
86+
chunk_size = settings['chunk_size']
87+
chunk_overlap = settings['chunk_overlap']
9488
# Iterate through files in the directory
9589
for filename in os.listdir(directory):
9690
if filename.endswith(('.txt', '.md', '.pdf', '.html')):
9791
file_path = os.path.join(directory, filename)
98-
# Load the file into the declarative memory
99-
with open(file_path, 'rb') as file:
100-
rabbit_hole.insert_memory(file)
101-
print(f"{datetime.now()} Loaded {filename} into memory.")
92+
try:
93+
# Load the file into the declarative memory
94+
with open(file_path, 'rb') as file:
95+
rabbit_hole.ingest_file(file, chunk_size=chunk_size, chunk_overlap=chunk_overlap)
96+
log.info(f"{datetime.now()} Loaded {filename} into memory.")
97+
except Exception as e:
98+
log.error(f"Error loading {filename}: {e}")
99+
100+
101+

0 commit comments

Comments
 (0)