Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions docs/docs/examples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ to run.
| Category | What it shows |
| -------- | ------------- |
| `intrinsics/` | [Guardian adapter functions](../how-to/safety-guardrails.md): `guardian_check()` for harm, jailbreak, social bias, groundedness; `policy_guardrails()`; `factuality_detection()` / `factuality_correction()` |
| `requirements/` | Validating code generation and plotting with execution tiers and security policies |
| `safety/` | *(Examples removed — see [Guardian how-to guide](../how-to/safety-guardrails.md) for the current API. The `RepairTemplateStrategy` gap is tracked in [#1071](https://github.com/generative-computing/mellea/issues/1071).)* |

### Integration and deployment
Expand All @@ -81,12 +82,14 @@ to run.
| `aLora/` | Training aLoRA adapters for fast constraint checking; performance optimisation |
| `intrinsics/` | *(Non-Guardian)* Answer relevance, hallucination detection, citation validation — specialised adapter-backed checks. For Guardian safety functions see [Safety and validation](#safety-and-validation) above |
| `granite-switch/` | Running adapter functions via OpenAI backend with Granite Switch embedded adapters |
| `sampling/` | Pre-configured sampling presets for Python code generation and data plotting |
| `sofai/` | Two-tier sampling: fast-model iteration with escalation to a slow model; cost optimisation |

### Multimodal

| Category | What it shows |
| -------- | ------------- |
| `audio_text_models/` | Audio-text-to-text models with `AudioBlock` and llama-server |
| `image_text_models/` | Vision-language models, `ImageBlock`, multimodal prompting, backend support matrix |

### Observability
Expand Down
42 changes: 7 additions & 35 deletions docs/examples/agents/README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
# Agent Examples

This directory contains examples of implementing agent patterns with Mellea, specifically the ReACT (Reasoning and Acting) pattern.
Examples demonstrating agent architectures and the ReACT (Reasoning and Acting) loop in Mellea.

## Files
## Examples

### [react.py](./react/react_from_scratch/react.py)
A complete implementation of the ReACT agent pattern that combines reasoning and action in an iterative loop. The agent:
- Thinks about what to do next
- Selects an appropriate tool/action
- Generates arguments for the tool
- Observes the result
- Determines if the goal is achieved

**Key Features:**
- Custom `ReactTool` and `ReactToolbox` classes for tool management
- Dynamic tool selection using Pydantic schemas
- Iterative thought-action-observation loop
- Example with weather lookup tools

**Usage:**
```python
python docs/examples/agents/react/react_from_scratch/react.py
```

### [react_instruct.py](./react/react_from_scratch/react_instruct.py)
An alternative implementation of the ReACT pattern using Mellea's instruct-validate-repair paradigm.

## Concepts Demonstrated

- **Tool Management**: Creating and organizing tools for agent use
- **Dynamic Prompting**: Building system prompts with tool descriptions
- **Chat Context**: Using `ChatContext` for multi-turn conversations
- **Structured Output**: Using Pydantic models for type-safe responses
- **Iterative Reasoning**: Implementing thought-action-observation loops

## Related Documentation

- See `mellea/stdlib/requirements/tool_reqs.py` for tool requirements
| File | Description |
| :--- | :--- |
| [`react/react_using_mellea.py`](./react/react_using_mellea.py) | ReACT agent using Mellea's stdlib `react()` framework with LangChain tool integration. |
Comment thread
planetf1 marked this conversation as resolved.
Resolved
| [`react/react_from_scratch/react.py`](./react/react_from_scratch/react.py) | ReACT loop built from scratch using custom tool abstractions and Pydantic schemas. |
| [`react/react_from_scratch/react_instruct.py`](./react/react_from_scratch/react_instruct.py) | ReACT loop implemented using Mellea's instruct-validate-repair paradigm. |
8 changes: 8 additions & 0 deletions docs/examples/agents/react/react_from_scratch/react.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# pytest: ollama, e2e, qualitative
"""ReACT agent implementation built from scratch.

Demonstrates implementing the ReACT (Reasoning and Acting) loop from first
principles, combining tool selection, argument generation, and observation.

Usage:
uv run python docs/examples/agents/react/react_from_scratch/react.py
"""

import datetime
import inspect
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# pytest: ollama, e2e, qualitative
"""ReACT agent using instruct-validate-repair.

Demonstrates implementing the ReACT (Reasoning and Acting) loop using Mellea's
instruct-validate-repair paradigm for robust tool calling and reasoning.

Usage:
uv run python docs/examples/agents/react/react_from_scratch/react_instruct.py
"""

import datetime
import inspect
Expand Down
8 changes: 7 additions & 1 deletion docs/examples/agents/react/react_using_mellea.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# pytest: ollama, e2e, qualitative, slow
"""ReACT agent using Mellea's built-in react() framework.

"""React examples using the Mellea library's framework."""
Demonstrates the ReACT (Reasoning and Acting) loop with tool integration,
using Mellea's stdlib react() function and LangChain tool interoperability.

Usage:
uv run python docs/examples/agents/react/react_using_mellea.py
Comment thread
planetf1 marked this conversation as resolved.
Resolved
"""

import asyncio

Expand Down
10 changes: 10 additions & 0 deletions docs/examples/sampling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Sampling Examples

Pre-configured sampling presets and strategies for constrained generation tasks such as Python code generation and data plotting.
Comment thread
rafvasq marked this conversation as resolved.
Resolved

## Examples

| File | Description |
| :--- | :--- |
| [`python_code_generation_preset.py`](./python_code_generation_preset.py) | Python code generation preset bundling extraction, syntax, and execution requirements with automatic repair feedback. |
| [`python_plotting_preset.py`](./python_plotting_preset.py) | Matplotlib plotting preset bundling headless backend setup, plot file output validation, and sandbox execution. |
Comment thread
planetf1 marked this conversation as resolved.
Resolved

@planetf1 planetf1 Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These examples need a running Ollama with the default model (granite4.2:3b), and the plotting ones need matplotlib, which isn't in any extra. example_without_output_path is the only one that runs generated code in Docker, so it also needs llm-sandbox (mellea[sandbox]), a running Docker daemon, and a sandbox image that runs on your machine. The default image is amd64-only, which can matter on Apple Silicon. A short prerequisites line would help.

It would also help that, when the sandbox can't run, the failure is hard to spot. The run reports Valid: 5/6 with the unmet requirement shown only as "sandbox execution with policy", and the underlying error never appears in the output. The script still prints "✓ completed", because it doesn't check the sampling result.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the scripts to include ollama and dependency install commands in their Prerequisites. I think this helps towards keeping the examples self-contained and in case future examples are added under this directory that may not need the same prerequisites.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partially fixed. I walked through these as someone running the examples fresh:

  1. ollama pull is listed before ollama serve, but pull needs the server up. Worth phrasing for users running Ollama as a service or via the desktop app too, e.g. "Ollama running (service, desktop app, or ollama serve), then ollama pull granite4.2:3b".
  2. pip install matplotlib numpy beside a uv run Usage line is a trap: uv-managed venvs don't include pip, and a system pip installs into a different environment than uv run uses. uv pip install matplotlib numpy is the drop-in fix. (A plotting extra would also fix the --extra plotting reference in presets.py:220, but that's a follow-up.)
  3. The plotting module's Prerequisites are incomplete for its own Usage line: running the module executes main(), which includes example_without_output_path, so it needs Docker and uv sync --extra sandbox. Those are only in that function's docstring. The amd64-only default sandbox image note is also still missing; on Apple Silicon that example can't pass as shipped even with Docker running, and the preset installs no packages into the container, so matplotlib may be missing there too.

On failure visibility: the docstring note is good, but the behaviour is unchanged. A newcomer sees four check lines, a Valid: 5/6 buried in the output, then a completed message, and reasonably concludes everything passed. SamplingResult.success exists (core/sampling.py:41), so checking it before printing a pass would close the gap. If documenting rather than fixing is preferred, I'd drop "fails silently" in the note, since the next sentence describes exactly what is reported.

P.S. while checking model tags: requirements/README.md:31 says ollama pull granite4.1:3b, but its examples use the default session, which is granite4.2:3b (session.py:271). Anyone following that README pulls the wrong 2 GB model and then triggers an auto-pull of the right one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reordered to ollama serve/ollama pull across all five scripts and switched to uv pip install matplotlib numpy.

The plotting module's Prerequisites now include uv sync --extra sandbox, and the docstring notes the Docker requirement and the amd64-only image.

On failure visibility, I went with the second option for this PR and dropped the "fails silently" message. The --extra plotting hint is tracked in the issue I opened #1763.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — the Prerequisites blocks and the "fails silently" rewording read well. Three residuals from this thread:

  1. requirements/README.md still points at the wrong model: granite4.1:3b at lines 31, 34, 144, 147, and 417. Its examples use the default session, which is granite4.2:3b (session.py:271, model_ids.py:119). Anyone following that README pulls the wrong ~2 GB model, then the session auto-pulls the right one. Four-line fix, and it's a README this PR already touches.

  2. Whether example_without_output_path can pass as shipped is still open, and I suspect it can't even with Docker running. DOCKER_POLICY (execution_policy.py:214) enables package installation but sets no packages list (packages defaults to empty, execution_policy.py:113), so LLMSandboxEnvironment installs nothing into the container (interpreter.py:354). If llm-sandbox's default image (ghcr.io/vndee/sandbox-python-311-bullseye) doesn't ship matplotlib, the sandbox requirement fails for a reason no prerequisite fixes. Could you verify, or note it in the docstring? I haven't pulled the image to check.

  3. Minor: the reorder to ollama serve / ollama pull fixes the order but isn't runnable as a block, since serve blocks the terminal. Phrasing like "Ollama running (service, desktop app, or ollama serve), then ollama pull granite4.2:3b" would cover it. Happy to drop this if you prefer the current form.

Separately, while checking the head merge: sampling/ is now listed twice in docs/docs/examples/index.md — the pre-existing row at line 83 and the row this PR adds at line 87. The tip commit deduped the requirements/ row; this one looks missed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed 1 and 2 along with the sampling/ duplicate. I'll leave ollama commands as-is for now, it's at least implied that an ollama server should be running by the command itself.

19 changes: 7 additions & 12 deletions docs/examples/sampling/python_code_generation_preset.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
# pytest: e2e, qualitative, ollama
"""Example demonstrating python_code_generation_sampling() preset.

Shows how the preset bundles requirements and feedback into a reusable
configuration for Python code generation tasks, with automatic repair on failure.

The preset handles:
1. Code extraction validation
2. Syntax validation
3. Execution validation with optional sandbox isolation
4. Import restrictions (optional allowlist)
5. Output size limits
6. Model-friendly repair feedback for each failure type
"""Python code generation sampling preset with automatic repair.

Demonstrates python_code_generation_sampling(), which bundles extraction, syntax,
and execution requirements with repair feedback into a reusable configuration.

Usage:
uv run python docs/examples/sampling/python_code_generation_preset.py
"""

import mellea
Expand Down
25 changes: 8 additions & 17 deletions docs/examples/sampling/python_plotting_preset.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
# pytest: e2e, qualitative, ollama
"""Example demonstrating python_plotting_sampling() preset.

Shows how the plotting preset bundles Python code generation requirements with
matplotlib-specific constraints for headless rendering and file output validation.

The preset validates:
1. Python code extraction and syntax
2. Matplotlib headless backend configuration (matplotlib.use('Agg'))
3. Plot file output (savefig() call with expected path)
4. Code execution with sandbox isolation (enabled by default)
5. Import restrictions and output size limits

Plotting typically requires more repair iterations and stricter sandboxing than
general code generation, which this preset reflects in its defaults:
- loop_budget=3 (vs. 2 for code generation)
- use_sandbox=True (vs. False for code generation)
- timeout_seconds=10 (vs. 5 for code generation)
"""Matplotlib plotting sampling preset with headless validation.

Demonstrates python_plotting_sampling(), which bundles Python code generation
requirements with headless matplotlib backend configuration, savefig() output
validation, and execution sandboxing.

Usage:
uv run python docs/examples/sampling/python_plotting_preset.py
"""

import tempfile
Expand Down
Loading