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

Caching for chat based models. #2976

Closed
gd1m3y opened this issue Apr 16, 2023 · 8 comments
Closed

Caching for chat based models. #2976

gd1m3y opened this issue Apr 16, 2023 · 8 comments

Comments

@gd1m3y
Copy link

gd1m3y commented Apr 16, 2023

Is there any docs or related issues for caching the Azure chat open ai responses i cannot find one.

@CelestialCoder03
Copy link

Is there any docs or related issues for caching the Azure chat open ai responses i cannot find one.

I found a possible solution from the release note of gptcache. It worked with ChatOpenAI class, not so sure about Azure though.

from langchain.chat_models import ChatOpenAI
from gptcache import cache
from gptcache.adapter.langchain_models import LangChainChat

def get_msg(data, **_):
    return data.get("messages")[-1].content


cache.init(
    pre_embedding_func=get_msg,
)

chat = LangChainChat(chat=ChatOpenAI(temperature=0))

@PhilipMay
Copy link

I can confirm, that AzureChatOpenAI does not use caching at the moment.

Cache init by:

from langchain.cache import SQLiteCache
import langchain
langchain.llm_cache = SQLiteCache(database_path="./langchain-cache.db")

LangChain Version 0.0.166

Would be nice if this can be added. Thanks!

@jakobsa
Copy link

jakobsa commented Jun 14, 2023

relates (#1644)

@iljoong
Copy link

iljoong commented Jun 19, 2023

Is there any docs or related issues for caching the Azure chat open ai responses i cannot find one.

I found a possible solution from the release note of gptcache. It worked with ChatOpenAI class, not so sure about Azure though.

from langchain.chat_models import ChatOpenAI
from gptcache import cache
from gptcache.adapter.langchain_models import LangChainChat

def get_msg(data, **_):
    return data.get("messages")[-1].content


cache.init(
    pre_embedding_func=get_msg,
)

chat = LangChainChat(chat=ChatOpenAI(temperature=0))

I have tested on Azure OpenAI and it works!

from langchain.chat_models import AzureChatOpenAI
from gptcache.adapter.langchain_models import LangChainChat
from gptcache import cache

def get_msg(data, **_):
    return data.get("messages")[-1].content


cache.init(
    pre_embedding_func=get_msg,
)

chat = LangChainChat(chat=AzureChatOpenAI(temperature=0.0, deployment_name='gpt-35-turbo'))

from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)

messages = [HumanMessage(content="Tell me a very long version of joke")]
--
%%time 
chat(messages)
> CPU times: user 15.1 ms, sys: 0 ns, total: 15.1 ms
> Wall time: 16.7 s
--
%%time
chat(messages)
> CPU times: user 238 µs, sys: 74 µs, total: 312 µs
> Wall time: 313 µs

@Praj-17
Copy link

Praj-17 commented Aug 18, 2023

@czuo0303 I tried the same code for using ChatOpenAI but I am facing the following error, which version of langchain and gptcache are you on ? Also, can someone, please help me figure out this error?

My Code

from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage
from gptcache import cache
from gptcache.adapter.langchain_models import LangChainChat,LangChainLLMs
from gptcache.embedding import  Onnx
from gptcache.manager import CacheBase, VectorBase, get_data_manager
from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation
onnx = Onnx()

data_manager = get_data_manager(CacheBase("sqlite"), VectorBase("faiss", dimension=onnx.dimension))
cache.init(
    embedding_func=onnx.to_embeddings,
    data_manager=data_manager,
    similarity_evaluation=SearchDistanceEvaluation(),
    )
cache.set_openai_key()

chat = LangChainChat(chat=ChatOpenAI(temperature=0))

This gives me the following error

Traceback (most recent call last): File "e:/CODING PLAYGROUND/CODE/YoZu/Updated Backend/Chat/actions/langchain_chat_completion.py", line 20, in <module> chat = LangChainChat(chat=ChatOpenAI(temperature=0)) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\site-packages\langchain\load\serializable.py", line 74, in __init__ super().__init__(**kwargs) File "pydantic\main.py", line 340, in pydantic.main.BaseModel.__init__ File "pydantic\main.py", line 1066, in pydantic.main.validate_model File "pydantic\fields.py", line 439, in pydantic.fields.ModelField.get_default File "pydantic\utils.py", line 695, in pydantic.utils.smart_deepcopy File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 172, in deepcopy y = _reconstruct(x, memo, *rv) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 270, in _reconstruct state = deepcopy(state, memo) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 146, in deepcopy y = copier(x, memo) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 230, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 146, in deepcopy y = copier(x, memo) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 237, in _deepcopy_method return type(x)(x.__func__, deepcopy(x.__self__, memo)) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 172, in deepcopy y = _reconstruct(x, memo, *rv) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 270, in _reconstruct state = deepcopy(state, memo) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 146, in deepcopy y = copier(x, memo) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 230, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 172, in deepcopy y = _reconstruct(x, memo, *rv) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 270, in _reconstruct state = deepcopy(state, memo) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 146, in deepcopy y = copier(x, memo) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 230, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "C:\Users\pwayk\anaconda3\envs\yozu2\lib\copy.py", line 161, in deepcopy rv = reductor(4) TypeError: cannot pickle 'onnxruntime.capi.onnxruntime_pybind11_state.InferenceSession' object

I am using langchain-0.0.263 and gptcache-0.1.39.1

@etlevents
Copy link

me too

@SimFG
Copy link
Contributor

SimFG commented Sep 14, 2023

it will be fixed, detail: zilliztech/GPTCache#538

Copy link

dosubot bot commented Dec 14, 2023

Hi, @gd1m3y,

I'm helping the LangChain team manage their backlog and am marking this issue as stale. From what I understand, you were looking for documentation or related issues on caching Azure chat OpenAI responses, and there have been several comments discussing potential solutions and related issues. One user confirmed that AzureChatOpenAI does not currently use caching, while another user provided a possible solution using gptcache. Additionally, there were discussions around code shared by a user and an error encountered when trying to use ChatOpenAI. Lastly, a user mentioned that the issue will be fixed and provided a link to a pull request for more details.

Could you please confirm if this issue is still relevant to the latest version of the LangChain repository? If it is, please let the LangChain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or the issue will be automatically closed in 7 days.

Thank you for your understanding and cooperation.

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Dec 14, 2023
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 21, 2023
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Dec 21, 2023
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

No branches or pull requests

8 participants