from http import HTTPStatus
import dashscope
dashscope.api_key='你的key'

def call_with_messages():
	messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
				{'role': 'user', 'content': '如何做炒西红柿鸡蛋?'}]

	response = dashscope.Generation.call(
		dashscope.Generation.Models.qwen_max,
		messages=messages,
		result_format='message',  # set the result to be "message" format.
	)
	if response.status_code == HTTPStatus.OK:
		print(response.output.choices[0]['message']['role'])
		print(response.output.choices[0]['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__':
    call_with_messages()

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