import random from http import HTTPStatus import dashscope ### 参考: ## export DASHSCOPE_API_KEY="sk-948adb3e65414e55961a9ad9d22d186b" dashscope.api_key = "sk-948adb3e65414e55961a9ad9d22d186b" def call_with_messages(prompt): messages = [{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': prompt}] # {'role': 'user', 'content': '如何做西红柿炒鸡蛋?'}] response = dashscope.Generation.call( "qwen-turbo", messages=messages, # set the random seed, optional, default to 1234 if not set seed=random.randint(1, 10000), # set the result to be "message" format. result_format='message', ) if response.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 )) return response['output']['choices'][0]['message']['content'] ### 这里是content的内容,不是message的全部内容。 # if __name__ == '__main__': # # call_with_messages() ### original code here. # res = call_with_messages() ## working. # # print(res)