|
327 | 327 | "source": [
|
328 | 328 | "## Caching Wrapper\n",
|
329 | 329 | "\n",
|
330 |
| - "`autogen_core` implements a {py:class}`~autogen_core.models.ChatCompletionCache` that can wrap any {py:class}`~autogen_core.models.ChatCompletionClient`. Using this wrapper avoids incurring token usage when querying the underlying client with the same prompt multiple times. \n", |
| 330 | + "`autogen_ext` implements {py:class}`~autogen_ext.models.cache.ChatCompletionCache` that can wrap any {py:class}`~autogen_core.models.ChatCompletionClient`. Using this wrapper avoids incurring token usage when querying the underlying client with the same prompt multiple times.\n", |
331 | 331 | "\n",
|
332 |
| - "{py:class}`~autogen_core.models.ChatCompletionCache` uses a {py:class}`~autogen_core.CacheStore` protocol to allow duck-typing any storage object that has a pair of `get` & `set` methods (such as `redis.Redis` or `diskcache.Cache`). Here's an example of using `diskcache` for local caching:" |
| 332 | + "{py:class}`~autogen_core.models.ChatCompletionCache` uses a {py:class}`~autogen_core.CacheStore` protocol. We have implemented some useful variants of {py:class}`~autogen_core.CacheStore` including {py:class}`~autogen_ext.cache_store.diskcache.DiskCacheStore` and {py:class}`~autogen_ext.cache_store.redis.RedisStore`.\n", |
| 333 | + "\n", |
| 334 | + "Here's an example of using `diskcache` for local caching:" |
| 335 | + ] |
| 336 | + }, |
| 337 | + { |
| 338 | + "cell_type": "code", |
| 339 | + "execution_count": null, |
| 340 | + "metadata": {}, |
| 341 | + "outputs": [], |
| 342 | + "source": [ |
| 343 | + "# pip install -U \"autogen-ext[openai, diskcache]\"" |
333 | 344 | ]
|
334 | 345 | },
|
335 | 346 | {
|
|
346 | 357 | }
|
347 | 358 | ],
|
348 | 359 | "source": [
|
349 |
| - "from typing import Any, Dict, Optional\n", |
| 360 | + "import asyncio\n", |
| 361 | + "import tempfile\n", |
350 | 362 | "\n",
|
351 |
| - "from autogen_core.models import ChatCompletionCache\n", |
| 363 | + "from autogen_core.models import UserMessage\n", |
| 364 | + "from autogen_ext.cache_store.diskcache import DiskCacheStore\n", |
| 365 | + "from autogen_ext.models.cache import CHAT_CACHE_VALUE_TYPE, ChatCompletionCache\n", |
| 366 | + "from autogen_ext.models.openai import OpenAIChatCompletionClient\n", |
352 | 367 | "from diskcache import Cache\n",
|
353 | 368 | "\n",
|
354 |
| - "diskcache_client = Cache(\"/tmp/diskcache\")\n", |
355 | 369 | "\n",
|
356 |
| - "cached_client = ChatCompletionCache(model_client, diskcache_client)\n", |
357 |
| - "response = await cached_client.create(messages=messages)\n", |
| 370 | + "async def main() -> None:\n", |
| 371 | + " with tempfile.TemporaryDirectory() as tmpdirname:\n", |
| 372 | + " # Initialize the original client\n", |
| 373 | + " openai_model_client = OpenAIChatCompletionClient(model=\"gpt-4o\")\n", |
| 374 | + "\n", |
| 375 | + " # Then initialize the CacheStore, in this case with diskcache.Cache.\n", |
| 376 | + " # You can also use redis like:\n", |
| 377 | + " # from autogen_ext.cache_store.redis import RedisStore\n", |
| 378 | + " # import redis\n", |
| 379 | + " # redis_instance = redis.Redis()\n", |
| 380 | + " # cache_store = RedisCacheStore[CHAT_CACHE_VALUE_TYPE](redis_instance)\n", |
| 381 | + " cache_store = DiskCacheStore[CHAT_CACHE_VALUE_TYPE](Cache(tmpdirname))\n", |
| 382 | + " cache_client = ChatCompletionCache(openai_model_client, cache_store)\n", |
| 383 | + "\n", |
| 384 | + " response = await cache_client.create([UserMessage(content=\"Hello, how are you?\", source=\"user\")])\n", |
| 385 | + " print(response) # Should print response from OpenAI\n", |
| 386 | + " response = await cache_client.create([UserMessage(content=\"Hello, how are you?\", source=\"user\")])\n", |
| 387 | + " print(response) # Should print cached response\n", |
| 388 | + "\n", |
358 | 389 | "\n",
|
359 |
| - "cached_response = await cached_client.create(messages=messages)\n", |
360 |
| - "print(cached_response.cached)" |
| 390 | + "asyncio.run(main())" |
361 | 391 | ]
|
362 | 392 | },
|
363 | 393 | {
|
|
0 commit comments