Skip to content

Add support for storing, maintaining user memories in the backend #1168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

sabaimran
Copy link
Member

@sabaimran sabaimran commented Apr 22, 2025

Memory

A major component of useful AI systems is adaptation to the user context. This is a major reason why we've prioritized uploading of knowledge bases. The next steps in this direction is dynamically updating state of the user as more conversations take place.

We introduce a new paradigm for maintaining medium and long term memories in Khoj. The scope of a conversation can be thought of as short term memory.

Medium term memory extends to the past week.

Long term memory extends to anytime in the past, where a search query results in a match.

Add support for viewing and managing the memories with user action in the settings page as well.

Fully integrate the memory object into all downstream usage, from image generation, notes extraction, online search, etc.

the user memory is comprised of the user to whom it belongs, the
generated embeddings, the raw text field, and the search model used to
create the embeddings

remove irrelevant datastore object
adapters provide various methods for creating, updating, deleting the
user memory objects
add logic to automatically create new memories and delete existing
memories when saving conversation to log. the logic prevents the agent
from editing existing memories, as a create + delete can suffice for
this case and reduces the complexity.
add backend APIs exposing basic CRUD capabilities on memories
add a section to the settings page that allows you to browse, view, edit
existing memories the backend has about you
@sabaimran sabaimran requested review from debanjum and Copilot April 22, 2025 02:00
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces support for storing and maintaining user memories in the backend as part of Khoj’s user adaptation improvements. Key changes include:

  • Adding a new UserMemory model (including a migration to delete the old DataStore model), along with corresponding admin registration and adapter methods.
  • Creating new API endpoints to retrieve, update, and delete memories.
  • Integrating memory support into conversation processing and updating the settings page UI to let users view and manage their memories.

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/khoj/routers/helpers.py Added functions for extracting facts and updating memories, with proper user validation.
src/khoj/routers/api_memories.py New API endpoints for fetching, updating, and deleting user memories.
src/khoj/routers/api_chat.py Integrated memory retrieval and de-duplication into chat telemetry generation.
src/khoj/processor/conversation/utils.py Updated conversation log saving to include memory updates.
src/khoj/database/models/init.py Introduced the new UserMemory model and removed obsolete DataStore model.
src/khoj/database/adapters/init.py Added a UserMemoryAdapters class with functions for pulling, saving, searching, and deleting memories.
src/configure.py Included the new api_memories router in the application configuration.
src/interface/web/app/settings/page.tsx Enhanced the settings page with UI for fetching, updating, and deleting memories.
src/interface/web/app/components/userMemory/userMemory.tsx Added a new UI component to display and manage individual user memories.
Comments suppressed due to low confidence (2)

src/khoj/routers/api_chat.py:1486

  • The code creates a set from UserMemory objects via 'set(recent_memories + long_term_memories)' which may raise issues if these model instances lack a defined hash function. Consider using a unique identifier (such as memory.id) or converting the objects appropriately before de-duplication.
matching_memories: List[UserMemory] = [],

src/khoj/database/adapters/init.py:2057

  • The parameter 'memory_id' is declared as a string, but the primary key for UserMemory is likely an integer. Consider updating the type to int or ensuring consistency with the model definition.
async def delete_memory(user: KhojUser, memory_id: str) -> bool:

@sabaimran sabaimran marked this pull request as ready for review April 22, 2025 04:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant