File tree Expand file tree Collapse file tree 2 files changed +54
-3
lines changed Expand file tree Collapse file tree 2 files changed +54
-3
lines changed Original file line number Diff line number Diff line change 11
11
12
12
13
13
class QianfanEmbeddingsEndpoint (BaseModel , Embeddings ):
14
- """` Baidu Qianfan Embeddings` embedding models."""
14
+ """Baidu Qianfan Embeddings embedding models.
15
15
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" )
17
50
"""Qianfan application apikey"""
18
51
19
- qianfan_sk : Optional [SecretStr ] = None
52
+ qianfan_sk : Optional [SecretStr ] = Field ( default = None , alias = "secret_key" )
20
53
"""Qianfan application secretkey"""
21
54
22
55
chunk_size : int = 16
Original file line number Diff line number Diff line change 1
1
"""Test Baidu Qianfan Embedding Endpoint."""
2
2
3
+ from typing import cast
4
+
5
+ from langchain_core .pydantic_v1 import SecretStr
6
+
3
7
from langchain_community .embeddings .baidu_qianfan_endpoint import (
4
8
QianfanEmbeddingsEndpoint ,
5
9
)
@@ -38,3 +42,17 @@ def test_rate_limit() -> None:
38
42
assert len (output ) == 2
39
43
assert len (output [0 ]) == 384
40
44
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
You can’t perform that action at this time.
0 commit comments