community: Update Replicate LLM and fix tests (#27655)

**Description:** 
- Fix bug in Replicate LLM class, where it was looking for parameter
names in a place where they no longer exist in pydantic 2, resulting in
the "Field required" validation error described in the issue.
- Fix Replicate LLM integration tests to:
  - Use active models on Replicate.
- Use the correct model parameter `max_new_tokens` as shown in the
[Replicate
docs](https://replicate.com/docs/guides/language-models/how-to-use#minimum-and-maximum-new-tokens).
  - Use callbacks instead of deprecated callback_manager.

**Issue:** #26937 

**Dependencies:** n/a

**Twitter handle:** n/a

---------

Signed-off-by: Fayvor Love <fayvor@gmail.com>
Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
fayvor 2024-10-30 12:07:08 -04:00 committed by GitHub
parent e593e017d2
commit 3b956b3a97
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 12 deletions

View File

@ -78,7 +78,7 @@ class Replicate(LLM):
@classmethod @classmethod
def build_extra(cls, values: Dict[str, Any]) -> Any: def build_extra(cls, values: Dict[str, Any]) -> Any:
"""Build extra kwargs from additional params that were passed in.""" """Build extra kwargs from additional params that were passed in."""
all_required_field_names = {field.alias for field in get_fields(cls).values()} all_required_field_names = {field for field in get_fields(cls).keys()}
input = values.pop("input", {}) input = values.pop("input", {})
if input: if input:

View File

@ -1,16 +1,18 @@
"""Test Replicate API wrapper.""" """Test Replicate API wrapper."""
from langchain_core.callbacks import CallbackManager
from langchain_community.llms.replicate import Replicate from langchain_community.llms.replicate import Replicate
from tests.unit_tests.callbacks.fake_callback_handler import FakeCallbackHandler from tests.unit_tests.callbacks.fake_callback_handler import FakeCallbackHandler
TEST_MODEL = "replicate/dolly-v2-12b:ef0e1aefc61f8e096ebe4db6b2bacc297daf2ef6899f0f7e001ec445893500e5" # noqa: E501 TEST_MODEL_HELLO = (
"replicate/hello-world:"
+ "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa"
)
TEST_MODEL_LANG = "meta/meta-llama-3-8b-instruct"
def test_replicate_call() -> None: def test_replicate_call() -> None:
"""Test simple non-streaming call to Replicate.""" """Test simple non-streaming call to Replicate."""
llm = Replicate(model=TEST_MODEL) llm = Replicate(model=TEST_MODEL_HELLO)
output = llm.invoke("What is LangChain") output = llm.invoke("What is LangChain")
assert output assert output
assert isinstance(output, str) assert isinstance(output, str)
@ -19,9 +21,10 @@ def test_replicate_call() -> None:
def test_replicate_streaming_call() -> None: def test_replicate_streaming_call() -> None:
"""Test streaming call to Replicate.""" """Test streaming call to Replicate."""
callback_handler = FakeCallbackHandler() callback_handler = FakeCallbackHandler()
callback_manager = CallbackManager([callback_handler])
llm = Replicate(streaming=True, callback_manager=callback_manager, model=TEST_MODEL) llm = Replicate(
streaming=True, callbacks=[callback_handler], model=TEST_MODEL_HELLO
)
output = llm.invoke("What is LangChain") output = llm.invoke("What is LangChain")
assert output assert output
assert isinstance(output, str) assert isinstance(output, str)
@ -30,17 +33,17 @@ def test_replicate_streaming_call() -> None:
def test_replicate_model_kwargs() -> None: def test_replicate_model_kwargs() -> None:
"""Test simple non-streaming call to Replicate.""" """Test simple non-streaming call to Replicate."""
llm = Replicate( # type: ignore[call-arg] llm = Replicate( # type: ignore[call-arg]
model=TEST_MODEL, model_kwargs={"max_length": 100, "temperature": 0.01} model=TEST_MODEL_LANG, model_kwargs={"max_new_tokens": 10, "temperature": 0.01}
) )
long_output = llm.invoke("What is LangChain") long_output = llm.invoke("What is LangChain")
llm = Replicate( # type: ignore[call-arg] llm = Replicate( # type: ignore[call-arg]
model=TEST_MODEL, model_kwargs={"max_length": 10, "temperature": 0.01} model=TEST_MODEL_LANG, model_kwargs={"max_new_tokens": 5, "temperature": 0.01}
) )
short_output = llm.invoke("What is LangChain") short_output = llm.invoke("What is LangChain")
assert len(short_output) < len(long_output) assert len(short_output) < len(long_output)
assert llm.model_kwargs == {"max_length": 10, "temperature": 0.01} assert llm.model_kwargs == {"max_new_tokens": 5, "temperature": 0.01}
def test_replicate_input() -> None: def test_replicate_input() -> None:
llm = Replicate(model=TEST_MODEL, input={"max_length": 10}) llm = Replicate(model=TEST_MODEL_LANG, input={"max_new_tokens": 10})
assert llm.model_kwargs == {"max_length": 10} assert llm.model_kwargs == {"max_new_tokens": 10}