Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
133 changes: 133 additions & 0 deletions .github/workflows/ag2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# This workflow comes from https://github.com/ofek/hatch-mypyc
# https://github.com/ofek/hatch-mypyc/blob/5a198c0ba8660494d02716cfc9d79ce4adfb1442/.github/workflows/test.yml
name: Test / ag2

on:
schedule:
- cron: "0 0 * * *"
pull_request:
paths:
- "integrations/ag2/**"
- "!integrations/ag2/*.md"
- ".github/workflows/ag2.yml"
push:
branches:
- main
paths:
- "integrations/ag2/**"
- "!integrations/ag2/*.md"
- ".github/workflows/ag2.yml"

defaults:
run:
working-directory: integrations/ag2

concurrency:
group: ag2-${{ github.head_ref || github.sha }}
cancel-in-progress: true

env:
PYTHONUNBUFFERED: "1"
FORCE_COLOR: "1"
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
TEST_MATRIX_OS: '["ubuntu-latest", "windows-latest", "macos-latest"]'
TEST_MATRIX_PYTHON: '["3.10", "3.14"]'

jobs:
compute-test-matrix:
runs-on: ubuntu-slim
defaults:
run:
working-directory: .
outputs:
os: ${{ steps.set.outputs.os }}
python-version: ${{ steps.set.outputs.python-version }}
steps:
- id: set
run: |
echo 'os=${{ github.event_name == 'push' && '["ubuntu-latest"]' || env.TEST_MATRIX_OS }}' >> "$GITHUB_OUTPUT"
echo 'python-version=${{ github.event_name == 'push' && '["3.10"]' || env.TEST_MATRIX_PYTHON }}' >> "$GITHUB_OUTPUT"

run:
name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }}
needs: compute-test-matrix
permissions:
contents: write
pull-requests: write
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: ${{ fromJSON(needs.compute-test-matrix.outputs.os) }}
python-version: ${{ fromJSON(needs.compute-test-matrix.outputs.python-version) }}

steps:
- name: Support longpaths
if: matrix.os == 'windows-latest'
working-directory: .
run: git config --system core.longpaths true

- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
with:
python-version: ${{ matrix.python-version }}

- name: Install Hatch
run: pip install hatch

- name: Lint
if: matrix.python-version == '3.10' && runner.os == 'Linux'
run: hatch run fmt-check && hatch run test:types

- name: Run unit tests
run: hatch run test:unit-cov-retry

- name: Store unit tests coverage
if: matrix.python-version == '3.10' && runner.os == 'Linux' && github.event_name != 'schedule'
uses: py-cov-action/python-coverage-comment-action@7188638f871f721a365d644f505d1ff3df20d683 # v3.40
with:
GITHUB_TOKEN: ${{ github.token }}
COVERAGE_PATH: integrations/ag2
SUBPROJECT_ID: ag2
COMMENT_ARTIFACT_NAME: coverage-comment-ag2
MINIMUM_GREEN: 90
MINIMUM_ORANGE: 60

- name: Run integration tests
run: hatch run test:integration-cov-append-retry

- name: Store combined coverage
if: github.event_name == 'push'
uses: py-cov-action/python-coverage-comment-action@7188638f871f721a365d644f505d1ff3df20d683 # v3.40
with:
GITHUB_TOKEN: ${{ github.token }}
COVERAGE_PATH: integrations/ag2
SUBPROJECT_ID: ag2-combined
COMMENT_ARTIFACT_NAME: coverage-comment-ag2-combined
MINIMUM_GREEN: 90
MINIMUM_ORANGE: 60

- name: Run unit tests with lowest direct dependencies
if: github.event_name != 'push'
run: |
hatch run uv pip compile pyproject.toml --resolution lowest-direct --output-file requirements_lowest_direct.txt
hatch -e test env run -- uv pip install -r requirements_lowest_direct.txt
hatch run test:unit

- name: Nightly - run unit tests with Haystack main branch
if: github.event_name == 'schedule'
run: |
hatch env prune
hatch -e test env run -- uv pip install git+https://github.com/deepset-ai/haystack.git@main
hatch run test:unit

notify-slack-on-failure:
needs: run
if: failure() && github.event_name == 'schedule'
runs-on: ubuntu-slim
steps:
- uses: deepset-ai/notify-slack-action@3cda73b77a148f16f703274198e7771340cf862b # v1
with:
slack-webhook-url: ${{ secrets.SLACK_WEBHOOK_URL_NOTIFICATIONS }}
64 changes: 64 additions & 0 deletions integrations/ag2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# ag2-haystack

