Skip to content

Commit 63ee01a

Browse files
Merge pull request #75 from ashinnotfound/feat/image
Feat/image
2 parents 27cf436 + 306cf0b commit 63ee01a

File tree

15 files changed

+183
-59
lines changed

15 files changed

+183
-59
lines changed

README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# ChatGPT-YourChatRobot
22

3-
> ### NEWS: 正在尝试接入OPENAI的ai画图功能([DALL·E模型](https://platform.openai.com/docs/models/dall-e))
3+
> ### NEWS: 已接入OPENAI的ai画图功能([DALL·E模型](https://platform.openai.com/docs/models/dall-e))
44
> - 无需为此功能额外配置apikey,使用原先的即可
5-
> - 可先在feat/image分支或release里的beta版试用
6-
> - --- 2023.8.10
7-
>
8-
> <img src="https://cdn.jsdelivr.net/gh/ashinnotfound/ImageHosting/img/f5cb7fb04594b894edf8d614ca3fe5f.jpg" style="zoom:25%;" /><img src="https://cdn.jsdelivr.net/gh/ashinnotfound/ImageHosting/img/28894abd84617a4efca4ddf9c3abc99.jpg" style="zoom:25%;" />
5+
> - 默认指令 “ai画图” 可在配置选项修改,亦可修改返回图片方式(限qq机器人)
6+
> ![](https://cdn.jsdelivr.net/gh/ashinnotfound/ImageHosting/img/%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202023-11-10%20133117.png)![](https://cdn.jsdelivr.net/gh/ashinnotfound/ImageHosting/img/%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202023-11-10%20132601.png)
7+
> --- 2023.11.10
98
109
## 简介
1110

@@ -29,6 +28,8 @@ qq机器人实现基于[TheoKanning/openai-java](https://github.com/TheoKanning/
2928

3029
使用mirai/itchat登录qq/微信并监听消息->调用openai接口将消息向gpt提问->使用mirai/itchat在qq/微信里回复gpt的回答
3130

31+
其中ai画图采用[DALL·E模型](https://platform.openai.com/docs/models/dall-e)[generation方法](https://platform.openai.com/docs/guides/images/generations)
32+
3233
## 使用
3334

3435
❤❤❤ 开箱即用!!! ❤❤❤
@@ -48,7 +49,6 @@ qq机器人实现基于[TheoKanning/openai-java](https://github.com/TheoKanning/
4849
并把它们配置在application.yml里:
4950

5051
```
51-
//这是application.yml文件
5252
proxy:
5353
# 代理配置
5454
# 国内墙了gpt的api,所以得用代理,一般你使用的代理软件会有相关信息,例子:
@@ -65,6 +65,8 @@ gpt:
6565
maxToken: 2048
6666
# 信息熵 越高回答越随机(Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.---via OPENAI)
6767
temperature: 0.5
68+
# 最大请求时间 超时自动中断请求
69+
ofSeconds: 10000
6870
# 基础提问 支持多个提问 可用来设定人格(对应api中的system角色)
6971
basicPrompt:
7072
- "用中文回答我的问题"
@@ -75,25 +77,31 @@ gpt:
7577
qq:
7678
# 是否使用qq true/false
7779
enable: true
78-
# 登陆方法:1.密码登录 2.扫码登录(推荐)
79-
method: 2
80-
# qq账号密码(扫码登录则无需填写账号密码)
80+
# 登陆方法:true扫码登录(推荐) false密码登录
81+
loginByQRCode: true
82+
# qq账号密码
8183
account:
84+
# (扫码登录则无需填写密码)
8285
password:
8386
# 是否自动同意好友申请
8487
acceptNewFriend: false
8588
# 是否自动同意被邀请入群
8689
acceptNewGroup: false
87-
# 重置会话指令
88-
resetWord: "重置会话"
90+
# ai画图时返回方法: true链接(更快) false图片
91+
returnDrawByURL: true
8992
9093
wechat:
9194
# 是否使用微信 true/false
9295
enable: false
9396
# 生成的登录二维码路径 默认与项目同级
9497
qrPath: "./"
98+
99+
keyword:
95100
# 重置会话指令
96-
resetWord: "重置会话"
101+
reset: "重置会话"
102+
# ai画图指令(DALL·E模型 https://platform.openai.com/docs/models/dall-e)
103+
# generation 根据关键词生成图片(https://platform.openai.com/docs/guides/images/generations)
104+
draw: "ai画图"
97105
```
98106

99107
3. 然后 run!!!😁😁😁
@@ -130,7 +138,11 @@ tips:机器人响应速度与你的网络环境挂钩。
130138
<details>
131139

132140
<summary></summary>
133-
141+
142+
### v3.8 (NOV 10, 2023)
143+
- 把之前写的([DALL·E模型](https://platform.openai.com/docs/models/dall-e))接入完善了,现在可以在聊天中直接调用其进行ai画图
144+
- qq机器人基于[TheoKanning/openai-java](https://github.com/TheoKanning/openai-java)[mamoe/mirai](https://github.com/mamoe/mirai.git)
145+
- 微信机器人基于[TheoKanning/openai-java](https://github.com/TheoKanning/openai-java)[wxmbaci/itchat4j-uos](https://github.com/wxmbaci/itchat4j-uos)
134146
### v3.7 (Aug 8, 2023)
135147
- 最近有空能闲下来看看这个项目,主要更新了项目依赖、优化了下代码结构、增加了token消耗的计算、优化了bot交互返回信息
136148
- qq机器人基于[TheoKanning/openai-java](https://github.com/TheoKanning/openai-java)[mamoe/mirai](https://github.com/mamoe/mirai.git)

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>com.ashin</groupId>
1212
<artifactId>ChatGPT-YourChatRobot</artifactId>
13-
<version>3.7</version>
13+
<version>3.8</version>
1414
<name>myGPT</name>
1515
<description>快来把你的qq或微信变为chatgpt</description>
1616
<properties>
@@ -36,15 +36,15 @@
3636
<dependency>
3737
<groupId>net.mamoe</groupId>
3838
<artifactId>mirai-core-jvm</artifactId>
39-
<version>2.15.0</version>
39+
<version>2.16.0</version>
4040
</dependency>
41-
<!-- 临时修复qq登录组件 -->
41+
<!-- 临时修复qq登录组件 https://github.com/cssxsh/fix-protocol-version.git -->
4242
<dependency>
4343
<groupId>mirai</groupId>
4444
<artifactId>fix-protocol</artifactId>
45-
<version>1.9.11</version>
45+
<version>1.12.0</version>
4646
<scope>system</scope>
47-
<systemPath>${pom.basedir}/src/main/resources/fix-protocol-version-1.9.11.mirai2.jar</systemPath>
47+
<systemPath>${pom.basedir}/src/main/resources/fix-protocol-version-1.12.0.mirai2.jar</systemPath>
4848
</dependency>
4949
<dependency>
5050
<groupId>org.asynchttpclient</groupId>
@@ -63,7 +63,7 @@
6363
<dependency>
6464
<groupId>com.theokanning.openai-gpt3-java</groupId>
6565
<artifactId>service</artifactId>
66-
<version>0.15.0</version>
66+
<version>0.16.1</version>
6767
</dependency>
6868
<dependency>
6969
<groupId>com.knuddels</groupId>

src/main/java/com/ashin/client/GptClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void init() {
3939
for (String apiKey : gptConfig.getApiKey()) {
4040
apiKey = apiKey.trim();
4141
if (!apiKey.isEmpty()) {
42-
openAiServiceList.add(new OpenAiService(apiKey, Duration.ofSeconds(1000)));
42+
openAiServiceList.add(new OpenAiService(apiKey, Duration.ofSeconds(gptConfig.getOfSeconds())));
4343
log.info("apiKey为 {} 的账号初始化成功", apiKey);
4444
}
4545
}

src/main/java/com/ashin/client/QqBotClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ public void init() {
3232
//登录 登陆协议有ANDROID_PHONE, ANDROID_PAD, ANDROID_WATCH, IPAD, MACOS
3333
try {
3434
log.info("正在登录qq,请按提示操作:");
35-
if (qqConfig.getMethod() == 1) {
35+
if (qqConfig.getLoginByQRCode()) {
36+
//扫码登陆
37+
qqBot = BotFactory.INSTANCE.newBot(qqConfig.getAccount(), BotAuthorization.byQRCode(), configuration -> configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_WATCH));
38+
} else {
3639
//密码登录
3740
qqBot = BotFactory.INSTANCE.newBot(qqConfig.getAccount(), qqConfig.getPassword().trim(), new BotConfiguration() {{
3841
setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PAD);
3942
}});
4043
//使用临时修复插件
4144
FixProtocolVersion.update();
42-
} else {
43-
//扫码登陆
44-
qqBot = BotFactory.INSTANCE.newBot(qqConfig.getAccount(), BotAuthorization.byQRCode(), configuration -> configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_WATCH));
4545
}
4646

4747
qqBot.login();
48-
log.info("成功登录账号为 {} 的qq, 登陆方式为 {}", qqConfig.getAccount(), qqConfig.getMethod() == 1 ? "密码登录" : "扫码登陆");
48+
log.info("成功登录账号为 {} 的qq, 登陆方式为 {}", qqConfig.getAccount(), qqConfig.getLoginByQRCode() ? "扫码登陆" : "密码登录");
4949
//订阅监听事件
5050
qqBot.getEventChannel().registerListenerHost(qqMessageHandler);
5151
} catch (Exception e) {
52-
log.error("登陆失败,qq账号为 {}, 登陆方式为 {} ,原因:{}", qqConfig.getAccount(), qqConfig.getMethod() == 1 ? "密码登录" : "扫码登陆", e.getMessage());
52+
log.error("登陆失败,qq账号为 {}, 登陆方式为 {} ,原因:{}", qqConfig.getAccount(), qqConfig.getLoginByQRCode() ? "扫码登陆" : "密码登录", e.getMessage());
5353
}
5454
}
5555
}

src/main/java/com/ashin/config/GptConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ public class GptConfig {
2121
private Double temperature;
2222
private List<String> basicPrompt;
2323
private List<String> apiKey;
24+
private Long ofSeconds;
2425
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.ashin.config;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.stereotype.Component;
6+
7+
/**
8+
* 关键字配置
9+
*
10+
* @author ashinnotfound
11+
* @date 2023/08/10
12+
*/
13+
@Data
14+
@Component
15+
@ConfigurationProperties("keyword")
16+
public class KeywordConfig {
17+
private String reset;
18+
private String draw;
19+
}

src/main/java/com/ashin/config/QqConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
@ConfigurationProperties("qq")
1616
public class QqConfig {
1717
private Boolean enable;
18-
private Integer method;
18+
private Boolean loginByQRCode;
1919
private Long account;
2020
private String password;
2121
private Boolean acceptNewFriend;
2222
private Boolean acceptNewGroup;
23-
private String resetWord;
23+
private Boolean returnDrawByURL;
2424
}

src/main/java/com/ashin/config/WechatConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@
1616
public class WechatConfig {
1717
private Boolean enable;
1818
private String qrPath;
19-
private String resetWord;
2019
}

src/main/java/com/ashin/entity/bo/ChatBO.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ public class ChatBO {
1818
* 问题
1919
*/
2020
private String prompt;
21+
/**
22+
* 是否ai画图功能
23+
*/
24+
private boolean isAiDraw;
2125
}

src/main/java/com/ashin/handler/QqMessageHandler.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package com.ashin.handler;
22

3+
import com.ashin.config.KeywordConfig;
34
import com.ashin.config.QqConfig;
45
import com.ashin.entity.bo.ChatBO;
56
import com.ashin.exception.ChatException;
67
import com.ashin.service.InteractService;
78
import com.ashin.util.BotUtil;
9+
import com.ashin.util.ImageUtil;
10+
import lombok.extern.slf4j.Slf4j;
11+
import net.mamoe.mirai.contact.Contact;
812
import net.mamoe.mirai.contact.MessageTooLargeException;
913
import net.mamoe.mirai.event.EventHandler;
1014
import net.mamoe.mirai.event.ListenerHost;
1115
import net.mamoe.mirai.event.events.*;
12-
import net.mamoe.mirai.message.data.At;
13-
import net.mamoe.mirai.message.data.MessageChain;
14-
import net.mamoe.mirai.message.data.MessageChainBuilder;
15-
import net.mamoe.mirai.message.data.QuoteReply;
16+
import net.mamoe.mirai.message.data.*;
1617
import org.jetbrains.annotations.NotNull;
1718
import org.springframework.stereotype.Component;
1819
import javax.annotation.Resource;
20+
import java.io.File;
1921

2022
/**
2123
* QQ消息处理程序
@@ -24,12 +26,15 @@
2426
* @date 2023/2/1
2527
*/
2628
@Component
29+
@Slf4j
2730
public class QqMessageHandler implements ListenerHost {
2831
@Resource
2932
private InteractService interactService;
3033
@Resource
3134
private QqConfig qqConfig;
3235
@Resource
36+
private KeywordConfig keywordConfig;
37+
@Resource
3338
private BotUtil botUtil;
3439

3540
/**
@@ -62,24 +67,33 @@ public void onGroupMessageEvent(GroupMessageEvent event){
6267
}
6368
}
6469
private void response(@NotNull MessageEvent event, ChatBO chatBO, String prompt) {
65-
if (qqConfig.getResetWord().equals(prompt)) {
70+
if (keywordConfig.getReset().equals(prompt)) {
6671
//检测到重置会话指令
6772
botUtil.resetPrompt(chatBO.getSessionId());
6873
event.getSubject().sendMessage("重置会话成功");
6974
} else {
7075
String response;
7176
try {
7277
chatBO.setPrompt(prompt);
78+
chatBO.setAiDraw(prompt.startsWith(keywordConfig.getDraw()));
7379
response = interactService.chat(chatBO);
7480
}catch (ChatException e){
7581
response = e.getMessage();
7682
}
7783
try {
78-
MessageChain messages = new MessageChainBuilder()
79-
.append(new QuoteReply(event.getMessage()))
80-
.append(response)
81-
.build();
82-
event.getSubject().sendMessage(messages);
84+
if (chatBO.isAiDraw() && !qqConfig.getReturnDrawByURL()){
85+
File file = ImageUtil.download(response);
86+
Contact.sendImage(event.getSubject(), file);
87+
if (!file.delete()){
88+
log.warn("图片({})删除失败, 请注意存储空间", file.getAbsolutePath());
89+
}
90+
}else {
91+
MessageChain messages = new MessageChainBuilder()
92+
.append(new QuoteReply(event.getMessage()))
93+
.append(response)
94+
.build();
95+
event.getSubject().sendMessage(messages);
96+
}
8397
}catch (MessageTooLargeException e){
8498
//信息太大,无法引用,采用直接回复
8599
event.getSubject().sendMessage(response);

0 commit comments

Comments
 (0)