Wechat bot

Wechat bot

通过接通现有大模型的方式,创建一个微信的问答机器人

LLM

Chocie

由于没有白名单地区的手机号,所以无法申请chatgpt的api,之后经道听途说,阿里云的通义千问有不错的问答能力,且api调用价格较为低廉(一次问答几分钱?一开始会送2M tokens)。综上,决定使用通义千问。

API KEY

申请/管理地址
设置API KEY (设为环境变量)

1
export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY

Code

详细文档

先安装阿里云的dashscope package

1
pip install dashscope

因为需要问答,属于多轮会话
以下为官网提供的多轮会话的示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from http import HTTPStatus
from dashscope import Generation
from dashscope.api_entities.dashscope_response import Role


def conversation_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))

接收回答:

1
2
3
4
5
6
7
8
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
))

将回答整合进上下文:

1
2
messages.append(Message(response.output.choices[0]['message']['role'],
response.output.choices[0]['message']['content']))

然后可以重新回到提问#1环节

一个简单的重写的module

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from http import HTTPStatus
from dashscope import Generation
from dashscope.aigc.generation import Message
from dashscope.api_entities.dashscope_response import Role
import dashscope

messages = []

def setKey():
dashscope.api_key = "sk-09dd84c7453e4f80a027a05970ab19e1"

def setup(prompt:str):
setKey()
messages.append(Message(Role.SYSTEM, prompt))

def ask(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])

