docs: Standardize QianfanEmbeddingsEndpoint (#24786)

- **Description:** Standardize QianfanEmbeddingsEndpoint, include:
  - docstrings, the issue #21983 
  - model init arg names, the issue #20085
This commit is contained in:
maang-h 2024-07-30 01:19:24 +08:00 committed by GitHub
parent 9998e55936
commit bf685c242f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 3 deletions

View File

@ -11,12 +11,45 @@ logger = logging.getLogger(__name__)
class QianfanEmbeddingsEndpoint(BaseModel, Embeddings): 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 application apikey"""
qianfan_sk: Optional[SecretStr] = None qianfan_sk: Optional[SecretStr] = Field(default=None, alias="secret_key")
"""Qianfan application secretkey""" """Qianfan application secretkey"""
chunk_size: int = 16 chunk_size: int = 16

View File

@ -1,5 +1,9 @@
"""Test Baidu Qianfan Embedding Endpoint.""" """Test Baidu Qianfan Embedding Endpoint."""
from typing import cast
from langchain_core.pydantic_v1 import SecretStr
from langchain_community.embeddings.baidu_qianfan_endpoint import ( from langchain_community.embeddings.baidu_qianfan_endpoint import (
QianfanEmbeddingsEndpoint, QianfanEmbeddingsEndpoint,
) )
@ -38,3 +42,17 @@ def test_rate_limit() -> None:
assert len(output) == 2 assert len(output) == 2
assert len(output[0]) == 384 assert len(output[0]) == 384
assert len(output[1]) == 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