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

是否支持event stream #333

Open
b0123456789 opened this issue May 17, 2022 · 8 comments
Open

是否支持event stream #333

b0123456789 opened this issue May 17, 2022 · 8 comments

Comments

@b0123456789
Copy link

No description provided.

@guonaihong
Copy link
Owner

怎么理解event stream, 可有具体的例子?

@b0123456789
Copy link
Author

e

试了一下,设置超时,可以抓取一条

@b0123456789
Copy link
Author

https://github.com/r3labs/sse
这个库可以持续接受数据,不过我要只一条,也算够了

@guonaihong
Copy link
Owner

https://github.com/r3labs/sse 这个库可以持续接受数据,不过我要只一条,也算够了

扫了下源代码, 明白你的意思了, 利用http chunked(分块传输编码) 模拟流式消息推送. 以前在websocket没出来之前用得挺多.
现在一般直接用websocket推送消息.

@Baiyuetribe
Copy link

@guonaihong 大佬,chatgpt目前使用的就是sse, 现有的gout代码在Debug请求时可正常输出流式消息。但如果加上bindbody时就会报503错误。急用的情况下,debug日志消息该如何输出到变量里呢?
image

@Baiyuetribe
Copy link

细看了文档,已解决, save to writer方法

@go-mixed
Copy link

FsChat、OpenAI等都使用SSE协议(Server-Sent Events)来依次输出结果,因为大语言模型回复的都很慢,一般前端都使用的EventSource来实现打字效果。

可以使用下面方法来读取


func sse(ctx context.Context, url string, req any, callback func(line string)) error {

	response, err := gout.New().
		SetHeader(gout.H{
			"Accept": "text/event-stream",
		}).
		WithContext(ctx).
		POST(url).
		SetJSON(req).
		Response()

	if err != nil {
		return err
	} else if response.StatusCode != http.StatusOK || !strings.Contains(response.Header.Get("Content-Type"), "text/event-stream") {
		return fmt.Errorf("invalid status code: %d, content-type: %s", response.StatusCode, response.Header.Get("Content-Type"))
	}

	defer response.Body.Close()

	buf := bufio.NewReader(response.Body)
	for {
		line, err := buf.ReadString('\n')
		if err != nil && err != io.EOF {
			return err
		}
		callback(line)
		if err == io.EOF {
			break
		}
	}

	return nil
}


@guonaihong
Copy link
Owner

@go-mixed 赞,我这个版本实现下sse的api。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants