Skip to content
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

[WIP] add mle memory crud api #271

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 69 additions & 3 deletions mle/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from mle.utils import CodeChunker

console = Console()
memory = LanceDBMemory(os.getcwd())
memory_db = LanceDBMemory(os.getcwd())


@click.group()
Expand Down Expand Up @@ -224,13 +224,13 @@ def chat(model, build_mem):
)

chunks = chunker.chunk(raw_code, token_limit=100)
memory.add(
memory_db.add(
texts=list(chunks.values()),
table_name=table_name,
metadata=[{'file': file_path, 'chunk_key': k} for k, _ in chunks.items()]
)

return workflow.chat(os.getcwd(), model=model, memory=memory)
return workflow.chat(os.getcwd(), model=model, memory=memory_db)


@cli.command()
Expand Down Expand Up @@ -353,3 +353,69 @@ def integrate(reset):
"token": pickle.dumps(token, fix_imports=False),
}
write_config(config)


@cli.command()
@click.option('--add', default=None, help='Add files or directories into the local memory.')
@click.option('--rm', default=None, help='Remove files or directories into the local memory.')
@click.option('--update', default=None, help='Update files or directories into the local memory.')
def memory(add, rm, update):
path = add or rm or update
if path is None:
return

source_files = []
if os.path.isdir(path):
source_files = list_files(path, ['*.py'])
else:
source_files = [path]

working_dir = os.getcwd()
table_name = 'mle_chat_' + working_dir.split('/')[-1]
chunker = CodeChunker(os.path.join(working_dir, '.mle', 'cache'), 'py')
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
TimeElapsedColumn(),
console=console,
) as progress:
process_task = progress.add_task("Processing files...", total=len(source_files))

for file_path in source_files:
raw_code = read_file(file_path)
progress.update(
process_task,
advance=1,
description=f"Process {os.path.basename(file_path)} for memory..."
)

if add:
# add file into memory
chunks = chunker.chunk(raw_code, token_limit=100)
memory_db.add(
texts=list(chunks.values()),
table_name=table_name,
metadata=[{'file': file_path, 'chunk_key': k} for k, _ in chunks.items()]
)
elif rm:
# remove file from memory
memory_db.delete_by_metadata(
key="file",
value=file_path,
table_name=table_name,
)
elif update:
# update file into memory
chunks = chunker.chunk(raw_code, token_limit=100)
memory_db.delete_by_metadata(
key="file",
value=file_path,
table_name=table_name,
)
memory_db.add(
texts=list(chunks.values()),
table_name=table_name,
metadata=[{'file': file_path, 'chunk_key': k} for k, _ in chunks.items()]
)
Loading