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

Need c.JSON() response like behaviour for c.SSEvent() response for supporting first party clients like OpenAI SDK #3892

Open
gavrishp opened this issue Mar 15, 2024 · 2 comments

Comments

@gavrishp
Copy link

Description

SSE sends response only in Event struct which follows Message and Data format. Need a Event format that sends the response in the struct defined by user. Behaviour same as c.JSON()

When consumers use client sdk like OpenAI for streaming- https://github.com/openai/openai-python?tab=readme-ov-file#streaming-responses

Expectation is to receive the response in json format

{
        "model": "gpt",
        "text": "text response"
} 

Using c.SSEvent() the response would be

message= output
data=
{
        "model": "gpt",
        "text": "text response"
} 

Expectations

Of having an option to respond similar to c.JSON()

@flc1125
Copy link
Contributor

flc1125 commented Mar 18, 2024

I can give it a try, just give me some time.

@flc1125
Copy link
Contributor

flc1125 commented Mar 18, 2024

You can use the c.Stream().

Example:

package main

import (
	"fmt"
	"io"
	"sync/atomic"
	"time"

	"github.com/gin-gonic/gin"
)

func main() {
	router := gin.Default()

	router.GET("/ping", func(c *gin.Context) {
		c.Header("Content-Type", "text/event-stream")
		c.Header("Cache-Control", "no-cache")

		var counter int64 = 0
		
		c.Stream(func(w io.Writer) bool {
			_, _ = w.Write([]byte(fmt.Sprintf("data: %d\n\n", atomic.LoadInt64(&counter))))

			time.Sleep(1 * time.Second)

			return atomic.AddInt64(&counter, 1) <= 10
		})
	})

	router.Run(":7777")
}

Result:

2024-03-18.20.44.47.mov

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

2 participants