1.引入ai大语言模型
首先这里的ai用的阿里云的通义千问大数据模型,地址:阿里云
申请一个api_key;
然后再页面上使用pip引入dashscope模块,再引入你的api_key:
import dashscope
dashscope.api_key = api_key
from dashscope import Generation
from http import HTTPStatus
response = Generation.call(
model='qwen-turbo',
prompt='就当前的海洋污染的情况,写一份限塑的倡议书提纲,需要有理有据地号召大家克制地使用塑料制品'
)
if response.status_code == HTTPStatus.OK:
print(response.output) # The output text
print(response.usage) # The usage information
else:
print(response.code) # The error code.
print(response.message) # The error message.
这里咱们只要传入相应的文字,response就会返回相应的答案;
2.微信自动回复
首先是要引入三个模块 使用pip install
import pandas as pd
import numpy as np
from uiautomation import WindowControl, MenuControl
打开微信窗口,查找未读消息,然后把或获取到的消息,调取ai模型的api接口,具体代码如下:
import pandas as pd
import numpy as np
import dashscope
dashscope.api_key = api_key
from http import HTTPStatus
# For prerequisites running the following sample, visit https://help.aliyun.com/document_detail/611472.html
from dashscope import Generation
from uiautomation import WindowControl, MenuControl
# 绑定微信主窗口
# wx = WindowControl(searchDepth=1, ClassName='WeChatMainWndForPC')
wx = WindowControl(Name="微信")
print(wx)
# 切换窗口
wx.SwitchToThisWindow()
hw = wx.ListControl(Name="会话")
print('寻找会话控件绑定', hw)
df = pd.read_csv('回复数据.csv', encoding='utf-8')
while True:
we = hw.TextControl(searchDepth=4)
print('查找会话', we)
while not we.Exists(0):
pass
if we.Name:
we.Click(simulateMove=False)
last_msg = wx.ListControl(Name="消息").GetChildren()[-1].Name
prompt_text = last_msg
response_generator = Generation.call(
model='qwen-turbo',
prompt=prompt_text,
stream=True,
top_p=0.8)
for response in response_generator:
if response.status_code == HTTPStatus.OK:
if (response.output.finish_reason == 'stop'):
wx.SendKeys(response.output.text, waitTime=0)
wx.SendKeys('{Enter}', waitTime=0)
wx.TextControl(SubName=last_msg[:5]).RightClick()
else:
print(response.code) # The error code.
print(response.message) # The