mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-16 08:06:14 +00:00
Hi there, I'm Célina from 🤗, This PR introduces support for Hugging Face's serverless Inference Providers (documentation [here](https://huggingface.co/docs/inference-providers/index)), allowing users to specify different providers for chat completion and text generation tasks. This PR also removes the usage of `InferenceClient.post()` method in `HuggingFaceEndpoint`, in favor of the task-specific `text_generation` method. `InferenceClient.post()` is deprecated and will be removed in `huggingface_hub v0.31.0`. --- ## Changes made - bumped the minimum required version of the `huggingface-hub` package to ensure compatibility with the latest API usage. - added a `provider` field to `HuggingFaceEndpoint`, enabling users to select the inference provider (e.g., 'cerebras', 'together', 'fireworks-ai'). Defaults to `hf-inference` (HF Inference API). - replaced the deprecated `InferenceClient.post()` call in `HuggingFaceEndpoint` with the task-specific `text_generation` method for future-proofing, `post()` will be removed in huggingface-hub v0.31.0. - updated the `ChatHuggingFace` component: - added async and streaming support. - added support for tool calling. - exposed underlying chat completion parameters for more granular control. - Added integration tests for `ChatHuggingFace` and updated the corresponding unit tests. ✅ All changes are backward compatible. --------- Co-authored-by: ccurme <chester.curme@gmail.com>
71 lines
2.6 KiB
Python
71 lines
2.6 KiB
Python
"""Standard LangChain interface tests"""
|
|
|
|
import pytest
|
|
from langchain_core.language_models import BaseChatModel
|
|
from langchain_core.tools import BaseTool
|
|
from langchain_tests.integration_tests import ChatModelIntegrationTests
|
|
|
|
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
|
|
|
|
|
|
class TestHuggingFaceEndpoint(ChatModelIntegrationTests):
|
|
@property
|
|
def chat_model_class(self) -> type[BaseChatModel]:
|
|
return ChatHuggingFace
|
|
|
|
@property
|
|
def chat_model_params(self) -> dict:
|
|
llm = HuggingFaceEndpoint( # type: ignore[call-arg]
|
|
repo_id="Qwen/Qwen2.5-72B-Instruct",
|
|
task="conversational",
|
|
provider="fireworks-ai",
|
|
temperature=0,
|
|
)
|
|
return {"llm": llm}
|
|
|
|
@pytest.fixture
|
|
def model(self) -> BaseChatModel:
|
|
return self.chat_model_class(**self.chat_model_params) # type: ignore[call-arg]
|
|
|
|
@pytest.mark.xfail(
|
|
reason=("Overrding, testing only typed dict and json schema structured output")
|
|
)
|
|
@pytest.mark.parametrize("schema_type", ["typeddict", "json_schema"])
|
|
def test_structured_output(self, model: BaseChatModel, schema_type: str) -> None:
|
|
super().test_structured_output(model, schema_type)
|
|
|
|
@pytest.mark.xfail(
|
|
reason=("Overrding, testing only typed dict and json schema structured output")
|
|
)
|
|
@pytest.mark.parametrize("schema_type", ["typeddict", "json_schema"])
|
|
async def test_structured_output_async(
|
|
self, model: BaseChatModel, schema_type: str
|
|
) -> None: # type: ignore[override]
|
|
super().test_structured_output(model, schema_type)
|
|
|
|
@pytest.mark.xfail(reason=("Pydantic structured output is not supported"))
|
|
def test_structured_output_pydantic_2_v1(self, model: BaseChatModel) -> None:
|
|
super().test_structured_output_pydantic_2_v1(model)
|
|
|
|
@pytest.mark.xfail(reason=("Pydantic structured output is not supported"))
|
|
def test_structured_output_optional_param(self, model: BaseChatModel) -> None:
|
|
super().test_structured_output_optional_param(model)
|
|
|
|
@pytest.mark.xfail(reason=("Not implemented"))
|
|
def test_tool_message_histories_list_content(
|
|
self, model: BaseChatModel, my_adder_tool: BaseTool
|
|
) -> None:
|
|
super().test_tool_message_histories_list_content(
|
|
model, my_adder_tool=my_adder_tool
|
|
)
|
|
|
|
@pytest.mark.xfail(reason=("Not implemented"))
|
|
def test_structured_few_shot_examples(
|
|
self, model: BaseChatModel, my_adder_tool: BaseTool
|
|
) -> None:
|
|
super().test_structured_few_shot_examples(model, my_adder_tool=my_adder_tool)
|
|
|
|
@property
|
|
def has_tool_choice(self) -> bool:
|
|
return False
|