Skip to main content

🧠 Autogen ContextPlus

Modular, customizable, and serializable context engine for AutoGen — enabling structured message summarization, filtering, and rewriting logic with full compatibility.


✨ What is ContextPlus?

autogen-contextplus provides a general-purpose context modifier system for AutoGen’s model_context layer. It supports:

  • ✅ Condition-triggered message summarization
  • ✅ Agent- or function-based message rewriting
  • ✅ Component-based serialization / deserialization
  • ✅ Full support for user-defined logic via AutoGen FunctionTool or custom agents

🔧 Installation

pip install autogen-contextplus

For development and type checking:

pip install -e ".[dev]"

Example

import asyncio
from pprint import pprint
from typing import List
from autogen_core.models import UserMessage, AssistantMessage
from autogen_ext.models.replay import ReplayChatCompletionClient
from autogen_ext.models.anthropic import AnthropicChatCompletionClient
from autogen_agentchat.agents import AssistantAgent
from autogen_core import CancellationToken 
from autogen_core.model_context import BufferedChatCompletionContext

from autogen_contextplus.conditions import (
    MaxMessageCondition
)
from autogen_contextplus.base.types import (
    ModifierFunction,
)
from autogen_contextplus import (
    ContextPlusChatCompletionContext
)
from autogen_core.models import LLMMessage


def buffered_summary(
    messages: List[LLMMessage],
    non_summarized_messages: List[LLMMessage],
) -> List[LLMMessage]:
    """Summarize the last `buffer_count` messages."""
    if len(messages) > 3:
        return messages[-3:]
    return messages


async def main():
    client = ReplayChatCompletionClient(
        chat_completions=[
            "paris",
            "seoul",
            "paris",
            "seoul",
        ]
    )
    
    context = ContextPlusChatCompletionContext(
        modifier_func = buffered_summary,
        modifier_condition = MaxMessageCondition(max_messages=2)
    )
    agent = AssistantAgent(
        "helper",
        model_client=client,
        system_message="You are a helpful agent",
        model_context=context
    )
    
    await agent.run(task="What is the capital of France?")
    res = await context.get_messages()
    print(f"[RESULTS] res:")
    pprint(res)
    print(f"[RESULTS] len_context : {len(res)}, context_type: {type(context)}")

    await agent.run(task="What is the capital of Korea?")
    res = await context.get_messages()
    print(f"[RESULTS] res:")
    pprint(res)
    print(f"[RESULTS] len_context : {len(res)}, context_type: {type(context)}")

    print("==========================")

    cancellation_token = CancellationToken() 
    await agent.on_reset(cancellation_token=cancellation_token)
    test = agent.dump_component()
    agent = AssistantAgent.load_component(test)
    context = agent.model_context

    await agent.run(task="What is the capital of France?")
    res = await context.get_messages()
    print(f"[RESULTS] res:")
    pprint(res)
    print(f"[RESULTS] len_context : {len(res)}, context_type: {type(context)}")
    await agent.run(task="What is the capital of Korea?")
    res = await context.get_messages()
    print(f"[RESULTS] res:")
    pprint(res)
    print(f"[RESULTS] len_context : {len(res)}, context_type: {type(context)}")
    
if __name__ == "__main__":
    asyncio.run(main())

Metadata

Release files for autogen-contextplus 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for autogen-contextplus 0.0.4
File Size Uploaded
autogen_contextplus-0.0.4.tar.gz 17.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for autogen-contextplus 0.0.4
File Interpreter ABI Platform
autogen_contextplus-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 38.9 kB

Release files / autogen_contextplus-0.0.4.tar.gz

Download URL autogen_contextplus-0.0.4.tar.gz
Size 17.7 kB
Tags Source
SHA-256 checksum
How to use checksums
08fdfd09f075fc1a41d4c171c3f8c0028161ef153a121069805608e15a81f9eb
BLAKE2b-256 checksum
How to use checksums
298dbbdbda10e97d1311b29836ca26e748b908d5e8b5abfa20bd340dc23a1e89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / autogen_contextplus-0.0.4-py3-none-any.whl

Download URL autogen_contextplus-0.0.4-py3-none-any.whl
Size 21.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
21c43de220c7cc1c4c1894d8be869e2d28cf74561562911ef774b03afab7d711
BLAKE2b-256 checksum
How to use checksums
938337b7c078ba406fef1a27b84e5643e30ba78a19bd4b5d47f7a92e17aff635
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page