diff --git a/libs/community/langchain_community/embeddings/baidu_qianfan_endpoint.py b/libs/community/langchain_community/embeddings/baidu_qianfan_endpoint.py index 42c840a4a4..44bfde0bf9 100644 --- a/libs/community/langchain_community/embeddings/baidu_qianfan_endpoint.py +++ b/libs/community/langchain_community/embeddings/baidu_qianfan_endpoint.py @@ -11,12 +11,45 @@ logger = logging.getLogger(__name__) class QianfanEmbeddingsEndpoint(BaseModel, Embeddings): - """`Baidu Qianfan Embeddings` embedding models.""" + """Baidu Qianfan Embeddings embedding models. - qianfan_ak: Optional[SecretStr] = None + Setup: + To use, you should have the ``qianfan`` python package installed, and set + environment variables ``QIANFAN_AK``, ``QIANFAN_SK``. + + .. code-block:: bash + + pip install qianfan + export QIANFAN_AK="your-api-key" + export QIANFAN_SK="your-secret_key" + + Instantiate: + .. code-block:: python + + from langchain_community.embeddings import QianfanEmbeddingsEndpoint + + embeddings = QianfanEmbeddingsEndpoint() + + Embed: + .. code-block:: python + + # embed the documents + vectors = embeddings.embed_documents([text1, text2, ...]) + + # embed the query + vectors = embeddings.embed_query(text) + + # embed the documents with async + vectors = await embeddings.aembed_documents([text1, text2, ...]) + + # embed the query with async + vectors = await embeddings.aembed_query(text) + """ # noqa: E501 + + qianfan_ak: Optional[SecretStr] = Field(default=None, alias="api_key") """Qianfan application apikey""" - qianfan_sk: Optional[SecretStr] = None + qianfan_sk: Optional[SecretStr] = Field(default=None, alias="secret_key") """Qianfan application secretkey""" chunk_size: int = 16 diff --git a/libs/community/tests/integration_tests/embeddings/test_qianfan_endpoint.py b/libs/community/tests/integration_tests/embeddings/test_qianfan_endpoint.py index 6fc8cfd7f8..a0edf46a6f 100644 --- a/libs/community/tests/integration_tests/embeddings/test_qianfan_endpoint.py +++ b/libs/community/tests/integration_tests/embeddings/test_qianfan_endpoint.py @@ -1,5 +1,9 @@ """Test Baidu Qianfan Embedding Endpoint.""" +from typing import cast + +from langchain_core.pydantic_v1 import SecretStr + from langchain_community.embeddings.baidu_qianfan_endpoint import ( QianfanEmbeddingsEndpoint, ) @@ -38,3 +42,17 @@ def test_rate_limit() -> None: assert len(output) == 2 assert len(output[0]) == 384 assert len(output[1]) == 384 + + +def test_initialization_with_alias() -> None: + """Test qianfan embedding model initialization with alias.""" + api_key = "your-api-key" + secret_key = "your-secret-key" + + embeddings = QianfanEmbeddingsEndpoint( # type: ignore[arg-type, call-arg] + api_key=api_key, # type: ignore[arg-type] + secret_key=secret_key, # type: ignore[arg-type] + ) + + assert cast(SecretStr, embeddings.qianfan_ak).get_secret_value() == api_key + assert cast(SecretStr, embeddings.qianfan_sk).get_secret_value() == secret_key