autogen_core.model_context#

class BufferedChatCompletionContext(buffer_size: int, initial_messages: 列表[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | = None)[源代码]#

基础类:ChatCompletionContext, Component[BufferedChatCompletionContextConfig]

一个缓冲的对话完成上下文,保留最后n条消息的视图,其中n是缓冲区大小。缓冲区大小在初始化时设置。

Parameters:
  • buffer_size (int) – 缓冲区的大小。

  • initial_messages (List[LLMMessage] | None) – 初始消息。

classmethod _from_config(config: BufferedChatCompletionContextConfig) 自我[源代码]#

从配置对象创建组件的新实例。

Parameters:

config (T) – 配置对象。

Returns:

Self – 组件的新实例。

_to_config() BufferedChatCompletionContextConfig[源代码]#

导出配置,该配置将用于创建一个与此实例配置相匹配的组件新实例。

Returns:

T – 组件的配置。

component_config_schema#

别名 BufferedChatCompletionContextConfig

component_provider_override: ClassVar[str | ] = 'autogen_core.model_context.BufferedChatCompletionContext'#

覆盖组件的提供商字符串。这应用于防止内部模块名称成为模块名称的一部分。

async get_messages() 列表[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]][源代码]#

获取最多buffer_size条最近的消息。

class ChatCompletionContext(initial_messages: 列表[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | = None)[源代码]#

基础:ABC, ComponentBase[BaseModel]

用于定义聊天完成上下文接口的抽象基类。 聊天完成上下文允许代理存储和检索LLM消息。 它可以通过不同的召回策略来实现。

Parameters:

initial_messages (List[LLMMessage] | None) – 初始消息。

示例

要创建一个自定义的模型上下文,以便从AssistantMessage中过滤掉thought字段。 这对于像DeepSeek R1这样的推理模型非常有用,因为它生成的thought非常长,后续的完成不需要这些内容。

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
async add_message(message: 已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]) [源代码]#

向上下文中添加一条消息。

async clear() [源代码]#

清除上下文。

component_type: ClassVar[ComponentType] = 'chat_completion_context'#

组件的逻辑类型。

abstract async get_messages() 列表[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]][源代码]#
async load_state(state: 映射[str, 任何]) [源代码]#
async save_state() 映射[str, 任何][源代码]#
pydantic model ChatCompletionContextState[源代码]#

基础:BaseModel

