一篇文章让你的微信变成AI,支持文心一言和通义千问的切换,附开源简单网页聊天机器人

目录

前置工作

代码展示

参考文档


前置工作

安装uiautomation UI自动化k库

安装百度大模型 erniebot库

安装 阿里 DashScope库

pip install uiautomation
pip install dashscope
pip install erniebot

代码展示

from uiautomation import WindowControl
import dashscope
import erniebot
import time

# 设置阿里云API密钥
dashscope.api_key = "your_key"

# 设置文心一言API访问令牌
erniebot.api_type = 'aistudio'
erniebot.access_token = 'your_token'

wx = WindowControl(Name='微信', searchDepth=1)  # 绑定微信主窗口
wx.ListControl()  # 切换窗口
wx.SwitchToThisWindow()  # 切换到微信窗口
hw = wx.ListControl(Name='会话')  # 寻找会话控件绑定

# 默认使用通义千问模型
current_model = 'qwen'

while True:  # 持续接收消息
    we = hw.TextControl(searchDepth=4)  # 查找未读信息
    while not we.Exists():
        pass
    if we.Name:  # 如果存在未读消息
        we.Click(simulateMove=False)  # 点击未读消息
        last_msg = wx.ListControl(Name='消息').GetChildren()[-1].Name  # 读取最后一条消息

        # 判断是否需要切换模型
        if last_msg.strip() == '切换模型':
            current_model = 'ernie' if current_model == 'qwen' else 'qwen'
            reply_text = '切换成功'
        else:
            # 根据当前模型生成回复
            if current_model == 'qwen':
                response_generator = dashscope.Generation.call(
                    model='qwen-turbo',
                    prompt=last_msg,
                    stream=True,
                    top_p=0.8)
                reply_text = ''
                for resp in response_generator:
                    # 从resp.output.text中获取生成的文本
                    paragraph = resp.output.text
                    reply_text = paragraph
            else:
                response = erniebot.ChatCompletion.create(
                    model='ernie-3.5',
                    messages=[{'role': 'user', 'content': last_msg}],
                    system="你是现在是一只可爱的猫娘,回答消息的时候会在前面加上是,主人,并在后面加上喵",
                )
                reply_text = response.get_result()

        wx.SendKeys(reply_text, waitTime=5)  # 将回复发送给微信
        wx.SendKeys('{Enter}', waitTime=1)  # 模拟按下回车键
        wx.TextControl(SubName=last_msg[:5]).Click()  # 点击对应的消息
        time.sleep(1)  # 设置延时防止频繁调用API

参考文档

https://mp.weixin.qq.com/s/cXgVML5m1TZqcdv9dYRfuQ

https://blog.csdn.net/SXTTL/article/details/134319652

https://blog.csdn.net/sfsgtc/article/details/133686011?ops_request_misc=&request_id=&biz_id=102&utm_term=erniebot&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-3-133686011.142^v100^control&spm=1018.2226.3001.4187

3.30修改
调用api做了个简单网页聊天机器人 

效果


开源地址

Aibot: This is an AI chat robot. In this interface, you can choose Wen Xin Yi Yan, Tong Yi Qian Wen and other large models to answer your questions.

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值