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 to close the flow when using the following code? #262

Closed
zhengyansheng opened this issue Apr 18, 2023 · 2 comments
Closed

How to close the flow when using the following code? #262

zhengyansheng opened this issue Apr 18, 2023 · 2 comments
Labels
question Further information is requested stale

Comments

@zhengyansheng
Copy link

package main

import (
"context"
"errors"
"fmt"
"io"
openai "github.com/sashabaranov/go-openai"
)

func main() {
c := openai.NewClient("your token")
ctx := context.Background()

req := openai.ChatCompletionRequest{
	Model:     openai.GPT3Dot5Turbo,
	MaxTokens: 20,
	Messages: []openai.ChatCompletionMessage{
		{
			Role:    openai.ChatMessageRoleUser,
			Content: "Lorem ipsum",
		},
	},
	Stream: true,
}
stream, err := c.CreateChatCompletionStream(ctx, req)
if err != nil {
	fmt.Printf("ChatCompletionStream error: %v\n", err)
	return
}
defer stream.Close()

fmt.Printf("Stream response: ")
for {
	response, err := stream.Recv()
	if errors.Is(err, io.EOF) {
		fmt.Println("\nStream finished")
		return
	}

	if err != nil {
		fmt.Printf("\nStream error: %v\n", err)
		return
	}

	fmt.Printf(response.Choices[0].Delta.Content)
}

}


// The response provided by chatgpt seems to be that go openai does not have this method
import (
"net/http"
"github.com/sashabaranov/go-openai"
"github.com/gin-gonic/gin"
)

// Initialize OpenAI API client
client := openai.NewCompletionAPI("YOUR_API_KEY")

// Define HTTP endpoint to cancel a stream
func cancelStream(c *gin.Context) {
// Extract stream ID from request data
var json struct {
StreamID string json:"stream_id" binding:"required"
}
if err := c.ShouldBindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

// Call OpenAI API to cancel stream
_, err := client.CancelStream(json.StreamID)
if err != nil {
    c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
    return
}

// Return success response
c.JSON(http.StatusOK, gin.H{"status": "success"})

}

// Start Gin HTTP server and register endpoint
r := gin.Default()
r.POST("/cancel_stream", cancelStream)
r.Run(":8080")

@imsprojo2fan
Copy link

usectx, cancel := context.WithCancel(context.Background()) and cancel the ctx

@vvatanabe vvatanabe added the question Further information is requested label Jun 30, 2023
@vvatanabe
Copy link
Collaborator

Closing this due to lack of comments for an extended period.

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

3 participants