diff --git a/docs/_config.yml b/docs/_config.yml index ee6af41..5c1855a 100644 --- a/docs/_config.yml +++ b/docs/_config.yml @@ -5,6 +5,9 @@ description: Minimalist LLM Framework in 100 Lines, Enabling LLMs to Program The # Theme settings remote_theme: just-the-docs/just-the-docs +# Navigation +nav_sort: case_sensitive + # Aux links (shown in upper right) aux_links: "View on GitHub": @@ -17,10 +20,4 @@ mermaid: version: "9.1.3" # Pick the version you want # Default configuration config: | - directionLR - - -callouts: - warning: - title: Warning - color: red + directionLR \ No newline at end of file diff --git a/docs/async.md b/docs/async.md new file mode 100644 index 0000000..c9d265a --- /dev/null +++ b/docs/async.md @@ -0,0 +1,61 @@ +--- +layout: default +title: "(Advanced) Async" +parent: "Core Abstraction" +nav_order: 5 +--- + +# (Advanced) Async + +**Async** Nodes implement `prep_async()`, `exec_async()`, `exec_fallback_async()`, and/or `post_async()`. This is useful for: + +1. **prep_async()** + - For *fetching/reading data (files, APIs, DB)* in an I/O-friendly way. + +2. **exec_async()** + - Typically used for async LLM calls. + +3. **post_async()** + - For *awaiting user feedback*, *coordinating across multi-agents* or any additional async steps after `exec_async()`. + + +**Note**: `AsyncNode` must be wrapped in `AsyncFlow`. `AsyncFlow` can also include regular (sync) nodes. + +### Example + +```python +class SummarizeThenVerify(AsyncNode): + async def prep_async(self, shared): + # Example: read a file asynchronously + doc_text = await read_file_async(shared["doc_path"]) + return doc_text + + async def exec_async(self, prep_res): + # Example: async LLM call + summary = await call_llm_async(f"Summarize: {prep_res}") + return summary + + async def post_async(self, shared, prep_res, exec_res): + # Example: wait for user feedback + decision = await gather_user_feedback(exec_res) + if decision == "approve": + shared["summary"] = exec_res + return "approve" + return "deny" + +summarize_node = SummarizeThenVerify() +final_node = Finalize() + +# Define transitions +summarize_node - "approve" >> final_node +summarize_node - "deny" >> summarize_node # retry + +flow = AsyncFlow(start=summarize_node) + +async def main(): + shared = {"doc_path": "document.txt"} + await flow.run_async(shared) + print("Final Summary:", shared.get("summary")) + +asyncio.run(main()) +``` \ No newline at end of file diff --git a/docs/batch.md b/docs/batch.md new file mode 100644 index 0000000..b263845 --- /dev/null +++ b/docs/batch.md @@ -0,0 +1,107 @@ +--- +layout: default +title: "Batch" +parent: "Core Abstraction" +nav_order: 4 +--- + +# Batch + +**Batch** makes it easier to handle large inputs in one Node or **rerun** a Flow multiple times. Handy for: +- **Chunk-based** processing (e.g., splitting large texts). +- **Multi-file** processing. +- **Iterating** over lists of params (e.g., user queries, documents, URLs). + +## 1. BatchNode + +A **BatchNode** extends `Node` but changes `prep()` and `exec()`: + +- **`prep(shared)`**: returns an **iterable** (e.g., list, generator). +- **`exec(item)`**: called **once** per item in that iterable. +- **`post(shared, prep_res, exec_res_list)`**: after all items are processed, receives a **list** of results (`exec_res_list`) and returns an **Action**. + + +### Example: Summarize a Large File + +```python +class MapSummaries(BatchNode): + def prep(self, shared): + # Suppose we have a big file; chunk it + content = shared["data"].get("large_text.txt", "") + chunk_size = 10000 + chunks = [content[i:i+chunk_size] for i in range(0, len(content), chunk_size)] + return chunks + + def exec(self, chunk): + prompt = f"Summarize this chunk in 10 words: {chunk}" + summary = call_llm(prompt) + return summary + + def post(self, shared, prep_res, exec_res_list): + combined = "\n".join(exec_res_list) + shared["summary"]["large_text.txt"] = combined + return "default" + +map_summaries = MapSummaries() +flow = Flow(start=map_summaries) +flow.run(shared) +``` + +--- + +## 2. BatchFlow + +A **BatchFlow** runs a **Flow** multiple times, each time with different `params`. Think of it as a loop that replays the Flow for each parameter set. + + +### Example: Summarize Many Files + +```python +class SummarizeAllFiles(BatchFlow): + def prep(self, shared): + # Return a list of param dicts (one per file) + filenames = list(shared["data"].keys()) # e.g., ["file1.txt", "file2.txt", ...] + return [{"filename": fn} for fn in filenames] + +# Suppose we have a per-file Flow (e.g., load_file >> summarize >> reduce): +summarize_file = SummarizeFile(start=load_file) + +# Wrap that flow into a BatchFlow: +summarize_all_files = SummarizeAllFiles(start=summarize_file) +summarize_all_files.run(shared) +``` + +### Under the Hood +1. `prep(shared)` returns a list of param dicts—e.g., `[{filename: "file1.txt"}, {filename: "file2.txt"}, ...]`. +2. The **BatchFlow** loops through each dict. For each one: + - It merges the dict with the BatchFlow’s own `params`. + - It calls `flow.run(shared)` using the merged result. +3. This means the sub-Flow is run **repeatedly**, once for every param dict. + +--- + +## 3. Nested or Multi-Level Batches + +You can nest a **BatchFlow** in another **BatchFlow**. For instance: +- **Outer** batch: returns a list of diretory param dicts (e.g., `{"directory": "/pathA"}`, `{"directory": "/pathB"}`, ...). +- **Inner** batch: returning a list of per-file param dicts. + +At each level, **BatchFlow** merges its own param dict with the parent’s. By the time you reach the **innermost** node, the final `params` is the merged result of **all** parents in the chain. This way, a nested structure can keep track of the entire context (e.g., directory + file name) at once. + +```python + +class FileBatchFlow(BatchFlow): + def prep(self, shared): + directory = self.params["directory"] + files = [f for f in os.listdir(directory) if f.endswith(".txt")] + return [{"filename": f} for f in files] + +class DirectoryBatchFlow(BatchFlow): + def prep(self, shared): + directories = [ "/path/to/dirA", "/path/to/dirB"] + return [{"directory": d} for d in directories] + + +inner_flow = FileBatchFlow(start=MapSummaries()) +outer_flow = DirectoryBatchFlow(start=inner_flow) +``` \ No newline at end of file diff --git a/docs/communication.md b/docs/communication.md new file mode 100644 index 0000000..ebba2e2 --- /dev/null +++ b/docs/communication.md @@ -0,0 +1,138 @@ +--- +layout: default +title: "Communication" +parent: "Core Abstraction" +nav_order: 3 +--- + +# Communication + +Nodes and Flows **communicate** in two ways: + +1. **Shared Store** – A global data structure (often an in-mem dict) that all nodes can read from and write to. Every Node’s `prep()` and `post()` methods receive the **same** `shared` store. +2. **Params** – Each node and Flow has a `params` dict assigned by the **parent Flow**. Params mostly serve as identifiers, letting each node/flow know what task it’s assigned. + +If you know memory management, **Shared Store** is like a **heap** shared across function calls, while **Params** is like a **stack** assigned by parent function calls. + + + +> **Why not use other communication models like Message Passing?** *Message passing* works well for simple DAGs, but with *nested graphs* (Flows containing Flows, repeated or cyclic calls), routing messages becomes hard to maintain. A shared store keeps the design simple and easy. +{: .note } + +--- + +## 1. Shared Store + +### Overview + +A shared store is typically an in-mem dictionary, like: +```python +shared = {"data": {}, "summary": {}, "config": {...}, ...} +``` + +It can also contain local file handlers, DB connections, or a combination for persistence. We recommend deciding the data structure or DB schema first based on your app requirements. + +### Example + +```python +class LoadData(Node): + def prep(self, shared): + # Suppose we read from disk or an API + shared["data"]["my_file.txt"] = "Some text content" + return None + +class Summarize(Node): + def prep(self, shared): + # We can read what LoadData wrote + content = shared["data"].get("my_file.txt", "") + return content + + def exec(self, prep_res): + prompt = f"Summarize: {prep_res}" + summary = call_llm(prompt) + return summary + + def post(self, shared, prep_res, exec_res): + shared["summary"]["my_file.txt"] = exec_res + return "default" + +load_data = LoadData() +summarize = Summarize() +load_data >> summarize +flow = Flow(start=load_data) + +shared = {} +flow.run(shared) +``` + +Here: +- `LoadData` writes to `shared["data"]`. +- `Summarize` reads from the same location. +No special data-passing—just the same `shared` object. + +--- + +## 2. Params + +**Params** let you store *per-Node* or *per-Flow* config that doesn't need to live in the shared store. They are: +- **Immutable** during a Node’s run cycle (i.e., they don’t change mid-`prep`, `exec`, `post`). +- **Set** via `set_params()`. +- **Cleared** and updated each time a parent Flow calls it. + + +> Only set the uppermost Flow params because others will be overwritten by the parent Flow. If you need to set child node params, see [Batch](./batch.md). +{: .warning } + +Typically, **Params** are identifiers (e.g., file name, page number). Use them to fetch the task you assigned or write to a specific part of the shared store. + +### Example + +```python +# 1) Create a Node that uses params +class SummarizeFile(Node): + def prep(self, shared): + # Access the node's param + filename = self.params["filename"] + return shared["data"].get(filename, "") + + def exec(self, prep_res): + prompt = f"Summarize: {prep_res}" + return call_llm(prompt) + + def post(self, shared, prep_res, exec_res): + filename = self.params["filename"] + shared["summary"][filename] = exec_res + return "default" + +# 2) Set params +node = SummarizeFile() + +# 3) Set Node params directly (for testing) +node.set_params({"filename": "doc1.txt"}) +node.run(shared) + +# 4) Create Flow +flow = Flow(start=node) + +# 5) Set Flow params (overwrites node params) +flow.set_params({"filename": "doc2.txt"}) +flow.run(shared) # The node summarizes doc2, not doc1 +``` + +--- + +## 3. Shared Store vs. Params + +Think of the **Shared Store** like a heap and **Params** like a stack. + +- **Shared Store**: + - Public, global. + - You can design and populate ahead, e.g., for the input to process. + - Great for data results, large content, or anything multiple nodes need. + - Keep it tidy—structure it carefully (like a mini schema). + +- **Params**: + - Local, ephemeral. + - Passed in by parent Flows. You should only set it for the uppermost flow. + - Perfect for small values like filenames or numeric IDs. + - Do **not** persist across different nodes and are reset. \ No newline at end of file diff --git a/docs/core_abstraction.md b/docs/core_abstraction.md new file mode 100644 index 0000000..6043574 --- /dev/null +++ b/docs/core_abstraction.md @@ -0,0 +1,6 @@ +--- +layout: default +title: "Core Abstraction" +nav_order: 2 +has_children: true +--- \ No newline at end of file diff --git a/docs/flow.md b/docs/flow.md new file mode 100644 index 0000000..6ac6208 --- /dev/null +++ b/docs/flow.md @@ -0,0 +1,171 @@ +--- +layout: default +title: "Flow" +parent: "Core Abstraction" +nav_order: 2 +--- + +# Flow + +A **Flow** orchestrates how Nodes connect and run, based on **Actions** returned from each Node’s `post()` method. You can chain Nodes in a sequence or create branching logic depending on the **Action** string. + +## 1. Action-based Transitions + +Each Node's `post(shared, prep_res, exec_res)` method returns an **Action** string. By default, if `post()` doesn't explicitly return anything, we treat that as `"default"`. + +You define transitions with the syntax: + +1. Basic default transition: `node_a >> node_b` + This means if `node_a.post()` returns `"default"` (or `None`), go to `node_b`. + (Equivalent to `node_a - "default" >> node_b`) + +2. Named action transition: `node_a - "action_name" >> node_b` + This means if `node_a.post()` returns `"action_name"`, go to `node_b`. + +It’s possible to create loops, branching, or multi-step flows. + +## 2. Creating a Flow + +A **Flow** begins with a **start** node (or flow). You call `Flow(start=some_node)` to specify the entry point. When you call `flow.run(shared)`, it executes the first node, looks at its `post()` return Action, follows the corresponding transition, and continues until there’s no next node or you explicitly stop. + +### Example: Simple Sequence + +Here’s a minimal flow of two nodes in a chain: + +```python +node_a >> node_b +flow = Flow(start=node_a) +flow.run(shared) +``` + +- When you run the flow, it executes `node_a`. +- Suppose `node_a.post()` returns `"default"`. +- The flow then sees `"default"` Action is linked to `node_b` and runs `node_b`. +- If `node_b.post()` returns `"default"` but we didn’t define `node_b >> something_else`, the flow ends there. + +### Example: Branching & Looping + +Here's a simple expense approval flow that demonstrates branching and looping. The `ReviewExpense` node can return three possible Actions: + +- `"approved"`: expense is approved, move to payment processing +- `"needs_revision"`: expense needs changes, send back for revision +- `"rejected"`: expense is denied, finish the process + +We can wire them like this: + +```python +# Define the flow connections +review - "approved" >> payment # If approved, process payment +review - "needs_revision" >> revise # If needs changes, go to revision +review - "rejected" >> finish # If rejected, finish the process + +revise >> review # After revision, go back for another review +payment >> finish # After payment, finish the process + +flow = Flow(start=review) +``` + +Let's see how it flows: + +1. If `review.post()` returns `"approved"`, the expense moves to `payment` node +2. If `review.post()` returns `"needs_revision"`, it goes to `revise` node, which then loops back to `review` +3. If `review.post()` returns `"rejected"`, it moves to `finish` node and stops + +```mermaid +flowchart TD + review[Review Expense] -->|approved| payment[Process Payment] + review -->|needs_revision| revise[Revise Report] + review -->|rejected| finish[Finish Process] + + revise --> review + payment --> finish +``` + +### Running Individual Nodes vs. Running a Flow + +- `node.run(shared)`: Just runs that node alone (calls `prep()`, `exec()`, `post()`), returns an Action. +- `flow.run(shared)`: Executes from the start node, follows Actions to the next node, and so on until the flow can’t continue (no next node or no next Action). + + +> node.run(shared) **does not** proceed automatically to the successor and may use incorrect parameters. +> This is mainly for debugging or testing a single node. +> Always use `flow.run(...)` in production to ensure the full pipeline runs correctly. +{: .warning } + +## 3. Nested Flows + +A **Flow** can act like a Node, which enables powerful composition patterns. This means you can: + +1. Use a Flow as a Node within another Flow's transitions. +2. Combine multiple smaller Flows into a larger Flow for reuse. +3. Node `params` will be a merging of **all** parents' `params`. + +### Basic Flow Nesting + +Here's how to connect a flow to another node: + +```python +# Create a sub-flow +node_a >> node_b +subflow = Flow(start=node_a) + +# Connect it to another node +subflow >> node_c + +# Create the parent flow +parent_flow = Flow(start=subflow) +``` + +When `parent_flow.run()` executes: +1. It starts `subflow` +2. `subflow` runs through its nodes (`node_a` then `node_b`) +3. After `subflow` completes, execution continues to `node_c` + +### Example: Order Processing Pipeline + +Here's a practical example that breaks down order processing into nested flows: + +```python +# Payment processing sub-flow +validate_payment >> process_payment >> payment_confirmation +payment_flow = Flow(start=validate_payment) + +# Inventory sub-flow +check_stock >> reserve_items >> update_inventory +inventory_flow = Flow(start=check_stock) + +# Shipping sub-flow +create_label >> assign_carrier >> schedule_pickup +shipping_flow = Flow(start=create_label) + +# Connect the flows into a main order pipeline +payment_flow >> inventory_flow >> shipping_flow + +# Create the master flow +order_pipeline = Flow(start=payment_flow) + +# Run the entire pipeline +order_pipeline.run(shared_data) +``` + +This creates a clean separation of concerns while maintaining a clear execution path: + +```mermaid +flowchart LR + subgraph order_pipeline[Order Pipeline] + subgraph paymentFlow["Payment Flow"] + A[Validate Payment] --> B[Process Payment] --> C[Payment Confirmation] + end + + subgraph inventoryFlow["Inventory Flow"] + D[Check Stock] --> E[Reserve Items] --> F[Update Inventory] + end + + subgraph shippingFlow["Shipping Flow"] + G[Create Label] --> H[Assign Carrier] --> I[Schedule Pickup] + end + + paymentFlow --> inventoryFlow + inventoryFlow --> shippingFlow + end +``` diff --git a/docs/index.md b/docs/index.md index bc50f0a..7099c1f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -4,14 +4,60 @@ title: "Home" nav_order: 1 --- -> **Heads up!** This is a note callout. +# Mini LLM Flow + +A [100-line](https://github.com/zachary62/miniLLMFlow/blob/main/minillmflow/__init__.py) minimalist LLM framework for *Agents, Task Decomposition, RAG, etc*. + + +We model the LLM workflow as a **Nested Directed Graph**: +- **Nodes** handle simple (LLM) tasks. +- Nodes connect through **Actions** (labeled edges) for *Agents*. +- **Flows** orchestrate a directed graph of Nodes for *Task Decomposition*. +- A Flow can be used as a Node (for **Nesting**). +- **Batch** Nodes/Flows for data-intensive tasks. +- **Async** Nodes/Flows allow waits or **Parallel** execution + + +
+