Skip to content

A programming framework for agentic AI

Add examples for custom model context in AssistantAgent and ChatCompletionContext (#5810)

Eric Zhu4858676bddf3
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py
index 165cc280e..043539775 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py
@@ -133,6 +133,8 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):
the `model_context` parameter to a :class:`~autogen_core.model_context.BufferedChatCompletionContext`.
This will limit the number of recent messages sent to the model and can be useful
when the model has a limit on the number of tokens it can process.
+ You can also create your own model context by subclassing
+ :class:`~autogen_core.model_context.ChatCompletionContext`.
Streaming mode:
@@ -531,6 +533,63 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):
The `o1-preview` and `o1-mini` models do not support system message and function calling.
So the `system_message` should be set to `None` and the `tools` and `handoffs` should not be set.
See `o1 beta limitations <https://platform.openai.com/docs/guides/reasoning#beta-limitations>`_ for more details.
+
+
+ **Example 8: agent using reasoning model with custom model context.**
+
+ The following example shows how to use a reasoning model (DeepSeek R1) with the assistant agent.
+ The model context is used to filter out the thought field from the assistant message.
+
+ .. code-block:: python
+
+ import asyncio
+ from typing import List
+
+ from autogen_agentchat.agents import AssistantAgent
+ from autogen_core.model_context import UnboundedChatCompletionContext
+ from autogen_core.models import AssistantMessage, LLMMessage, ModelFamily
+ from autogen_ext.models.ollama import OllamaChatCompletionClient
+
+
+ class ReasoningModelContext(UnboundedChatCompletionContext):
+ \"\"\"A model context for reasoning models.\"\"\"
+
+ async def get_messages(self) -> List[LLMMessage]:
+ messages = await super().get_messages()
+ # Filter out thought field from AssistantMessage.
+ messages_out: List[LLMMessage] = []
+ for message in messages:
+ if isinstance(message, AssistantMessage):
+ message.thought = None
+ messages_out.append(message)
+ return messages_out
+
+
+ # Create an instance of the model client for DeepSeek R1 hosted locally on Ollama.
+ model_client = OllamaChatCompletionClient(
+ model="deepseek-r1:8b",
+ model_info={
+ "vision": False,
+ "function_calling": False,
+ "json_output": False,
+ "family": ModelFamily.R1,
+ },
+ )
+
+ agent = AssistantAgent(
+ "reasoning_agent",
+ model_client=model_client,
+ model_context=ReasoningModelContext(), # Use the custom model context.
+ )
+
+
+ async def run_reasoning_agent() -> None:
+ result = await agent.run(task="What is the capital of France?")
+ print(result)
+
+
+ asyncio.run(run_reasoning_agent())
+
"""
component_config_schema = AssistantAgentConfig
diff --git a/python/packages/autogen-core/src/autogen_core/model_context/_chat_completion_context.py b/python/packages/autogen-core/src/autogen_core/model_context/_chat_completion_context.py
index d2b82ec1f..1b14b8e85 100644
--- a/python/packages/autogen-core/src/autogen_core/model_context/_chat_completion_context.py
+++ b/python/packages/autogen-core/src/autogen_core/model_context/_chat_completion_context.py
@@ -14,6 +14,34 @@ class ChatCompletionContext(ABC, ComponentBase[BaseModel]):
Args:
initial_messages (List[LLMMessage] | None): The initial messages.
+
+ Example:
+
+ To create a custom model context that filters out the thought field from AssistantMessage.
+ This is useful for reasoning models like DeepSeek R1, which produces
+ very long thought that is not needed for subsequent completions.
+
+ .. code-block:: python
+
+ from typing import List
+
+ from autogen_core.model_context import UnboundedChatCompletionContext
+ from autogen_core.models import AssistantMessage, LLMMessage
+
+
+ class ReasoningModelContext(UnboundedChatCompletionContext):
+ \"\"\"A model context for reasoning models.\"\"\"
+
+ async def get_messages(self) -> List[LLMMessage]:
+ messages = await super().get_messages()
+ # Filter out thought field from AssistantMessage.
+ messages_out: List[LLMMessage] = []
+ for message in messages:
+ if isinstance(message, AssistantMessage):
+ message.thought = None
+ messages_out.append(message)
+ return messages_out
+
"""
component_type = "chat_completion_context"
diff --git a/python/packages/autogen-ext/src/autogen_ext/models/ollama/_model_info.py b/python/packages/autogen-ext/src/autogen_ext/models/ollama/_model_info.py
index 7c9f45597..8e5495dcf 100644
--- a/python/packages/autogen-ext/src/autogen_ext/models/ollama/_model_info.py
+++ b/python/packages/autogen-ext/src/autogen_ext/models/ollama/_model_info.py
@@ -18,7 +18,7 @@ _MODEL_INFO: Dict[str, ModelInfo] = {
"json_output": True,
"family": ModelFamily.UNKNOWN,
},
- "deepseek-r1": {"vision": False, "function_calling": False, "json_output": True, "family": ModelFamily.UNKNOWN},
+ "deepseek-r1": {"vision": False, "function_calling": False, "json_output": True, "family": ModelFamily.R1},
"dolphin-llama3": {"vision": False, "function_calling": False, "json_output": True, "family": ModelFamily.UNKNOWN},
"dolphin-mistral": {"vision": False, "function_calling": False, "json_output": True, "family": ModelFamily.UNKNOWN},
"dolphin-mixtral": {"vision": False, "function_calling": False, "json_output": True, "family": ModelFamily.UNKNOWN},