mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-03 00:12:42 +08:00
This commit is contained in:
parent
ba400854b0
commit
69c18db9ed
@ -54,3 +54,38 @@ func chatGPTContinueWriteBlocks(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
ret.Data = model.ChatGPTContinueWriteBlocks(ids)
|
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)
|
||||||
|
}
|
||||||
|
@ -330,4 +330,6 @@ func ServeAPI(ginServer *gin.Engine) {
|
|||||||
|
|
||||||
ginServer.Handle("POST", "/api/ai/chatGPT", model.CheckAuth, chatGPT)
|
ginServer.Handle("POST", "/api/ai/chatGPT", model.CheckAuth, chatGPT)
|
||||||
ginServer.Handle("POST", "/api/ai/chatGPTContinueWriteBlocks", model.CheckAuth, chatGPTContinueWriteBlocks)
|
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)
|
||||||
}
|
}
|
||||||
|
@ -23,20 +23,32 @@ import (
|
|||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"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) {
|
func ChatGPTContinueWriteBlocks(ids []string) (ret string) {
|
||||||
if !isOpenAIAPIEnabled() {
|
if !isOpenAIAPIEnabled() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlBlocks := sql.GetBlocks(ids)
|
msg := getBlocksContent(ids)
|
||||||
|
|
||||||
buf := bytes.Buffer{}
|
|
||||||
for _, sqlBlock := range sqlBlocks {
|
|
||||||
buf.WriteString(sqlBlock.Content)
|
|
||||||
buf.WriteString("\n\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := buf.String()
|
|
||||||
ret, _ = util.ChatGPTContinueWrite(msg, nil)
|
ret, _ = util.ChatGPTContinueWrite(msg, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -56,3 +68,14 @@ func isOpenAIAPIEnabled() bool {
|
|||||||
}
|
}
|
||||||
return true
|
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()
|
||||||
|
}
|
||||||
|
@ -45,6 +45,18 @@ func ChatGPT(msg string) (ret string) {
|
|||||||
return
|
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) {
|
func ChatGPTContinueWrite(msg string, contextMsgs []string) (ret string, retContextMsgs []string) {
|
||||||
if "" == OpenAIAPIKey {
|
if "" == OpenAIAPIKey {
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user