Transformers
Italian
English
semantic-search
explainable-ai
faiss
ai-ethics
responsible-ai
llm
prompt-engineering
multimodal-ai
ai-transparency
ethical-intelligence
explainable-llm
cognitive-ai
ethical-ai
scientific-retrieval
modular-ai
memory-augmented-llm
trustworthy-ai
reasoning-engine
ai-alignment
next-gen-llm
thinking-machines
open-source-ai
explainability
ai-research
semantic audit
cognitive agent
human-centered-ai
Create semantic_retrieval.py
Browse files
src/memory/semantic_retrieval.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# © 2025 Elena Marziali — Code released under Apache 2.0 license.
|
| 2 |
+
# See LICENSE in the repository for details.
|
| 3 |
+
# Removal of this copyright is prohibited.
|
| 4 |
+
|
| 5 |
+
# Function to retrieve similar responses
|
| 6 |
+
def retrieve_context(question, top_k=2):
|
| 7 |
+
""" Searches for similar responses in FAISS memory. """
|
| 8 |
+
emb_question = embedding_model.encode([question])
|
| 9 |
+
_, indices = index.search(np.array(emb_question, dtype=np.float32), top_k)
|
| 10 |
+
return [f"Previous response {i+1}" for i in indices[0]] if indices[0][0] != -1 else []
|
| 11 |
+
|
| 12 |
+
# **Usage example**
|
| 13 |
+
add_to_memory("What is general relativity?", "General relativity is Einstein's theory of gravity.")
|
| 14 |
+
similar_responses = retrieve_context("Can you explain relativity?")
|
| 15 |
+
print("Related responses:", similar_responses)
|
| 16 |
+
|
| 17 |
+
# Retrieve multi-turn context
|
| 18 |
+
def retrieve_multiturn_context(question, top_k=5):
|
| 19 |
+
""" Searches for related previous responses to build a broader context. """
|
| 20 |
+
emb_question = embedding_model.encode([question])
|
| 21 |
+
_, indices = index.search(np.array(emb_question, dtype=np.float32), top_k)
|
| 22 |
+
|
| 23 |
+
context = [f"Previous turn {i+1}" for i in indices[0] if i != -1]
|
| 24 |
+
return " ".join(context) if context else ""
|