Package: dashscope
EnglishDashScope Python LibraryInstallationTo install the DashScope Python SDK, simply run:pipinstalldashscopeIf you clone the code from github, you can install from  source by running:pipinstall-e.To use tokenizer in local mode without downloading any files, run:pipinstalldashscope[tokenizer]QuickStartYou can useGenerationapi to call model qwen-turbo(通义千问).fromhttpimportHTTPStatusimportdashscopefromdashscopeimportGenerationdashscope.api_key='YOUR-DASHSCOPE-API-KEY'responses=Generation.call(model=Generation.Models.qwen_turbo,prompt='今天天气好吗？')ifresponses.status_code==HTTPStatus.OK:print('Result is:%s'%responses.output)else:print('Failed request_id:%s, status_code:%s, code:%s, message:%s'%(responses.request_id,responses.status_code,responses.code,responses.message))API Key AuthenticationThe SDK uses API key for authentication. Please refer toofficial documentationregarding how to obtain your api-key.Using the API KeySet the API key via codeimportdashscopedashscope.api_key='YOUR-DASHSCOPE-API-KEY'# Or specify the API key file path via code# dashscope.api_key_file_path='~/.dashscope/api_key'Set the API key via environment variablesa. Set the API key directly using the environment variable belowexportDASHSCOPE_API_KEY='YOUR-DASHSCOPE-API-KEY'b. Specify the API key file path via an environment variableexportDASHSCOPE_API_KEY_FILE_PATH='~/.dashscope/api_key'Save the API key to a filefromdashscopeimportsave_api_keysave_api_key(api_key='YOUR-DASHSCOPE-API-KEY',api_key_file_path='api_key_file_location or (None, will save to default location "~/.dashscope/api_key"')Sample Codecallfunction provides  synchronous call, the function call will return when computation is done on the server side.fromhttpimportHTTPStatusfromdashscopeimportGeneration# export DASHSCOPE_API_KEY='YOUR-DASHSCOPE-API-KEY' in environmentdefsync_dashscope_sample():responses=Generation.call(model=Generation.Models.qwen_turbo,prompt='Is the weather good today?')ifresponses.status_code==HTTPStatus.OK:print('Result is:%s'%responses.output)else:print('Code:%s, status_code:%s, code:%s, message:%s'%(responses.status_code,responses.code,responses.message))if__name__=='__main__':sync_dashscope_sample()For requests with longer processing times, you can obtain partial results before the full output is generated. Set thestreamparameter toTrue. In this case, the results will be returned in batches, and the current output mode is incremental (output will overwrite the previous content). When the output is in stream mode, the interface returns a generator, and you need to iterate through the generator to get the results. Each output contains partial data for streaming, and the last output contains the final generated result.Example with simple streaming:fromhttpimportHTTPStatusfromdashscopeimportGenerationdefsample_sync_call_stream():prompt_text='Give me a recipe using carrots, potatoes, and eggplants'response_generator=Generation.call(model=Generation.Models.qwen_turbo,prompt=prompt_text,stream=True,max_length=512,top_p=0.8)forrespinresponse_generator:# Iterate through the streaming output resultsifresp.status_code==HTTPStatus.OK:print(resp.output)else:print('Request failed, message:%s'%resp.message)if__name__=='__main__':sample_sync_call_stream()Stream with MessagesfromhttpimportHTTPStatusfromdashscopeimportGenerationfromdashscope.api_entities.dashscope_responseimportRoledefstream_with_messages():messages=[{'role':Role.SYSTEM,'content':'You are a helpful assistant.'},{'role':Role.USER,'content':'如何做西红柿炖牛腩？'}]responses=Generation.call(Generation.Models.qwen_turbo,messages=messages,result_format='message',# set the result to be "message" format.stream=True,)forresponseinresponses:ifresponse.status_code==HTTPStatus.OK:print(response)else:print('Request id:%s, Status code:%s, error code:%s, error message:%s'%(response.request_id,response.status_code,response.code,response.message))if__name__=='__main__':stream_with_messages()LoggingTo output Dashscope logs, you need to configure the logger.exportDASHSCOPE_LOGGING_LEVEL='info'OutputThe output contains the following fields:request_id (str): The request id.
     status_code (int): HTTP status code, 200 indicates that the
         request was successful, others indicate an error。
     code (str): Error code if error occurs, otherwise empty str.
     message (str): Set to error message on error.
     output (Any): The request output.
     usage (Any): The request usage information.Error HandlingCurrently, errors are thrown as exceptions.ContributingComing soon.LicenseThis project is licensed under the Apache License (Version 2.0).