本文最后更新于 165 天前,其中的信息可能已经有所发展或是发生改变。
使用wxauto与kimi 的ai 接口
import time
from wxauto import WeChat
from openai import OpenAI
import os
wx = WeChat()
listen_list = ['好友id']
for i in listen_list:
wx.AddListenChat(who=i)
while True:
# 获取列表所有窗口消息
msgs = wx.GetListenMessage()
for chat in msgs:
one_msgs = msgs.get(chat)
if one_msgs:
for msg in one_msgs:
if msg.type == 'sys':
print(f'【系统消息】{msg.content}')
elif msg.type == 'friend':
sender = msg.sender_remark # 这里可以将msg.sender改为msg.sender_remark,获取备注名
print(f'{sender.rjust(20)}:{msg.content}')
#chat.SendMsg('收到')
# 此处将msg.content传递给大模型
time.sleep(5)
client = OpenAI(
api_key="接口秘钥",
base_url="https://api.moonshot.cn/v1"
)
completion = client.chat.completions.create(
model="moonshot-v1-8k",
messages=[
{"role": "system",
"content": "提示语"},
{"role": "user", "content": msg.content} # 传递提取出来的 msg_content
],
temperature=0.7,
)
# 获取返回的内容并发送
content = completion.choices[0].message.content
#此处为`chat`而不是`wx` !!!
chat.SendMsg(content)
elif msg.type == 'self':
print(f'{msg.sender.ljust(20)}:{msg.content}')
elif msg.type == 'time':
print(f'\n【时间消息】{msg.time}')
elif msg.type == 'recall':
print(f'【撤回消息】{msg.content}')