@@ -158,33 +158,102 @@ async def agenerate_with_last_element_mark(
158
158
159
159
160
160
class Tongyi (BaseLLM ):
161
- """Tongyi Qwen large language models .
161
+ """Tongyi completion model integration .
162
162
163
- To use, you should have the ``dashscope`` python package installed, and the
164
- environment variable ``DASHSCOPE_API_KEY`` set with your API key, or pass
165
- it as a named parameter to the constructor.
163
+ Setup:
164
+ Install ``dashscope`` and set environment variables ``DASHSCOPE_API_KEY``.
166
165
167
- Example:
166
+ .. code-block:: bash
167
+
168
+ pip install dashscope
169
+ export DASHSCOPE_API_KEY="your-api-key"
170
+
171
+ Key init args — completion params:
172
+ model: str
173
+ Name of Tongyi model to use.
174
+ top_p: float
175
+ Total probability mass of tokens to consider at each step.
176
+ streaming: bool
177
+ Whether to stream the results or not.
178
+
179
+ Key init args — client params:
180
+ api_key: Optional[str]
181
+ Dashscope API KEY. If not passed in will be read from env var DASHSCOPE_API_KEY.
182
+ max_retries: int
183
+ Maximum number of retries to make when generating.
184
+
185
+ See full list of supported init args and their descriptions in the params section.
186
+
187
+ Instantiate:
168
188
.. code-block:: python
169
189
170
190
from langchain_community.llms import Tongyi
171
- tongyi = tongyi()
172
- """
191
+
192
+ llm = Tongyi(
193
+ model="qwen-max",
194
+ # top_p="...",
195
+ # api_key="...",
196
+ # other params...
197
+ )
198
+
199
+ Invoke:
200
+ .. code-block:: python
201
+
202
+ messages = [
203
+ ("system", "你是一名专业的翻译家,可以将用户的中文翻译为英文。"),
204
+ ("human", "我喜欢编程。"),
205
+ ]
206
+ llm.invoke(messages)
207
+
208
+ .. code-block:: python
209
+
210
+ 'I enjoy programming.'
211
+
212
+ Stream:
213
+ .. code-block:: python
214
+
215
+ for chunk in llm.stream(messages):
216
+ print(chunk)
217
+
218
+ .. code-block:: python
219
+
220
+ I
221
+ enjoy
222
+ programming
223
+ .
224
+
225
+ Async:
226
+ .. code-block:: python
227
+
228
+ await llm.ainvoke(messages)
229
+
230
+ # stream:
231
+ # async for chunk in llm.astream(messages):
232
+ # print(chunk)
233
+
234
+ # batch:
235
+ # await llm.abatch([messages])
236
+
237
+ .. code-block:: python
238
+
239
+ 'I enjoy programming.'
240
+
241
+ """ # noqa: E501
173
242
174
243
@property
175
244
def lc_secrets (self ) -> Dict [str , str ]:
176
245
return {"dashscope_api_key" : "DASHSCOPE_API_KEY" }
177
246
178
247
client : Any #: :meta private:
179
- model_name : str = "qwen-plus"
248
+ model_name : str = Field ( default = "qwen-plus" , alias = "model" )
180
249
181
250
"""Model name to use."""
182
251
model_kwargs : Dict [str , Any ] = Field (default_factory = dict )
183
252
184
253
top_p : float = 0.8
185
254
"""Total probability mass of tokens to consider at each step."""
186
255
187
- dashscope_api_key : Optional [str ] = None
256
+ dashscope_api_key : Optional [str ] = Field ( default = None , alias = "api_key" )
188
257
"""Dashscope api key provide by Alibaba Cloud."""
189
258
190
259
streaming : bool = False
@@ -202,7 +271,7 @@ def _llm_type(self) -> str:
202
271
def validate_environment (cls , values : Dict ) -> Dict :
203
272
"""Validate that api key and python package exists in environment."""
204
273
values ["dashscope_api_key" ] = get_from_dict_or_env (
205
- values , "dashscope_api_key" , "DASHSCOPE_API_KEY"
274
+ values , [ "dashscope_api_key" , "api_key" ] , "DASHSCOPE_API_KEY"
206
275
)
207
276
try :
208
277
import dashscope
0 commit comments