dashscope 1.22.1__py3-none-any.whl → 1.22.2__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/finetune.py ADDED
@@ -0,0 +1,175 @@
1
+ from typing import Union
2
+
3
+ from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
4
+ from dashscope.client.base_api import (CancelMixin, CreateMixin, DeleteMixin,
5
+ GetStatusMixin, ListMixin, LogMixin,
6
+ StreamEventMixin)
7
+
8
+
9
+ class FineTune(CreateMixin, CancelMixin, DeleteMixin, ListMixin,
10
+ GetStatusMixin, StreamEventMixin, LogMixin):
11
+ SUB_PATH = 'fine-tunes'
12
+
13
+ @classmethod
14
+ def call(cls,
15
+ model: str,
16
+ training_file_ids: Union[list, str],
17
+ validation_file_ids: Union[list, str] = None,
18
+ mode: str = None,
19
+ hyper_parameters: dict = {},
20
+ api_key: str = None,
21
+ workspace: str = None,
22
+ **kwargs) -> DashScopeAPIResponse:
23
+ """Create fine-tune job
24
+
25
+ Args:
26
+ model (str): The model to be fine-tuned
27
+ training_file_ids (list, str): Ids of the fine-tune training data,
28
+ which can be pre-uploaded using the File API.
29
+ validation_file_ids ([list,str], optional): Ids of the fine-tune
30
+ validating data, which can be pre-uploaded using the File API.
31
+ mode (str): The fine-tune mode, sft or efficient_sft.
32
+ hyper_parameters (dict, optional): The fine-tune hyper parameters.
33
+ Defaults to empty.
34
+ api_key (str, optional): The api key. Defaults to None.
35
+ workspace (str): The dashscope workspace id.
36
+
37
+ Returns:
38
+ DashScopeAPIResponse: The request result.
39
+ """
40
+ request = {
41
+ 'model': model,
42
+ 'training_file_ids': training_file_ids,
43
+ 'validation_file_ids': validation_file_ids,
44
+ 'hyper_parameters': hyper_parameters if hyper_parameters else {},
45
+ }
46
+ if mode is not None:
47
+ request['training_type'] = mode
48
+ if 'finetuned_output' in kwargs:
49
+ request['finetuned_output'] = kwargs['finetuned_output']
50
+ return super().call(request,
51
+ api_key=api_key,
52
+ workspace=workspace,
53
+ **kwargs)
54
+
55
+ @classmethod
56
+ def cancel(cls,
57
+ job_id: str,
58
+ api_key: str = None,
59
+ workspace: str = None,
60
+ **kwargs) -> DashScopeAPIResponse:
61
+ """Cancel a running fine-tune job.
62
+
63
+ Args:
64
+ job_id (str): The fine-tune job id.
65
+ api_key (str, optional): The api api_key, can be None,
66
+ if None, will get by default rule(TODO: api key doc).
67
+ workspace (str): The dashscope workspace id.
68
+
69
+ Returns:
70
+ DashScopeAPIResponse: The request result.
71
+ """
72
+ return super().cancel(job_id, api_key, workspace=workspace, **kwargs)
73
+
74
+ @classmethod
75
+ def list(cls,
76
+ page=1,
77
+ page_size=10,
78
+ api_key: str = None,
79
+ workspace: str = None,
80
+ **kwargs) -> DashScopeAPIResponse:
81
+ """List fine-tune job.
82
+
83
+ Args:
84
+ api_key (str, optional): The api key
85
+ page (int, optional): Page number. Defaults to 1.
86
+ page_size (int, optional): Items per page. Defaults to 10.
87
+ workspace (str): The dashscope workspace id.
88
+
89
+ Returns:
90
+ DashScopeAPIResponse: The fine-tune jobs in the result.
91
+ """
92
+ return super().list(page,
93
+ page_size,
94
+ api_key,
95
+ workspace=workspace,
96
+ **kwargs)
97
+
98
+ @classmethod
99
+ def get(cls,
100
+ job_id: str,
101
+ api_key: str = None,
102
+ workspace: str = None,
103
+ **kwargs) -> DashScopeAPIResponse:
104
+ """Get fine-tune job information.
105
+
106
+ Args:
107
+ job_id (str): The fine-tune job id
108
+ api_key (str, optional): The api key. Defaults to None.
109
+ workspace (str): The dashscope workspace id.
110
+
111
+ Returns:
112
+ DashScopeAPIResponse: The job info
113
+ """
114
+ return super().get(job_id, api_key, workspace=workspace, **kwargs)
115
+
116
+ @classmethod
117
+ def delete(cls,
118
+ job_id: str,
119
+ api_key: str = None,
120
+ workspace: str = None,
121
+ **kwargs) -> DashScopeAPIResponse:
122
+ """Delete a fine-tune job.
123
+
124
+ Args:
125
+ job_id (str): The fine-tune job id.
126
+ api_key (str, optional): The api key. Defaults to None.
127
+ workspace (str): The dashscope workspace id.
128
+
129
+ Returns:
130
+ DashScopeAPIResponse: The delete result.
131
+ """
132
+ return super().delete(job_id, api_key, workspace=workspace, **kwargs)
133
+
134
+ @classmethod
135
+ def stream_events(cls,
136
+ job_id: str,
137
+ api_key: str = None,
138
+ workspace: str = None,
139
+ **kwargs) -> DashScopeAPIResponse:
140
+ """Get fine-tune job events.
141
+
142
+ Args:
143
+ job_id (str): The fine-tune job id
144
+ api_key (str, optional): the api key. Defaults to None.
145
+ workspace (str): The dashscope workspace id.
146
+
147
+ Returns:
148
+ DashScopeAPIResponse: The job log events.
149
+ """
150
+ return super().stream_events(job_id,
151
+ api_key,
152
+ workspace=workspace,
153
+ **kwargs)
154
+
155
+ @classmethod
156
+ def logs(cls,
157
+ job_id: str,
158
+ offset=1,
159
+ line=1000,
160
+ api_key: str = None,
161
+ workspace: str = None,
162
+ **kwargs) -> DashScopeAPIResponse:
163
+ """Get log of the job.
164
+
165
+ Args:
166
+ job_id (str): The job id(used for fine-tune)
167
+ offset (int, optional): start log line. Defaults to 1.
168
+ line (int, optional): total line return. Defaults to 1000.
169
+ api_key (str, optional): The api key. Defaults to None.
170
+ workspace (str): The dashscope workspace id.
171
+
172
+ Returns:
173
+ DashScopeAPIResponse: The response
174
+ """
175
+ return super().logs(job_id, offset, line, workspace=workspace)
dashscope/version.py CHANGED
@@ -1 +1 @@
1
- __version__ = '1.22.1'
1
+ __version__ = '1.22.2'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dashscope
3
- Version: 1.22.1
3
+ Version: 1.22.2
4
4
  Summary: dashscope client sdk library
