mirror of
https://github.com/hwchase17/langchain
synced 2024-11-10 01:10:59 +00:00
f92006de3c
0.2rc migrations - [x] Move memory - [x] Move remaining retrievers - [x] graph_qa chains - [x] some dependency from evaluation code potentially on math utils - [x] Move openapi chain from `langchain.chains.api.openapi` to `langchain_community.chains.openapi` - [x] Migrate `langchain.chains.ernie_functions` to `langchain_community.chains.ernie_functions` - [x] migrate `langchain/chains/llm_requests.py` to `langchain_community.chains.llm_requests` - [x] Moving `langchain_community.cross_enoders.base:BaseCrossEncoder` -> `langchain_community.retrievers.document_compressors.cross_encoder:BaseCrossEncoder` (namespace not ideal, but it needs to be moved to `langchain` to avoid circular deps) - [x] unit tests langchain -- add pytest.mark.community to some unit tests that will stay in langchain - [x] unit tests community -- move unit tests that depend on community to community - [x] mv integration tests that depend on community to community - [x] mypy checks Other todo - [x] Make deprecation warnings not noisy (need to use warn deprecated and check that things are implemented properly) - [x] Update deprecation messages with timeline for code removal (likely we actually won't be removing things until 0.4 release) -- will give people more time to transition their code. - [ ] Add information to deprecation warning to show users how to migrate their code base using langchain-cli - [ ] Remove any unnecessary requirements in langchain (e.g., is SQLALchemy required?) --------- Co-authored-by: Erick Friis <erick@langchain.dev>
98 lines
3.0 KiB
Python
98 lines
3.0 KiB
Python
"""Test Momento cache functionality.
|
|
|
|
To run tests, set the environment variable MOMENTO_AUTH_TOKEN to a valid
|
|
Momento auth token. This can be obtained by signing up for a free
|
|
Momento account at https://gomomento.com/.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from datetime import timedelta
|
|
from typing import Iterator
|
|
|
|
import pytest
|
|
from langchain.globals import set_llm_cache
|
|
from langchain_core.outputs import Generation, LLMResult
|
|
|
|
from langchain_community.cache import MomentoCache
|
|
from tests.unit_tests.llms.fake_llm import FakeLLM
|
|
|
|
|
|
def random_string() -> str:
|
|
return str(uuid.uuid4())
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def momento_cache() -> Iterator[MomentoCache]:
|
|
from momento import CacheClient, Configurations, CredentialProvider
|
|
|
|
cache_name = f"langchain-test-cache-{random_string()}"
|
|
client = CacheClient(
|
|
Configurations.Laptop.v1(),
|
|
CredentialProvider.from_environment_variable("MOMENTO_API_KEY"),
|
|
default_ttl=timedelta(seconds=30),
|
|
)
|
|
try:
|
|
llm_cache = MomentoCache(client, cache_name)
|
|
set_llm_cache(llm_cache)
|
|
yield llm_cache
|
|
finally:
|
|
client.delete_cache(cache_name)
|
|
|
|
|
|
def test_invalid_ttl() -> None:
|
|
from momento import CacheClient, Configurations, CredentialProvider
|
|
|
|
client = CacheClient(
|
|
Configurations.Laptop.v1(),
|
|
CredentialProvider.from_environment_variable("MOMENTO_API_KEY"),
|
|
default_ttl=timedelta(seconds=30),
|
|
)
|
|
with pytest.raises(ValueError):
|
|
MomentoCache(client, cache_name=random_string(), ttl=timedelta(seconds=-1))
|
|
|
|
|
|
def test_momento_cache_miss(momento_cache: MomentoCache) -> None:
|
|
llm = FakeLLM()
|
|
stub_llm_output = LLMResult(generations=[[Generation(text="foo")]])
|
|
assert llm.generate([random_string()]) == stub_llm_output
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"prompts, generations",
|
|
[
|
|
# Single prompt, single generation
|
|
([random_string()], [[random_string()]]),
|
|
# Single prompt, multiple generations
|
|
([random_string()], [[random_string(), random_string()]]),
|
|
# Single prompt, multiple generations
|
|
([random_string()], [[random_string(), random_string(), random_string()]]),
|
|
# Multiple prompts, multiple generations
|
|
(
|
|
[random_string(), random_string()],
|
|
[[random_string()], [random_string(), random_string()]],
|
|
),
|
|
],
|
|
)
|
|
def test_momento_cache_hit(
|
|
momento_cache: MomentoCache, prompts: list[str], generations: list[list[str]]
|
|
) -> None:
|
|
llm = FakeLLM()
|
|
params = llm.dict()
|
|
params["stop"] = None
|
|
llm_string = str(sorted([(k, v) for k, v in params.items()]))
|
|
|
|
llm_generations = [
|
|
[
|
|
Generation(text=generation, generation_info=params)
|
|
for generation in prompt_i_generations
|
|
]
|
|
for prompt_i_generations in generations
|
|
]
|
|
for prompt_i, llm_generations_i in zip(prompts, llm_generations):
|
|
momento_cache.update(prompt_i, llm_string, llm_generations_i)
|
|
|
|
assert llm.generate(prompts) == LLMResult(
|
|
generations=llm_generations, llm_output={}
|
|
)
|