2022-10-24 21:51:15 +00:00
|
|
|
"""Test Cohere API wrapper."""
|
|
|
|
|
2022-12-13 14:46:01 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2024-03-01 20:27:53 +00:00
|
|
|
from langchain_core.pydantic_v1 import SecretStr
|
|
|
|
from pytest import MonkeyPatch
|
|
|
|
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_community.llms.cohere import Cohere
|
|
|
|
from langchain_community.llms.loading import load_llm
|
2022-12-13 14:46:01 +00:00
|
|
|
from tests.integration_tests.llms.utils import assert_llm_equality
|
2022-10-24 21:51:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_cohere_call() -> None:
|
|
|
|
"""Test valid call to cohere."""
|
2024-05-13 18:55:07 +00:00
|
|
|
llm = Cohere(max_tokens=10) # type: ignore[call-arg]
|
2024-04-24 23:39:23 +00:00
|
|
|
output = llm.invoke("Say foo:")
|
2022-10-24 21:51:15 +00:00
|
|
|
assert isinstance(output, str)
|
2022-12-13 14:46:01 +00:00
|
|
|
|
|
|
|
|
2024-03-01 20:27:53 +00:00
|
|
|
def test_cohere_api_key(monkeypatch: MonkeyPatch) -> None:
|
|
|
|
"""Test that cohere api key is a secret key."""
|
|
|
|
# test initialization from init
|
2024-05-13 18:55:07 +00:00
|
|
|
assert isinstance(Cohere(cohere_api_key="1").cohere_api_key, SecretStr) # type: ignore[arg-type, call-arg]
|
2024-03-01 20:27:53 +00:00
|
|
|
|
|
|
|
# test initialization from env variable
|
|
|
|
monkeypatch.setenv("COHERE_API_KEY", "secret-api-key")
|
2024-05-13 18:55:07 +00:00
|
|
|
assert isinstance(Cohere().cohere_api_key, SecretStr) # type: ignore[call-arg]
|
2024-03-01 20:27:53 +00:00
|
|
|
|
|
|
|
|
2022-12-13 14:46:01 +00:00
|
|
|
def test_saving_loading_llm(tmp_path: Path) -> None:
|
|
|
|
"""Test saving/loading an Cohere LLM."""
|
2024-05-13 18:55:07 +00:00
|
|
|
llm = Cohere(max_tokens=10) # type: ignore[call-arg]
|
2022-12-13 14:46:01 +00:00
|
|
|
llm.save(file_path=tmp_path / "cohere.yaml")
|
|
|
|
loaded_llm = load_llm(tmp_path / "cohere.yaml")
|
|
|
|
assert_llm_equality(llm, loaded_llm)
|