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

core[minor]: Enhance cache flexibility in BaseChatModel #17386

Merged
merged 21 commits into from
Mar 19, 2024

Conversation

alhridoy
Copy link
Contributor

Please title your PR "core: Enhance cache flexibility in BaseChatModel".

  • Description: Enhanced the BaseChatModel to support an Optional[Union[bool, BaseCache]] type for the cache attribute, allowing for both boolean flags and custom cache implementations. Implemented logic within chat model methods to utilize the provided custom cache implementation effectively. This change aims to provide more flexibility in caching strategies for chat models.
  • Issue: Implements enhancement request Enhancement: Add ability to pass local cache to chat models #17242.
  • Dependencies: No additional dependencies required for this change.

Copy link

vercel bot commented Feb 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Mar 19, 2024 3:03pm

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. Ɑ: models Related to LLMs or chat model modules 🤖:improvement Medium size change to existing code to handle new use-cases 🔌: redis Primarily related to Redis integrations labels Feb 12, 2024
Copy link
Contributor

@Lord-Haji Lord-Haji left a comment

Choose a reason for hiding this comment

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

Why are you making so many indent changes? The issue only specified cache. I'd suggest you to revert them

@eyurtsev eyurtsev self-assigned this Feb 13, 2024
@eyurtsev
Copy link
Collaborator

Hi @alhridoy, thanks for taking this on!!

Could you review the acceptance criteria in the original issue: #17242 -- we want to implement all the items in the acceptance criteria so we can merge the PR.

@hasansustcse13
Copy link
Contributor

@eyurtsev Could you please confirm whether you intend to include caching functionality in the astream, astream_event, and astream_log methods of a LCEL chain? If so, kindly add it to the acceptance criteria. It's important to note that caching won't function with these methods as the _agenerate_with_cache and _generate_with_cache methods are only called from the agenerate and generate methods.

@eyurtsev
Copy link
Collaborator

We should iterate in smalls steps and match existing global cache functionality. If we want to extend the caching for streaming, we can do so, but let's do it as a separate task if it's not covered by existing global cache.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Feb 15, 2024
@alhridoy
Copy link
Contributor Author

Hi @alhridoy, thanks for taking this on!!

Could you review the acceptance criteria in the original issue: #17242 -- we want to implement all the items in the acceptance criteria so we can merge the PR.

Hi @eyurtsev I added the inline docstring and added test. Could not work on external documentation. Could anybody help me here @hasansustcse13 @Lord-Haji

@eyurtsev eyurtsev self-requested a review February 21, 2024 16:42
libs/core/langchain_core/language_models/chat_models.py Outdated Show resolved Hide resolved
InMemoryCache,
SQLAlchemyCache,
)
from langchain.globals import get_llm_cache, set_llm_cache


class MockCache(BaseCache):
Copy link
Collaborator

Choose a reason for hiding this comment

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

@alhridoy This is not the correct BaseCache interface.

Do you want to try and fix up the code? You can use InMemoryCache implementation to test things out.

At the moment, the included unit tests have an number of errors in them, so testing code does not work

@@ -15,12 +16,27 @@
from sqlalchemy.orm import Session

from langchain.cache import (
BaseCache,
Copy link
Collaborator

Choose a reason for hiding this comment

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

BaseCache is not available from langchain.cache, instead it's in langchain_core.cache

@eyurtsev
Copy link
Collaborator

eyurtsev commented Mar 1, 2024

I'm going to commandeer to we can land this

@eyurtsev eyurtsev changed the title core: Enhance cache flexibility in BaseChatModel core[minor]: Enhance cache flexibility in BaseChatModel Mar 1, 2024
@alhridoy
Copy link
Contributor Author

alhridoy commented Mar 1, 2024

I'm going to commandeer to we can land this

Hi @eyurtsev Give me a few days. I'll try it first. Thanks for your patience and guidance.

@eyurtsev
Copy link
Collaborator

eyurtsev commented Mar 1, 2024

@alhridoy just saw your message -- i made the changes already. I'm going to merge for now if tests pass, feel free to review the changes. And there are a lot of contribution opportunities so we'll appreciate any help :)

@dosubot dosubot bot added the lgtm PR looks good. Use to confirm that a PR is ready for merging. label Mar 1, 2024
Copy link
Contributor Author

@alhridoy alhridoy left a comment

Choose a reason for hiding this comment

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

LGTM!

@eyurtsev eyurtsev merged commit 50f93d8 into langchain-ai:master Mar 19, 2024
95 checks passed
gkorland pushed a commit to FalkorDB/langchain that referenced this pull request Mar 30, 2024
…#17386)

- **Description:** Enhanced the `BaseChatModel` to support an
`Optional[Union[bool, BaseCache]]` type for the `cache` attribute,
allowing for both boolean flags and custom cache implementations.
Implemented logic within chat model methods to utilize the provided
custom cache implementation effectively. This change aims to provide
more flexibility in caching strategies for chat models.
  - **Issue:** Implements enhancement request langchain-ai#17242.
- **Dependencies:** No additional dependencies required for this change.

---------

Co-authored-by: Eugene Yurtsev <[email protected]>
chrispy-snps pushed a commit to chrispy-snps/langchain that referenced this pull request Mar 30, 2024
…#17386)

- **Description:** Enhanced the `BaseChatModel` to support an
`Optional[Union[bool, BaseCache]]` type for the `cache` attribute,
allowing for both boolean flags and custom cache implementations.
Implemented logic within chat model methods to utilize the provided
custom cache implementation effectively. This change aims to provide
more flexibility in caching strategies for chat models.
  - **Issue:** Implements enhancement request langchain-ai#17242.
- **Dependencies:** No additional dependencies required for this change.

---------

Co-authored-by: Eugene Yurtsev <[email protected]>
hinthornw pushed a commit that referenced this pull request Apr 26, 2024
- **Description:** Enhanced the `BaseChatModel` to support an
`Optional[Union[bool, BaseCache]]` type for the `cache` attribute,
allowing for both boolean flags and custom cache implementations.
Implemented logic within chat model methods to utilize the provided
custom cache implementation effectively. This change aims to provide
more flexibility in caching strategies for chat models.
  - **Issue:** Implements enhancement request #17242.
- **Dependencies:** No additional dependencies required for this change.

---------

Co-authored-by: Eugene Yurtsev <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:improvement Medium size change to existing code to handle new use-cases lgtm PR looks good. Use to confirm that a PR is ready for merging. Ɑ: models Related to LLMs or chat model modules 🔌: redis Primarily related to Redis integrations size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants