搭建Python(3.7及以上版本)环境并安装DashScope SDK
搭建Python环境这里忽略,搭建好环境后安装
pip install dashscope
python,demo
import dashscope from dashscope.api_entities.dashscope_response import Message dashscope.api_key="填写自己的key" def call_with_messages(prompt): messages = [ {'role': 'user', 'content':prompt }] response = dashscope.Generation.call( '填写模型', messages=messages, result_format='message', # set the result is message format. ) if response.status_code == HTTPStatus.OK: message = Message.from_conversation_response(response) if len(message.content) < 400: call_with_messages(prompt) return message.content 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__': prompt = '帮我写一篇关于{0}的报道' content = call_with_messages(prompt) print(content)