2023-02-07 06:29:25 +00:00
|
|
|
"""Test Anthropic API wrapper."""
|
|
|
|
from typing import Generator
|
|
|
|
|
2023-03-29 02:49:14 +00:00
|
|
|
import pytest
|
|
|
|
|
2023-03-28 00:25:00 +00:00
|
|
|
from langchain.callbacks.base import CallbackManager
|
2023-02-07 06:29:25 +00:00
|
|
|
from langchain.llms.anthropic import Anthropic
|
2023-03-29 02:49:14 +00:00
|
|
|
from langchain.schema import LLMResult
|
2023-03-28 00:25:00 +00:00
|
|
|
from tests.unit_tests.callbacks.fake_callback_handler import FakeCallbackHandler
|
2023-02-07 06:29:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_anthropic_call() -> None:
|
|
|
|
"""Test valid call to anthropic."""
|
|
|
|
llm = Anthropic(model="bare-nano-0")
|
|
|
|
output = llm("Say foo:")
|
|
|
|
assert isinstance(output, str)
|
|
|
|
|
|
|
|
|
|
|
|
def test_anthropic_streaming() -> None:
|
|
|
|
"""Test streaming tokens from anthropic."""
|
|
|
|
llm = Anthropic(model="bare-nano-0")
|
|
|
|
generator = llm.stream("I'm Pickle Rick")
|
|
|
|
|
|
|
|
assert isinstance(generator, Generator)
|
|
|
|
|
|
|
|
for token in generator:
|
|
|
|
assert isinstance(token["completion"], str)
|
2023-03-28 00:25:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_anthropic_streaming_callback() -> None:
|
|
|
|
"""Test that streaming correctly invokes on_llm_new_token callback."""
|
|
|
|
callback_handler = FakeCallbackHandler()
|
|
|
|
callback_manager = CallbackManager([callback_handler])
|
|
|
|
llm = Anthropic(
|
|
|
|
streaming=True,
|
|
|
|
callback_manager=callback_manager,
|
|
|
|
verbose=True,
|
|
|
|
)
|
|
|
|
llm("Write me a sentence with 100 words.")
|
|
|
|
assert callback_handler.llm_streams > 1
|
2023-03-29 02:49:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_anthropic_async_generate() -> None:
|
|
|
|
"""Test async generate."""
|
|
|
|
llm = Anthropic()
|
|
|
|
output = await llm.agenerate(["How many toes do dogs have?"])
|
|
|
|
assert isinstance(output, LLMResult)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_anthropic_async_streaming_callback() -> None:
|
|
|
|
"""Test that streaming correctly invokes on_llm_new_token callback."""
|
|
|
|
callback_handler = FakeCallbackHandler()
|
|
|
|
callback_manager = CallbackManager([callback_handler])
|
|
|
|
llm = Anthropic(
|
|
|
|
streaming=True,
|
|
|
|
callback_manager=callback_manager,
|
|
|
|
verbose=True,
|
|
|
|
)
|
|
|
|
result = await llm.agenerate(["How many toes do dogs have?"])
|
|
|
|
assert callback_handler.llm_streams > 1
|
|
|
|
assert isinstance(result, LLMResult)
|