Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ChatCompletionCache along with AbstractStore for caching completions #4924

Merged
merged 6 commits into from
Jan 16, 2025

Conversation

srjoglekar246
Copy link
Contributor

@srjoglekar246 srjoglekar246 commented Jan 7, 2025

Why are these changes needed?

Add a caching wrapper to ChatCompletionClient, that can use any store with get/set methods for storage.

Related issue number

Closes #4752

Checks

@srjoglekar246 srjoglekar246 requested review from ekzhu and Copilot January 7, 2025 18:48

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 5 out of 6 changed files in this pull request and generated no comments.

Files not reviewed (1)
  • python/packages/autogen-core/src/autogen_core/store/abstract_store_base.py: Evaluated as low risk
@srjoglekar246
Copy link
Contributor Author

@ekzhu do we need any documentation changes? I suppose API Reference gets updated automatically?

Copy link

codecov bot commented Jan 7, 2025

Codecov Report

Attention: Patch coverage is 97.19626% with 3 lines in your changes missing coverage. Please review.

Project coverage is 69.69%. Comparing base (2e1a9c7) to head (89c73fe).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ages/autogen-core/src/autogen_core/_cache_store.py 85.71% 2 Missing ⚠️
...autogen_ext/models/cache/_chat_completion_cache.py 98.36% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4924      +/-   ##
==========================================
- Coverage   72.92%   69.69%   -3.24%     
==========================================
  Files         115      168      +53     
  Lines        6785    10605    +3820     
==========================================
+ Hits         4948     7391    +2443     
- Misses       1837     3214    +1377     
Flag Coverage Δ
unittests 69.69% <97.19%> (-3.24%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@ekzhu ekzhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srjoglekar246
Copy link
Contributor Author

@ekzhu added docs as you mentioned :-)

@srjoglekar246 srjoglekar246 requested a review from ekzhu January 8, 2025 20:34
Copy link
Member

@jackgerrits jackgerrits left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should spend some more time to see if the interface can be improved. Especially if this is going into core

@srjoglekar246 srjoglekar246 force-pushed the cachedclient branch 2 times, most recently from 652333d to 7addcf4 Compare January 9, 2025 19:12
@srjoglekar246
Copy link
Contributor Author

@jackgerrits @ekzhu I am not sure if a simple protocol for duc-typing will work in all cases, unless we (or the user) implements a a wrapper around (say) redis. For eg with the latest change I get this error:

/tmp/tmpxi9c9pui.py
  /tmp/tmpxi9c9pui.py:23:52 - error: Argument of type "Redis" cannot be assigned to parameter "store" of type "CacheStore" in function "__init__"
    "Redis" is incompatible with protocol "CacheStore"
      "get" is an incompatible type
        Type "(name: KeyT) -> ResponseT" is not assignable to type "(key: str, default: Any | None = None) -> (Any | None)"
          Function accepts too many positional parameters; expected 1 but received 2
            Parameter name mismatch: "key" versus "name"
      "set" is an incompatible type
        Type "(name: KeyT, value: EncodableT, ex: ExpiryT | None = None, px: ExpiryT | None = None, nx: bool = False, xx: bool = False, keepttl: bool = False, get: bool = False, exat: AbsExpiryT | None = None, pxat: AbsExpiryT | None = None) -> ResponseT" is not assignable to type "(key: str, value: Any) -> (Any | None)"
          Parameter name mismatch: "key" versus "name" (reportArgumentType)
1 error, 0 warnings, 0 informations 
WARNING: there is a new pyright version available (v1.1.389 -> v1.1.391).
Please install the new version or set PYRIGHT_PYTHON_FORCE_VERSION to `latest

@ekzhu
Copy link
Collaborator

ekzhu commented Jan 10, 2025

@jackgerrits could this be resolved by making the input types more flexible?

Also do we need the default argument in the parameter list?

For now we may not be able to get 100% type match as we are approximate duck typing. It is not going to pass the type check for all classes. We can ignore this typing issue for now -- add "type: ignore" to the example line and let's get this PR in. Fix this in a follow up work to add redis wrapper.

@srjoglekar246
Copy link
Contributor Author

In that case the current impl should just work. Will let @jackgerrits chime in. Otherwise I can also add diskcache & redis builtin implementations.

@jackgerrits
Copy link
Member

jackgerrits commented Jan 11, 2025

Sorry I haven't had time to look at this. Would like to get the patch release out first then we can merge as eric said. I'd love if we can get the types to behave nicely but I understand its a bit tricky when interfacing with external libs.

I do think that using a thin redis wrapper is the best thing to do here as it gives us some flexibility about dealing with issues

@srjoglekar246
Copy link
Contributor Author

In that case @ekzhu should we merge this, and I can send a followup for the redis wrapper?

@ekzhu
Copy link
Collaborator

ekzhu commented Jan 11, 2025

As jack said let's wait until the patch fix for user input is released on Monday. Then we'll merge this.

@ekzhu
Copy link
Collaborator

ekzhu commented Jan 14, 2025

We can merge this for 0.4.2 once the doc fixes are completed.

@ekzhu ekzhu self-requested a review January 14, 2025 07:39
@ekzhu ekzhu dismissed their stale review January 14, 2025 07:39

Need more work.

@srjoglekar246
Copy link
Contributor Author

@ekzhu @jackgerrits PTAL. I added specific implementations of CacheStore & RedisStore, and improved the documentation as you suggested.

Copy link
Collaborator

@ekzhu ekzhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@srjoglekar246 srjoglekar246 force-pushed the cachedclient branch 4 times, most recently from 66b04f2 to 5850e18 Compare January 16, 2025 18:14
Copy link
Member

@jackgerrits jackgerrits left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The casts should be removable right?

@ekzhu ekzhu dismissed jackgerrits’s stale review January 16, 2025 23:47

Jack has approved per our discussion

@ekzhu ekzhu merged commit 8bd65c6 into microsoft:main Jan 16, 2025
64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ChatCompletionClient to support request caching
3 participants