api调用阿里云的千问大模型

2023-12-14 15:30:35
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()

文章来源:https://blog.csdn.net/lanyan90/article/details/134995822
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。