110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 | class DashScope(CustomLLM):
"""DashScope LLM.
Examples:
`pip install llama-index-llms-dashscope`
```python
from llama_index.llms.dashscope import DashScope, DashScopeGenerationModels
dashscope_llm = DashScope(model_name=DashScopeGenerationModels.QWEN_MAX)
response = llm.complete("What is the meaning of life?")
print(response.text)
```
"""
""" In Pydantic V2, protected_namespaces is a configuration option used to prevent certain namespace keywords
(such as model_, etc.) from being used as field names. so we need to disable it here.
"""
model_config = ConfigDict(protected_namespaces=())
model_name: str = Field(
default=DashScopeGenerationModels.QWEN_MAX,
description="The DashScope model to use.",
)
max_tokens: Optional[int] = Field(
description="The maximum number of tokens to generate.",
default=DEFAULT_NUM_OUTPUTS,
gt=0,
)
incremental_output: Optional[bool] = Field(
description="Control stream output, If False, the subsequent \
output will include the content that has been \
output previously.",
default=True,
)
enable_search: Optional[bool] = Field(
description="The model has a built-in Internet search service. \
This parameter controls whether the model refers to \
the Internet search results when generating text.",
default=False,
)
stop: Optional[Any] = Field(
description="str, list of str or token_id, list of token id. It will automatically \
stop when the generated content is about to contain the specified string \
or token_ids, and the generated content does not contain \
the specified content.",
default=None,
)
temperature: Optional[float] = Field(
description="The temperature to use during generation.",
default=DEFAULT_TEMPERATURE,
ge=0.0,
le=2.0,
)
top_k: Optional[int] = Field(
description="Sample counter when generate.", default=None
)
top_p: Optional[float] = Field(
description="Sample probability threshold when generate."
)
seed: Optional[int] = Field(
description="Random seed when generate.", default=1234, ge=0
)
repetition_penalty: Optional[float] = Field(
description="Penalty for repeated words in generated text; \
1.0 is no penalty, values greater than 1 discourage \
repetition.",
default=None,
)
api_key: str = Field(
default=None, description="The DashScope API key.", exclude=True
)
context_window: int = Field(
default=DEFAULT_CONTEXT_WINDOW,
description="The maximum number of context tokens for the model.",
gt=0,
)
is_function_calling_model: bool = Field(
default=True,
description="Whether the model is a function calling model.",
)
def __init__(
self,
model_name: Optional[str] = DashScopeGenerationModels.QWEN_MAX,
max_tokens: Optional[int] = DEFAULT_NUM_OUTPUTS,
incremental_output: Optional[int] = True,
enable_search: Optional[bool] = False,
stop: Optional[Any] = None,
temperature: Optional[float] = DEFAULT_TEMPERATURE,
top_k: Optional[int] = None,
top_p: Optional[float] = None,
seed: Optional[int] = 1234,
api_key: Optional[str] = None,
callback_manager: Optional[CallbackManager] = None,
is_function_calling_model: Optional[bool] = True,
context_window: Optional[int] = DEFAULT_CONTEXT_WINDOW,
**kwargs: Any,
):
super().__init__(
model_name=model_name,
max_tokens=max_tokens,
incremental_output=incremental_output,
enable_search=enable_search,
stop=stop,
temperature=temperature,
top_k=top_k,
top_p=top_p,
seed=seed,
api_key=api_key,
callback_manager=callback_manager,
is_function_calling_model=is_function_calling_model,
context_window=context_window,
kwargs=kwargs,
)
@classmethod
def class_name(cls) -> str:
return "DashScope_LLM"
@property
def metadata(self) -> LLMMetadata:
"""LLM metadata."""
return LLMMetadata(
context_window=self.context_window,
num_output=self.max_tokens,
model_name=self.model_name,
is_chat_model=True,
is_function_calling_model=self.is_function_calling_model,
)
def _get_default_parameters(self) -> Dict:
params: Dict[Any, Any] = {}
if self.max_tokens is not None:
params["max_tokens"] = self.max_tokens
params["incremental_output"] = self.incremental_output
params["enable_search"] = self.enable_search
if self.stop is not None:
params["stop"] = self.stop
if self.temperature is not None:
params["temperature"] = self.temperature
if self.top_k is not None:
params["top_k"] = self.top_k
if self.top_p is not None:
params["top_p"] = self.top_p
if self.seed is not None:
params["seed"] = self.seed
return params
def _get_input_parameters(
self, prompt: str, **kwargs: Any
) -> Tuple[ChatMessage, Dict]:
parameters = self._get_default_parameters()
parameters.update(kwargs)
parameters["stream"] = False
# we only use message response
parameters["result_format"] = "message"
message = ChatMessage(
role=MessageRole.USER.value,
content=prompt,
)
return message, parameters
@llm_completion_callback()
def complete(
self, prompt: str, formatted: bool = False, **kwargs: Any
) -> CompletionResponse:
message, parameters = self._get_input_parameters(prompt=prompt, **kwargs)
parameters.pop("incremental_output", None)
parameters.pop("stream", None)
messages = chat_message_to_dashscope_messages([message])
response = call_with_messages(
model=self.model_name,
messages=messages,
api_key=self.api_key,
parameters=parameters,
)
return dashscope_response_to_completion_response(response)
@llm_completion_callback()
async def acomplete(
self, prompt: str, formatted: bool = False, **kwargs: Any
) -> CompletionResponse:
message, parameters = self._get_input_parameters(prompt=prompt, **kwargs)
parameters.pop("incremental_output", None)
parameters.pop("stream", None)
messages = chat_message_to_dashscope_messages([message])
response = await acall_with_messages(
model=self.model_name,
messages=messages,
api_key=self.api_key,
parameters=parameters,
)
return dashscope_response_to_completion_response(response)
@llm_completion_callback()
def stream_complete(
self, prompt: str, formatted: bool = False, **kwargs: Any
) -> CompletionResponseGen:
message, parameters = self._get_input_parameters(prompt=prompt, kwargs=kwargs)
parameters["incremental_output"] = True
parameters["stream"] = True
responses = call_with_messages(
model=self.model_name,
messages=chat_message_to_dashscope_messages([message]),
api_key=self.api_key,
parameters=parameters,
)
def gen() -> CompletionResponseGen:
content = ""
for response in responses:
if response.status_code == HTTPStatus.OK:
top_choice = response.output.choices[0]
incremental_output = top_choice["message"]["content"]
if not incremental_output:
incremental_output = ""
content += incremental_output
yield CompletionResponse(
text=content, delta=incremental_output, raw=response
)
else:
yield CompletionResponse(text="", raw=response)
return
return gen()
@llm_chat_callback()
def chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
parameters = self._get_default_parameters()
parameters.update({**kwargs})
parameters.pop("stream", None)
parameters.pop("incremental_output", None)
parameters["result_format"] = "message" # only use message format.
response = call_with_messages(
model=self.model_name,
messages=chat_message_to_dashscope_messages(messages),
api_key=self.api_key,
parameters=parameters,
)
return dashscope_response_to_chat_response(response)
@llm_chat_callback()
async def achat(
self, messages: Sequence[ChatMessage], **kwargs: Any
) -> ChatResponse:
parameters = self._get_default_parameters()
parameters.update({**kwargs})
parameters.pop("stream", None)
parameters.pop("incremental_output", None)
parameters["result_format"] = "message" # only use message format.
response = await acall_with_messages(
model=self.model_name,
messages=chat_message_to_dashscope_messages(messages),
api_key=self.api_key,
parameters=parameters,
)
return dashscope_response_to_chat_response(response)
@llm_chat_callback()
def stream_chat(
self, messages: Sequence[ChatMessage], **kwargs: Any
) -> ChatResponseGen:
parameters = self._get_default_parameters()
parameters.update({**kwargs})
parameters["stream"] = True
parameters["incremental_output"] = True
parameters["result_format"] = "message" # only use message format.
response = call_with_messages(
model=self.model_name,
messages=chat_message_to_dashscope_messages(messages),
api_key=self.api_key,
parameters=parameters,
)
def gen() -> ChatResponseGen:
content = ""
for r in response:
if r.status_code == HTTPStatus.OK:
top_choice = r.output.choices[0]
incremental_output = top_choice["message"]["content"]
role = top_choice["message"]["role"]
content += incremental_output
yield ChatResponse(
message=ChatMessage(role=role, content=content),
delta=incremental_output,
raw=r,
)
else:
yield ChatResponse(message=ChatMessage(), raw=response)
return
return gen()
|