langchain-ai / langchain-google

MIT License
92 stars 108 forks source link

Caching for Gemini models not working as expected #371

Open aCampello opened 1 month ago

aCampello commented 1 month ago

Checked other resources

Example Code

from langchain.globals import set_llm_cache
from langchain_google_vertexai import ChatVertexAI

from langchain_community.cache import SQLiteCache

import sqlite3

!rm llm_cache.db
set_llm_cache(SQLiteCache("./llm_cache.db"))

llm = ChatVertexAI(
    model_name="gemini-1.0-pro",
)

llm.invoke("This is a drill")
llm.invoke("This is a drill")

con = sqlite3.connect("llm_cache.db")
cur = con.cursor()

cur.execute("SELECT * FROM full_llm_cache")
rows = cur.fetchall()

print(len(rows))

Error Message and Stack Trace (if applicable)

No response

Description

When invoking the cache for Gemini models, it does not cache as expected. If we run the aforementoned example (provided you have a google account with VertexAI access), the expected number of rows is 1, but it returns 2. It turns out that the "model string" is being modified internally. Check the below output when running

print(rows[0][1])
print(rows[1][1])
{"id": ["langchain", "chat_models", "vertexai", "ChatVertexAI"], "kwargs": {"default_metadata": [], "full_model_name": "projects/XXX/locations/us-central1/publishers/google/models/gemini-1.0-pro", "location": "us-central1", "max_retries": 6, "model_family": "1", "model_name": "gemini-1.0-pro", "n": 1, "project": "XXXX", "request_parallelism": 5}, "lc": 1, "name": "ChatVertexAI", "type": "constructor"}---[('stop', None)]
{"id": ["langchain", "chat_models", "vertexai", "ChatVertexAI"], "kwargs": {"client": {"id": ["google", "cloud", "aiplatform_v1beta1", "services", "prediction_service", "client", "PredictionServiceClient"], "lc": 1, "type": "not_implemented"}, "default_metadata": [], "full_model_name": "projects/XXXX/locations/us-central1/publishers/google/models/gemini-1.0-pro", "location": "us-central1", "max_retries": 6, "model_family": "1", "model_name": "gemini-1.0-pro", "n": 1, "project": "XXXX", "request_parallelism": 5}, "lc": 1, "name": "ChatVertexAI", "type": "constructor"}---[('stop', None)]

System Info

langchain==0.2.8
langchain-aws==0.1.6
langchain-community==0.2.7
langchain-core==0.2.19
langchain-google-vertexai==1.0.6
langchain-openai==0.1.7
langchain-text-splitters==0.2.2

platform: macOs Sonoma 14.5 Apple M2 python version: 3.11

efriis commented 1 month ago

moving to langchain-google repo!

aCampello commented 1 month ago

Thanks @efriis !