5
5
  Home-page: https://dashscope.aliyun.com/
6
6
  Author: Alibaba Cloud
@@ -18,7 +18,6 @@ Classifier: Programming Language :: Python :: 3.10
18
18
  Classifier: Programming Language :: Python :: 3.11
19
19
  Requires-Python: >=3.8.0
20
20
  Description-Content-Type: text/markdown
21
- License-File: LICENSE
22
21
  Requires-Dist: aiohttp
23
22
  Requires-Dist: requests
24
23
  Requires-Dist: websocket-client
@@ -77,7 +76,7 @@ else:
77
76
 
78
77
  ## API Key Authentication
79
78
 
80
- The SDK uses API key for authentication. Please refer to [official documentation](https://dashscope.aliyun.com) regarding how to obtain your api-key.
79
+ The SDK uses API key for authentication. Please refer to [official documentation for alibabacloud china](https://www.alibabacloud.com/help/en/model-studio/) and [official documentation for alibabacloud international](https://www.alibabacloud.com/help/en/model-studio/) regarding how to obtain your api-key.
81
80
 
82
81
  ### Using the API Key
83
82
 
@@ -222,3 +221,5 @@ Coming soon.
222
221
 
223
222
  ## License
224
223
  This project is licensed under the Apache License (Version 2.0).
224
+
225
+
@@ -1,26 +1,27 @@
1
1
  dashscope/__init__.py,sha256=_9PaKXKpYc6PXO35BjcH3R0As9sTsXGOW2iQ6R5HAVc,3011
2
2
  dashscope/cli.py,sha256=amegoTkGOs6TlHMdoo4JVOqBePo3lGs745rc7leEyrE,24020
3
+ dashscope/deployment.py,sha256=ljmVi-ny6SjEs8v4oIGNWIw8UQTorE7dl5QJv7dEPIQ,5728
4
+ dashscope/file.py,sha256=Dv2Fz3DLbcye2uuQxyQwRM7ky27OthouLXIpSQagQy4,3324
3
5
  dashscope/files.py,sha256=QgJjwhtn9F548nCA8jD8OvE6aQEj-20hZqJgYXsUdQU,3930
6
+ dashscope/finetune.py,sha256=_tflDUvu0KagSoCzLaf0hofpG_P8NU6PylL8CPjVhrA,6243
4
7
  dashscope/model.py,sha256=UPOn1qMYFhX-ovXi3BMxZEBk8qOK7WLJOYHMbPZwYBo,1440
5
8
  dashscope/models.py,sha256=1-bc-Ue68zurgu_y6RhfFr9uzeQMF5AZq-C32lJGMGU,1224
6
- dashscope/version.py,sha256=sUm-r83NIqFP93EJcBrI6UFP5e6fmMbnDa6TglGqS1o,23
9
+ dashscope/version.py,sha256=VzBJug2jhAHnqtcPEyOBjewkNpFsBuxDvTGCG_kKqzk,23
7
10
  dashscope/aigc/__init__.py,sha256=xmdalVw7wS0cLIuU8Q0qk0q8XGw-iGk8NnQwAQZ3jAc,391
8
- dashscope/aigc/chat_completion.py,sha256=28puJrtHkqTMZlyKLXb3UjSuICy2OPvysj9a6oNU7Vs,14585
9
11
  dashscope/aigc/code_generation.py,sha256=KAJVrGp6tiNFBBg64Ovs9RfcP5SrIhrbW3wdA89NKso,10885
10
12
  dashscope/aigc/conversation.py,sha256=xRoJlCR-IXHjSdkDrK74a9ut1FJg0FZhTNXZAJC18MA,14231
11
13
  dashscope/aigc/generation.py,sha256=53oMCmN5ZbqeqAsKxmdunXlRh-XP8ZtnA7hB2id4Koo,17897
12
14
  dashscope/aigc/image_synthesis.py,sha256=UWHW-nvf7_aDZKr4uZDusVHjqWr9TSZjCsZI8YSWaek,11052
13
- dashscope/aigc/multimodal_conversation.py,sha256=SlNnnsUPV19gdx8fYJAtsMFWPNGY6vhk5IGHZ5ZczpI,5369
15
+ dashscope/aigc/multimodal_conversation.py,sha256=NPg4gjG24zKdu7lQ4wH2v2m9n_rfZERTLf5tjy5fQyg,5523
14
16
  dashscope/aigc/video_synthesis.py,sha256=gbp5XG_DMVN0JnBOgxftdwsCIyrV7nM2gVnlyO8X-ek,10400
15
17
  dashscope/api_entities/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
16
18
  dashscope/api_entities/aiohttp_request.py,sha256=ZFbdpJh7SwHnBzbYLhqr_FdcDVRgLVMLhLUS_vXbUGs,10228
17
19
  dashscope/api_entities/api_request_data.py,sha256=QHGgIShcQ1jANO1szfsqUGtYix1nD2RN4LyC_fNciCM,5462
18
20
  dashscope/api_entities/api_request_factory.py,sha256=iLjOqZkbuQkn9JVduI39XPrhm1UefekAekaNqxFpNLM,5061
19
21
  dashscope/api_entities/base_request.py,sha256=cXUL7xqSV8wBr5d-1kx65AO3IsRR9A_ps6Lok-v-MKM,926
20
- dashscope/api_entities/chat_completion_types.py,sha256=U1nYR5krZYbCedPwlRufJok3hz95mm4kYn2mp20hNAc,12807
21
- dashscope/api_entities/dashscope_response.py,sha256=FC9zGLNb2iu5fl4bJNcjLnVren_4kNGRW0i9xs4b4Q8,19839
22
+ dashscope/api_entities/dashscope_response.py,sha256=3e-W0UnfPGTsrd_DiZaD_xeq2I5CnrXFMEMRoHHDga8,20131
22
23
  dashscope/api_entities/http_request.py,sha256=Pr6mr01uXELK9LwIPXrJAhNtGMkWH3gQYORXShRiQRo,13258
23
- dashscope/api_entities/websocket_request.py,sha256=XzLj9vsJfBAhYMyRRcbEBoJu_dAVm0HpM60k46lBqBw,16281
24
+ dashscope/api_entities/websocket_request.py,sha256=h3RcvgvXpyzF8xvC4hqNUkev6HGN_YgO5emvqYaXw78,16094
24
25
  dashscope/app/__init__.py,sha256=UiN_9i--z84Dw5wUehOh_Tkk_9Gq_td_Kbz1dobBEKg,62
25
26
  dashscope/app/application.py,sha256=Cnd62LFpG70XJUo4Oibry9KzXPhPNmNkKFD4R5YuGTA,9343
26
27
  dashscope/app/application_response.py,sha256=0pulI3O3z4R4h_YaDwzVimamo3XwTXGy5TiHCzysTBg,7011
@@ -32,6 +33,7 @@ dashscope/audio/__init__.py,sha256=-ZRxrK-gV4QsUtlThIT-XwqB6vmyEsnhxIxdLmhCUuc,6
32
33
  dashscope/audio/asr/__init__.py,sha256=VaWX5DRWcB81_5z2o7IPwz6Jrs9vFFJ5GEVarzVOvPY,1004
33
34
  dashscope/audio/asr/asr_phrase_manager.py,sha256=EjtbI3zz9UQGS1qv6Yb4zzEMj4OJJVXmwkqZyIrzvEA,7642
34
35
  dashscope/audio/asr/recognition.py,sha256=7ApnKJKVp1JS6e6KBZzM8y1yiMB2fEun07S6BOvqfrs,20699
36
+ dashscope/audio/asr/transcribe.py,sha256=HfZYpvpVfvGRAIIIzX65Af33E6vsIFGd_qqhQ8LaNcM,9651
35
37
  dashscope/audio/asr/transcription.py,sha256=D8CW0XDqJuEJVmNFJ6qczTysSV3Sz_rzk2C6NIKTtVc,9042
36
38
  dashscope/audio/asr/translation_recognizer.py,sha256=j1oKgggSA2HDdEJAOJ4jY78_bK0FGwpfIf_AxbrAzWM,39629
37
39
  dashscope/audio/asr/vocabulary.py,sha256=2MHxeaL0ANWk-TILrHhArKSdj0d5M_YHw0cnjB-E4dY,6476
@@ -58,7 +60,7 @@ dashscope/customize/finetunes.py,sha256=iFnEUMGY6FGogVClIJMEeFhUwfYi7gD05Iq1-eMP
58
60
  dashscope/embeddings/__init__.py,sha256=-dxHaoxZZVuP-wAGUIa3sNNh8CQwaeWj2UlqsDy1sV4,240
59
61
  dashscope/embeddings/batch_text_embedding.py,sha256=P32LFO9v7ehdJsl0c32In94hUET6K6AaGJ_pDRtFqco,8791
60
62
  dashscope/embeddings/batch_text_embedding_response.py,sha256=hf17wfhJKvX3ggnigOiTqD4O1lFhx_NG_O1ioKCx0gk,1999
61
- dashscope/embeddings/multimodal_embedding.py,sha256=yojtGNoT2N54g0jcAYUwNIiwzueun1ouqS0S0tvnyQc,3966
63
+ dashscope/embeddings/multimodal_embedding.py,sha256=EvXoVRExkenpeRE8Nqfas0Vrl5wzCZCJIxfgbOI7Qv0,4058
62
64
  dashscope/embeddings/text_embedding.py,sha256=I3zRvuT2HWcaZYH-zrtGcAQmzuLQxFswIHJMiQXfaJQ,2009
63
65
  dashscope/io/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
64
66
  dashscope/io/input_output.py,sha256=iZ1X1x1btdoZK2VeC9JsKkag2eaXwqfNT3Q6SrmRi2w,3941
@@ -85,10 +87,9 @@ dashscope/tokenizers/tokenizer.py,sha256=y6P91qTCYo__pEx_0VHAcj9YECfbUdRqZU1fdGT
85
87
  dashscope/tokenizers/tokenizer_base.py,sha256=REDhzRyDT13iequ61-a6_KcTy0GFKlihQve5HkyoyRs,656
86
88
  dashscope/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
87
89
  dashscope/utils/oss_utils.py,sha256=TlqaMAmVRtBJIm5aIaXsrRZGKc_7cwWQ7liMB2f9Css,7331
88
- dashscope/utils/temporary_storage.py,sha256=CWBpEmuIthYg5DhU_qu63DbF2dsmqnLdKFkbWF8yX6A,6072
89
- dashscope-1.22.1.dist-info/LICENSE,sha256=Izp5L1DF1Mbza6qojkqNNWlE_mYLnr4rmzx2EBF8YFw,11413
90
- dashscope-1.22.1.dist-info/METADATA,sha256=xk089kHugSTlkhuy17skH7fOIT60VivJMcsYUUKAI-4,6661
91
- dashscope-1.22.1.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
92
- dashscope-1.22.1.dist-info/entry_points.txt,sha256=e9C3sOf9zDYL0O5ROEGX6FT8w-QK_kaGRWmPZDHAFys,49
93
- dashscope-1.22.1.dist-info/top_level.txt,sha256=woqavFJK9zas5xTqynmALqOtlafghjsk63Xk86powTU,10
94
- dashscope-1.22.1.dist-info/RECORD,,
90
+ dashscope-1.22.2.dist-info/LICENSE,sha256=Izp5L1DF1Mbza6qojkqNNWlE_mYLnr4rmzx2EBF8YFw,11413
91
+ dashscope-1.22.2.dist-info/METADATA,sha256=Cf_GT_W-HPppanGcy9RBKHS40yWSfL93_IXwc0SxAzc,6798
92
+ dashscope-1.22.2.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
93
+ dashscope-1.22.2.dist-info/entry_points.txt,sha256=raEp5dOuj8whJ7yqZlDM8WQ5p2RfnGrGNo0QLQEnatY,50
94
+ dashscope-1.22.2.dist-info/top_level.txt,sha256=woqavFJK9zas5xTqynmALqOtlafghjsk63Xk86powTU,10
95
+ dashscope-1.22.2.dist-info/RECORD,,
@@ -1,2 +1,3 @@
1
1
  [console_scripts]
2
2
  dashscope = dashscope.cli:main
3
+
@@ -1,271 +0,0 @@
1
- import json
2
- from typing import Any, Dict, Generator, List, Union
3
-
4
- import dashscope
5
- from dashscope.api_entities.dashscope_response import (GenerationResponse,
6
- Message)
7
- from dashscope.client.base_api import BaseAioApi, CreateMixin
8
- from dashscope.common import constants
9
- from dashscope.common.error import InputRequired, ModelRequired
10
- from dashscope.common.utils import _get_task_group_and_task
11
- from dashscope.api_entities.chat_completion_types import ChatCompletion, ChatCompletionChunk
12
-
13
-
14
- class Completions(CreateMixin):
15
- """Support openai compatible chat completion interface.
16
-
17
- """
18
- SUB_PATH = ''
19
- @classmethod
20
- def create(
21
- cls,
22
- *,
23
- model: str,
24
- messages: List[Message],
25
- stream: bool = False,
26
- temperature: float = None,
27
- top_p: float = None,
28
- top_k: int = None,
29
- stop: Union[List[str], List[List[int]]] = None,
30
- max_tokens: int = None,
31
- repetition_penalty: float = None,
32
- api_key: str = None,
33
- workspace: str = None,
34
- extra_headers: Dict = None,
35
- extra_body: Dict = None,
36
- **kwargs
37
- ) -> Union[ChatCompletion, Generator[ChatCompletionChunk, None, None]]:
38
- """Call openai compatible chat completion model service.
39
-
40
- Args:
41
- model (str): The requested model, such as qwen-long
42
- messages (list): The generation messages.
43
- examples:
44
- [{'role': 'user',
45
- 'content': 'The weather is fine today.'},
46
- {'role': 'assistant', 'content': 'Suitable for outings'}]
47
- stream(bool, `optional`): Enable server-sent events
48
- (ref: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) # noqa E501
49
- the result will back partially[qwen-turbo,bailian-v1].
50
- temperature(float, `optional`): Used to control the degree
51
- of randomness and diversity. Specifically, the temperature
52
- value controls the degree to which the probability distribution
53
- of each candidate word is smoothed when generating text.
54
- A higher temperature value will reduce the peak value of
55
- the probability, allowing more low-probability words to be
56
- selected, and the generated results will be more diverse;
57
- while a lower temperature value will enhance the peak value
58
- of the probability, making it easier for high-probability
59
- words to be selected, the generated results are more
60
- deterministic.
61
- top_p(float, `optional`): A sampling strategy, called nucleus
62
- sampling, where the model considers the results of the
63
- tokens with top_p probability mass. So 0.1 means only
64
- the tokens comprising the top 10% probability mass are
65
- considered.
66
- top_k(int, `optional`): The size of the sample candidate set when generated. # noqa E501
67
- For example, when the value is 50, only the 50 highest-scoring tokens # noqa E501
68
- in a single generation form a randomly sampled candidate set. # noqa E501
69
- The larger the value, the higher the randomness generated; # noqa E501
70
- the smaller the value, the higher the certainty generated. # noqa E501
71
- The default value is 0, which means the top_k policy is # noqa E501
72
- not enabled. At this time, only the top_p policy takes effect. # noqa E501
73
- stop(list[str] or list[list[int]], `optional`): Used to control the generation to stop # noqa E501
74
- when encountering setting str or token ids, the result will not include # noqa E501
75
- stop words or tokens.
76
- max_tokens(int, `optional`): The maximum token num expected to be output. It should be # noqa E501
77
- noted that the length generated by the model will only be less than max_tokens, # noqa E501
78
- not necessarily equal to it. If max_tokens is set too large, the service will # noqa E501
79
- directly prompt that the length exceeds the limit. It is generally # noqa E501
80
- not recommended to set this value.
81
- repetition_penalty(float, `optional`): Used to control the repeatability when generating models. # noqa E501
82
- Increasing repetition_penalty can reduce the duplication of model generation. # noqa E501
83
- 1.0 means no punishment.
84
- api_key (str, optional): The api api_key, can be None,
85
- if None, will get by default rule.
86
- workspace (str, optional): The bailian workspace id.
87
- **kwargs:
88
- timeout: set request timeout.
89
- Raises:
90
- InvalidInput: The history and auto_history are mutually exclusive.
91
-
92
- Returns:
93
- Union[ChatCompletion,
94
- Generator[ChatCompletionChunk, None, None]]: If
95
- stream is True, return Generator, otherwise ChatCompletion.
96
- """
97
- if messages is None or not messages:
98
- raise InputRequired('Messages is required!')
99
- if model is None or not model:
100
- raise ModelRequired('Model is required!')
101
- data = {}
102
- data['model'] = model
103
- data['messages'] = messages
104
- if temperature is not None:
105
- data['temperature'] = temperature
106
- if top_p is not None:
107
- data['top_p'] = top_p
108
- if top_k is not None:
109
- data['top_k'] = top_k
110
- if stop is not None:
111
- data['stop'] = stop
112
- if max_tokens is not None:
113
- data[max_tokens] = max_tokens
114
- if repetition_penalty is not None:
115
- data['repetition_penalty'] = repetition_penalty
116
- if extra_body is not None and extra_body:
117
- data = {**data, **extra_body}
118
-
119
- if extra_headers is not None and extra_headers:
120
- kwargs = {'headers': extra_headers} if kwargs else {**kwargs, **{'headers': extra_headers}}
121
-
122
- response = super().call(data=data,
123
- path='chat/completions',
124
- base_address=dashscope.base_compatible_api_url,
125
- api_key=api_key,
126
- flattened_output=True,
127
- stream=stream,
128
- workspace=workspace,
129
- **kwargs)
130
- if stream:
131
- return (ChatCompletionChunk(**item) for _, item in response)
132
- else:
133
- return ChatCompletion(**response)
134
-
135
-
136
- class AioGeneration(BaseAioApi):
137
- task = 'text-generation'
138
- """API for AI-Generated Content(AIGC) models.
139
-
140
- """
141
- class Models:
142
- """@deprecated, use qwen_turbo instead"""
143
- qwen_v1 = 'qwen-v1'
144
- """@deprecated, use qwen_plus instead"""
145
- qwen_plus_v1 = 'qwen-plus-v1'
146
-
147
- bailian_v1 = 'bailian-v1'
148
- dolly_12b_v2 = 'dolly-12b-v2'
149
- qwen_turbo = 'qwen-turbo'
150
- qwen_plus = 'qwen-plus'
151
- qwen_max = 'qwen-max'
152
-
153
- @classmethod
154
- async def call(
155
- cls,
156
- model: str,
157
- prompt: Any = None,
158
- history: list = None,
159
- api_key: str = None,
160
- messages: List[Message] = None,
161
- plugins: Union[str, Dict[str, Any]] = None,
162
- workspace: str = None,
163
- **kwargs
164
- ) -> Union[GenerationResponse, Generator[GenerationResponse, None, None]]:
165
- """Call generation model service.
166
-
167
- Args:
168
- model (str): The requested model, such as qwen-turbo
169
- prompt (Any): The input prompt.
170
- history (list):The user provided history, deprecated
171
- examples:
172
- [{'user':'The weather is fine today.',
173
- 'bot': 'Suitable for outings'}].
174
- Defaults to None.
175
- api_key (str, optional): The api api_key, can be None,
176
- if None, will get by default rule(TODO: api key doc).
177
- messages (list): The generation messages.
178
- examples:
179
- [{'role': 'user',
180
- 'content': 'The weather is fine today.'},
181
- {'role': 'assistant', 'content': 'Suitable for outings'}]
182
- plugins (Any): The plugin config. Can be plugins config str, or dict.
183
- **kwargs:
184
- stream(bool, `optional`): Enable server-sent events
185
- (ref: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) # noqa E501
186
- the result will back partially[qwen-turbo,bailian-v1].
187
- temperature(float, `optional`): Used to control the degree
188
- of randomness and diversity. Specifically, the temperature
189
- value controls the degree to which the probability distribution
190
- of each candidate word is smoothed when generating text.
191
- A higher temperature value will reduce the peak value of
192
- the probability, allowing more low-probability words to be
193
- selected, and the generated results will be more diverse;
194
- while a lower temperature value will enhance the peak value
195
- of the probability, making it easier for high-probability
196
- words to be selected, the generated results are more
197
- deterministic, range(0, 2) .[qwen-turbo,qwen-plus].
198
- top_p(float, `optional`): A sampling strategy, called nucleus
199
- sampling, where the model considers the results of the
200
- tokens with top_p probability mass. So 0.1 means only
201
- the tokens comprising the top 10% probability mass are
202
- considered[qwen-turbo,bailian-v1].
203
- top_k(int, `optional`): The size of the sample candidate set when generated. # noqa E501
204
- For example, when the value is 50, only the 50 highest-scoring tokens # noqa E501
205
- in a single generation form a randomly sampled candidate set. # noqa E501
206
- The larger the value, the higher the randomness generated; # noqa E501
207
- the smaller the value, the higher the certainty generated. # noqa E501
208
- The default value is 0, which means the top_k policy is # noqa E501
209
- not enabled. At this time, only the top_p policy takes effect. # noqa E501
210
- enable_search(bool, `optional`): Whether to enable web search(quark). # noqa E501
211
- Currently works best only on the first round of conversation.
212
- Default to False, support model: [qwen-turbo].
213
- customized_model_id(str, required) The enterprise-specific
214
- large model id, which needs to be generated from the
215
- operation background of the enterprise-specific
216
- large model product, support model: [bailian-v1].
217
- result_format(str, `optional`): [message|text] Set result result format. # noqa E501
218
- Default result is text
219
- incremental_output(bool, `optional`): Used to control the streaming output mode. # noqa E501
220
- If true, the subsequent output will include the previously input content. # noqa E501
221
- Otherwise, the subsequent output will not include the previously output # noqa E501
222
- content. Default false.
223
- stop(list[str] or list[list[int]], `optional`): Used to control the generation to stop # noqa E501
224
- when encountering setting str or token ids, the result will not include # noqa E501
225
- stop words or tokens.
226
- max_tokens(int, `optional`): The maximum token num expected to be output. It should be # noqa E501
227
- noted that the length generated by the model will only be less than max_tokens, # noqa E501
228
- not necessarily equal to it. If max_tokens is set too large, the service will # noqa E501
229
- directly prompt that the length exceeds the limit. It is generally # noqa E501
230
- not recommended to set this value.
231
- repetition_penalty(float, `optional`): Used to control the repeatability when generating models. # noqa E501
232
- Increasing repetition_penalty can reduce the duplication of model generation. # noqa E501
233
- 1.0 means no punishment.
234
- workspace (str): The dashscope workspace id.
235
- Raises:
236
- InvalidInput: The history and auto_history are mutually exclusive.
237
-
238
- Returns:
239
- Union[GenerationResponse,
240
- Generator[GenerationResponse, None, None]]: If
241
- stream is True, return Generator, otherwise GenerationResponse.
242
- """
243
- if (prompt is None or not prompt) and (messages is None
244
- or not messages):
245
- raise InputRequired('prompt or messages is required!')
246
- if model is None or not model:
247
- raise ModelRequired('Model is required!')
248
- task_group, function = _get_task_group_and_task(__name__)
249
- if plugins is not None:
250
- headers = kwargs.pop('headers', {})
251
- if isinstance(plugins, str):
252
- headers['X-DashScope-Plugin'] = plugins
253
- else:
254
- headers['X-DashScope-Plugin'] = json.dumps(plugins)
255
- kwargs['headers'] = headers
256
- input, parameters = Generation._build_input_parameters(
257
- model, prompt, history, messages, **kwargs)
258
- response = await super().call(model=model,
259
- task_group=task_group,
260
- task=Generation.task,
261
- function=function,
262
- api_key=api_key,
263
- input=input,
264
- workspace=workspace,
265
- **parameters)
266
- is_stream = kwargs.get('stream', False)
267
- if is_stream:
268
- return (GenerationResponse.from_api_response(rsp)
269
- async for rsp in response)
270
- else:
271
- return GenerationResponse.from_api_response(response)