23
23
24
24
25
25
class QianfanLLMEndpoint (LLM ):
26
- """Baidu Qianfan hosted open source or customized models.
26
+ """Baidu Qianfan completion model integration.
27
+
28
+ Setup:
29
+ Install ``qianfan`` and set environment variables ``QIANFAN_AK``, ``QIANFAN_SK``.
30
+
31
+ .. code-block:: bash
32
+
33
+ pip install qianfan
34
+ export QIANFAN_AK="your-api-key"
35
+ export QIANFAN_SK="your-secret_key"
36
+
37
+ Key init args — completion params:
38
+ model: str
39
+ Name of Qianfan model to use.
40
+ temperature: Optional[float]
41
+ Sampling temperature.
42
+ endpoint: Optional[str]
43
+ Endpoint of the Qianfan LLM
44
+ top_p: Optional[float]
45
+ What probability mass to use.
46
+
47
+ Key init args — client params:
48
+ timeout: Optional[int]
49
+ Timeout for requests.
50
+ api_key: Optional[str]
51
+ Qianfan API KEY. If not passed in will be read from env var QIANFAN_AK.
52
+ secret_key: Optional[str]
53
+ Qianfan SECRET KEY. If not passed in will be read from env var QIANFAN_SK.
54
+
55
+ See full list of supported init args and their descriptions in the params section.
56
+
57
+ Instantiate:
58
+ .. code-block:: python
27
59
28
- To use, you should have the ``qianfan`` python package installed, and
29
- the environment variable ``qianfan_ak`` and ``qianfan_sk`` set with
30
- your API key and Secret Key.
60
+ from langchain_community.llms import QianfanLLMEndpoint
31
61
32
- ak, sk are required parameters which you could get from
33
- https://cloud.baidu.com/product/wenxinworkshop
62
+ llm = QianfanLLMEndpoint(
63
+ model="ERNIE-3.5-8K",
64
+ # api_key="...",
65
+ # secret_key="...",
66
+ # other params...
67
+ )
34
68
35
- Example :
69
+ Invoke :
36
70
.. code-block:: python
37
71
38
- from langchain_community.llms import QianfanLLMEndpoint
39
- qianfan_model = QianfanLLMEndpoint(model="ERNIE-Bot",
40
- endpoint="your_endpoint", qianfan_ak="your_ak", qianfan_sk="your_sk")
41
- """
72
+ messages = [
73
+ ("system", "你是一名专业的翻译家,可以将用户的中文翻译为英文。"),
74
+ ("human", "我喜欢编程。"),
75
+ ]
76
+ llm.invoke(messages)
77
+
78
+ .. code-block:: python
79
+
80
+ 'I like programming.'
81
+
82
+ Stream:
83
+ .. code-block:: python
84
+
85
+ for chunk in llm.stream(messages):
86
+ print(chunk)
87
+
88
+ .. code-block:: python
89
+
90
+ I like
91
+ programming.
92
+
93
+ .. code-block:: python
94
+
95
+ stream = llm.stream(messages)
96
+ full = next(stream)
97
+ for chunk in stream:
98
+ full += chunk
99
+ full
100
+
101
+ .. code-block::
102
+
103
+ 'I like programming.'
104
+
105
+ Async:
106
+ .. code-block:: python
107
+
108
+ await llm.ainvoke(messages)
109
+
110
+ # stream:
111
+ # async for chunk in llm.astream(messages):
112
+ # print(chunk)
113
+
114
+ # batch:
115
+ # await llm.abatch([messages])
116
+
117
+ .. code-block:: python
118
+
119
+ 'I like programming.'
120
+
121
+ """ # noqa: E501
42
122
43
123
init_kwargs : Dict [str , Any ] = Field (default_factory = dict )
44
124
"""init kwargs for qianfan client init, such as `query_per_second` which is
@@ -49,8 +129,8 @@ class QianfanLLMEndpoint(LLM):
49
129
50
130
client : Any
51
131
52
- qianfan_ak : Optional [SecretStr ] = None
53
- qianfan_sk : Optional [SecretStr ] = None
132
+ qianfan_ak : Optional [SecretStr ] = Field ( default = None , alias = "api_key" )
133
+ qianfan_sk : Optional [SecretStr ] = Field ( default = None , alias = "secret_key" )
54
134
55
135
streaming : Optional [bool ] = False
56
136
"""Whether to stream the results or not."""
@@ -68,7 +148,7 @@ class QianfanLLMEndpoint(LLM):
68
148
endpoint : Optional [str ] = None
69
149
"""Endpoint of the Qianfan LLM, required if custom model used."""
70
150
71
- request_timeout : Optional [int ] = 60
151
+ request_timeout : Optional [int ] = Field ( default = 60 , alias = "timeout" )
72
152
"""request timeout for chat http requests"""
73
153
74
154
top_p : Optional [float ] = 0.8
@@ -83,15 +163,15 @@ def validate_environment(cls, values: Dict) -> Dict:
83
163
values ["qianfan_ak" ] = convert_to_secret_str (
84
164
get_from_dict_or_env (
85
165
values ,
86
- "qianfan_ak" ,
166
+ [ "qianfan_ak" , "api_key" ] ,
87
167
"QIANFAN_AK" ,
88
168
default = "" ,
89
169
)
90
170
)
91
171
values ["qianfan_sk" ] = convert_to_secret_str (
92
172
get_from_dict_or_env (
93
173
values ,
94
- "qianfan_sk" ,
174
+ [ "qianfan_sk" , "secret_key" ] ,
95
175
"QIANFAN_SK" ,
96
176
default = "" ,
97
177
)
0 commit comments