Skip to content

Commit 1922684

Browse files
committed
feat:新增调用DALL-E模型的generation功能
1 parent 86e7995 commit 1922684

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ private void response(@NotNull MessageEvent event, ChatBO chatBO, String prompt)
7373
String response;
7474
try {
7575
chatBO.setPrompt(prompt);
76+
chatBO.setAiDraw(prompt.startsWith(keywordConfig.getImageGeneration()));
7677
response = interactService.chat(chatBO);
7778
}catch (ChatException e){
7879
response = e.getMessage();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.ashin.handler;
22

33
import com.ashin.config.KeywordConfig;
4-
import com.ashin.config.WechatConfig;
54
import com.ashin.entity.bo.ChatBO;
65
import com.ashin.exception.ChatException;
76
import com.ashin.service.InteractService;
@@ -53,6 +52,7 @@ private String textResponse(String userName, String content) {
5352
ChatBO chatBO = new ChatBO();
5453
chatBO.setPrompt(content);
5554
chatBO.setSessionId(userName);
55+
chatBO.setAiDraw(content.startsWith(keywordConfig.getImageGeneration()));
5656
String response;
5757
try {
5858
response = interactService.chat(chatBO);

src/main/java/com/ashin/service/impl/InteractServiceImpl.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.ashin.service.impl;
22

3+
import com.ashin.config.KeywordConfig;
34
import com.ashin.entity.bo.ChatBO;
45
import com.ashin.exception.ChatException;
56
import com.ashin.service.InteractService;
67
import com.ashin.util.BotUtil;
78
import com.theokanning.openai.OpenAiHttpException;
89
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
910
import com.theokanning.openai.completion.chat.ChatMessage;
11+
import com.theokanning.openai.image.CreateImageRequest;
1012
import lombok.extern.slf4j.Slf4j;
1113
import org.springframework.stereotype.Service;
1214

@@ -25,34 +27,44 @@ public class InteractServiceImpl implements InteractService {
2527

2628
@Resource
2729
private BotUtil botUtil;
30+
@Resource
31+
private KeywordConfig keywordConfig;
2832

2933
@Override
3034
public String chat(ChatBO chatBO) throws ChatException {
3135

3236
List<ChatMessage> prompt = botUtil.buildPrompt(chatBO.getSessionId(), chatBO.getPrompt());
33-
if (prompt == null){
37+
if (prompt == null) {
3438
throw new ChatException("提问失败: 提问内容过长");
3539
}
3640

37-
//向gpt提问
38-
ChatCompletionRequest completionRequest = botUtil.getCompletionRequestBuilder().messages(prompt).build();
3941
ChatMessage answer = null;
4042
try {
41-
answer = botUtil.getOpenAiService().createChatCompletion(completionRequest).getChoices().get(0).getMessage();
42-
}catch (OpenAiHttpException e){
43+
//向gpt提问
44+
if (chatBO.isAiDraw()) {
45+
String description = chatBO.getPrompt().replaceFirst(keywordConfig.getImageGeneration() + " ", "");
46+
CreateImageRequest createImageRequest = CreateImageRequest.builder().n(1).size("1024x1024").prompt(description).build();
47+
answer = new ChatMessage();
48+
answer.setRole("assistant");
49+
answer.setContent(botUtil.getOpenAiService().createImage(createImageRequest).getData().get(0).getUrl());
50+
} else {
51+
ChatCompletionRequest completionRequest = botUtil.getCompletionRequestBuilder().messages(prompt).build();
52+
answer = botUtil.getOpenAiService().createChatCompletion(completionRequest).getChoices().get(0).getMessage();
53+
}
54+
} catch (OpenAiHttpException e) {
4355
log.error("向gpt提问失败,提问内容:{},\n原因:{}\n", chatBO.getPrompt(), e.getMessage(), e);
4456
// https://platform.openai.com/docs/guides/error-codes/api-errors
45-
if (401 == e.statusCode){
57+
if (401 == e.statusCode) {
4658
throw new ChatException("提问失败: 无效的apikey, 请确保apikey正确且你拥有权限");
47-
}else if(429 == e.statusCode){
59+
} else if (429 == e.statusCode) {
4860
throw new ChatException("提问过于频繁 或者 apikey余额不足");
49-
}else if(500 == e.statusCode) {
61+
} else if (500 == e.statusCode) {
5062
throw new ChatException("提问失败: openai服务异常, 请稍后重试");
51-
}else if(503 == e.statusCode) {
63+
} else if (503 == e.statusCode) {
5264
throw new ChatException("提问失败: openai服务过载, 请稍后重试");
5365
}
5466
}
55-
if (null == answer){
67+
if (null == answer) {
5668
throw new ChatException("提问失败: 未知错误, 若频繁出现请提issue");
5769
}
5870

0 commit comments

Comments
 (0)