dashscope 1.24.2__py3-none-any.whl → 1.24.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of dashscope might be problematic. Click here for more details.

dashscope/__init__.py CHANGED
@@ -7,7 +7,7 @@ from dashscope.aigc.code_generation import CodeGeneration
7
7
  from dashscope.aigc.conversation import Conversation, History, HistoryItem
8
8
  from dashscope.aigc.generation import AioGeneration, Generation
9
9
  from dashscope.aigc.image_synthesis import ImageSynthesis
10
- from dashscope.aigc.multimodal_conversation import MultiModalConversation
10
+ from dashscope.aigc.multimodal_conversation import MultiModalConversation, AioMultiModalConversation
11
11
  from dashscope.aigc.video_synthesis import VideoSynthesis
12
12
  from dashscope.app.application import Application
13
13
  from dashscope.assistants import Assistant, AssistantList, Assistants
@@ -60,6 +60,7 @@ __all__ = [
60
60
  MultiModalEmbeddingItemText,
61
61
  SpeechSynthesizer,
62
62
  MultiModalConversation,
63
+ AioMultiModalConversation,
63
64
  BatchTextEmbedding,
64
65
  BatchTextEmbeddingResponse,
65
66
  Understanding,
@@ -3,7 +3,7 @@
3
3
  from .conversation import Conversation, History, HistoryItem
4
4
  from .generation import Generation
5
5
  from .image_synthesis import ImageSynthesis
6
- from .multimodal_conversation import MultiModalConversation
6
+ from .multimodal_conversation import MultiModalConversation, AioMultiModalConversation
7
7
  from .video_synthesis import VideoSynthesis
8
8
 
9
9
  __all__ = [
@@ -13,5 +13,6 @@ __all__ = [
13
13
  History,
14
14
  ImageSynthesis,
15
15
  MultiModalConversation,
16
+ AioMultiModalConversation,
16
17
  VideoSynthesis,
17
18
  ]
@@ -4,7 +4,7 @@ from typing import Any, Dict, List, Union
4
4
 
5
5
  from dashscope.api_entities.dashscope_response import (DashScopeAPIResponse,
6
6
  ImageSynthesisResponse)
7
- from dashscope.client.base_api import BaseAsyncApi
7
+ from dashscope.client.base_api import BaseAsyncApi, BaseApi, BaseAsyncAioApi, BaseAioApi
8
8
  from dashscope.common.constants import IMAGES, NEGATIVE_PROMPT, PROMPT
9
9
  from dashscope.common.error import InputRequired
10
10
  from dashscope.common.utils import _get_task_group_and_task
@@ -90,7 +90,7 @@ class ImageSynthesis(BaseAsyncApi):
90
90
  **kwargs)
91
91
 
92
92
  @classmethod
93
- def async_call(cls,
93
+ def sync_call(cls,
94
94
  model: str,
95
95
  prompt: Any,
96
96
  negative_prompt: Any = None,
@@ -105,83 +105,73 @@ class ImageSynthesis(BaseAsyncApi):
105
105
  mask_image_url: str = None,
106
106
  base_image_url: str = None,
107
107
  **kwargs) -> ImageSynthesisResponse:
108
- """Create a image(s) synthesis task, and return task information.
109
-
110
- Args:
111
- model (str): The model, reference ``Models``.
112
- prompt (Any): The prompt for image(s) synthesis.
113
- negative_prompt (Any): The negative_prompt. Defaults to None.
114
- images (List[str]): The input list of images url.
115
- api_key (str, optional): The api api_key. Defaults to None.
116
- sketch_image_url (str, optional): Only for wanx-sketch-to-image-v1.
117
- Defaults to None.
118
- workspace (str): The dashscope workspace id.
119
- extra_input (Dict): The extra input parameters.
120
- task (str): The task of api, ref doc.
121
- function (str): The specific functions to be achieved. like:
122
- colorization,super_resolution,expand,remove_watermaker,doodle,
123
- description_edit_with_mask,description_edit,stylization_local,stylization_all
124
- base_image_url (str): Enter the URL address of the target edited image.
125
- mask_image_url (str): Provide the URL address of the image of the marked area by the user. It should be consistent with the image resolution of the base_image_url.
126
- **kwargs(wanx-v1):
127
- n(int, `optional`): Number of images to synthesis.
128
- size: The output image(s) size, Default 1024*1024
129
- similarity(float, `optional`): The similarity between the
130
- output image and the input image.
131
- sketch_weight(int, optional): How much the input sketch
132
- affects the output image[0-10], only for wanx-sketch-to-image-v1. # noqa E501
133
- Default 10.
134
- realisticness(int, optional): The realisticness of the output
135
- image[0-10], only for wanx-sketch-to-image-v1. Default 5
136
-
137
- Raises:
138
- InputRequired: The prompt cannot be empty.
139
-
140
- Returns:
141
- DashScopeAPIResponse: The image synthesis
142
- task id in the response.
143
108
  """
109
+ Note: This method currently now only supports wan2.2-t2i-flash and wan2.2-t2i-plus.
110
+ Using other models will result in an error,More raw image models may be added for use later
111
+ """
112
+ task_group, f = _get_task_group_and_task(__name__)
113
+ inputs, kwargs, task = cls._get_input(model, prompt, negative_prompt,
114
+ images, api_key, sketch_image_url,
115
+ ref_img, extra_input, task, function,
116
+ mask_image_url, base_image_url, **kwargs)
117
+ response = BaseApi.call(model, inputs, task_group, task, f, api_key, workspace, **kwargs)
118
+ return ImageSynthesisResponse.from_api_response(response)
119
+
120
+ @classmethod
121
+ def _get_input(cls,
122
+ model: str,
123
+ prompt: Any,
124
+ negative_prompt: Any = None,
125
+ images: List[str] = None,
126
+ api_key: str = None,
127
+ sketch_image_url: str = None,
128
+ ref_img: str = None,
129
+ extra_input: Dict = None,
130
+ task: str = None,
131
+ function: str = None,
132
+ mask_image_url: str = None,
133
+ base_image_url: str = None,
134
+ **kwargs):
144
135
  if prompt is None or not prompt:
145
136
  raise InputRequired('prompt is required!')
146
- task_group, f = _get_task_group_and_task(__name__)
147
- input = {PROMPT: prompt}
137
+ inputs = {PROMPT: prompt}
148
138
  has_upload = False
149
139
  if negative_prompt is not None:
150
- input[NEGATIVE_PROMPT] = negative_prompt
140
+ inputs[NEGATIVE_PROMPT] = negative_prompt
151
141
  if images is not None:
152
- input[IMAGES] = images
142
+ inputs[IMAGES] = images
153
143
  if sketch_image_url is not None and sketch_image_url:
154
144
  is_upload, sketch_image_url = check_and_upload_local(
155
145
  model, sketch_image_url, api_key)
156
146
  if is_upload:
157
147
  has_upload = True
158
- input['sketch_image_url'] = sketch_image_url
148
+ inputs['sketch_image_url'] = sketch_image_url
159
149
  if ref_img is not None and ref_img:
160
150
  is_upload, ref_img = check_and_upload_local(
161
151
  model, ref_img, api_key)
162
152
  if is_upload:
163
153
  has_upload = True
164
- input['ref_img'] = ref_img
154
+ inputs['ref_img'] = ref_img
165
155
 
166
156
  if function is not None and function:
167
- input['function'] = function
157
+ inputs['function'] = function
168
158
 
169
159
  if mask_image_url is not None and mask_image_url:
170
160
  is_upload, res_mask_image_url = check_and_upload_local(
171
161
  model, mask_image_url, api_key)
172
162
  if is_upload:
173
163
  has_upload = True
174
- input['mask_image_url'] = res_mask_image_url
164
+ inputs['mask_image_url'] = res_mask_image_url
175
165
 
176
166
  if base_image_url is not None and base_image_url:
177
167
  is_upload, res_base_image_url = check_and_upload_local(
178
168
  model, base_image_url, api_key)
179
169
  if is_upload:
180
170
  has_upload = True
181
- input['base_image_url'] = res_base_image_url
171
+ inputs['base_image_url'] = res_base_image_url
182
172
 
183
173
  if extra_input is not None and extra_input:
184
- input = {**input, **extra_input}
174
+ inputs = {**inputs, **extra_input}
185
175
 
186
176
  if has_upload:
187
177
  headers = kwargs.pop('headers', {})
@@ -193,13 +183,72 @@ class ImageSynthesis(BaseAsyncApi):
193
183
  if model is not None and model and 'imageedit' in model:
194
184
  task = 'image2image'
195
185
 
186
+ return inputs, kwargs, task
187
+
188
+ @classmethod
189
+ def async_call(cls,
190
+ model: str,
191
+ prompt: Any,
192
+ negative_prompt: Any = None,
193
+ images: List[str] = None,
194
+ api_key: str = None,
195
+ sketch_image_url: str = None,
196
+ ref_img: str = None,
197
+ workspace: str = None,
198
+ extra_input: Dict = None,
199
+ task: str = None,
200
+ function: str = None,
201
+ mask_image_url: str = None,
202
+ base_image_url: str = None,
203
+ **kwargs) -> ImageSynthesisResponse:
204
+ """Create a image(s) synthesis task, and return task information.
205
+
206
+ Args:
207
+ model (str): The model, reference ``Models``.
208
+ prompt (Any): The prompt for image(s) synthesis.
209
+ negative_prompt (Any): The negative_prompt. Defaults to None.
210
+ images (List[str]): The input list of images url.
211
+ api_key (str, optional): The api api_key. Defaults to None.
212
+ sketch_image_url (str, optional): Only for wanx-sketch-to-image-v1.
213
+ Defaults to None.
214
+ workspace (str): The dashscope workspace id.
215
+ extra_input (Dict): The extra input parameters.
216
+ task (str): The task of api, ref doc.
217
+ function (str): The specific functions to be achieved. like:
218
+ colorization,super_resolution,expand,remove_watermaker,doodle,
219
+ description_edit_with_mask,description_edit,stylization_local,stylization_all
220
+ base_image_url (str): Enter the URL address of the target edited image.
221
+ mask_image_url (str): Provide the URL address of the image of the marked area by the user. It should be consistent with the image resolution of the base_image_url.
222
+ **kwargs(wanx-v1):
223
+ n(int, `optional`): Number of images to synthesis.
224
+ size: The output image(s) size, Default 1024*1024
225
+ similarity(float, `optional`): The similarity between the
226
+ output image and the input image.
227
+ sketch_weight(int, optional): How much the input sketch
228
+ affects the output image[0-10], only for wanx-sketch-to-image-v1. # noqa E501
229
+ Default 10.
230
+ realisticness(int, optional): The realisticness of the output
231
+ image[0-10], only for wanx-sketch-to-image-v1. Default 5
232
+
233
+ Raises:
234
+ InputRequired: The prompt cannot be empty.
235
+
236
+ Returns:
237
+ DashScopeAPIResponse: The image synthesis
238
+ task id in the response.
239
+ """
240
+ task_group, f = _get_task_group_and_task(__name__)
241
+ inputs, kwargs, task = cls._get_input(model, prompt, negative_prompt,
242
+ images, api_key, sketch_image_url,
243
+ ref_img, extra_input, task, function,
244
+ mask_image_url, base_image_url, **kwargs)
196
245
  response = super().async_call(
197
246
  model=model,
198
247
  task_group=task_group,
199
248
  task=task,
200
249
  function=f,
201
250
  api_key=api_key,
202
- input=input,
251
+ input=inputs,
203
252
  workspace=workspace,
204
253
  **kwargs)
205
254
  return ImageSynthesisResponse.from_api_response(response)
@@ -306,3 +355,259 @@ class ImageSynthesis(BaseAsyncApi):
306
355
  api_key=api_key,
307
356
  workspace=workspace,
308
357
  **kwargs)
358
+
359
+ class AioImageSynthesis(BaseAsyncAioApi):
360
+ @classmethod
361
+ async def call(cls,
362
+ model: str,
363
+ prompt: Any,
364
+ negative_prompt: Any = None,
365
+ images: List[str] = None,
366
+ api_key: str = None,
367
+ sketch_image_url: str = None,
368
+ ref_img: str = None,
369
+ workspace: str = None,
370
+ extra_input: Dict = None,
371
+ task: str = None,
372
+ function: str = None,
373
+ mask_image_url: str = None,
374
+ base_image_url: str = None,
375
+ **kwargs) -> ImageSynthesisResponse:
376
+ """Call image(s) synthesis service and get result.
377
+
378
+ Args:
379
+ model (str): The model, reference ``Models``.
380
+ prompt (Any): The prompt for image(s) synthesis.
381
+ negative_prompt (Any): The negative_prompt. Defaults to None.
382
+ images (List[str]): The input list of images url,
383
+ currently not supported.
384
+ api_key (str, optional): The api api_key. Defaults to None.
385
+ sketch_image_url (str, optional): Only for wanx-sketch-to-image-v1,
386
+ can be local file.
387
+ Defaults to None.
388
+ workspace (str): The dashscope workspace id.
389
+ extra_input (Dict): The extra input parameters.
390
+ task (str): The task of api, ref doc.
391
+ function (str): The specific functions to be achieved. like:
392
+ colorization,super_resolution,expand,remove_watermaker,doodle,
393
+ description_edit_with_mask,description_edit,stylization_local,stylization_all
394
+ base_image_url (str): Enter the URL address of the target edited image.
395
+ mask_image_url (str): Provide the URL address of the image of the marked area by the user. It should be consistent with the image resolution of the base_image_url.
396
+ **kwargs:
397
+ n(int, `optional`): Number of images to synthesis.
398
+ size(str, `optional`): The output image(s) size(width*height).
399
+ similarity(float, `optional`): The similarity between the
400
+ output image and the input image
401
+ sketch_weight(int, optional): How much the input sketch
402
+ affects the output image[0-10], only for wanx-sketch-to-image-v1. # noqa E501
403
+ Default 10.
404
+ realisticness(int, optional): The realisticness of the output
405
+ image[0-10], only for wanx-sketch-to-image-v1. Default 5
406
+
407
+ Raises:
408
+ InputRequired: The prompt cannot be empty.
409
+
410
+ Returns:
411
+ ImageSynthesisResponse: The image(s) synthesis result.
412
+ """
413
+ task_group, f = _get_task_group_and_task(__name__)
414
+ inputs, kwargs, task = ImageSynthesis._get_input(model, prompt, negative_prompt,
415
+ images, api_key, sketch_image_url,
416
+ ref_img, extra_input, task, function,
417
+ mask_image_url, base_image_url, **kwargs)
418
+ response = await super().call(model, inputs, task_group, task, f, api_key, workspace, **kwargs)
419
+ return ImageSynthesisResponse.from_api_response(response)
420
+
421
+ @classmethod
422
+ async def sync_call(cls,
423
+ model: str,
424
+ prompt: Any,
425
+ negative_prompt: Any = None,
426
+ images: List[str] = None,
427
+ api_key: str = None,
428
+ sketch_image_url: str = None,
429
+ ref_img: str = None,
430
+ workspace: str = None,
431
+ extra_input: Dict = None,
432
+ task: str = None,
433
+ function: str = None,
434
+ mask_image_url: str = None,
435
+ base_image_url: str = None,
436
+ **kwargs) -> ImageSynthesisResponse:
437
+ """
438
+ Note: This method currently now only supports wan2.2-t2i-flash and wan2.2-t2i-plus.
439
+ Using other models will result in an error,More raw image models may be added for use later
440
+ """
441
+ task_group, f = _get_task_group_and_task(__name__)
442
+ inputs, kwargs, task = ImageSynthesis._get_input(model, prompt, negative_prompt,
443
+ images, api_key, sketch_image_url,
444
+ ref_img, extra_input, task, function,
445
+ mask_image_url, base_image_url, **kwargs)
446
+ response = await BaseAioApi.call(model, inputs, task_group, task, f, api_key, workspace, **kwargs)
447
+ return ImageSynthesisResponse.from_api_response(response)
448
+
449
+ @classmethod
450
+ async def async_call(cls,
451
+ model: str,
452
+ prompt: Any,
453
+ negative_prompt: Any = None,
454
+ images: List[str] = None,
455
+ api_key: str = None,
456
+ sketch_image_url: str = None,
457
+ ref_img: str = None,
458
+ workspace: str = None,
459
+ extra_input: Dict = None,
460
+ task: str = None,
461
+ function: str = None,
462
+ mask_image_url: str = None,
463
+ base_image_url: str = None,
464
+ **kwargs) -> ImageSynthesisResponse:
465
+ """Create a image(s) synthesis task, and return task information.
466
+
467
+ Args:
468
+ model (str): The model, reference ``Models``.
469
+ prompt (Any): The prompt for image(s) synthesis.
470
+ negative_prompt (Any): The negative_prompt. Defaults to None.
471
+ images (List[str]): The input list of images url.
472
+ api_key (str, optional): The api api_key. Defaults to None.
473
+ sketch_image_url (str, optional): Only for wanx-sketch-to-image-v1.
474
+ Defaults to None.
475
+ workspace (str): The dashscope workspace id.
476
+ extra_input (Dict): The extra input parameters.
477
+ task (str): The task of api, ref doc.
478
+ function (str): The specific functions to be achieved. like:
479
+ colorization,super_resolution,expand,remove_watermaker,doodle,
480
+ description_edit_with_mask,description_edit,stylization_local,stylization_all
481
+ base_image_url (str): Enter the URL address of the target edited image.
482
+ mask_image_url (str): Provide the URL address of the image of the marked area by the user. It should be consistent with the image resolution of the base_image_url.
483
+ **kwargs(wanx-v1):
484
+ n(int, `optional`): Number of images to synthesis.
485
+ size: The output image(s) size, Default 1024*1024
486
+ similarity(float, `optional`): The similarity between the
487
+ output image and the input image.
488
+ sketch_weight(int, optional): How much the input sketch
489
+ affects the output image[0-10], only for wanx-sketch-to-image-v1. # noqa E501
490
+ Default 10.
491
+ realisticness(int, optional): The realisticness of the output
492
+ image[0-10], only for wanx-sketch-to-image-v1. Default 5
493
+
494
+ Raises:
495
+ InputRequired: The prompt cannot be empty.
496
+
497
+ Returns:
498
+ DashScopeAPIResponse: The image synthesis
499
+ task id in the response.
500
+ """
501
+ task_group, f = _get_task_group_and_task(__name__)
502
+ inputs, kwargs, task = ImageSynthesis._get_input(model, prompt, negative_prompt,
503
+ images, api_key, sketch_image_url,
504
+ ref_img, extra_input, task, function,
505
+ mask_image_url, base_image_url, **kwargs)
506
+ response = await super().async_call(model, inputs, task_group, task, f, api_key, workspace, **kwargs)
507
+ return ImageSynthesisResponse.from_api_response(response)
508
+
509
+ @classmethod
510
+ async def fetch(cls,
511
+ task: Union[str, ImageSynthesisResponse],
512
+ api_key: str = None,
513
+ workspace: str = None,
514
+ **kwargs,) -> ImageSynthesisResponse:
515
+ """Fetch image(s) synthesis task status or result.
516
+
517
+ Args:
518
+ task (Union[str, ImageSynthesisResponse]): The task_id or
519
+ ImageSynthesisResponse return by async_call().
520
+ api_key (str, optional): The api api_key. Defaults to None.
521
+ workspace (str): The dashscope workspace id.
522
+
523
+ Returns:
524
+ ImageSynthesisResponse: The task status or result.
525
+ """
526
+ response = await super().fetch(task, api_key=api_key, workspace=workspace)
527
+ return ImageSynthesisResponse.from_api_response(response)
528
+
529
+ @classmethod
530
+ async def wait(cls,
531
+ task: Union[str, ImageSynthesisResponse],
532
+ api_key: str = None,
533
+ workspace: str = None,
534
+ **kwargs) -> ImageSynthesisResponse:
535
+ """Wait for image(s) synthesis task to complete, and return the result.
536
+
537
+ Args:
538
+ task (Union[str, ImageSynthesisResponse]): The task_id or
539
+ ImageSynthesisResponse return by async_call().
540
+ api_key (str, optional): The api api_key. Defaults to None.
541
+ workspace (str): The dashscope workspace id.
542
+
543
+ Returns:
544
+ ImageSynthesisResponse: The task result.
545
+ """
546
+ response = await super().wait(task, api_key, workspace=workspace)
547
+ return ImageSynthesisResponse.from_api_response(response)
548
+
549
+ @classmethod
550
+ async def cancel(cls,
551
+ task: Union[str, ImageSynthesisResponse],
552
+ api_key: str = None,
553
+ workspace: str = None,
554
+ **kwargs,) -> DashScopeAPIResponse:
555
+ """Cancel image synthesis task.
556
+ Only tasks whose status is PENDING can be canceled.
557
+
558
+ Args:
559
+ task (Union[str, ImageSynthesisResponse]): The task_id or
560
+ ImageSynthesisResponse return by async_call().
561
+ api_key (str, optional): The api api_key. Defaults to None.
562
+ workspace (str): The dashscope workspace id.
563
+
564
+ Returns:
565
+ DashScopeAPIResponse: The response data.
566
+ """
567
+ return await super().cancel(task, api_key, workspace=workspace)
568
+
569
+ @classmethod
570
+ async def list(cls,
571
+ start_time: str = None,
572
+ end_time: str = None,
573
+ model_name: str = None,
574
+ api_key_id: str = None,
575
+ region: str = None,
576
+ status: str = None,
577
+ page_no: int = 1,
578
+ page_size: int = 10,
579
+ api_key: str = None,
580
+ workspace: str = None,
581
+ **kwargs) -> DashScopeAPIResponse:
582
+ """List async tasks.
583
+
584
+ Args:
585
+ start_time (str, optional): The tasks start time,
586
+ for example: 20230420000000. Defaults to None.
587
+ end_time (str, optional): The tasks end time,
588
+ for example: 20230420000000. Defaults to None.
589
+ model_name (str, optional): The tasks model name. Defaults to None.
590
+ api_key_id (str, optional): The tasks api-key-id. Defaults to None.
591
+ region (str, optional): The service region,
592
+ for example: cn-beijing. Defaults to None.
593
+ status (str, optional): The status of tasks[PENDING,
594
+ RUNNING, SUCCEEDED, FAILED, CANCELED]. Defaults to None.
595
+ page_no (int, optional): The page number. Defaults to 1.
596
+ page_size (int, optional): The page size. Defaults to 10.
597
+ api_key (str, optional): The user api-key. Defaults to None.
598
+ workspace (str): The dashscope workspace id.
599
+
600
+ Returns:
601
+ DashScopeAPIResponse: The response data.
602
+ """
603
+ return await super().list(start_time=start_time,
604
+ end_time=end_time,
605
+ model_name=model_name,
606
+ api_key_id=api_key_id,
607
+ region=region,
608
+ status=status,
609
+ page_no=page_no,
610
+ page_size=page_size,
611
+ api_key=api_key,
612
+ workspace=workspace,
613
+ **kwargs)
@@ -5,7 +5,7 @@ from typing import Generator, List, Union
5
5
 
6
6
  from dashscope.api_entities.dashscope_response import \
7
7
  MultiModalConversationResponse
8
- from dashscope.client.base_api import BaseApi
8
+ from dashscope.client.base_api import BaseAioApi, BaseApi
9
9
  from dashscope.common.error import InputRequired, ModelRequired
10
10
  from dashscope.common.utils import _get_task_group_and_task
11
11
  from dashscope.utils.oss_utils import preprocess_message_element
@@ -130,3 +130,124 @@ class MultiModalConversation(BaseApi):
130
130
  if is_upload and not has_upload:
131
131
  has_upload = True
132
132
  return has_upload
133
+
134
+
135
+ class AioMultiModalConversation(BaseAioApi):
136
+ """Async MultiModal conversational robot interface.
137
+ """
138
+ task = 'multimodal-generation'
139
+ function = 'generation'
140
+
141
+ class Models:
142
+ qwen_vl_chat_v1 = 'qwen-vl-chat-v1'
143
+
144
+ @classmethod
145
+ async def call(
146
+ cls,
147
+ model: str,
148
+ messages: List,
149
+ api_key: str = None,
150
+ workspace: str = None,
151
+ **kwargs
152
+ ) -> Union[MultiModalConversationResponse, Generator[
153
+ MultiModalConversationResponse, None, None]]:
154
+ """Call the conversation model service asynchronously.
155
+
156
+ Args:
157
+ model (str): The requested model, such as 'qwen-multimodal-v1'
158
+ messages (list): The generation messages.
159
+ examples:
160
+ [
161
+ {
162
+ "role": "system",
163
+ "content": [
164
+ {"text": "你是达摩院的生活助手机器人。"}
165
+ ]
166
+ },
167
+ {
168
+ "role": "user",
169
+ "content": [
170
+ {"image": "http://XXXX"},
171
+ {"text": "这个图片是哪里?"},
172
+ ]
173
+ }
174
+ ]
175
+ api_key (str, optional): The api api_key, can be None,
176
+ if None, will retrieve by rule [1].
177
+ [1]: https://help.aliyun.com/zh/dashscope/developer-reference/api-key-settings. # noqa E501
178
+ workspace (str): The dashscope workspace id.
179
+ **kwargs:
180
+ stream(bool, `optional`): Enable server-sent events
181
+ (ref: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) # noqa E501
182
+ the result will back partially[qwen-turbo,bailian-v1].
183
+ max_length(int, `optional`): The maximum length of tokens to
184
+ generate. The token count of your prompt plus max_length
185
+ cannot exceed the model's context length. Most models
186
+ have a context length of 2000 tokens[qwen-turbo,bailian-v1]. # noqa E501
187
+ top_p(float, `optional`): A sampling strategy, called nucleus
188
+ sampling, where the model considers the results of the
189
+ tokens with top_p probability mass. So 0.1 means only
190
+ the tokens comprising the top 10% probability mass are
191
+ considered[qwen-turbo,bailian-v1].
192
+ top_k(float, `optional`):
193
+
194
+ Raises:
195
+ InvalidInput: The history and auto_history are mutually exclusive.
196
+
197
+ Returns:
198
+ Union[MultiModalConversationResponse,
199
+ Generator[MultiModalConversationResponse, None, None]]: If
200
+ stream is True, return Generator, otherwise MultiModalConversationResponse.
201
+ """
202
+ if (messages is None or not messages):
203
+ raise InputRequired('prompt or messages is required!')
204
+ if model is None or not model:
205
+ raise ModelRequired('Model is required!')
206
+ task_group, _ = _get_task_group_and_task(__name__)
207
+ msg_copy = copy.deepcopy(messages)
208
+ has_upload = cls._preprocess_messages(model, msg_copy, api_key)
209
+ if has_upload:
210
+ headers = kwargs.pop('headers', {})
211
+ headers['X-DashScope-OssResourceResolve'] = 'enable'
212
+ kwargs['headers'] = headers
213
+ input = {'messages': msg_copy}
214
+ response = await super().call(model=model,
215
+ task_group=task_group,
216
+ task=AioMultiModalConversation.task,
217
+ function=AioMultiModalConversation.function,
218
+ api_key=api_key,
219
+ input=input,
220
+ workspace=workspace,
221
+ **kwargs)
222
+ is_stream = kwargs.get('stream', False)
223
+ if is_stream:
224
+ return (MultiModalConversationResponse.from_api_response(rsp)
225
+ async for rsp in response)
226
+ else:
227
+ return MultiModalConversationResponse.from_api_response(response)
228
+
229
+ @classmethod
230
+ def _preprocess_messages(cls, model: str, messages: List[dict],
231
+ api_key: str):
232
+ """
233
+ messages = [
234
+ {
235
+ "role": "user",
236
+ "content": [
237
+ {"image": ""},
238
+ {"text": ""},
239
+ ]
240
+ }
241
+ ]
242
+ """
243
+ has_upload = False
244
+ for message in messages:
245
+ content = message['content']
246
+ for elem in content:
247
+ if not isinstance(elem,
248
+ (int, float, bool, str, bytes, bytearray)):
249
+ is_upload = preprocess_message_element(
250
+ model, elem, api_key)
251
+ if is_upload and not has_upload:
252
+ has_upload = True
253
+ return has_upload