LlamaFS Documentation

repository·main·Indexed 26 days ago

https://github.com/iyaja/llama-fs

A self-organizing file manager that uses LLMs (Llama 3 via Groq or Ollama) to automatically rename and organize files based on content. It features a batch mode for one-off directory organization and a watch mode as an interactive daemon. LlamaFS includes a FastAPI server with endpoints for batch processing (/batch), filesystem monitoring (/watch), and committing file changes (/commit), as well as a CLI for generating directory structures.

Tokens
5K
Snippets
15
Records
30
Agent score
91%

What's inside LlamaFS

  1. Install Electron React Boilerplate

    main

    To set up a new project using the Electron React Boilerplate, clone the repository and install the dependencies using npm.

    git clone --depth 1 --branch main https://github.com/electron-react-boilerplate/electron-react-boilerplate.git your-project-name
    cd your-project-name
    npm install
  2. Configure environment variables for LlamaFS

    main

    LlamaFS requires an .env file for API keys. Copy .env.example to .env and provide the following keys:

    • GROQ: Required for fast cloud inference (can be replaced with Ollama for local processing).
    • AGENTOPS: Used for logging, monitoring latency, cost per session, and session replays.

    If you wish to use 'incognito mode' (routing requests through Ollama instead of Groq), you should also pull the moondream model via Ollama.

  3. Create a directory structure from file summaries

    main

    After generating summaries, you can use an LLM to propose an optimal directory structure. The LLM should return a JSON object where each file entry includes a path key representing its new location. You can then use pathlib to physically create these directories and empty files in a target base directory.

    import pathlib
    import json
    
    # Assuming 'file_tree' is the JSON list returned by the LLM
    BASE_DIR = pathlib.Path("test_dir")
    BASE_DIR.mkdir(exist_ok=True)
    
    for file in file_tree:
        file["path"] = pathlib.Path(file["path"])
        # Create file in specified base directory
        (BASE_DIR / file["path"]).parent.mkdir(parents=True, exist_ok=True)
        with open(BASE_DIR / file["path"], "w") as f:
            f.write("")
  4. Summarize files using Groq and LlamaIndex

    main

    You can summarize the contents of a directory by using llama_index.core.SimpleDirectoryReader to load documents and then passing their content and metadata to a LLM via the Groq API.

    To ensure reliable parsing, use the response_format={"type": "json_object"} parameter in the Groq chat completion call and instruct the system to always return JSON.

    from llama_index.core import SimpleDirectoryReader
    from groq import Groq
    import os
    import json
    
    # 1. Load documents
    reader = SimpleDirectoryReader(input_dir=".")
    documents = reader.load_data()
    doc_dicts = [{"content": d.text, **d.metadata} for d in documents]
    
    # 2. Prepare Prompt
    PROMPT = f"""
    The following is a list of file contents, along with their metadata. For each file, provide a summary of the contents.
    
    {doc_dicts}
    
    Return a JSON list with the following schema:
    
    ```json
    {{
      "files": [
        {{
          "filename": "name of the file",
          "summary": "summary of the content"
        }}
      ]
    }}

    """.strip()

    3. Call Groq

    client = Groq(api_key=os.environ.get("GROQ_API_KEY")) chat_completion = client.chat.completions.create( messages=[ {"role": "system", "content": "Always return JSON. Do not include any other text or formatting characters."}, {"role": "user", "content": PROMPT}, ], model="llama3-70b-8192", response_format={"type": "json_object"}, )

    4. Parse results

    summaries = json.loads(chat_completion.choices[0].message.content)["files"]

  5. Optimize LlamaIndex document loading with TokenTextSplitter

    main

    When using SimpleDirectoryReader, you can improve document handling by iterating through data and manually splitting text using TokenTextSplitter. This is useful if you want to consolidate multiple small document chunks from a single file into a single larger Document object with shared metadata.

    from llama_index.core import SimpleDirectoryReader, Document
    from llama_index.core.node_parser import TokenTextSplitter
    
    splitter = TokenTextSplitter(chunk_size=6144)
    reader = SimpleDirectoryReader(input_dir=".", recursive=True)
    all_docs = []
    
    for docs in reader.iter_data():
        # If multiple chunks exist for one file, merge them into one Document
        if len(docs) > 1:
            text = splitter.split_text("\n".join([d.text for d in docs]))[0]
            docs = [Document(text=text, metadata=docs[0].metadata)]
        all_docs.extend(docs)
  6. Use the /batch API endpoint

    main

    The /batch endpoint allows you to send a directory to LlamaFS to receive a suggested file structure and organization. You can query this endpoint using curl by passing a JSON payload containing the target path, an instruction, and the incognito toggle.

    curl -X POST http://127.0.0.1:8000/batch \
     -H "Content-Type: application/json" \
     -d '{"path": "/Users/<username>/Downloads/", "instruction": "string", "incognito": false}'