1
2
3
4
5
6
7
8
9
{"role": "assistant", "content": "我是陈语林家的可
爱小女仆,负责照顾主人和提供温馨的生活服务。有什么
需要我帮忙的吗?"}
{"role": "assistant", "content": "作为陈语林家的小
女仆,我知道一些关于家庭日常的事物,比如家务管理、
烹饪技巧、以及如何让主人感到舒适。但请记住,我并非
无所不知,对于超出这个设定范围的问题,我会尽力给出
符合情境的回答。如果你有任何关于家居生活或角色扮演
的问题,我很乐意帮忙。"}

Wechaty

document

因为博主准备在wsl2中使用wechaty,而wechaty需要先启动Puppet的docker服务,所以安装Docker Desktop Windows

要在wsl2中使用docker的话需要更改一下用户组

1
sudo usermod -a -G docker chenyulin

然后重启一下wsl2,重新启动一下Docker Desktop服务

wsl2更新并开启服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
docker pull wechaty/wechaty:latest
export WECHATY_LOG="verbose"
export WECHATY_PUPPET="wechaty-puppet-wechat"
export WECHATY_PUPPET_SERVER_PORT="8080"
export WECHATY_TOKEN="python-wechaty-uos-token"

docker run -ti \
--name wechaty_puppet_service_token_gateway \
--rm \
-e WECHATY_LOG \
-e WECHATY_PUPPET \
-e WECHATY_PUPPET_SERVER_PORT \
-e WECHATY_TOKEN \
-p "$WECHATY_PUPPET_SERVER_PORT:$WECHATY_PUPPET_SERVER_PORT" \
wechaty/wechaty:latest

安装wechaty python对应的包:

1
pip install wechaty -i https://pypi.tuna.tsinghua.edu.cn/simple/

tnnd突然发现有个简化版的wechaty用起来更方便

Ding-dong bot

简化版的wechaty

经了解,wechatbot需要实名账号,且存在封控风险,qq同理,故暂且CLOSE本博客,可能后续会考虑转成网页端的问答

QQ bot

咱就是,感觉可以用qq救一下,况且qq也小号free,干就完了!

[]

DataFrame

DataFrame

Creating a DataFrame

1
2
3
4
import pandas as pd
df = pd.read_csv("a.csv")
df = pd.DataFrame({dict_t}) #convert a dict to a dataframe
df = Series_t.to_frame()

Location

1
2
3
4
5
6
7
df.loc[0:4,"Year":"Party"]
df.loc[[1,2,5],["Year","Candidate"]]
df.loc[:,["Year","Candidate"]]
df.loc[:,"year"] # return series
df.loc[1,"year":"Party"] # return series
df.shape[0] # count of row
df[["Year","Candidate"]][0:5]

type transforming

1
2
3
df['column_name'] = df['column_name'].astype(int)
df['column_name'] = df['column_name'].astype(float)
df['column_name'] = df['column_name'].astype(str)

Groupby and agg

1
2
3
df.groupby('column_name').agg(['sum', 'mean', 'max', 'min'])
df.groupby(['column_name_1', 'column_name_2']).agg({'column_name_3': ['sum', 'mean'], 'column_name_4': ['max', 'min']})
df.groupby(['column_name_1', 'column_name_2']).agg(['sum', 'mean', 'max', 'min'])

Example

Example 1

Extracting the top 20 categories from a DataFrame using groupby:

  1. Use the groupby and size methods to calculate the size of each group:

    1
    2
    3
    4
    5
    6
    7
    group_sizes = df.groupby('category').size()
    ````

    2. Use the `sort_values` method to sort the group sizes in descending order:

    ```python
    sorted_groups = group_sizes.sort_values(ascending=False)
  2. Use the head method to extract the top 20 categories:

1
top_20 = sorted_groups.head(20)
  1. Use the isin method to filter the original DataFrame to only include rows with the top 20 categories:
1
df_filtered = df[df['category'].isin(top_20.index)]

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.

Scrap

Scrap

Beautiful Soup

Generate bs4

1
soup = bs4.BeautifulSoup(text)

find node

1
2
3
soup.find("div", attrs={"id":"..."})
soup.find_all("...")
soup.div

siblings

1
2
3
4
soup.find("...").next_sibling
soup.find("...").previous_sibling
for elm in soup.div.next_siblings:
...

with Regular Expression

1
2
soup(class_=re.compile('item-'))
soup(attrs={"class":re.compile("star-rating.*")})[0].get("class")[1]
regex

regex

s—
id: “regex”
aliases:
- “Usage”
- “Syntax”
tags:
- “regex”
- “python”

Syntax

regex101
regexr

Usage

Use re

1
2
3
re.sub(pattern,repl,str)
re.search(pattern, str)
re.findall(pattern,str)

Use Pandas

1
2
3
df['column_name'].str.contains('pattern')
df['column_name'].str.findall('pattern')
df['column_name'].str.replace('pattern', 'replacement')

Example

Example 1

Extracting room numbers from a ‘Description’ column in a DataFrame using regular expressions:

  1. 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
    def extract_room_number(description):
    match = re.search(r'(\d+\.\d+|\d+)(?=\s+of which are bedrooms)', description)
    if match:
    return float(match.group(1))
    else:
    return None
  2. Use the apply() method to apply the function to the ‘Description’ column and create a new ‘RoomNumber’ column:

1
df['RoomNumber'] = df['Description'].apply(extract_room_number)

This will extract the room number from the ‘Description’ column and store it in the new ‘RoomNumber’ column.

sklearn

sklearn

Some basic operation

Shuffle the training set for hold out validation

1
2
from sklearn.utils import shuffle
training_set,dev_set = np.split(shuffle(data_sample_35),[25])

Also capable of spliting training set, validation(development) set, test set

Environmen generation

Environmen generation

Generator file

File Path: catkin_ws/src/test/script/env_pkl_generator.py

Launch Ros

1
2
cd ~
roslaunch test demo_gazebo.launch

It will open Gazebo and RViz and display the robotic arm on both sofware.

Load Scene

1
2
3
cd ~
rosrun test moveit_setscene.py
rosrun test gazebo_setscene.py

远程链接

向日葵
识别吗
341 866 266
ge6v9Q

pandas

pandas

Environment

Install package for sql

1
2
3
pip install sqlalchemy
pip install ibm_db_sa
pip install ipython-sql

Example

InterestingPlugin

InterestingPlugin

Python related

Magma

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.