from http import HTTPStatus from dashscope import Generation from dashscope.api_entities.dashscope_response import Role
defconversation_with_messages(): messages = [{'role': Role.SYSTEM, 'content': 'You are a helpful assistant.'}, {'role': Role.USER, 'content': '如何做西红柿炖牛腩?'}] response = Generation.call( Generation.Models.qwen_turbo, messages=messages, # set the result to be "message" format. result_format='message', ) if response.status_code == HTTPStatus.OK: print(response) # append result to messages. messages.append({'role': response.output.choices[0]['message']['role'], 'content': 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 )) messages.append({'role': Role.USER, 'content': '不放糖可以吗?'}) # make second round call response = Generation.call( Generation.Models.qwen_turbo, messages=messages, result_format='message', # set the result to be "message" format. ) 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 ))
if __name__ == '__main__': conversation_with_messages()
写成notebook形式
基本的包以及api-key指定:
1 2 3 4 5 6 7
from http import HTTPStatus from dashscope import Generation from dashscope.aigc.generation import Message from dashscope.api_entities.dashscope_response import Role import dashscope
dashscope.api_key = "..."
创建初始message:
1
messages = [Message(Role.SYSTEM, 'you are a cyl家的小女仆口牙')]
提问#1:
1 2 3 4 5 6 7
messages.append(Message(Role.USER, 'how to install archlinux')) response = Generation.call( Generation.Models.qwen_turbo, messages=messages, # set the result to be "message" format. result_format='message', )
1
response
1
GenerationResponse(status_code=<HTTPStatus.OK: 200>, request_id='dcf58c98-17c0-95fd-80c1-3f88fc8dd9db', code='', message='', output=GenerationOutput(text=None, choices=[Choice(finish_reason='stop', message=Message({'role': 'assistant', 'content': 'Installing Arch Linux can be done in several steps, ... Remember to read the Arch Linux documentation for further guidance and troubleshooting: [https://wiki.archlinux.org/](https://wiki.archlinux.org/)'}))], finish_reason=None), usage=GenerationUsage(input_tokens=24, output_tokens=687))
from http import HTTPStatus from dashscope import Generation from dashscope.aigc.generation import Message from dashscope.api_entities.dashscope_response import Role import dashscope
defask(question:str): messages.append(Message(Role.USER, question)) response = Generation.call( Generation.Models.qwen_turbo, messages=messages, # set the result to be "message" format. result_format='message', ) if response.status_code == HTTPStatus.OK: messages.append(Message(response.output.choices[0]['message']['role'], response.output.choices[0]['message']['content'])) else: pass
if __name__ == '__main__': setup("你是陈语林家的可爱小女仆呀") ask("你是谁呀") print(messages[-1]) ask("你知道些什么") print(messages[-1])