zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.
https://go-zero.dev
MIT License
29.38k stars 3.97k forks source link

how can i handle streaming response by gozero #3980

Closed qianjiangchao1992 closed 7 months ago

qianjiangchao1992 commented 8 months ago

我现在处理GPT流式返回,我用Gin可以实现,Demo代码如下:

r.GET("/stream", func(c *gin.Context) {
        chanStream := make(chan string, 100)
        c.Header("Content-Type", "text/event-stream; charset=utf-8")
        go http.GetData("三体是什么?请用100字介绍", chanStream)
        c.Stream(func(w io.Writer) bool {
            if msg, ok := <-chanStream; ok {
                fmt.Println("msg:" + msg)
                outputBytes := bytes.NewBufferString(msg)
                c.Writer.Write(append(outputBytes.Bytes(), []byte("\n")...))
                return true
            }
            return false
})

如果我想用go-zero该怎么处理,我看了下httpx这个包,更多是writeJson没找到处理流式写入方法

Issues-translate-bot commented 8 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


I am now processing GPT streaming returns. I can use Gin to achieve this. The Demo code is as follows: r.GET("/stream", func(c *gin.Context) { chanStream := make(chan string, 100) c.Header("Content-Type", "text/event-stream; charset=utf-8") go http.GetData("What is Three-Body Problem? Please introduce it in 100 words", chanStream) c.Stream(func(w io.Writer) bool { if msg, ok := <-chanStream; ok { fmt.Println("msg:" + msg) outputBytes := bytes.NewBufferString(msg) c.Writer.Write(append(outputBytes.Bytes(), []byte("\n")...)) return true } return false }) What should I do if I want to use go-zero? I looked at the httpx package and found that writeJson did not find a way to handle streaming writing.

fynxiu commented 8 months ago

Regarding your issue with handling streaming data using go-zero, it seems that go-zero currently doesn't offer built-in support for EventStream handling like Gin does. You might need to implement the streaming logic yourself by referencing other code examples.