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

Terminates a "stream for" loop #251

Closed
chenxi2015 opened this issue Apr 14, 2023 · 3 comments
Closed

Terminates a "stream for" loop #251

chenxi2015 opened this issue Apr 14, 2023 · 3 comments
Labels
question Further information is requested stale

Comments

@chenxi2015
Copy link

How do I terminate the stream for loop,How do I trigger stream.close() manually?

func (g *GPT) ChatCompletionStream() {
	var err error
	if Stream == false {
		panic("ChatCompletionStream should be set stream")
	}
	g.ChatCompStream, err = g.Client.CreateChatCompletionStream(g.App.Ctx, g.ChatCompletionRequest)
	if err != nil {
		return
	}
	defer g.ChatCompStream.Close()
	for {
		select {
		default:
			response, err := g.ChatCompStream.Recv()
			if errors.Is(err, io.EOF) {
				g.App.WsPushChan <- service.PushResp{
					Code: 1,
					Data: "Chatcat Finished",
				}
				return
			}
			if err != nil {
				g.App.WsPushChan <- service.PushResp{
					Code: -1,
					Data: fmt.Sprintf("Chatcat Warm Reminder: %s", err.Error()),
				}
				return
			}
			g.App.WsPushChan <- service.PushResp{
				Code: 0,
				Data: response.Choices[0].Delta.Content,
			}
		}
	}
}
@darvenking
Copy link

darvenking commented Apr 26, 2023

Execute return when your business is completed in order to exit the select block.

@ethanchancc18
Copy link

Execute return when your business is completed in order to exit the select block.

thanks I know how to solve it. By manually calling g.ChatCompStream.Close(), the problem can be solved directly.

@vvatanabe vvatanabe added the question Further information is requested label Jul 1, 2023
@vvatanabe
Copy link
Collaborator

Closing this due to lack of comments for an extended period. Hopefully, the proposed solution has resolved your issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested stale
Projects
None yet
Development

No branches or pull requests

4 participants