Giter Club home page Giter Club logo

wechatbot's Introduction

微信机器人(ChatGPT官方API版)

最近chatGPT异常火爆,想到将其接入到个人微信是件比较有趣的事,所以有了这个项目。本项目使用了OpenAI最新(2023-03-02)发布的ChatGPT在使用的大型语言模型gpt-3.5-turbo以其API和基于openwechat开发。

本项目修改自 @djun 的 wechatbot

目前实现了以下功能

  • 群聊@回复
  • 私聊回复
  • 自动通过回复

注意: 若在使用过程中有任何技术问题请自行处理。本人只确保发布的源码是可运行的(可运行不代表要符合任何人的心意,本项目属于实验性质的demo级项目,若有任何不快请自行修改代码)。

注册OpenAI开发者帐号

OpenAI开发者帐号注册可以参考这里

安装使用

获取项目

git clone https://github.com/poorjobless/wechatbot.git

进入项目目录

cd wechatbot

复制配置文件

linux下执行 cp config.dev.json config.json

windows下执行 copy config.dev.json config.json

启动项目

go run main.go

启动前需替换config中的api_key

若想实现连续对话功能请参考以下内容:

为什么要提供一个对话列表呢?因为 API 调用都是单次的接口请求,不会自动记录之前的聊天信息,没有上下文,要让 ChatGPT 在单次的请求中了解你的上下文,就需要提供这样一个完整的对话列表,比如这样一个对话

import openai

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
)

每条对话消息都需要提供角色和内容,角色分三种:系统(system)、用户(user)、助手(assistant)。
system:系统消息相当于一个管理员,可以设置助手的行为和特征。在上面的例子中,助手被指示你是一个有用的助手。
user:用户消息就是我们自己,可以由用户发出提问,或者直接让开发者提前内置一些 Prompts 指令。一些可以参考的 ChatGPT Prompts
assistant:助手消息就是 ChatGPT API 在之前提供的回复,在这里存储起来。这个回复也可以自己修改或者自己编一段对话出来,来让整个对话更通顺。
如果不需要对话的话,只需要提供单个的user消息即可,就像刚刚 Python 代码里演示的。

鸣谢

  • @djun
  • @eatmoreapple

wechatbot's People

Contributors

csterkuroi avatar elllusion avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wechatbot's Issues

连续对话可以支持吗

非常感谢大大,由于不动go语言所以不知道怎么让他支持连续对话,能否有空更新此功能

目前如何使用呢

2023/10/28 18:58:13 hadler Received msg : 你好
2023/10/28 18:58:15 Received User Meeee Text Msg : 你好
panic: Post "https://api.openai.com/v1/chat/completions": proxyconnect tcp: dial tcp :0: connectex: The requested address is not valid in its context.

goroutine 11 [running]:
github.com/poorjobless/wechatbot/chatgpt.Completions({0xc0002b69c6, 0x9}, {0xc000433040, 0x6})
D:/10-28/wechatbot-main/chatgpt/chatgpt.go:31 +0x325
github.com/poorjobless/wechatbot/handlers.(*UserMessageHandler).ReplyText(0xc000237db0?, 0xc000208500)
D:/10-28/wechatbot-main/handlers/user_msg_handler.go:46 +0x1bd
github.com/poorjobless/wechatbot/handlers.(*UserMessageHandler).handle(0x4eeea0?, 0xc00009b410?)
D:/10-28/wechatbot-main/handlers/user_msg_handler.go:21 +0x25
github.com/poorjobless/wechatbot/handlers.Handler(0xc000208500)
D:/10-28/wechatbot-main/handlers/handler.go:54 +0x282
github.com/eatmoreapple/openwechat.(*Bot).syncCheck(0xc0000ec1c0)
C:/Users/linweiliang02/go/pkg/mod/github.com/eatmoreapple/[email protected]/bot.go:269 +0x227
github.com/eatmoreapple/openwechat.(*Bot).WebInit.func1()
C:/Users/linweiliang02/go/pkg/mod/github.com/eatmoreapple/[email protected]/bot.go:190 +0x79
created by github.com/eatmoreapple/openwechat.(*Bot).WebInit in goroutine 1
C:/Users/linweiliang02/go/pkg/mod/github.com/eatmoreapple/[email protected]/bot.go:185 +0x276

这个错误是什么原因

2023/03/04 04:30:08 Received User ~大兵 Text Msg : 你的名字是?出生日期是?
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x28 pc=0x55c0a9]

goroutine 34 [running]:
github.com/poorjobless/wechatbot/chatgpt.Completions({0xc0000bdcb0, 0x24})
F:/wechatbot-main/chatgpt/chatgpt.go:22 +0x149
github.com/poorjobless/wechatbot/handlers.(*UserMessageHandler).ReplyText(0x3a?, 0xc0002c0500)
F:/wechatbot-main/handlers/user_msg_handler.go:39 +0xf1
github.com/poorjobless/wechatbot/handlers.(*UserMessageHandler).handle(0x58cb20?, 0xc00008ad80?)
F:/wechatbot-main/handlers/user_msg_handler.go:20 +0x2a
github.com/poorjobless/wechatbot/handlers.Handler(0xc0002c0500)
F:/wechatbot-main/handlers/handler.go:53 +0x21a
github.com/eatmoreapple/openwechat.(*Bot).syncCheck(0xc000112000)
C:/Users/llelle/go/pkg/mod/github.com/eatmoreapple/[email protected]/bot.go:291 +0x1d8
github.com/eatmoreapple/openwechat.(*Bot).WebInit.func1()
C:/Users/llelle/go/pkg/mod/github.com/eatmoreapple/[email protected]/bot.go:243 +0x7d
sync.(*Once).doSlow(0x0?, 0x0?)
C:/Program Files/Go/src/sync/once.go:74 +0xc2
sync.(*Once).Do(0x0?, 0x0?)
C:/Program Files/Go/src/sync/once.go:65 +0x1f
created by github.com/eatmoreapple/openwechat.(*Bot).WebInit
C:/Users/llelle/go/pkg/mod/github.com/eatmoreapple/[email protected]/bot.go:238 +0x1cd
exit status 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.