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])
This will extract the top 20 categories from the ‘category’ column of the DataFrame and create a new DataFrame that only includes rows with those categories.
Posted Updated a few seconds read (About 81 words)
Extracting room numbers from a ‘Description’ column in a DataFrame using regular expressions:
Import the re module:
1 2 3 4 5 6 7 8 9 10 11 12
import re ````
2. Define a function to extract the room number from a description:
```python defextract_room_number(description): match = re.search(r'(\d+\.\d+|\d+)(?=\s+of which are bedrooms)', description) ifmatch: returnfloat(match.group(1)) else: returnNone
Use the apply() method to apply the function to the ‘Description’ column and create a new ‘RoomNumber’ column:
magma-nvim Magma is a NeoVim plugin for running code interactively with Jupyter.
Jupynium
Jupynium.nvim It’s just like a markdown live preview, but it’s Jupyter Notebook live preview!
Jupynium uses Selenium to automate Jupyter Notebook, synchronising everything you type on Neovim. Never leave Neovim. Switch tabs on the browser as you switch files on Neovim.