Skip to content

Commit

Permalink
docs: Standardize QianfanEmbeddingsEndpoint (#24786)
Browse files Browse the repository at this point in the history
- **Description:** Standardize QianfanEmbeddingsEndpoint, include:
  - docstrings, the issue #21983 
  - model init arg names, the issue #20085
  • Loading branch information
maang-h authored Jul 29, 2024
1 parent 9998e55 commit bf685c2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
)
Expand Down Expand Up @@ -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

0 comments on commit bf685c2

Please sign in to comment.