一、可以实现微信机器人获取消息后+大模型自动答复+向量化知识自动答复
二、实现群消息自动抓取+大模型批量提炼答复
比如:我们要抓取聊天内容中二手车信息(群OR个人都可以)
1、PYTHON代码部分自动实时获取最新消息:
import json
import re
import time
import requests
from wxautox import WeChat
def load_settings(file_path='settings.txt'):
try:
with open(file_path, 'r', encoding='utf-8') as f:
return [line.strip() for line in f if line.strip()]
except FileNotFoundError:
return []
# 判断是否包含手机号
def contains_phone_number(text):
"""
判断字符串中是否包含中国大陆手机号
手机号规则:
- 1开头
- 第二位:3/4/5/6/7/8/9
- 总共11位数字
"""
pattern = r'(?<!\d)(1[3-9]\d{9})(?!\d)'
return bool(re.search(pattern, text))
if __name__ == '__main__':
# 获取微信窗口对象
wx = WeChat()
listen_list = load_settings("settings.txt")
print(listen_list)
for i in listen_list:
wx.AddListenChat(who=i, savepic=True)
wait = 5 # 设置1秒查看一次是否有新消息
while True:
msgs = wx.GetListenMessage()
for chat in msgs:
who = chat.who # 获取聊天窗口名(人或群名)
one_msgs = msgs.get(chat) # 获取消息内容
# 回复收到
for msg in one_msgs:
msgtype = msg.type # 获取消息类型
content = msg.content # 获取消息内容,字符串类型的消息内容
if (contains_phone_number(content)):
print(f'【{who}】:{content}')
# 接口URL
url = "http://localhost:8810/big/insert_user" # 替换为实际的服务器地址
# 准备请求数据
user_data = {
"pythonInfo": who + content
# 根据User类的其他字段,可以添加更多数据
}
# 设置请求头
headers = {
"Content-Type": "application/json",
"Accept": "application/json"
}
try:
# 发送POST请求
response = requests.post(
url,
data=json.dumps(user_data), # 将字典转换为JSON字符串
headers=headers
)
with open('out.txt', 'a', encoding='utf-8') as f: # 'a' 表示追加
try:
if response.status_code == 200:
msg = "请求成功!"
content = f"响应内容: {response.json()}"
else:
msg = f"请求失败,状态码: {response.status_code}"
content = f"响应内容: {response.text}"
# 打印到控制台
print(msg)
print(content)
# 追加写入文件
f.write(f"{msg}\n{content}\n")
except Exception as e:
error_msg = f"请求过程中发生错误: {str(e)}"
print(error_msg)
f.write(f"{error_msg}\n")
except Exception as e:
print(f"请求过程中发生错误: {str(e)}")
time.sleep(wait)
2、可以看到我们获取信息后,自动请求服务http://localhost:8810/big/insert_user
这个服务的目的就是调用大模型分析文本
JAVA服务代码:
package com.black.controller;
import com.black.pojo.User;
import com.black.util.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@RestController
@RequestMapping("/big")
public class BigController {
// 创建一个线程池用于异步执行
private final Executor executor = Executors.newCachedThreadPool();
@PostMapping("/insert_user")
public CompletableFuture<Res> insert_user(@RequestBody User user) throws Exception {
// 规整完毕的数据通过这个服务提交pythonInfo
System.out.println(user.getPythonInfo());
String question = "你的角色是:二手车信息分析专家。\n" + "你的任务是:分析输入的二手车信息,严格按照此JSON格式返回结果{\"isTheSenderTypeTheRecipientOrTheSeller\":\"xxx\",\"informationRelatedToTheTypeOfVehicleReceived\":\"xxx\",\"regionalInformation\":\"xxx\",\"contactInformation\":\"xxx\"}\n" + "我的要求:重点分析四个方面并返回中文数据。发送方类型为收车方还是卖车方、收车类型相关信息、地区信息、联系方式。换行尽量保留。\n" + "根据以上要求,我的输入是:" + user.getPythonInfo();
// 使用CompletableFuture异步执行两个任务
CompletableFuture<String> douBaoFuture = CompletableFuture.supplyAsync(() -> DouBaoModel.modelDoWork(question), executor);
CompletableFuture<String> deepSeekFuture = CompletableFuture.supplyAsync(() -> A_Http_V3_New.useDeepSeek(question), executor);
// 合并两个Future的结果
return douBaoFuture.thenCombineAsync(deepSeekFuture, (douBaoRes, deepSeekRes) -> {
HashMap<Object, Object> hashMap = new HashMap<>();
hashMap.put("douBaoRes", douBaoRes);
hashMap.put("deepSeekRes", deepSeekRes);
return Res.success(hashMap);
}, executor);
}
}
请求大模型后就可以对微信信息进行实时获取分析或者实时答复:
请求成功!
响应内容: {'code': '200', 'message': '', 'object': {'douBaoRes': '\n\n{"isTheSenderTypeTheRecipientOrTheSeller":"收车方","informationRelatedToTheTypeOfVehicleReceived":"事故修复车,新款BBA、保时捷、丰田、本田等类型低中高款车型","regionalInformation":"无","contactInformation":"18766093996"}', 'deepSeekRes': '```json\n{\n "isTheSenderTypeTheRecipientOrTheSeller": "收车方",\n "informationRelatedToTheTypeOfVehicleReceived": "事故修复车,专收新款BBA、保时捷、丰田、本田等类型低中高款车型",\n "regionalInformation": "无明确地区信息",\n "contactInformation": "18766093996 长期换二手车群。24小时欢迎同行骚扰"\n}\n``` \n'}}
请求成功!
响应内容: {'code': '200', 'message': '', 'object': {'douBaoRes': '\n\n{"isTheSenderTypeTheRecipientOrTheSeller":"收车方","informationRelatedToTheTypeOfVehicleReceived":"专收新款BBA、保时捷、丰田、本田等类型低中高款车型,事故修复车","regionalInformation":"杭州","contactInformation":"18766093996"}', 'deepSeekRes': '```json\n{\n "isTheSenderTypeTheRecipientOrTheSeller": "收车方",\n "informationRelatedToTheTypeOfVehicleReceived": "专收新款BBA、保时捷、丰田、本田等类型低中高款车型。收事故修复车。",\n "regionalInformation": "杭州",\n "contactInformation": "18766093996\\n长期换二手车群。\\n24小时欢迎同行骚扰。"\n}\n``` \n'}}