Skip to content

Commit 8f1f6d9

Browse files
maang-holgamurraft
authored andcommitted
docs: Standardize QianfanEmbeddingsEndpoint (langchain-ai#24786)
- **Description:** Standardize QianfanEmbeddingsEndpoint, include: - docstrings, the issue langchain-ai#21983 - model init arg names, the issue langchain-ai#20085
1 parent 1a6a07a commit 8f1f6d9

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

libs/community/langchain_community/embeddings/baidu_qianfan_endpoint.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,45 @@
1111

1212

1313
class QianfanEmbeddingsEndpoint(BaseModel, Embeddings):
14-
"""`Baidu Qianfan Embeddings` embedding models."""
14+
"""Baidu Qianfan Embeddings embedding models.
1515
16-
qianfan_ak: Optional[SecretStr] = None
16+
Setup:
17+
To use, you should have the ``qianfan`` python package installed, and set
18+
environment variables ``QIANFAN_AK``, ``QIANFAN_SK``.
19+
20+
.. code-block:: bash
21+
22+
pip install qianfan
23+
export QIANFAN_AK="your-api-key"
24+
export QIANFAN_SK="your-secret_key"
25+
26+
Instantiate:
27+
.. code-block:: python
28+
29+
from langchain_community.embeddings import QianfanEmbeddingsEndpoint
30+
31+
embeddings = QianfanEmbeddingsEndpoint()
32+
33+
Embed:
34+
.. code-block:: python
35+
36+
# embed the documents
37+
vectors = embeddings.embed_documents([text1, text2, ...])
38+
39+
# embed the query
40+
vectors = embeddings.embed_query(text)
41+
42+
# embed the documents with async
43+
vectors = await embeddings.aembed_documents([text1, text2, ...])
44+
45+
# embed the query with async
46+
vectors = await embeddings.aembed_query(text)
47+
""" # noqa: E501
48+
49+
qianfan_ak: Optional[SecretStr] = Field(default=None, alias="api_key")
1750
"""Qianfan application apikey"""
1851

19-
qianfan_sk: Optional[SecretStr] = None
52+
qianfan_sk: Optional[SecretStr] = Field(default=None, alias="secret_key")
2053
"""Qianfan application secretkey"""
2154

2255
chunk_size: int = 16

libs/community/tests/integration_tests/embeddings/test_qianfan_endpoint.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"""Test Baidu Qianfan Embedding Endpoint."""
22

3+
from typing import cast
4+
5+
from langchain_core.pydantic_v1 import SecretStr
6+
37
from langchain_community.embeddings.baidu_qianfan_endpoint import (
48
QianfanEmbeddingsEndpoint,
59
)
@@ -38,3 +42,17 @@ def test_rate_limit() -> None:
3842
assert len(output) == 2
3943
assert len(output[0]) == 384
4044
assert len(output[1]) == 384
45+
46+
47+
def test_initialization_with_alias() -> None:
48+
"""Test qianfan embedding model initialization with alias."""
49+
api_key = "your-api-key"
50+
secret_key = "your-secret-key"
51+
52+
embeddings = QianfanEmbeddingsEndpoint( # type: ignore[arg-type, call-arg]
53+
api_key=api_key, # type: ignore[arg-type]
54+
secret_key=secret_key, # type: ignore[arg-type]
55+
)
56+
57+
assert cast(SecretStr, embeddings.qianfan_ak).get_secret_value() == api_key
58+
assert cast(SecretStr, embeddings.qianfan_sk).get_secret_value() == secret_key

0 commit comments

Comments
 (0)