Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Standardize QianfanEmbeddingsEndpoint #24786

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading