Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: 块级加入 `人工智能` https://github.com/siyuan-note/siyuan/issues/7566

Liang Ding69c18db9ed79
diff --git a/kernel/api/ai.go b/kernel/api/ai.go
index 2aa5b10055..13717e233e 100644
--- a/kernel/api/ai.go
+++ b/kernel/api/ai.go
@@ -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)
+}
diff --git a/kernel/api/router.go b/kernel/api/router.go
index 2030ed49cf..de415a5157 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -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)
}
diff --git a/kernel/model/ai.go b/kernel/model/ai.go
index de3f1872a4..401e690140 100644
--- a/kernel/model/ai.go
+++ b/kernel/model/ai.go
@@ -23,20 +23,32 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
-func ChatGPTContinueWriteBlocks(ids []string) (ret string) {
+func ChatGPTSummary(ids []string) (ret string) {
if !isOpenAIAPIEnabled() {
return
}
- sqlBlocks := sql.GetBlocks(ids)
+ msg := getBlocksContent(ids)
+ ret = util.ChatGPTSummary(msg, Conf.Lang)
+ return
+}
- buf := bytes.Buffer{}
- for _, sqlBlock := range sqlBlocks {
- buf.WriteString(sqlBlock.Content)
- buf.WriteString("\n\n")
+func ChatGPTTranslate(ids []string, lang string) (ret string) {
+ if !isOpenAIAPIEnabled() {
+ return
}
- msg := buf.String()
+ msg := getBlocksContent(ids)
+ ret = util.ChatGPTTranslate(msg, lang)
+ return
+}
+
+func ChatGPTContinueWriteBlocks(ids []string) (ret string) {
+ if !isOpenAIAPIEnabled() {
+ return
+ }
+
+ 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()
+}
diff --git a/kernel/util/openai.go b/kernel/util/openai.go
index db71654c4d..ca98d6528a 100644
--- a/kernel/util/openai.go
+++ b/kernel/util/openai.go
@@ -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