async def get_relevant_memories(
    query: str, memories: list[SingleMemory], k: int = 5
) -> list[SingleMemory]:
    """Returns a list of the top k most relevant NON MESSAGE memories, based on the query string"""

    memories_with_relevance = [
        RelatedMemory(memory=memory, relevance=await memory.relevance(query))
        for memory in memories
    ]

    # Sort the list of dictionaries based on the 'relevance' key in descending order
    sorted_by_relevance = sorted(
        memories_with_relevance, key=lambda x: x.relevance, reverse=True
    )

    # get the top k memories, as a list of SingleMemory object
    top_memories = [memory.memory for memory in sorted_by_relevance[:k]]

    # now sort the list based on the created_at field, with the oldest memories first
    sorted_by_created_at = sorted(
        top_memories, key=lambda x: x.created_at, reverse=False
    )

    return sorted_by_created_at
Returns a list of the top k most relevant NON MESSAGE memories, based on the query string
src/memory/base.py