diff --git a/memos/cmds/library.py b/memos/cmds/library.py index 4e79ee6..0e59ee5 100644 --- a/memos/cmds/library.py +++ b/memos/cmds/library.py @@ -584,7 +584,7 @@ def reindex( continue update_response = client.post(f"{BASE_URL}/entities/{entity['id']}/last-scan-at") - if update_response.status_code != 200: + if update_response.status_code != 204: print(f"Failed to update last_scan_at for entity {entity['id']}: {update_response.status_code} - {update_response.text}") else: print(f"Updated last_scan_at for entity {entity['id']}") diff --git a/memos/commands.py b/memos/commands.py index 50ad7e6..39c4993 100644 --- a/memos/commands.py +++ b/memos/commands.py @@ -18,7 +18,7 @@ import sys import subprocess import platform from .cmds.plugin import plugin_app, bind -from .cmds.library import lib_app, scan, index, watch +from .cmds.library import lib_app, scan, typesense_index, reindex, watch import psutil import signal from tabulate import tabulate @@ -139,8 +139,8 @@ def scan_default_library(force: bool = False): scan(default_library["id"], plugins=None, folders=None, force=force) -@app.command("index") -def index_default_library( +@app.command("typesense-index") +def typsense_index_default_library( batchsize: int = typer.Option( 4, "--batchsize", "-bs", help="Number of entities to index in a batch" ), @@ -164,7 +164,32 @@ def index_default_library( print("Default library does not exist.") return - index(default_library["id"], force=force, folders=None, batchsize=batchsize) + typesense_index(default_library["id"], force=force, folders=None, batchsize=batchsize) + + +@app.command("reindex") +def reindex_default_library(): + """ + Reindex the default library for memos. + """ + # Get the default library + response = httpx.get(f"{BASE_URL}/libraries") + if response.status_code != 200: + print(f"Failed to retrieve libraries: {response.status_code} - {response.text}") + return + + libraries = response.json() + default_library = next( + (lib for lib in libraries if lib["name"] == settings.default_library), None + ) + + if not default_library: + print("Default library does not exist.") + return + + # Reindex the library + print(f"Reindexing library: {default_library['name']}") + reindex(default_library["id"]) @app.command("record") diff --git a/memos/crud.py b/memos/crud.py index c4fac52..0e5ade1 100644 --- a/memos/crud.py +++ b/memos/crud.py @@ -290,12 +290,15 @@ def update_entity( return Entity(**db_entity.__dict__) -def touch_entity(entity_id: int, db: Session): +def touch_entity(entity_id: int, db: Session) -> bool: db_entity = db.query(EntityModel).filter(EntityModel.id == entity_id).first() if db_entity: db_entity.last_scan_at = func.now() db.commit() db.refresh(db_entity) + return True + else: + return False def update_entity_tags(entity_id: int, tags: List[str], db: Session) -> Entity: diff --git a/memos/embedding.py b/memos/embedding.py index ac348f9..e676dd3 100644 --- a/memos/embedding.py +++ b/memos/embedding.py @@ -33,7 +33,7 @@ def init_embedding_model(): model_dir = settings.embedding.model logger.info(f"Using model: {model_dir}") - model = SentenceTransformer(model_dir, trust_remote_code=True) + model = SentenceTransformer(model_dir, trust_remote_code=True, truncate_dim=768) model.to(device) logger.info(f"Embedding model initialized on device: {device}") diff --git a/memos/plugins/ocr/main.py b/memos/plugins/ocr/main.py index 46df093..690a139 100644 --- a/memos/plugins/ocr/main.py +++ b/memos/plugins/ocr/main.py @@ -7,7 +7,7 @@ import json import base64 from PIL import Image import numpy as np -from rapidocr_openvino import RapidOCR +from rapidocr_onnxruntime import RapidOCR from concurrent.futures import ThreadPoolExecutor from functools import partial import yaml diff --git a/memos/server.py b/memos/server.py index 544031b..3f1f708 100644 --- a/memos/server.py +++ b/memos/server.py @@ -372,7 +372,7 @@ async def update_entity( return entity -@app.post("/entities/{entity_id}/last-scan-at", response_model=Entity, tags=["entity"]) +@app.post("/entities/{entity_id}/last-scan-at", status_code=status.HTTP_204_NO_CONTENT, tags=["entity"]) def update_entity_last_scan_at( entity_id: int, db: Session = Depends(get_db) @@ -380,13 +380,12 @@ def update_entity_last_scan_at( """ Update the last_scan_at timestamp for an entity and trigger update for fts and vec. """ - entity = crud.touch_entity(entity_id, db) - if entity is None: + succeeded = crud.touch_entity(entity_id, db) + if not succeeded: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Entity not found", ) - return entity def typesense_required(func):