|
| 1 | +# AG2 |
| 2 | + |
| 3 | +**Extra:** `pip install "unplug-ai[ag2]"` |
| 4 | +**Module:** `unplug.integrations.ag2` |
| 5 | + |
| 6 | +[AG2](https://docs.ag2.ai/) (the community fork of AutoGen, imported as `autogen`) |
| 7 | +attaches hooks to a `ConversableAgent` via `register_hook`. Unplug wires into: |
| 8 | + |
| 9 | +- **Incoming** — `process_last_received_message` scans the last received message |
| 10 | + for injection (redacts or raises). |
| 11 | +- **Outgoing** — `process_message_before_send` scans a message before it is sent |
| 12 | + to another agent, catching leaked secrets / unsafe content. |
| 13 | +- **Tools** — `ag2_guard_tool` wraps a callable so a destructive call is blocked |
| 14 | + before it runs; pair it with `register_function`. |
| 15 | + |
| 16 | +> **Not the same as `autogen`.** The `autogen` extra targets Microsoft's |
| 17 | +> `autogen-agentchat` (imported as `autogen_agentchat`). AG2 installs as the `ag2` |
| 18 | +> package and imports as `autogen` — the two can coexist. |
| 19 | +
|
| 20 | +## Register the hooks |
| 21 | + |
| 22 | +```python |
| 23 | +from autogen import ConversableAgent |
| 24 | +from unplug import Guard |
| 25 | +from unplug.integrations.hooks import AgentHooks |
| 26 | +from unplug.integrations.ag2 import register_unplug_hooks |
| 27 | + |
| 28 | +hooks = AgentHooks(Guard()) # or Guard(mode="server") |
| 29 | + |
| 30 | +agent = ConversableAgent(name="assistant", llm_config=llm_config) |
| 31 | +register_unplug_hooks(agent, hooks) # incoming + outgoing message guards |
| 32 | +``` |
| 33 | + |
| 34 | +## Tool policy |
| 35 | + |
| 36 | +```python |
| 37 | +from autogen import register_function |
| 38 | +from unplug.integrations.ag2 import ag2_guard_tool |
| 39 | + |
| 40 | +register_function( |
| 41 | + ag2_guard_tool(run_sql, hooks), |
| 42 | + caller=assistant, |
| 43 | + executor=user_proxy, |
| 44 | + description="Run a read-only SQL query", |
| 45 | +) |
| 46 | +``` |
| 47 | + |
| 48 | +A blocked tool raises before it runs, so the destructive call never executes. |
| 49 | + |
| 50 | +## Hook points |
| 51 | + |
| 52 | +| AG2 stage | Unplug | |
| 53 | +|-----------|--------| |
| 54 | +| `process_last_received_message` | `ag2_received_message_hook` | |
| 55 | +| `process_message_before_send` | `ag2_message_hook` | |
| 56 | +| both, in one call | `register_unplug_hooks` | |
| 57 | +| `register_function` | `ag2_guard_tool` | |
| 58 | +| Manual tool gate | `ag2_tool_guard` | |
| 59 | + |
| 60 | +## Hosted API |
| 61 | + |
| 62 | +```python |
| 63 | +hooks = AgentHooks(Guard(mode="server", server_api_key="sk-...")) |
| 64 | +``` |
| 65 | + |
| 66 | +## Demo (no ag2 install required) |
| 67 | + |
| 68 | +```bash |
| 69 | +python examples/ag2_hooks_demo.py |
| 70 | +``` |
| 71 | + |
| 72 | +See also: [integrations/README.md](../README.md) |
0 commit comments