Skip to content

Commit

Permalink
feat: ai摘要
Browse files Browse the repository at this point in the history
  • Loading branch information
Redish101 committed Feb 23, 2025
1 parent 0bffca1 commit e28ba79
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"makefile.configureOnOpen": false
}
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/swaggo/swag v1.16.4
github.com/yankeguo/zhipu v0.1.5
gitlab.com/golang-commonmark/markdown v0.0.0-20211110145824-bf3e522c626a
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/mysql v1.5.7
Expand All @@ -31,6 +32,7 @@ require (
github.com/go-openapi/swag v0.19.15 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-resty/resty/v2 v2.16.5 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
Expand All @@ -53,11 +55,11 @@ require (
gitlab.com/golang-commonmark/linkify v0.0.0-20200225224916-64bca66f6ad3 // indirect
gitlab.com/golang-commonmark/mdurl v0.0.0-20191124015652-932350d1cb84 // indirect
gitlab.com/golang-commonmark/puny v0.0.0-20191124015043-9f83538fa04f // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/crypto v0.33.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/sync v0.11.0 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gorm.io/datatypes v1.2.0 // indirect
Expand All @@ -77,7 +79,7 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.55.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/sys v0.30.0 // indirect
gorm.io/driver/sqlite v1.5.6
gorm.io/gorm v1.25.12
)
26 changes: 16 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA=
github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptdhTM=
github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
Expand Down Expand Up @@ -131,6 +133,8 @@ github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8
github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
github.com/yankeguo/zhipu v0.1.5 h1:OmReVbwo5rRFjeKHGA8GUafKwsNYPl07oX27yJX0No0=
github.com/yankeguo/zhipu v0.1.5/go.mod h1:QcOtbAmKMuA9ILlCoUwJlrQzoEWFh9vkn4dHx8uTHsc=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
Expand All @@ -149,8 +153,8 @@ gitlab.com/opennota/wd v0.0.0-20180912061657-c5d65f63c638 h1:uPZaMiz6Sz0PZs3IZJW
gitlab.com/opennota/wd v0.0.0-20180912061657-c5d65f63c638/go.mod h1:EGRJaqe2eO9XGmFtQCvV3Lm9NLico3UhFwUpCG/+mVU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
Expand All @@ -160,13 +164,13 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -179,8 +183,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand All @@ -191,8 +195,10 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
91 changes: 91 additions & 0 deletions internal/ai/summary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package ai

import (
"context"

"github.com/redish101/reblog/internal/core"
"github.com/redish101/reblog/internal/log"
"github.com/yankeguo/zhipu"
)

const prompt = `
【角色定义】
你是一位资深技术文档工程师,专注于为复杂的技术博客生成精准摘要。请严格基于原文内容生成专业摘要,保持技术严谨性。
【核心要求】
内容忠实度:
完全基于原文事实,禁止添加任何外部知识或个人见解
保留关键专业术语(如Transformer、CUDA核心、RAG架构等)
准确呈现技术方案的核心创新点
结构规范:
首段:总览句(20字内)点明文章核心价值
主体内容包含:
• 待解决的技术问题
• 采用的解决方案架构
• 关键算法/工具链(如使用PyTorch 2.1+TorchDynamo)
• 实验结果(量化指标需精确,如"QPS提升37.2%")
• 实际应用场景
语言风格:
专业而不晦涩,避免过度学术化表达
使用技术领域标准表达(如"微调"而非"调整","消融实验"而非"对比测试")
保持客观中立,区分作者主张和客观事实
格式规范:
纯文本输出,禁用任何Markdown格式、禁止换行
中文字数严格控制在280-320字
段落间自然过渡,保持技术逻辑连贯性
不要出现任何文章内没有出现的内容,包括类似“实验结果:无具体量化指标“
【典型示例】
[合格摘要]
"本文探讨PyTorch 2.1的编译优化技术。针对动态图执行效率问题,提出基于TorchDynamo的图捕获方案,配合TensorRT的后端优化,实现训练速度提升。核心创新包括:1)通过字节码分析实现Python动态特性兼容 2)选择性子图JIT编译策略 3)与CUDA 11.7的深度适配。在ResNet-152训练任务中,相较原生PyTorch获得37.2%的加速比,内存占用降低19%。该方案适用于需要兼顾开发灵活性和执行效率的DL训练场景。"
[不合格摘要]
"这篇文章讲PyTorch的优化方法,作者搞了个新的编译方案,速度提升明显,这对AI训练很有帮助。"(缺乏技术细节和量化指标)
**GN(Generate Ninja) 快速入门指南**\n\n本文介绍了 Google 开发的元构建系统 GN 的使用方法。GN 用于生成 build.ninja 文件,类似于 cmake。文章通过构建一个基于 glfw + glad 的 C++ 程序,详细讲解了 GN 的安装、配置编译工具链、声明构建配置以及构建过程。\n\n**待解决的技术问题**:GN 的使用方法和配置流程。\n\n**解决方案架构**:通过构建一个简单的 C++ 程序,展示 GN 的使用步骤。\n\n**关键算法/工具链**:GN、ninja、glfw、glad。\n\n**实验结果**:无具体量化指标。\n\n**实际应用场景**:GN 适用于规模较大的项目中,更为清晰的描述构建过程。(使用了markdown语法以及换行)
你需要牢记: 禁止使用任何markdown语法、禁止换行!
你需要牢记: 禁止使用任何markdown语法、禁止换行!
你需要牢记: 禁止使用任何markdown语法、禁止换行!
请严格遵循以上规范生成专业技术摘要,现在请处理以下技术博客内容:
`
func Summary(app *core.App, text string) string {
log.Debug("[AI] 正生成摘要")
client, err := zhipu.NewClient(zhipu.WithAPIKey(app.Config().Ai.ApiKey))

service := client.ChatCompletion("glm-4-flash").
AddMessage(zhipu.ChatCompletionMessage{
Role: "user",
Content: prompt + text,

})

res, err := service.Do(context.Background())

if err != nil {
zhipu.GetAPIErrorCode(err)
log.Warnf("[AI] 摘要生成失败: %s", err)
} else {
log.Debugf("[AI] 成功生成摘要: %s", res.Choices[0].Message.Content)
return res.Choices[0].Message.Content
}

return "生成摘要失败"
}
10 changes: 9 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ type DBConfig struct {
}

type RssConfig struct {
Limit int `yaml:"limit"`
Limit int `yaml:"limit"`
ContentLinkFormat string `yaml:"content_link_format"`
}

type AiConfig struct {
ApiKey string `yaml:"apikey"`
}

type Config struct {
Server ServerConfig `yaml:"server"`
DB DBConfig `yaml:"db"`
Rss RssConfig `yaml:"rss"`
Ai AiConfig `yaml:"ai"`
Plugins []string `yaml:"plugins"`
}

Expand Down Expand Up @@ -96,6 +101,9 @@ func DefaultConfig() *Config {
Rss: RssConfig{
Limit: 10,
},
Ai: AiConfig{
ApiKey: "",
},
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/model/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ type Article struct {
Cover *string `json:"cover"`
Content string `json:"content"`
Draft *bool `json:"draft"`
AiSummary *string `json:"ai_summary"`
}
6 changes: 5 additions & 1 deletion internal/query/articles.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/themekit/src/types/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export type Article = {

// 文章内容
content: string;

ai_summary?: string;
};

export type ArticleList = {
Expand Down
3 changes: 3 additions & 0 deletions reblog.example.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ rss:
limit: 10
content_link_format: "%s/article/%s"

ai:
apikey: your_api_key

plugins:
# - example/plugin

16 changes: 16 additions & 0 deletions server/handler/article_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package handler
import (
"net/http"

"github.com/redish101/reblog/internal/ai"
"github.com/redish101/reblog/internal/core"
"github.com/redish101/reblog/internal/log"
"github.com/redish101/reblog/internal/model"
"github.com/redish101/reblog/server/common"

Expand Down Expand Up @@ -64,6 +66,20 @@ func ArticleAdd(app *core.App, router fiber.Router) {
return common.RespServerError(c, err)
}

go func() {
aiSummary := ai.Summary(app, params.Content)

if aiSummary != "" {
article.AiSummary = &aiSummary
_, err = a.Where(a.Slug.Eq(article.Slug)).Updates(article)

if err != nil {
log.Error(err)
return
}
}
}()

return common.RespSuccess(c, "操作成功", nil)
}, common.Auth(app))
}
16 changes: 16 additions & 0 deletions server/handler/article_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package handler
import (
"net/http"

"github.com/redish101/reblog/internal/ai"
"github.com/redish101/reblog/internal/core"
"github.com/redish101/reblog/internal/log"
"github.com/redish101/reblog/internal/model"
"github.com/redish101/reblog/server/common"

Expand Down Expand Up @@ -61,6 +63,20 @@ func ArticleUpdate(app *core.App, router fiber.Router) {
return common.RespServerError(c, err)
}

go func() {
aiSummary := ai.Summary(app, params.Content)

if aiSummary != "" {
article.AiSummary = &aiSummary
_, err = a.Where(a.Slug.Eq(article.Slug)).Updates(article)

if err != nil {
log.Error(err)
return
}
}
}()

return common.RespSuccess(c, "更新成功", nil)
}, common.Auth(app))
}

0 comments on commit e28ba79

Please sign in to comment.