### Install Project Dependencies (Bash) Source: https://github.com/iyaja/llama-fs/blob/main/electron-react-app/README.md Clones the repository and installs project dependencies using npm. This is the initial setup step for the project. ```bash 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 ``` -------------------------------- ### Start Development Server (Bash) Source: https://github.com/iyaja/llama-fs/blob/main/electron-react-app/README.md Starts the application in the development environment using npm. This command is used to run the app during development. ```bash npm start ``` -------------------------------- ### Bash: Environment Configuration for Llama-FS Source: https://context7.com/iyaja/llama-fs/llms.txt Details the environment variables and setup required for Llama-FS, including API keys for cloud services like Groq and AgentOps. It also provides instructions for setting up local inference with Ollama, pulling necessary models, and starting the FastAPI development server. ```bash # .env file configuration GROQ_API_KEY=gsk_your_groq_api_key_here AGENTOPS_API_KEY=your_agentops_api_key_here # For local-only processing (incognito mode), only Ollama is needed: # Install Ollama and pull models: # ollama pull llama3.1 # ollama pull moondream # Start the FastAPI server # fastapi dev server.py # Server runs on http://localhost:8000 # Endpoints available: # - GET / (health check) # - POST /batch (batch processing) ``` -------------------------------- ### Run File Watcher (Python) Source: https://github.com/iyaja/llama-fs/blob/main/watch-api.ipynb This Python snippet demonstrates how to initialize and run a file watcher. It instantiates a `Watcher` object, providing a directory to monitor and a callback function (`create_file_tree`) to execute when changes are detected. The watcher then starts its operation. ```python w = Watcher("./sample_data", create_file_tree) w.run() ``` -------------------------------- ### Environment Configuration Source: https://context7.com/iyaja/llama-fs/llms.txt Details the necessary API keys and environment setup for running Llama-FS, including local Ollama configuration. ```APIDOC ## Environment Configuration ### Description Required API keys and configuration for cloud inference and monitoring capabilities. ### Method Environment variable configuration and local service setup. ### Endpoint N/A (Configuration file and local services) ### Parameters #### Environment Variables (.env file) - **GROQ_API_KEY** (string) - Required for Groq Cloud inference. - **AGENTOPS_API_KEY** (string) - Required for AgentOps monitoring. #### Local Ollama Setup - **Install Ollama**: Download and install Ollama from [ollama.ai](https://ollama.ai/). - **Pull Models**: Use Ollama CLI to pull necessary models: ```bash ollama pull llama3.1 ollama pull moondream ``` ### Request Example ```bash # .env file configuration GROQ_API_KEY=gsk_your_groq_api_key_here AGENTOPS_API_KEY=your_agentops_api_key_here # For local-only processing (incognito mode), only Ollama is needed: # Install Ollama and pull models: ollama pull llama3.1 ollama pull moondream # Start the FastAPI server fastapi dev server.py # Server runs on http://localhost:8000 # Endpoints available: # - GET / (health check) # - POST /batch (batch processing) ``` ### Response #### Success Response (Server Status) - **GET /**: Returns a health check status, typically `200 OK` if the server is running. ``` -------------------------------- ### Install LlamaFS Project Requirements Source: https://github.com/iyaja/llama-fs/blob/main/README.md Installs the necessary Python packages for LlamaFS by installing from the requirements.txt file. Ensure Python 3.10+ and pip are installed beforehand. ```bash git clone https://github.com/iyaja/llama-fs.git cd llama-fs pip install -r requirements.txt ``` -------------------------------- ### Create File Tree using AI (Python) Source: https://github.com/iyaja/llama-fs/blob/main/watch-api.ipynb This Python function utilizes the Groq AI client to generate a structured file tree. It constructs a conversation with the AI, providing file summaries and naming convention examples to prompt for optimal file organization. The function expects the AI to return a JSON object. ```python import json from groq import Groq def create_file_tree(summaries, fs_events): client = Groq() cmpl = client.chat.completions.create( messages=[ {"content": FILE_PROMPT, "role": "system"}, {"content": json.dumps(summaries), "role": "user"}, {"content": WATCH_PROMPT.format(fs_events), "role": "system"}, {"content": json.dumps(summaries), "role": "user"}, ], model="llama3-70b-8192", response_format={"type": "json_object"}, temperature=0, ) print(cmpl.choices[0].message.content) return json.loads(cmpl.choices[0].message.content) ``` -------------------------------- ### Serve LlamaFS Application Locally with FastAPI Source: https://github.com/iyaja/llama-fs/blob/main/README.md Starts the LlamaFS application locally using FastAPI. The server runs on port 8000 by default and can be interacted with via API requests, such as the batch mode example provided. ```bash fastapi dev server.py ``` -------------------------------- ### POST /watch - Watch Mode Endpoint Source: https://context7.com/iyaja/llama-fs/llms.txt Starts a file system watcher that monitors directory changes and suggests real-time reorganization based on learned user patterns. This is for continuous monitoring. ```APIDOC ## POST /watch ### Description Starts a file system watcher that monitors directory changes and suggests real-time reorganization based on learned user patterns. ### Method POST ### Endpoint `/watch` ### Parameters #### Request Body - **path** (string) - Required - The directory path to watch. - **incognito** (boolean) - Optional - If true, runs in incognito mode. ### Request Example ```json { "path": "/Users/username/Documents/", "incognito": false } ``` ### Response #### Success Response (200) Returns a streaming response with JSON updates containing file reorganization suggestions. - **files** (array) - An array of file objects with suggested changes. - **src_path** (string) - The original path of the file. - **dst_path** (string) - The suggested destination path for the file. - **summary** (string) - A summary of the file's content. #### Response Example ```json {"files": [{"src_path": "report.pdf", "dst_path": "work/2024/reports/quarterly_report_q1.pdf", "summary": "Q1 financial performance report"}]} ``` ```json {"files": [{"src_path": "notes.txt", "dst_path": "work/2024/meeting_notes/standup_2024-03-15.txt", "summary": "Daily standup meeting notes"}]} ``` ``` -------------------------------- ### AI Prompt for File Naming Conventions (Python) Source: https://github.com/iyaja/llama-fs/blob/main/watch-api.ipynb Defines a system prompt for an AI model that includes examples of good file naming conventions. This prompt is used in conjunction with file summaries to help the AI understand and apply best practices when suggesting new file paths. ```python WATCH_PROMPT = """ Here are a few examples of good file naming conventions to emulate, based on the files provided: ```json {fs_events} ``` Include the above items in your response exactly as is, along all other proposed changes. """.strip() ``` -------------------------------- ### AI Prompt for File Path Suggestions (Python) Source: https://github.com/iyaja/llama-fs/blob/main/watch-api.ipynb Defines a system prompt for an AI model to suggest optimal file paths and filenames based on provided file summaries. It specifies the expected JSON output schema for the AI's response, guiding the organization of files. ```python FILE_PROMPT = """ You will be provided with list of source files and a summary of their contents. For each file, propose a new path and filename, using a directory structure that optimally organizes the files using known conventions and best practices. If the file is already named well or matches a known convention, set the destination path to the same as the source path. Your response must be a JSON object with the following schema: ```json { "files": [ { "src_path": "original file path", "dst_path": "new file path under proposed directory structure with proposed file name" } ] } ``` """.strip() ``` -------------------------------- ### Project Dependencies: requirements.txt Source: https://github.com/iyaja/llama-fs/blob/main/notebooks/data_loading_processing.ipynb This file lists the essential Python packages required to run the Llama-FS project. These dependencies include libraries for AI model interaction (ollama, litellm, groq), document handling (llama-index, docx2txt), and database integration (chromadb). Ensure all listed packages are installed in your environment before running the project. ```text ollama chromadb lama-index litellm groq docx2txt ``` -------------------------------- ### Query LlamaFS Batch Mode API Source: https://github.com/iyaja/llama-fs/blob/main/README.md Example cURL command to query the LlamaFS batch mode API. It sends a POST request to the /batch endpoint with a JSON payload containing the target directory path, an instruction, and an incognito flag. ```bash curl -X POST http://127.0.0.1:8000/batch \ -H "Content-Type: application/json" \ -d '{"path": "/Users//Downloads/", "instruction": "string", "incognito": false}' ``` -------------------------------- ### Watch Mode Endpoint - API Request Source: https://context7.com/iyaja/llama-fs/llms.txt This endpoint starts a file system watcher that monitors directory changes and suggests real-time reorganization based on learned user patterns. It takes a path and an incognito flag as input and returns a streaming JSON response with proposed file movements. ```bash curl -X POST http://127.0.0.1:8000/watch \ -H "Content-Type: application/json" \ -d '{ "path": "/Users/username/Documents/", "incognito": false }' ``` -------------------------------- ### Create Prompt for File Summarization Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb Constructs a prompt string for the Groq API. This prompt includes the loaded document data and instructs the AI to return a JSON list of file summaries with 'filename' and 'summary' keys. ```python 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() ``` -------------------------------- ### Create Prompt for Directory Structure Proposal Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb Constructs a new prompt for the Groq API. This prompt uses the previously generated file summaries and asks the AI to propose an optimal directory structure, adding a 'path' key to the JSON output. ```python PROMPT = f""" The following is a list of files and a summary of their contents. Read them carefully, then propose a directory structure that optimally organizes the files using known conventions and best practices. {summaries} You will solve this task by adding a `path` key to the JSON object below. The value of the `path` key should be the path to the file that you think is the most relevant to the summary. """.strip() ``` -------------------------------- ### Python: Real-time File System Watcher with Learning Source: https://context7.com/iyaja/llama-fs/llms.txt Implements a file system watcher using `watchdog` to monitor a specified directory. It uses a `Handler` to process file system events, `create_file_tree` for generating suggestions, and a queue for inter-process communication. The watcher learns from user organization patterns to provide auto-suggestions. ```python from src.watch_utils import Handler, create_file_tree from watchdog.observers import Observer import queue import asyncio async def setup_watcher(directory_path): response_queue = queue.Queue() # Initialize handler with callback and queue event_handler = Handler( base_path=directory_path, callback=create_file_tree, queue=response_queue ) # Load initial summaries await event_handler.set_summaries() # Start observer observer = Observer() observer.schedule(event_handler, directory_path, recursive=True) observer.start() # Process events while True: if not response_queue.empty(): suggestion = response_queue.get() print(f"Suggested changes: {suggestion}") # Example: When user moves files, system learns patterns # User action: moves "report_q1.pdf" to "work/2024/reports/" # System learns: similar reports should go to same location # Auto-suggestion: "report_q2.pdf" → "work/2024/reports/report_q2.pdf" if __name__ == "__main__": asyncio.run(setup_watcher("/Users/username/Documents")) ``` -------------------------------- ### Initialize Llama Index Core Components in Python Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb This snippet demonstrates the initialization of core components from the Llama Index library, specifically `SimpleDirectoryReader`, `Document`, and `TokenTextSplitter`. These are essential for reading and processing text data. ```python from llama_index.core import SimpleDirectoryReader from llama_index.core import Document from llama_index.core.node_parser import TokenTextSplitter ``` -------------------------------- ### Package Application for Production (Bash) Source: https://github.com/iyaja/llama-fs/blob/main/electron-react-app/README.md Packages the application for the local platform, preparing it for production distribution. This command creates a distributable version of the app. ```bash npm run package ``` -------------------------------- ### Display File Summaries Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb Prints the 'summaries' variable, which holds the list of file summaries obtained from the Groq API. This allows for verification of the summarization results. ```python summaries ``` -------------------------------- ### Load Documents with LlamaIndex Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb Initializes a SimpleDirectoryReader to load data from the current directory ('.'). It then loads all documents and converts them into a list of dictionaries, including content and metadata. ```python reader = SimpleDirectoryReader(input_dir=".") documents = reader.load_data() doc_dicts = [{"content": d.text, **d.metadata} for d in documents] ``` -------------------------------- ### Import Libraries for LlamaIndex and AI Interaction Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb Imports necessary libraries including openai, groq, os, json, and SimpleDirectoryReader from llama_index.core. These are foundational for data loading and interacting with AI models. ```python import openai import groq import os import json from groq import Groq from llama_index.core import SimpleDirectoryReader ``` -------------------------------- ### Create Directory Structure Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb Defines a base directory 'test_dir' using pathlib. It then iterates through the 'file_tree' proposed by the AI. For each file, it ensures the parent directory exists and creates an empty file at the specified path within the base directory. ```python 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("") ``` -------------------------------- ### Import Ollama Library in Python Source: https://github.com/iyaja/llama-fs/blob/main/ollama-moondream.ipynb This snippet demonstrates the initial import of the Ollama library, which is necessary for interacting with the Ollama API. No external dependencies are required beyond the library itself. ```python import ollama ``` -------------------------------- ### Propose Directory Structure using Groq API Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb Re-initializes the Groq client and makes another chat completion request. This time, the prompt asks for a directory structure proposal. The response format is commented out, implying it might not strictly enforce JSON here or is for debugging purposes. The result is parsed as JSON. ```python 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"}, ) file_tree = json.loads(chat_completion.choices[0].message.content) ``` -------------------------------- ### Python Autoreload and Imports Source: https://github.com/iyaja/llama-fs/blob/main/watch-api.ipynb This snippet sets up the Python environment by enabling autoreload for imported modules and importing necessary libraries such as Groq, JSON, OS, watchdog, asyncio, and a custom loader. ```python %load_ext autoreload %autoreload 2 from groq import Groq import json import os import watchdog import asyncio from src.loader import get_dir_summaries ``` -------------------------------- ### Summarize Files using Groq API Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb Initializes the Groq client with the API key, then creates a chat completion request. It specifies the system role to always return JSON and the user prompt for summarization. The model 'llama3-70b-8192' is used, and the response format is set to 'json_object'. Finally, it parses the JSON response to extract file summaries. ```python 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"}, ) summaries = json.loads(chat_completion.choices[0].message.content)["files"] ``` -------------------------------- ### Import Pathlib for File System Operations Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb Imports the 'pathlib' module, which provides an object-oriented approach to file system paths. This is used for creating and manipulating directories and files. ```python import pathlib ``` -------------------------------- ### Watch Mode File Handler Source: https://context7.com/iyaja/llama-fs/llms.txt Monitors a directory in real-time, learning from user organization patterns and suggesting intelligent file placements. ```APIDOC ## Watch Mode File Handler ### Description Implements real-time file system monitoring with intelligent learning from user organization patterns. ### Method Python script execution with `watchdog` library. ### Endpoint `asyncio.run(setup_watcher(directory_path))` ### Parameters #### Path Parameters - **directory_path** (string) - Required - The directory to monitor for file system events. ### Request Example ```python from src.watch_utils import Handler, create_file_tree from watchdog.observers import Observer import queue import asyncio async def setup_watcher(directory_path): response_queue = queue.Queue() # Initialize handler with callback and queue event_handler = Handler( base_path=directory_path, callback=create_file_tree, queue=response_queue ) # Load initial summaries await event_handler.set_summaries() # Start observer observer = Observer() observer.schedule(event_handler, directory_path, recursive=True) observer.start() # Process events while True: if not response_queue.empty(): suggestion = response_queue.get() print(f"Suggested changes: {suggestion}") # Example: When user moves files, system learns patterns # User action: moves "report_q1.pdf" to "work/2024/reports/" # System learns: similar reports should go to same location # Auto-suggestion: "report_q2.pdf" -> "work/2024/reports/report_q2.pdf" asyncio.run(setup_watcher("/Users/username/Documents")) ``` ### Response #### Success Response (stdout) - **Suggested changes**: A string representing the suggested file organization based on learned patterns. ``` -------------------------------- ### File Tree Generation Function (Python) Source: https://context7.com/iyaja/llama-fs/llms.txt Creates optimized directory structures using LLM-based analysis of file content summaries and naming conventions, leveraging the `create_file_tree` function. ```APIDOC ## File Tree Generation Function ### Description Creates optimized directory structures using LLM-based analysis of file content summaries and naming conventions. ### Method Python Function (Async) ### Function Signature `create_file_tree(summaries: list, session: object) -> list` ### Parameters - **summaries** (list) - Required - A list of file summary objects obtained from `get_dir_summaries`. - **session** (object) - Required - An AgentOps session object for monitoring. ### Request Example ```python from src.tree_generator import create_file_tree from src.loader import get_dir_summaries import asyncio import os import agentops async def generate_structure(): summaries = await get_dir_summaries("/Users/username/messy_folder") session = agentops.start_session(tags=["file-organization"]) files = create_file_tree(summaries, session) for file in files: print(f"{file['src_path']} → {file['dst_path']}") agentops.end_session("Success") return files asyncio.run(generate_structure()) ``` ### Response #### Success Response - **src_path** (string) - The original path of the file. - **dst_path** (string) - The generated destination path for the file. #### Response Example ``` IMG_2024.jpg → 2024-03/photos/family/birthday_celebration.jpg receipt.pdf → 2024-03/financial/receipts/grocery_store_receipt_2024-03-15.pdf ``` ``` -------------------------------- ### Python Groq API Key Configuration Source: https://github.com/iyaja/llama-fs/blob/main/watch-api.ipynb This code snippet configures the Groq API key by setting it as an environment variable. Ensure you replace the placeholder key with your actual API key for authentication. ```python os.environ["GROQ_API_KEY"] = "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" ``` -------------------------------- ### Electron App Integration API Source: https://context7.com/iyaja/llama-fs/llms.txt Provides API endpoints for an Electron desktop application to perform batch file organization and commit changes. ```APIDOC ## Electron App Integration API ### Description Desktop application providing visual interface for file organization with drag-and-drop approval workflow. ### Method `fetch` API calls to a local server. ### Endpoints - `POST /batch` - `POST /commit` ### Parameters #### POST /batch ##### Request Body - **path** (string) - Required - The root directory for batch organization. - **instruction** (string) - Optional - Natural language instruction for organizing files. - **incognito** (boolean) - Optional - If true, runs in a privacy-preserving mode. ##### Request Example ```javascript async function organizeFiles() { const response = await fetch('http://localhost:8000/batch', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ path: '/Users/username/Downloads', instruction: 'Organize by date and category', incognito: false }) }); const suggestions = await response.json(); // suggestions = [ // {src_path: "photo.jpg", dst_path: "2024-03/photos/sunset.jpg", summary: "..."}, // {src_path: "doc.pdf", dst_path: "2024-03/documents/invoice.pdf", summary: "..."} // ] return suggestions; } ``` ##### Response Example (200 OK) ```json [ { "src_path": "photo.jpg", "dst_path": "2024-03/photos/sunset.jpg", "summary": "A sunset over the ocean." }, { "src_path": "doc.pdf", "dst_path": "2024-03/documents/invoice.pdf", "summary": "Q1 invoice document." } ] ``` #### POST /commit ##### Request Body - **base_path** (string) - Required - The base path from which `src_path` is relative. - **src_path** (string) - Required - The original path of the file. - **dst_path** (string) - Required - The destination path for the file. ##### Request Example ```javascript async function commitChanges(changes) { for (const change of changes) { await fetch('http://localhost:8000/commit', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ base_path: '/Users/username/Downloads', src_path: change.src_path, dst_path: change.dst_path }) }); } } ``` ##### Response Example (200 OK) `{ "message": "File moved successfully" }` ``` -------------------------------- ### Python: Run Directory Structure Generation Source: https://context7.com/iyaja/llama-fs/llms.txt Executes the asynchronous function to generate a directory structure. This function likely orchestrates the file organization process based on predefined rules or learned patterns. ```python import asyncio async def generate_structure(): # Placeholder for actual directory generation logic print("Generating directory structure...") await asyncio.sleep(1) # Simulate work print("Directory structure generated.") if __name__ == "__main__": asyncio.run(generate_structure()) ``` -------------------------------- ### Python File System Watcher with Async Handling Source: https://github.com/iyaja/llama-fs/blob/main/watch-api.ipynb Implements a file system watcher using Python's watchdog library. The Watcher class monitors a specified directory and triggers a callback on file system events. The Handler class processes events, including file creation, deletion, and movement, and attempts to handle asynchronous operations for initial directory summarization. ```python import time from watchdog.observers import Observer from watchdog.events import FileSystemEvent, FileSystemEventHandler class Watcher: def __init__(self, base_path, callback): self.observer = Observer() self.base_path = base_path self.callback = callback def run(self): event_handler = Handler(self.base_path, self.callback) self.observer.schedule(event_handler, self.base_path, recursive=True) self.observer.start() try: while True: time.sleep(5) except KeyboardInterrupt: self.observer.stop() print(f"Observer on directory {self.base_path} stopped") self.observer.join() class Handler(FileSystemEventHandler): def __init__(self, base_path, callback): self.base_path = base_path self.callback = callback self.events = [] print(f"Watching directory {base_path}") # Hack to get async function to work loop = asyncio.new_event_loop() # Ensure the async setup is run thread-safely future = loop.run_until_complete(get_doc_summaries(base_path)) self.summaries = future.result() # This will block until the coroutine is done print(self.summaries) def on_created(self, event: FileSystemEvent) -> None: src_path = os.path.relpath(event.src_path, self.base_path) print(f"Created {src_path}") # self.callback(event.src_path) def on_deleted(self, event: FileSystemEvent) -> None: src_path = os.path.relpath(event.src_path, self.base_path) print(f"Deleted {src_path}") # self.callback(event.src_path) def on_moved(self, event: FileSystemEvent) -> None: src_path = os.path.relpath(event.src_path, self.base_path) dest_path = os.path.relpath(event.dest_path, self.base_path) print(f"Moved {src_path} > {dest_path}") self.events.append({"src_path": src_path, "dst_path": dest_path}) self.callback(self.summaries, {"files": self.events}) # def on_any_event(self, event): # print(event) # print(event.event_type, event.src_path, event.dest_path) # if not event.is_directory: # self.callback(event.src_path) ``` -------------------------------- ### Bash: Command-Line Batch File Organization Source: https://context7.com/iyaja/llama-fs/llms.txt Enables batch processing of file organization directly from the terminal. Supports basic usage with source and destination paths, an auto-approval flag to skip prompts, and provides an ASCII tree visualization of the proposed changes. ```bash # Basic usage python main.py /path/to/source /path/to/destination # Auto-approve all changes without prompts python main.py /path/to/source /path/to/destination --auto-yes # Example session: python main.py ~/Downloads ~/Documents/Organized # Output displays ASCII tree: # ~/Documents/Organized # ├── 2024-03 # │ ├── financial # │ │ └── tax_return_2024.pdf # │ ├── photos # │ │ ├── vacation_beach_01.jpg # │ │ └── vacation_beach_02.jpg # │ └── work # │ └── project_proposal.docx # # Proceed with directory structure? [Y/n]: y ``` -------------------------------- ### Load Documents and Process Metadata using LlamaIndex and Groq Source: https://github.com/iyaja/llama-fs/blob/main/notebooks/data_loading_processing.ipynb This snippet defines functions to load documents from a specified directory using LlamaIndex's SimpleDirectoryReader, extract metadata, and process it to remove duplicate file entries. It then utilizes the Groq API to generate summaries for each document's content and merges these summaries with the document metadata. ```python from collections import defaultdict import os import json from groq import Groq from llama_index.core import SimpleDirectoryReader def load_documents(path: str): # reader = SimpleDirectoryReader(input_dir=path, recursive=True) reader = SimpleDirectoryReader(input_dir=path) documents = reader.load_data() doc_dicts = [{"content": d.text, **d.metadata} for d in documents] return doc_dicts def process_metadata(doc_dicts): file_seen = set() metadata_list = [] for doc in doc_dicts: if doc["file_path"] not in file_seen: file_seen.add(doc["file_path"]) metadata_list.append(doc) return metadata_list def query_summaries(doc_dicts): client = Groq( api_key=os.environ.get("GROQ_API_KEY"), ) PROMPT = f""" The following is a list of file contents, along with their metadata. For each file, provide a summary of the contents. The purpose of the summary is to organize files based on their content. To this end provide a concise but informative summary. Try to make the summary as specific to the file as possible. {doc_dicts} Return a JSON list with the following schema: ```json {{ "files": [ {{ "file_path": "path to the file including name", "summary": "summary of the content" }} ] }} ``` """.strip() 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"}, ) summaries = json.loads(chat_completion.choices[0].message.content)["files"] return summaries def merge_summary_documents(summaries, metadata_list): list_summaries = defaultdict(list) for item in summaries: list_summaries[item["file_path"]].append(item["summary"]) file_summaries = {path: '. '.join(summaries) for path, summaries in list_summaries.items()} file_list = [{"summary": file_summaries[file["file_path"]], **file} for file in metadata_list] return file_list def get_doc_summaries(path: str): doc_dicts = load_documents(path) metadata = process_metadata(doc_dicts) summaries = query_summaries(doc_dicts) file_summaries = merge_summary_documents(summaries, metadata) return file_summaries ``` -------------------------------- ### Command-Line Batch Processing Source: https://context7.com/iyaja/llama-fs/llms.txt Organize files in batch mode from the terminal. Supports interactive approval, tree visualization, and an auto-approve option. ```APIDOC ## Command-Line Batch Processing ### Description Runs batch organization from the terminal with interactive approval and tree visualization. ### Method Command-line execution ### Endpoint `python main.py /path/to/source /path/to/destination [--auto-yes]` ### Parameters #### Path Parameters - **/path/to/source** (string) - Required - The directory containing the files to organize. - **/path/to/destination** (string) - Required - The directory where organized files will be placed. #### Query Parameters - **--auto-yes** (boolean) - Optional - Automatically approve all changes without prompts. ### Request Example ```bash # Basic usage python main.py /path/to/source /path/to/destination # Auto-approve all changes without prompts python main.py /path/to/source /path/to/destination --auto-yes # Example session: python main.py ~/Downloads ~/Documents/Organized # Output displays ASCII tree: # ~/Documents/Organized # ├── 2024-03 # │ ├── financial # │ │ └── tax_return_2024.pdf # │ ├── photos # │ │ ├── vacation_beach_01.jpg # │ │ └── vacation_beach_02.jpg # │ └── work # │ └── project_proposal.docx # # Proceed with directory structure? [Y/n]: y ``` ### Response #### Success Response (0) - **Output**: ASCII tree visualization of the proposed directory structure and a prompt for user confirmation. #### Response Example ``` ~/Documents/Organized ├── 2024-03 │ ├── financial │ │ └── tax_return_2024.pdf │ ├── photos │ │ ├── vacation_beach_01.jpg │ │ └── vacation_beach_02.jpg │ └── work │ └── project_proposal.docx ``` Proceed with directory structure? [Y/n]: y ``` -------------------------------- ### Import Core Python and Llama-Index Libraries Source: https://github.com/iyaja/llama-fs/blob/main/notebooks/data_loading_processing.ipynb This snippet imports essential Python libraries such as 'os' for operating system interactions and 'json' for data serialization. It also imports key components from the 'llama-index.core' library, including 'SimpleDirectoryReader', and integrates with AI services through 'ollama' and 'groq'. These imports are foundational for data loading, processing, and interaction with large language models. ```python import os import json from llama_index.core import SimpleDirectoryReader import ollama from groq import Groq ``` -------------------------------- ### Execute Document Summarization Source: https://github.com/iyaja/llama-fs/blob/main/notebooks/data_loading_processing.ipynb This snippet demonstrates how to call the `get_doc_summaries` function to process documents in a specified directory and then prints the resulting file summaries. ```python file_summaries = get_doc_summaries("../") ``` ```python file_summaries ``` -------------------------------- ### Read and Process Documents with Llama Index in Python Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb This snippet illustrates how to read documents from a directory using `SimpleDirectoryReader` and process them with a `TokenTextSplitter`. It iterates through files, splits text content into chunks, and stores processed documents. ```python reader = SimpleDirectoryReader(input_dir=".", recursive=True) all_docs = [] for docs in reader.iter_data(): # 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) ``` -------------------------------- ### JavaScript: Electron App API for File Organization Source: https://context7.com/iyaja/llama-fs/llms.txt Provides JavaScript functions for interacting with a local file organization API, likely served by a backend. It includes functions to initiate batch organization via POST requests and to commit approved changes, facilitating a drag-and-drop approval workflow in a desktop application. ```javascript // Batch organization from Electron frontend async function organizeFiles() { const response = await fetch('http://localhost:8000/batch', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ path: '/Users/username/Downloads', instruction: 'Organize by date and category', incognito: false }) }); const suggestions = await response.json(); // suggestions = [ // {src_path: "photo.jpg", dst_path: "2024-03/photos/sunset.jpg", summary: "..."}, // {src_path: "doc.pdf", dst_path: "2024-03/documents/invoice.pdf", summary: "..."} // ] return suggestions; } // Commit approved changes async function commitChanges(changes) { for (const change of changes) { await fetch('http://localhost:8000/commit', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ base_path: '/Users/username/Downloads', src_path: change.src_path, dst_path: change.dst_path }) }); } } ``` -------------------------------- ### Python: Image Analysis with Moondream via Ollama Source: https://context7.com/iyaja/llama-fs/llms.txt Analyzes image files using the Moondream vision model served through Ollama. It loads images as `ImageDocument` objects and uses a synchronous utility function `summarize_image_document_sync` to generate textual descriptions, enabling content-based file organization. ```python from llama_index.core.schema import ImageDocument from src.loader import summarize_image_document_sync import ollama def analyze_images(image_paths): client = ollama.Client() for image_path in image_paths: doc = ImageDocument(image_path=image_path) summary = summarize_image_document_sync(doc, client) print(f"Image: {summary['file_path']}") print(f"Description: {summary['summary']}") print("-" * 80) # Example output: # Image: /photos/IMG_5234.jpg # Description: A sunset over the ocean with vibrant orange and pink colors reflecting on the water surface # -------------------------------------------------------------------------------- # Image: /photos/IMG_5235.jpg # Description: Group photo of five people standing in front of mountain landscape during daytime # -------------------------------------------------------------------------------- if __name__ == "__main__": analyze_images([ "/Users/username/Photos/IMG_5234.jpg", "/Users/username/Photos/IMG_5235.jpg" ]) ``` -------------------------------- ### POST /batch - Batch Processing Endpoint Source: https://context7.com/iyaja/llama-fs/llms.txt Analyzes a directory and generates an optimized file organization structure using AI-powered content summarization and intelligent path generation. This is useful for one-time directory reorganization. ```APIDOC ## POST /batch ### Description Analyzes a directory and generates an optimized file organization structure using AI-powered content summarization and intelligent path generation. ### Method POST ### Endpoint `/batch` ### Parameters #### Request Body - **path** (string) - Required - The directory path to analyze. - **instruction** (string) - Required - The instruction for organizing files (e.g., "Organize by project and date"). - **incognito** (boolean) - Optional - If true, runs in incognito mode. ### Request Example ```json { "path": "/Users/username/Downloads/", "instruction": "Organize by project and date", "incognito": false } ``` ### Response #### Success Response (200) - **src_path** (string) - The original path of the file. - **dst_path** (string) - The suggested destination path for the file. - **summary** (string) - A summary of the file's content. #### Response Example ```json [ { "src_path": "document1.pdf", "dst_path": "2024-03/financial/tax_document_2024-03-15.pdf", "summary": "Tax form 1040 for fiscal year 2023" }, { "src_path": "IMG_5234.jpg", "dst_path": "2024-03/photos/vacation_beach_sunset.jpg", "summary": "Sunset photograph taken at beach during vacation" } ] ``` ``` -------------------------------- ### File Tree Generation Function - Python Source: https://context7.com/iyaja/llama-fs/llms.txt This Python function creates optimized directory structures using LLM-based analysis of file content summaries and naming conventions. It utilizes `get_dir_summaries` and `create_file_tree`, and integrates with AgentOps for monitoring. It returns a list of dictionaries mapping source paths to destination paths. ```python from src.tree_generator import create_file_tree from src.loader import get_dir_summaries import asyncio import os from groq import Groq async def generate_structure(): # Get file summaries summaries = await get_dir_summaries("/Users/username/messy_folder") # Create AgentOps session for monitoring import agentops session = agentops.start_session(tags=["file-organization"]) # Generate optimized file tree files = create_file_tree(summaries, session) for file in files: print(f"{file['src_path']} → {file['dst_path']}") agentops.end_session("Success") return files ``` -------------------------------- ### Configure TokenTextSplitter in Python Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb This snippet shows how to configure and instantiate a `TokenTextSplitter` from Llama Index. The `chunk_size` parameter determines the maximum number of tokens in each resulting text chunk. ```python splitter = TokenTextSplitter(chunk_size=6144) ``` -------------------------------- ### Batch Processing Endpoint - API Request Source: https://context7.com/iyaja/llama-fs/llms.txt This endpoint analyzes a directory and generates an optimized file organization structure using AI-powered content summarization and intelligent path generation. It takes a path, an instruction, and an incognito flag as input and returns a list of proposed file movements with summaries. ```bash curl -X POST http://127.0.0.1:8000/batch \ -H "Content-Type: application/json" \ -d '{ "path": "/Users/username/Downloads/", "instruction": "Organize by project and date", "incognito": false }' ``` -------------------------------- ### Set Groq API Key Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb Sets the GROQ_API_KEY environment variable. This is crucial for authenticating with the Groq API, allowing the application to make requests to their models. ```python os.environ["GROQ_API_KEY"] = "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" ``` -------------------------------- ### Stream Watch Request to Local Server Source: https://github.com/iyaja/llama-fs/blob/main/scratch.ipynb Imports 'requests' and 'json'. It sets up a POST request to the '/watch' endpoint of a local server, sending a directory path. The response is streamed, and each line is decoded, parsed as JSON, and printed. Includes error handling for HTTP status codes. ```python import requests import json url = "http://127.0.0.1:8000/watch" data = {"path": "/Users/iyaja/Git/llama-fs/sample_data"} with requests.post(url, json=data, stream=True) as r: r.raise_for_status() # This will raise an exception for HTTP errors for line in r.iter_lines(decode_unicode=True): if line: # Load the JSON object from the line print(json.loads(line)) ``` -------------------------------- ### Python Document Processing and Summary Generation Source: https://github.com/iyaja/llama-fs/blob/main/notebooks/data_loading_processing.ipynb This Python script defines functions to load documents from a directory, process their metadata, query AI models for content summaries, and merge summaries with metadata. It utilizes libraries like llama-index, groq, and ollama. The main function `get_doc_summaries` orchestrates these steps, returning a list of dictionaries, each containing file details and its summary. ```Python import json import os from collections import defaultdict from llama_index.core import SimpleDirectoryReader import ollama from groq import Groq def get_doc_summaries(path: str) -> List[Dict[str, str, str, str, str, str]]: doc_dicts = load_documents(path) metadata = process_metadata(doc_dicts) summaries = query_summaries(doc_dicts) file_summaries = merge_summary_documents(summaries, metadata) return file_summaries # [ # { # file_path: # file_name: # file_size: # content: # summary: # creation_date: # last_modified_date: # } # ] def load_documents(path: str): # reader = SimpleDirectoryReader(input_dir=path, recursive=True) reader = SimpleDirectoryReader(input_dir=path) documents = reader.load_data() doc_dicts = [{"content": d.text, **d.metadata} for d in documents] return doc_dicts def process_metadata(doc_dicts): file_seen = set() metadata_list = [] for doc in doc_dicts: if doc["file_path"] not in file_seen: file_seen.add(doc["file_path"]) metadata_list.append(doc) return metadata_list def query_summaries(doc_dicts): client = Groq( api_key=os.environ.get("GROQ_API_KEY"), ) 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": [ {{ "file_path": "path to the file including name", "summary": "summary of the content" }} ] }} ``` """.strip() 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"}, ) summaries = json.loads(chat_completion.choices[0].message.content)["files"] return summaries def merge_summary_documents(summaries, metadata_list): list_summaries = defaultdict(list) for item in summaries: list_summaries[item["file_path"]].append(item["summary"]) file_summaries = {path: '. '.join(summaries) for path, summaries in list_summaries.items()} file_list = [{"summary": file_summaries[file["file_path"]], **file} for file in metadata_list] return file_list ```