接续上一篇,用 Go 打造本地 LLM 聊天机器人:整合 llm-go 与 go-llama.cpp,此篇开始建构前端与 API 接口
执行环境需求
• ✅ Go 1.20+
• ✅ C++ toolchain(macOS: Xcode Command Line Tools / Linux: g++)
• ✅ GGUF 格式模型(如:Gemma-2B.gguf, Llama2-7B.gguf)
• ✅ 可选:make 工具简化编译
一、专案整合架构更新
llama-chatbot-go/
├── cmd/
│ ├── main.go # CLI 主程式
│ └── main_web.go # 启动 HTTP Server
├── llm/
│ ├── model.go # 封装模型逻辑
│ └── chat.go # 管理 prompt 历史
├── web/
│ ├── handler/
│ │ └── chat.go # 处理 POST /api/chat
│ ├── routes/
│ │ └── router.go # gin 路由
│ └── ui/
│ └── index.html # Web 前端页面
├── config/
│ └── persona.yaml # 角色设定
├── go.mod
└── docs/ # Swagger 文档生成目录
二、Swagger 接口文档
go install github.com/swaggo/swag/cmd/swag@latest
go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files
新增註解至 web/routes/router.go:
// @title LLaMA ChatBot API
// @version 1.0
// @description 基于 go-llama.cpp 与 llm-go 的本地语言模型聊天机器人 API
// @host localhost:8080
// @BasePath /// @contact.name 开发者支援
// @contact.email dev@example.com
在 main_web.go 中加入 Swagger 路由:
import ("github.com/swaggo/gin-swagger""github.com/swaggo/files"_ "your_project/docs" // docs 是 swag init 成的目录
)r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
在专案根目录执行
swag init
浏览 http://localhost:8080/swagger/index.html 即可查看中文接口文档。
三、Web 前端介面(純 HTML)
在 web/ui/index.html 建立:
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><title>LLaMA Chat</title>
</head>
<body><h2>LLaMA 聊天介面</h2><div><textarea id="input" rows="3" cols="60" placeholder="輸入你的問題..."></textarea><br><button onclick="send()">发送</button></div><pre id="response"></pre><script>async function send() {const input = document.getElementById('input').value;const res = await fetch('/api/chat', {method: 'POST',headers: { 'Content-Type': 'application/json' },body: JSON.stringify({ message: input })});const data = await res.json();document.getElementById('response').innerText = data.response || JSON.stringify(data);}</script>
</body>
</html>
并在你的 main_web.go 中加入静态档案伺服:
r.Static("/ui", "./web/ui")
四、Swagger 集成步驟
改寫 main.go
以下是具 Swagger 支援的 main.go,加入 /chat 路由文檔與 /swagger/index.html 的瀏覽功能:
package mainimport ("net/http""github.com/gin-gonic/gin""github.com/go-skynet/go-llama.cpp"_ "llama-chat/docs" // swagger 文件註冊swaggerFiles "github.com/swaggo/files"ginSwagger "github.com/swaggo/gin-swagger"
)// ChatRequest 用户输入
type ChatRequest struct {Message string `json:"message" example:"你好,請介紹你自己"`
}// ChatResponse 模型回应
type ChatResponse struct {Reply string `json:"reply" example:"你好,我是部署在本地的语言模型。"`
}// @title Gemma LLM Chatbot API
// @version 1.0
// @description 使用 Llama.cpp 驱动的本地 LLM 聊天接口
// @host localhost:8080
// @BasePath /
func main() {r := gin.Default()model := llama.LoadModel("path/to/gemma-2b.gguf", llama.Params{Threads: 4})defer model.Close()// swagger 文档路由r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))// 聊天接口// @Summary 启动聊天// @Description 提交一段文字并取得回应// @Accept json// @Produce json// @Param message body ChatRequest true "用户输入"// @Success 200 {object} ChatResponse// @Router /chat [post]r.POST("/chat", func(c *gin.Context) {var req ChatRequestif err := c.ShouldBindJSON(&req); err != nil {c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})return}resp := model.Predict(llama.PredictOptions{Prompt: req.Message,MaxTokens: 128,Stream: false,})c.JSON(http.StatusOK, ChatResponse{Reply: resp.Content})})r.Run(":8080")
}
瀏覽與測試
打開你的瀏覽器進入:
http://localhost:8080/swagger/index.html
补充说明
• 所有 @XXX 是 Swagger 的注解标签。
• 如果你日后有多模型、多任务接口,可以用 @Tag 区分模组。
• 所有 example: 设定会在 Swagger UI 预填样例值,便于测试。
下一步将会
✅ Swagger + Swagger UI(开发者介面)
使用技术:Swagger YAML + Swagger UI
特征:
• 适合 API 测试、技术人员验证用
• 具备 POST /chat、GET /healthz 等接口说明
• 支援 request/response 模拟
✅ 美化版 Chat Web App(推荐给公开展示用)
使用技术::Vue.js / React + Tailwind + WebSocket Streaming
特征:
• 支援上下文显示
• 支援多轮对话
• 支援多角色选择与人格切换
• UI 类似 ChatGPT 界面
参考链接:
本项目 README
🧾 GitCode 開源項目地址:
👉 GitCode - 全球开发者的开源社区,开源代码托管平台
我是一位独立开发者,加入使用者社群,一起讨论私有化 LLM 与 RAG 架构实践,欢迎 Star、Fork、Issue 交流。