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

LangChainLLMs cache_obj update #508

Merged

Conversation

keenborder786
Copy link
Contributor

If one were required to pass a custom cache_obj, it would necessitate passing the cache_obj during inference time, potentially leading to repetitive passes of the cache_obj. However, with this PR, such repetition will no longer be necessary:

from gptcache import cache
from gptcache.core import Cache
from gptcache.processor.pre import get_prompt,get_messages_last_content
from gptcache.processor.pre import get_messages_last_content
from langchain.llms import OpenAI
from gptcache.adapter.langchain_models import LangChainLLMs
import os
os.environ['OPENAI_API_KEY']="KEY"

# init gptcache
llm_cache = Cache()
llm_cache.init(
    pre_embedding_func=get_prompt,
)


# run llm with gptcache
llm = LangChainLLMs(llm=OpenAI(temperature=0) , cache_obj = llm_cache) ## passing cache_obj once
llm("Hello world") ## Not during inference
llm("How are you?") ## Not during inference

Furthermore, the current limitation of not being able to pass cache_obj during the construction of LangChainLLMs prevents you from using a custom cache_obj in other sequential chains like SQLDatabaseSequentialChain, where only the global cache object can be used. This restriction arises because there is no provision to pass a custom cache_obj when SQLDatabaseSequentialChain calls the predict method.

However, with the implementation of my update, you will have the ability to initialize LangChainLLMs with a custom cache_obj. As a result, when you pass this LangChainLLMs instance into the chain, custom cache_obj will function correctly:

from gptcache import cache
from gptcache.core import Cache
from gptcache.processor.pre import get_prompt,get_messages_last_content
from gptcache.processor.pre import get_messages_last_content
from langchain.llms import OpenAI
from gptcache.adapter.langchain_models import LangChainLLMs
from langchain_experimental.sql.base import (
    SQLDatabaseSequentialChain,
)
import os

os.environ['OPENAI_API_KEY']="KEY"
llm_cache = Cache()
llm_cache.init(
  pre_embedding_func=get_prompt,
)
# run llm with gptcache
llm = LangChainLLMs(llm=OpenAI(temperature=0) , cache_obj = llm_cache)
SQLDatabaseSequentialChain.from_llm(
chain_llm,
database=chain_db,
query_prompt=prompt,
decider_prompt=decider_prompt
) 

I hope this clarfies the use case and why this will improve the usability of the package.

…ng for passing in custom cache_obj once rather than passing again and again during inference
@sre-ci-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: keenborder786
To complete the pull request process, please assign xiaofan-luan after the PR has been reviewed.
You can assign the PR to them by writing /assign @xiaofan-luan in a comment when ready.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@keenborder786
Copy link
Contributor Author

/assign @xiaofan-luan

@SimFG
Copy link
Collaborator

SimFG commented Aug 7, 2023

@keenborder786 thanks your improvement

@SimFG SimFG merged commit fdf1630 into zilliztech:dev Aug 7, 2023
3 of 6 checks passed
@keenborder786 keenborder786 deleted the langchain_llm_multiple_cache_config branch August 7, 2023 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants