Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can i handle streaming response by gozero #3980

Closed
qianjiangchao1992 opened this issue Mar 6, 2024 · 2 comments · Fixed by #4055
Closed

how can i handle streaming response by gozero #3980

qianjiangchao1992 opened this issue Mar 6, 2024 · 2 comments · Fixed by #4055
Labels
area/restful Categorizes issue or PR as related to restful apis.

Comments

@qianjiangchao1992
Copy link

qianjiangchao1992 commented Mar 6, 2024

我现在处理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
Copy link

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
Copy link
Contributor

fynxiu commented Mar 9, 2024

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.

@kevwan kevwan added the area/restful Categorizes issue or PR as related to restful apis. label Apr 6, 2024
@kevwan kevwan linked a pull request Apr 9, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/restful Categorizes issue or PR as related to restful apis.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants