A programming framework for agentic AI
Implement default in-memory store for ChatCompletionCache (#5188)
Sachin Joglekar89262064796b
diff --git a/python/packages/autogen-core/src/autogen_core/tools/_function_tool.py b/python/packages/autogen-core/src/autogen_core/tools/_function_tool.pyindex b43e06135..12cee716f 100644--- a/python/packages/autogen-core/src/autogen_core/tools/_function_tool.py+++ b/python/packages/autogen-core/src/autogen_core/tools/_function_tool.py@@ -1,8 +1,8 @@import asyncioimport functools+import warningsfrom textwrap import dedentfrom typing import Any, Callable, Sequence-import warningsfrom pydantic import BaseModelfrom typing_extensions import Selfdiff --git a/python/packages/autogen-ext/src/autogen_ext/models/cache/_chat_completion_cache.py b/python/packages/autogen-ext/src/autogen_ext/models/cache/_chat_completion_cache.pyindex 79ed5f166..15ea1cfd2 100644--- a/python/packages/autogen-ext/src/autogen_ext/models/cache/_chat_completion_cache.py+++ b/python/packages/autogen-ext/src/autogen_ext/models/cache/_chat_completion_cache.py@@ -3,7 +3,7 @@ import jsonimport warningsfrom typing import Any, AsyncGenerator, List, Mapping, Optional, Sequence, Union, cast-from autogen_core import CacheStore, CancellationToken+from autogen_core import CacheStore, CancellationToken, InMemoryStorefrom autogen_core.models import (ChatCompletionClient,CreateResult,@@ -74,11 +74,16 @@ class ChatCompletionCache(ChatCompletionClient):client (ChatCompletionClient): The original ChatCompletionClient to wrap.store (CacheStore): A store object that implements get and set methods.The user is responsible for managing the store's lifecycle & clearing it (if needed).+ Defaults to using in-memory cache."""- def __init__(self, client: ChatCompletionClient, store: CacheStore[CHAT_CACHE_VALUE_TYPE]):+ def __init__(+ self,+ client: ChatCompletionClient,+ store: Optional[CacheStore[CHAT_CACHE_VALUE_TYPE]] = None,+ ):self.client = client- self.store = store+ self.store = store or InMemoryStore[CHAT_CACHE_VALUE_TYPE]()def _check_cache(self,diff --git a/python/packages/autogen-ext/tests/models/test_chat_completion_cache.py b/python/packages/autogen-ext/tests/models/test_chat_completion_cache.pyindex ceb4d9a9f..5a023b02d 100644--- a/python/packages/autogen-ext/tests/models/test_chat_completion_cache.py+++ b/python/packages/autogen-ext/tests/models/test_chat_completion_cache.py@@ -2,7 +2,6 @@ import copyfrom typing import List, Tuple, Unionimport pytest-from autogen_core import InMemoryStorefrom autogen_core.models import (ChatCompletionClient,CreateResult,@@ -10,7 +9,7 @@ from autogen_core.models import (SystemMessage,UserMessage,)-from autogen_ext.models.cache import CHAT_CACHE_VALUE_TYPE, ChatCompletionCache+from autogen_ext.models.cache import ChatCompletionCachefrom autogen_ext.models.replay import ReplayChatCompletionClient@@ -21,8 +20,7 @@ def get_test_data() -> Tuple[list[str], list[str], SystemMessage, ChatCompletionsystem_prompt = SystemMessage(content="This is a system prompt")replay_client = ReplayChatCompletionClient(responses)replay_client.set_cached_bool_value(False)- store = InMemoryStore[CHAT_CACHE_VALUE_TYPE]()- cached_client = ChatCompletionCache(replay_client, store)+ cached_client = ChatCompletionCache(replay_client)return responses, prompts, system_prompt, replay_client, cached_client