This commit is contained in:
Liang Ding 2023-03-05 11:49:57 +08:00
parent ba400854b0
commit 69c18db9ed
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
4 changed files with 81 additions and 9 deletions

View file

@ -54,3 +54,38 @@ func chatGPTContinueWriteBlocks(c *gin.Context) {
}
ret.Data = model.ChatGPTContinueWriteBlocks(ids)
}
func chatGPTTranslate(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
arg, ok := util.JsonArg(c, ret)
if !ok {
return
}
idsArg := arg["ids"].([]interface{})
var ids []string
for _, id := range idsArg {
ids = append(ids, id.(string))
}
lang := arg["lang"].(string)
ret.Data = model.ChatGPTTranslate(ids, lang)
}
func chatGPTSummary(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
arg, ok := util.JsonArg(c, ret)
if !ok {
return
}
idsArg := arg["ids"].([]interface{})
var ids []string
for _, id := range idsArg {
ids = append(ids, id.(string))
}
ret.Data = model.ChatGPTSummary(ids)
}

View file

@ -330,4 +330,6 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/ai/chatGPT", model.CheckAuth, chatGPT)
ginServer.Handle("POST", "/api/ai/chatGPTContinueWriteBlocks", model.CheckAuth, chatGPTContinueWriteBlocks)
ginServer.Handle("POST", "/api/ai/chatGPTTranslate", model.CheckAuth, chatGPTTranslate)
ginServer.Handle("POST", "/api/ai/chatGPTSummary", model.CheckAuth, chatGPTSummary)
}

View file

@ -23,20 +23,32 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
func ChatGPTSummary(ids []string) (ret string) {
if !isOpenAIAPIEnabled() {
return
}
msg := getBlocksContent(ids)
ret = util.ChatGPTSummary(msg, Conf.Lang)
return
}
func ChatGPTTranslate(ids []string, lang string) (ret string) {
if !isOpenAIAPIEnabled() {
return
}
msg := getBlocksContent(ids)
ret = util.ChatGPTTranslate(msg, lang)
return
}
func ChatGPTContinueWriteBlocks(ids []string) (ret string) {
if !isOpenAIAPIEnabled() {
return
}
sqlBlocks := sql.GetBlocks(ids)
buf := bytes.Buffer{}
for _, sqlBlock := range sqlBlocks {
buf.WriteString(sqlBlock.Content)
buf.WriteString("\n\n")
}
msg := buf.String()
msg := getBlocksContent(ids)
ret, _ = util.ChatGPTContinueWrite(msg, nil)
return
}
@ -56,3 +68,14 @@ func isOpenAIAPIEnabled() bool {
}
return true
}
func getBlocksContent(ids []string) string {
sqlBlocks := sql.GetBlocks(ids)
buf := bytes.Buffer{}
for _, sqlBlock := range sqlBlocks {
buf.WriteString(sqlBlock.Content)
buf.WriteString("\n\n")
}
return buf.String()
}

View file

@ -45,6 +45,18 @@ func ChatGPT(msg string) (ret string) {
return
}
func ChatGPTTranslate(msg string, lang string) (ret string) {
msg = "Translate to " + lang + ":\n" + msg
ret, _ = ChatGPTContinueWrite(msg, nil)
return
}
func ChatGPTSummary(msg string, lang string) (ret string) {
msg = "Summarized as follows, the result is in {" + lang + "}:\n" + msg
ret, _ = ChatGPTContinueWrite(msg, nil)
return
}
func ChatGPTContinueWrite(msg string, contextMsgs []string) (ret string, retContextMsgs []string) {
if "" == OpenAIAPIKey {
return