From bf685c242fe224bc15c904cd921eb4c5930b9bc9 Mon Sep 17 00:00:00 2001 From: maang-h <55082429+maang-h@users.noreply.github.com> Date: Tue, 30 Jul 2024 01:19:24 +0800 Subject: [PATCH] docs: Standardize QianfanEmbeddingsEndpoint (#24786) - **Description:** Standardize QianfanEmbeddingsEndpoint, include: - docstrings, the issue #21983 - model init arg names, the issue #20085 --- .../embeddings/baidu_qianfan_endpoint.py | 39 +++++++++++++++++-- .../embeddings/test_qianfan_endpoint.py | 18 +++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/libs/community/langchain_community/embeddings/baidu_qianfan_endpoint.py b/libs/community/langchain_community/embeddings/baidu_qianfan_endpoint.py index 42c840a4a4752..44bfde0bf949a 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 @@ 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 6fc8cfd7f8c6c..a0edf46a6f946 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