Show JSON schema
{
   "title": "ChatCompletionContextState",
   "type": "object",
   "properties": {
      "messages": {
         "items": {
            "discriminator": {
               "mapping": {
                  "AssistantMessage": "#/$defs/AssistantMessage",
                  "FunctionExecutionResultMessage": "#/$defs/FunctionExecutionResultMessage",
                  "SystemMessage": "#/$defs/SystemMessage",
                  "UserMessage": "#/$defs/UserMessage"
               },
               "propertyName": "type"
            },
            "oneOf": [
               {
                  "$ref": "#/$defs/SystemMessage"
               },
               {
                  "$ref": "#/$defs/UserMessage"
               },
               {
                  "$ref": "#/$defs/AssistantMessage"
               },
               {
                  "$ref": "#/$defs/FunctionExecutionResultMessage"
               }
            ]
         },
         "title": "Messages",
         "type": "array"
      }
   },
   "$defs": {
      "AssistantMessage": {
         "description": "Assistant message are sampled from the language model.",
         "properties": {
            "content": {
               "anyOf": [
                  {
                     "type": "string"
                  },
                  {
                     "items": {
                        "$ref": "#/$defs/FunctionCall"
                     },
                     "type": "array"
                  }
               ],
               "title": "Content"
            },
            "thought": {
               "anyOf": [
                  {
                     "type": "string"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Thought"
            },
            "source": {
               "title": "Source",
               "type": "string"
            },
            "type": {
               "const": "AssistantMessage",
               "default": "AssistantMessage",
               "title": "Type",
               "type": "string"
            }
         },
         "required": [
            "content",
            "source"
         ],
         "title": "AssistantMessage",
         "type": "object"
      },
      "FunctionCall": {
         "properties": {
            "id": {
               "title": "Id",
               "type": "string"
            },
            "arguments": {
               "title": "Arguments",
               "type": "string"
            },
            "name": {
               "title": "Name",
               "type": "string"
            }
         },
         "required": [
            "id",
            "arguments",
            "name"
         ],
         "title": "FunctionCall",
         "type": "object"
      },
      "FunctionExecutionResult": {
         "description": "Function execution result contains the output of a function call.",
         "properties": {
            "content": {
               "title": "Content",
               "type": "string"
            },
            "name": {
               "title": "Name",
               "type": "string"
            },
            "call_id": {
               "title": "Call Id",
               "type": "string"
            },
            "is_error": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Is Error"
            }
         },
         "required": [
            "content",
            "name",
            "call_id"
         ],
         "title": "FunctionExecutionResult",
         "type": "object"
      },
      "FunctionExecutionResultMessage": {
         "description": "Function execution result message contains the output of multiple function calls.",
         "properties": {
            "content": {
               "items": {
                  "$ref": "#/$defs/FunctionExecutionResult"
               },
               "title": "Content",
               "type": "array"
            },
            "type": {
               "const": "FunctionExecutionResultMessage",
               "default": "FunctionExecutionResultMessage",
               "title": "Type",
               "type": "string"
            }
         },
         "required": [
            "content"
         ],
         "title": "FunctionExecutionResultMessage",
         "type": "object"
      },
      "SystemMessage": {
         "description": "System message contains instructions for the model coming from the developer.\n\n.. note::\n\n    Open AI is moving away from using 'system' role in favor of 'developer' role.\n    See `Model Spec <https://cdn.openai.com/spec/model-spec-2024-05-08.html#definitions>`_ for more details.\n    However, the 'system' role is still allowed in their API and will be automatically converted to 'developer' role\n    on the server side.\n    So, you can use `SystemMessage` for developer messages.",
         "properties": {
            "content": {
               "title": "Content",
               "type": "string"
            },
            "type": {
               "const": "SystemMessage",
               "default": "SystemMessage",
               "title": "Type",
               "type": "string"
            }
         },
         "required": [
            "content"
         ],
         "title": "SystemMessage",
         "type": "object"
      },
      "UserMessage": {
         "description": "User message contains input from end users, or a catch-all for data provided to the model.",
         "properties": {
            "content": {
               "anyOf": [
                  {
                     "type": "string"
                  },
                  {
                     "items": {
                        "anyOf": [
                           {
                              "type": "string"
                           },
                           {}
                        ]
                     },
                     "type": "array"
                  }
               ],
               "title": "Content"
            },
            "source": {
               "title": "Source",
               "type": "string"
            },
            "type": {
               "const": "UserMessage",
               "default": "UserMessage",
               "title": "Type",
               "type": "string"
            }
         },
         "required": [
            "content",
            "source"
         ],
         "title": "UserMessage",
         "type": "object"
      }
   }
}

Fields:
  • messages (List[autogen_core.models._types.SystemMessage | autogen_core.models._types.UserMessage | autogen_core.models._types.AssistantMessage | autogen_core.models._types.FunctionExecutionResultMessage])

field messages: 列表[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] [Optional]#
class HeadAndTailChatCompletionContext(head_size: int, tail_size: int, initial_messages: 列表[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | = None)[源代码]#

基础类: ChatCompletionContext, Component[HeadAndTailChatCompletionContextConfig]

一个聊天完成上下文,保持前n条和后m条消息的视图,其中n是头部大小,m是尾部大小。头部和尾部大小在初始化时设置。

Parameters:
  • head_size (int) – 头部的大小。

  • tail_size (int) – 尾部的大小。

  • initial_messages (List[LLMMessage] | None) – 初始消息。

classmethod _from_config(config: HeadAndTailChatCompletionContextConfig) 自我[源代码]#

从配置对象创建组件的新实例。

Parameters:

config (T) – 配置对象。

Returns:

Self – 组件的新实例。

_to_config() HeadAndTailChatCompletionContextConfig[源代码]#

导出配置,该配置将用于创建一个与此实例配置相匹配的组件新实例。

Returns:

T – 组件的配置。

component_config_schema#

HeadAndTailChatCompletionContextConfig 的别名

component_provider_override: ClassVar[str | ] = 'autogen_core.model_context.HeadAndTailChatCompletionContext'#

覆盖组件的提供商字符串。这应用于防止内部模块名称成为模块名称的一部分。

async get_messages() 列表[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]][源代码]#

获取最多head_size条最新消息和tail_size条最旧消息。

class UnboundedChatCompletionContext(initial_messages: 列表[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | = None)[源代码]#

基类:ChatCompletionContext, Component[UnboundedChatCompletionContextConfig]

一个无界的聊天完成上下文,它保留了所有消息的视图。

classmethod _from_config(config: UnboundedChatCompletionContextConfig) 自我[源代码]#

从配置对象创建组件的新实例。

Parameters:

config (T) – 配置对象。

Returns:

Self – 组件的新实例。

_to_config() UnboundedChatCompletionContextConfig[源代码]#

导出配置,该配置将用于创建一个与此实例配置相匹配的组件新实例。

Returns:

T – 组件的配置。

component_config_schema#

UnboundedChatCompletionContextConfig 的别名

component_provider_override: ClassVar[str | ] = 'autogen_core.model_context.UnboundedChatCompletionContext'#

覆盖组件的提供商字符串。这应用于防止内部模块名称成为模块名称的一部分。

async get_messages() 列表[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]][源代码]#

获取最多buffer_size条最近的消息。