[![PyPI](https://img.shields.io/pypi/v/ag2-haystack.svg)](https://pypi.org/project/ag2-haystack/)

An integration of [AG2](https://ag2.ai/) (formerly AutoGen) with [Haystack](https://haystack.deepset.ai/).

AG2 is a multi-agent conversation framework with 500K+ monthly PyPI downloads, 4,300+ GitHub stars, and 400+ contributors. This integration brings AG2's powerful multi-agent orchestration capabilities into Haystack pipelines.

## Installation

```bash
pip install ag2-haystack
```

## Usage

### Standalone

```python
import os
from haystack_integrations.components.agents.ag2 import AG2Agent

os.environ["OPENAI_API_KEY"] = "your-key"

agent = AG2Agent(
model="gpt-4o-mini",
system_message="You are a helpful research assistant.",
)

result = agent.run(query="What are the latest advances in RAG?")
print(result["reply"])
```

### In a Haystack Pipeline

```python
from haystack import Pipeline
from haystack_integrations.components.agents.ag2 import AG2Agent

pipeline = Pipeline()
pipeline.add_component("agent", AG2Agent(
model="gpt-4o-mini",
system_message="Answer questions clearly and concisely.",
))

result = pipeline.run({"agent": {"query": "Explain retrieval-augmented generation."}})
print(result["agent"]["reply"])
```

## Parameters

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `model` | str | `"gpt-4o-mini"` | LLM model name |
| `system_message` | str | `"You are a helpful AI assistant."` | System message for the assistant |
| `api_key_env_var` | str | `"OPENAI_API_KEY"` | Env var name for the API key |
| `api_type` | str | `"openai"` | API type (`"openai"`, `"bedrock"`, etc.) |
| `max_consecutive_auto_reply` | int | `10` | Max auto-replies |
| `human_input_mode` | str | `"NEVER"` | Human input mode |
| `code_execution` | bool | `False` | Enable code execution |

## License

Apache-2.0 — See [LICENSE](../../LICENSE) for details.
12 changes: 12 additions & 0 deletions integrations/ag2/examples/pipeline_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
from haystack import Pipeline
from haystack_integrations.components.agents.ag2 import AG2Agent

pipeline = Pipeline()
pipeline.add_component("agent", AG2Agent(
model="gpt-4o-mini",
system_message="Answer questions clearly and concisely. Return TERMINATE when you have finished answering.",
))

result = pipeline.run({"agent": {"query": "Explain retrieval-augmented generation."}})
print(result["agent"]["reply"])
91 changes: 91 additions & 0 deletions integrations/ag2/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"

[project]
name = "ag2-haystack"
dynamic = ["version"]
description = "Haystack integration for AG2 multi-agent framework"
readme = "README.md"
license = "Apache-2.0"
requires-python = ">=3.10"
authors = [
{ name = "deepset GmbH", email = "info@deepset.ai" },
]
classifiers = [
"Development Status :: 4 - Beta",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
]
dependencies = [
"haystack-ai",
"ag2[openai]>=0.11.4,<1.0",
]

[project.optional-dependencies]
dev = [
"pytest",
"pytest-asyncio",
"ruff",
"mypy",
]

[tool.hatch.version]
source = "vcs"
tag-pattern = "integrations/ag2-v(?P<version>.*)"
fallback-version = "0.1.0"

[tool.hatch.version.raw-options]
root = "../.."

[tool.hatch.build.targets.wheel]
packages = ["src/haystack_integrations"]

[tool.hatch.envs.default]
dependencies = [
"haystack-ai",
"ag2[openai]>=0.11.4,<1.0",
"pytest",
"pytest-asyncio",
]

[tool.hatch.envs.default.scripts]
test = "pytest tests/ {args}"
fmt = "ruff format src tests && ruff check --fix src tests"
fmt-check = "ruff format --check src tests && ruff check src tests"
lint = "ruff check src tests"
types = "mypy src"

[tool.hatch.envs.test]
dependencies = [
"pytest",
"pytest-asyncio",
"pytest-cov",
"pytest-rerunfailures",
"mypy",
"pip",
]

[tool.hatch.envs.test.scripts]
unit = 'pytest -m "not integration" {args:tests}'
integration = 'pytest -m "integration" {args:tests}'
all = 'pytest {args:tests}'
unit-cov-retry = 'pytest --cov=haystack_integrations --reruns 3 --reruns-delay 30 -x -m "not integration" {args:tests}'
integration-cov-append-retry = 'pytest --cov=haystack_integrations --cov-append --reruns 3 --reruns-delay 30 -x -m "integration" {args:tests}'
types = "mypy -p haystack_integrations.components.agents.ag2 {args}"

[tool.ruff]
target-version = "py310"

[tool.ruff.lint]
select = ["E", "F", "I", "W", "UP"]

[tool.mypy]
python_version = "3.10"
warn_return_any = true
warn_unused_configs = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-FileCopyrightText: 2024-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0

from haystack_integrations.components.agents.ag2.agent import AG2Agent

__all__ = ["AG2Agent"]
Loading
Loading