https://python.langchain.com/docs/modules/model_io/llms/llm_caching # LLM 缓存 在开发LLM应用时,缓存是一个重要技术,可以通过存储API调用的响应来: - 减少API调用次数 - 提高应用响应速度 - 降低使用成本 - 在离线开发时提供确定性响应 LangChain提供两种缓存实现:内存缓存和本地文件缓存。 ## 内存缓存 使用内存作为缓存存储,适合开发调试: ```python from langchain.globals import set_llm_cache from langchain.cache import InMemoryCache set_llm_cache(InMemoryCache()) ``` ## SQLite缓存 持久化缓存到本地SQLite数据库: ```python from langchain.globals import set_llm_cache from langchain.cache import SQLiteCache set_llm_cache(SQLiteCache(database_path=".langchain.db")) ``` ## 其它缓存方案 也可集成其他缓存后端: ```python from redis import Redis from langchain.cache import RedisCache redis_client = Redis() set_llm_cache(RedisCache(redis_client)) ``` ## 关闭缓存 如需完全关闭缓存功能: ```python from langchain.globals import set_llm_cache set_llm_cache(None) ``` 通过合理使用缓存可以显著提升LLM应用的开发效率和运行性能。

https://python.langchain.com/docs/modules/model_io/llms/llm_caching

# LLM 缓存

在开发LLM应用时,缓存是一个重要技术,可以通过存储API调用的响应来:
- 减少API调用次数
- 提高应用响应速度
- 降低使用成本
- 在离线开发时提供确定性响应

LangChain提供两种缓存实现:内存缓存和本地文件缓存。

## 内存缓存

使用内存作为缓存存储,适合开发调试:

```python
from langchain.globals import set_llm_cache
from langchain.cache import InMemoryCache

set_llm_cache(InMemoryCache())
```

## SQLite缓存

持久化缓存到本地SQLite数据库:

```python
from langchain.globals import set_llm_cache
from langchain.cache import SQLiteCache

set_llm_cache(SQLiteCache(database_path=

原创文章,作者:海博社,如若转载,请注明出处:https://www.23btc.com/41110/

(0)
上一篇 1天前
下一篇 1天前

相关推荐