-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
345c2a2
commit 84cc59e
Showing
12 changed files
with
266 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
zhiliao-api/src/main/java/com/lin/dao/mapper/OpenaiAnswersMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.lin.dao.mapper; | ||
|
||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | ||
import com.lin.dao.pojo.OpenaiAnswers; | ||
|
||
public interface OpenaiAnswersMapper extends BaseMapper<OpenaiAnswers> { | ||
} |
12 changes: 12 additions & 0 deletions
12
zhiliao-api/src/main/java/com/lin/dao/pojo/OpenaiAnswers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.lin.dao.pojo; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class OpenaiAnswers { | ||
private Long id; | ||
|
||
private String articleId; | ||
|
||
private String answer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.lin.service; | ||
|
||
import java.io.IOException; | ||
|
||
public interface OpenAIService { | ||
String doChatGPT(String question) throws IOException; | ||
|
||
void generateAnswers(String content, Long id, Boolean isEdit) throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
zhiliao-api/src/main/java/com/lin/service/impl/OpenAIServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.lin.service.impl; | ||
|
||
|
||
import com.alibaba.fastjson.JSON; | ||
import com.lin.dao.mapper.OpenaiAnswersMapper; | ||
import com.lin.dao.pojo.OpenaiAnswers; | ||
import com.lin.service.OpenAIService; | ||
import com.lin.vo.ai.AIAnswer; | ||
import com.lin.vo.ai.Choices; | ||
import org.apache.http.HttpStatus; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.entity.ContentType; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.apache.http.util.EntityUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.annotation.Resource; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Service | ||
@Async("taskExecutor") | ||
public class OpenAIServiceImpl implements OpenAIService { | ||
|
||
|
||
@Resource | ||
OpenaiAnswersMapper openaiAnswersMapper; | ||
|
||
private Logger logger = LoggerFactory.getLogger(OpenAIServiceImpl.class); | ||
|
||
// @Value("${chatbot-api.openAiKey}") | ||
// private String openAiKey; | ||
|
||
@Override | ||
public String doChatGPT(String question) throws IOException { | ||
String openAiKey="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"; | ||
|
||
CloseableHttpClient httpClient = HttpClientBuilder.create().build(); | ||
|
||
HttpPost post = new HttpPost("https://api.openai.com/v1/completions"); | ||
post.addHeader("Content-Type", "application/json"); | ||
post.addHeader("Authorization", "Bearer " + openAiKey); | ||
|
||
String paramJson = "{\"model\": \"text-davinci-003\", \"prompt\": \"" + question + "\", \"temperature\": 0, \"max_tokens\": 1024}"; | ||
|
||
StringEntity stringEntity = new StringEntity(paramJson, ContentType.create("text/json", "UTF-8")); | ||
post.setEntity(stringEntity); | ||
|
||
CloseableHttpResponse response = httpClient.execute(post); | ||
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { | ||
String jsonStr = EntityUtils.toString(response.getEntity()); | ||
AIAnswer aiAnswer = JSON.parseObject(jsonStr, AIAnswer.class); | ||
StringBuilder answers = new StringBuilder(); | ||
List<Choices> choices = aiAnswer.getChoices(); | ||
for (Choices choice : choices) { | ||
answers.append(choice.getText()); | ||
} | ||
|
||
return answers.toString(); | ||
} else { | ||
logger.error("api.openai.com Err Code is " + response.getStatusLine().getStatusCode()); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* 保存ChatGPT数据 | ||
* @param content 文本 | ||
* @param id 文章id | ||
* @param isEdit 是否修改 | ||
* @throws IOException | ||
*/ | ||
|
||
@Override | ||
public void generateAnswers(String content, Long id, Boolean isEdit) throws IOException { | ||
OpenaiAnswers openaiAnswers = new OpenaiAnswers(); | ||
//更新 | ||
if(isEdit){ | ||
OpenaiAnswers openaiAnswersRest = openaiAnswersMapper.selectById(id); | ||
openaiAnswers.setId(openaiAnswersRest.getId()); | ||
} | ||
//新增 | ||
openaiAnswers.setArticleId(String.valueOf(id)); | ||
String s = this.doChatGPT(content); | ||
if(s!=null){ | ||
openaiAnswers.setAnswer(s); | ||
openaiAnswersMapper.insert(openaiAnswers); | ||
} | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,8 @@ public class ArticleVo { | |
|
||
private CategoryVo category; | ||
|
||
/** | ||
* ai 放回值 | ||
*/ | ||
private String answer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.lin.vo.ai; | ||
|
||
|
||
import java.util.List; | ||
|
||
public class AIAnswer { | ||
|
||
private String id; | ||
|
||
private String object; | ||
|
||
private int created; | ||
|
||
private String model; | ||
|
||
private List<Choices> choices; | ||
|
||
public void setId(String id){ | ||
this.id = id; | ||
} | ||
public String getId(){ | ||
return this.id; | ||
} | ||
public void setObject(String object){ | ||
this.object = object; | ||
} | ||
public String getObject(){ | ||
return this.object; | ||
} | ||
public void setCreated(int created){ | ||
this.created = created; | ||
} | ||
public int getCreated(){ | ||
return this.created; | ||
} | ||
public void setModel(String model){ | ||
this.model = model; | ||
} | ||
public String getModel(){ | ||
return this.model; | ||
} | ||
public void setChoices(List<Choices> choices){ | ||
this.choices = choices; | ||
} | ||
public List<Choices> getChoices(){ | ||
return this.choices; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.lin.vo.ai; | ||
|
||
|
||
public class Choices { | ||
|
||
private String text; | ||
|
||
private int index; | ||
|
||
private String logprobs; | ||
|
||
private String finish_reason; | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public void setText(String text) { | ||
this.text = text; | ||
} | ||
|
||
public int getIndex() { | ||
return index; | ||
} | ||
|
||
public void setIndex(int index) { | ||
this.index = index; | ||
} | ||
|
||
public String getLogprobs() { | ||
return logprobs; | ||
} | ||
|
||
public void setLogprobs(String logprobs) { | ||
this.logprobs = logprobs; | ||
} | ||
|
||
public String getFinish_reason() { | ||
return finish_reason; | ||
} | ||
|
||
public void setFinish_reason(String finish_reason) { | ||
this.finish_reason = finish_reason; | ||
} | ||
} |
Oops, something went wrong.