Skip to content

MemoryCache

不使用缓存

python
from langchain_community.llms import Ollama
import time
from functools import wraps

llm = Ollama(model="qwen2:1.5b",
             temperature = 0,)
def timer(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(f"\n函数 {func.__name__} 执行时间: {end_time - start_time:.2f} 秒")
        return result
    return wrapper

@timer
def predict():
    result = llm.predict("江苏的省会是哪里?")
    print(result)

predict()
predict()

输出结果

shell
江苏省的省会是南京。
函数 predict 执行时间: 0.28

江苏省的省会是南京。
函数 predict 执行时间: 0.15

使用缓存

set_llm_cache(InMemoryCache()) // 启动缓存

python
from langchain_community.llms import Ollama
from langchain.globals import set_llm_cache
from langchain.cache import InMemoryCache
import time
from functools import wraps

set_llm_cache(InMemoryCache()) 
llm = Ollama(model="qwen2:1.5b",
             temperature = 0,
             )
def timer(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(f"\n函数 {func.__name__} 执行时间: {end_time - start_time:.2f} 秒")
        return result
    return wrapper

@timer
def predict():
    result = llm.predict("江苏的省会是哪里?")
    print(result)

predict()
predict()

执行结果

shell
江苏省的省会是南京。
函数 predict 执行时间: 0.19

江苏省的省会是南京。
函数 predict 执行时间: 0.00

可见,第二次执行走的缓存

计时函数

python
import time
from functools import wraps

def timer(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(f"\n函数 {func.__name__} 执行时间: {end_time - start_time:.2f} 秒")
        return result
    return wrapper

jupyter环境测试

python
%%time
#导入需要的依赖包
from langchain_community.llms import Ollama
from langchain.globals import set_llm_cache
from langchain.cache import InMemoryCache

set_llm_cache(InMemoryCache())

llm = Ollama(model="qwen2:1.5b",temperature = 0)

llm.predict("江苏的省会是哪里?")
python
%%time
llm.predict("江苏的省会是哪里?")

image-20250401131626943

jupyter 可使用 %%time 统计执行耗时