實戰: 使用 LangChainGo - Gin 實現流式 AI 問答系統

在本篇文章中,我們將使用 LangChainGo + Gin 框架,結合 Ollama 大語言模型,實現一個流式 AI 問答系統。最終,我們還會使用 curl 進行測試,由於是實戰例子所以我會提供一個簡單的 html+css+js 的前端來實時顯示 AI 的回答。

流式響應的意義

在傳統 API 調用中,我們往往要等到大語言模型(LLM) 計算完成後,才能返回完整的回答。這會導致:

流式響應可以:

後端:Gin+LangChainGo 實現流式響應

我們閒話少說,開始今天的正題,具體步驟如下所示:

1. 首先我們創建一個名爲 robot-go 的項目,並安裝其所需要的依賴

mkdir robot-go
cd robot-go/
go mod init github.com/xxx/robot-go
go get github.com/gin-gonic/gin
go get github.com/tmc/langchaingo@v0.1.13
go get github.com/tmc/langchaingo/llms@v0.1.13

2. 鍵入如下代碼

func chatHandler(c *gin.Context) {
    var request struct {
        Question string `json:"question"`
    }
    if err := c.ShouldBindJSON(&request); err != nil {
        log.Printf("Invalid request: %v", err)
        c.JSON(http.StatusBadRequest, gin.H{"error""invalid request"})
        return
    }
    if request.Question == "" {
        log.Print("Empty question received")
        c.JSON(http.StatusBadRequest, gin.H{"error""question cannot be empty"})
        return
    }
    ctx := context.Background()
    // 設置 SSE 頭部
    c.Writer.Header().Set("Content-Type""text/event-stream")
    c.Writer.Header().Set("Cache-Control""no-cache")
    c.Writer.Header().Set("Connection""keep-alive")
    c.Writer.Flush()
    content := []llms.MessageContent{
        llms.TextParts(llms.ChatMessageTypeHuman, request.Question),
    }
    // 調用流式 API
    _, err := llmClient.GenerateContent(ctx, content, llms.WithStreamingFunc(func(ctx context.Context, chunk []byte) error {
        fmt.Fprintf(c.Writer, "data: %s\n\n", string(chunk))
        c.Writer.Flush()
        return nil
    }))
    if err != nil {
        log.Printf("Failed to generate content: %v", err)
        c.JSON(http.StatusInternalServerError, gin.H{"error""failed to get response"})
        return
    }
    fmt.Fprintln(c.Writer, "data: [DONE]\n")
    c.Writer.Flush()
}

代碼解析

使用 curl 進行測試

我們先運行後端服務,具體命令如下所示:

go run main.go

然後,使用 curl 進行測試:

curl -X POST http://localhost:9527/api/chat \
     -H "Content-Type: application/json" \
     -d '{"question": "請介紹一下Go語言"}' \
     --no-buffer

⚠️注意: --no-buffer 讓 curl 立即顯示流式數據。

測試結果如下所示:

⚠️注意: 別忘記了在本地運行 ollama,這裏我使用的模型是 qwen2:7b!!!

前端實現

由於是一個簡單的例子所以就沒有用 react 框架來做,前端的效果如下所示:

總結

本篇文章,我們從後端實現前端流式渲染,完整實現了一個流式 AI 問答系統

✅ 使用 LangChainGo + Ollama 處理 LLM 調用

✅ Gin 提供 SSE(Server-Sent Events)流式 API

✅ curl 終端測試,逐步返回 AI 生成文本

✅ 簡單的使用了 html+css+js 實現前端實時顯示

🚀 完整代碼已開源,你可以嘗試改進擴展,比如:

實戰例子源碼地址: https://github.com/JXLSP/robot-go, 歡迎點個 star☺️,今天的內容就到此結束,感謝您的收看!!!🌹🌹🌹🦀🦀🦀

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/k29VYhXsg3RlCvtBuJCZFA