autogen_ext.models.cache#
- class ChatCompletionCache(client: ChatCompletionClient, store: CacheStore[CreateResult | 列表[str | CreateResult]] | 无 = None)[源代码]#
基础:
ChatCompletionClient
,Component
[ChatCompletionCacheConfig
]一个围绕着
ChatCompletionClient
的包装器,它缓存了从底层客户端创建的生成结果。缓存命中不会计入原始客户端的令牌使用量。典型用法:
让我们以openai客户端为例,使用磁盘缓存。 首先安装带有必要包的autogen-ext:
pip install -U "autogen-ext[openai, diskcache]"
并按照以下方式使用:
import asyncio import tempfile from autogen_core.models import UserMessage from autogen_ext.models.openai import OpenAIChatCompletionClient from autogen_ext.models.cache import ChatCompletionCache, CHAT_CACHE_VALUE_TYPE from autogen_ext.cache_store.diskcache import DiskCacheStore from diskcache import Cache async def main(): with tempfile.TemporaryDirectory() as tmpdirname: # Initialize the original client openai_model_client = OpenAIChatCompletionClient(model="gpt-4o") # Then initialize the CacheStore, in this case with diskcache.Cache. # You can also use redis like: # from autogen_ext.cache_store.redis import RedisStore # import redis # redis_instance = redis.Redis() # cache_store = RedisCacheStore[CHAT_CACHE_VALUE_TYPE](redis_instance) cache_store = DiskCacheStore[CHAT_CACHE_VALUE_TYPE](Cache(tmpdirname)) cache_client = ChatCompletionCache(openai_model_client, cache_store) response = await cache_client.create([UserMessage(content="Hello, how are you?", source="user")]) print(response) # Should print response from OpenAI response = await cache_client.create([UserMessage(content="Hello, how are you?", source="user")]) print(response) # Should print cached response asyncio.run(main())
您现在可以像使用原始客户端一样使用 cached_client,但启用了缓存。
- Parameters:
client (ChatCompletionClient) – 要包装的原始 ChatCompletionClient。
store (CacheStore) – 一个实现了get和set方法的存储对象。 用户负责管理存储对象的生命周期并在需要时清除它。 默认使用内存缓存。
- classmethod _from_config(config: ChatCompletionCacheConfig) 自我 [源代码]#
从配置对象创建组件的新实例。
- Parameters:
config (T) – 配置对象。
- Returns:
Self – 组件的新实例。
- actual_usage() RequestUsage [源代码]#
- property capabilities: ModelCapabilities#
- component_config_schema#
别名
ChatCompletionCacheConfig
- component_provider_override: ClassVar[str | 无] = 'autogen_ext.models.cache.ChatCompletionCache'#
覆盖组件的提供商字符串。这应用于防止内部模块名称成为模块名称的一部分。
- component_type: ClassVar[ComponentType] = 'chat_completion_cache'#
组件的逻辑类型。
- count_tokens(messages: Sequence[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[工具 | 工具模式] = []) int [源代码]#
- async create(messages: Sequence[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[工具 | 工具模式] = [], json_output: bool | 无 = None, extra_create_args: 映射[str, 任何] = {}, cancellation_token: CancellationToken | 无 = None) CreateResult [源代码]#
ChatCompletionClient.create的缓存版本。 如果调用的结果已被缓存,将立即返回,而不会调用底层客户端。
注意:cancellation_token 对于缓存结果被忽略。
- create_stream(messages: Sequence[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[工具 | 工具模式] = [], json_output: bool | 无 = None, extra_create_args: 映射[str, 任何] = {}, cancellation_token: CancellationToken | 无 = None) AsyncGenerator[str | CreateResult, 无] [源代码]#
ChatCompletionClient.create_stream 的缓存版本。 如果已经缓存了调用 create_stream 的结果,它将直接返回,而无需从底层客户端进行流式传输。
注意:对于缓存结果,cancellation_token 被忽略。
- remaining_tokens(messages: Sequence[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[工具 | 工具模式] = []) int [源代码]#
- total_usage() RequestUsage [源代码]#