langchain_community.cache.MomentoCache

class langchain_community.cache.MomentoCache(cache_client: momento.CacheClient, cache_name: str, *, ttl: Optional[timedelta] = None, ensure_cache_exists: bool = True)[source]

使用Momento作为后端的缓存。请参阅https://gomomento.com/

实例化一个使用Momento作为后端的提示缓存。

注意:要实例化传递给MomentoCache的缓存客户端,您必须拥有Momento帐户。请参阅https://gomomento.com/。

参数:

cache_client(CacheClient):Momento缓存客户端。 cache_name(str):用于存储数据的缓存的名称。 ttl(Optional[timedelta],可选):缓存项的生存时间。默认为None,即使用客户端默认的TTL。 ensure_cache_exists(bool,可选):如果缓存不存在,则创建缓存。默认为True。

引发:

ImportError:未安装Momento python包。 TypeError:cache_client不是momento.CacheClientObject类型。 ValueError:ttl既不是空值也不是非负值。

Methods

__init__(cache_client, cache_name, *[, ttl, ...])

实例化一个使用Momento作为后端的提示缓存。

aclear(**kwargs)

清除可以接受额外关键字参数的缓存。

alookup(prompt, llm_string)

根据提示和llm_string进行查找。

aupdate(prompt, llm_string, return_val)

根据提示和llm_string更新缓存。

clear(**kwargs)

清除缓存。

from_client_params(cache_name, ttl, *[, ...])

从CacheClient参数构建缓存。

lookup(prompt, llm_string)

在缓存中通过提示和关联的模型和设置查找LLM生成。

update(prompt, llm_string, return_val)

将llm生成存储在缓存中。

Parameters
  • cache_client (momento.CacheClient) –

  • cache_name (str) –

  • ttl (Optional[timedelta]) –

  • ensure_cache_exists (bool) –

__init__(cache_client: momento.CacheClient, cache_name: str, *, ttl: Optional[timedelta] = None, ensure_cache_exists: bool = True)[source]

实例化一个使用Momento作为后端的提示缓存。

注意:要实例化传递给MomentoCache的缓存客户端,您必须拥有Momento帐户。请参阅https://gomomento.com/。

参数:

cache_client(CacheClient):Momento缓存客户端。 cache_name(str):用于存储数据的缓存的名称。 ttl(Optional[timedelta],可选):缓存项的生存时间。默认为None,即使用客户端默认的TTL。 ensure_cache_exists(bool,可选):如果缓存不存在,则创建缓存。默认为True。

引发:

ImportError:未安装Momento python包。 TypeError:cache_client不是momento.CacheClientObject类型。 ValueError:ttl既不是空值也不是非负值。

Parameters
  • cache_client (momento.CacheClient) –

  • cache_name (str) –

  • ttl (Optional[timedelta]) –

  • ensure_cache_exists (bool) –

async aclear(**kwargs: Any) None

清除可以接受额外关键字参数的缓存。

Parameters

kwargs (Any) –

Return type

None

async alookup(prompt: str, llm_string: str) Optional[Sequence[Generation]]

根据提示和llm_string进行查找。

期望缓存实现从提示和llm_string的2元组中生成一个键(例如,通过使用分隔符将它们连接起来)。

参数:
prompt:提示的字符串表示。

对于Chat模型,提示是将提示序列化为语言模型的非平凡表示。

llm_string:LLM配置的字符串表示。

这用于捕获LLM的调用参数 (例如,模型名称、温度、停止标记、最大标记等)。 这些调用参数被序列化为字符串表示。

返回:

在缓存未命中时,返回None。在缓存命中时,返回缓存的值。 缓存的值是Generations(或子类)的列表。

Parameters
  • prompt (str) –

  • llm_string (str) –

Return type

Optional[Sequence[Generation]]

async aupdate(prompt: str, llm_string: str, return_val: Sequence[Generation]) None

根据提示和llm_string更新缓存。

提示和llm_string用于生成缓存的键。 键应该与查找方法的键匹配。

参数:
prompt:提示的字符串表示。

对于Chat模型,提示是将提示序列化为语言模型的非平凡表示。

llm_string:LLM配置的字符串表示。

这用于捕获LLM的调用参数 (例如,模型名称、温度、停止标记、最大标记等)。 这些调用参数被序列化为字符串表示。

return_val:要缓存的值。该值是Generations的列表(或子类)。

Parameters
  • prompt (str) –

  • llm_string (str) –

  • return_val (Sequence[Generation]) –

Return type

None

clear(**kwargs: Any) None[source]

清除缓存。

抛出:

SdkException: Momento 服务或网络错误

Parameters

kwargs (Any) –

Return type

None

classmethod from_client_params(cache_name: str, ttl: timedelta, *, configuration: Optional[momento.config.Configuration] = None, api_key: Optional[str] = None, auth_token: Optional[str] = None, **kwargs: Any) MomentoCache[source]

从CacheClient参数构建缓存。

Parameters
  • cache_name (str) –

  • ttl (timedelta) –

  • configuration (Optional[momento.config.Configuration]) –

  • api_key (Optional[str]) –

  • auth_token (Optional[str]) –

  • kwargs (Any) –

Return type

MomentoCache

lookup(prompt: str, llm_string: str) Optional[Sequence[Generation]][source]

在缓存中通过提示和关联的模型和设置查找LLM生成。

参数:

prompt(str):通过语言模型运行的提示。 llm_string(str):语言模型版本和设置。

引发:

SdkException:Momento服务或网络错误

返回:

Optional[RETURN_VAL_TYPE]:语言模型生成的列表。

Parameters
  • prompt (str) –

  • llm_string (str) –

Return type

Optional[Sequence[Generation]]

update(prompt: str, llm_string: str, return_val: Sequence[Generation]) None[source]

将llm生成存储在缓存中。

参数:

prompt(str):通过语言模型运行的提示。 llm_string(str):语言模型字符串。 return_val(RETURN_VAL_TYPE):语言模型生成列表。

引发:

SdkException:Momento服务或网络错误 Exception:意外响应

Parameters
  • prompt (str) –

  • llm_string (str) –

  • return_val (Sequence[Generation]) –

Return type

None

Examples using MomentoCache