Skip to content

Commit

Permalink
enhance: check whether openai.token is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Mar 22, 2023
1 parent 94408cd commit e11bc16
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions server/api/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/pkg/errors"
"github.com/sashabaranov/go-openai"
"io"
"log"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -39,7 +38,17 @@ func MakeChatCompletionRequest(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
c.Writer.Header().Set("Connection", "keep-alive")
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
log.Println(settings.OpenAISettings.Token)

if settings.OpenAISettings.Token == "" {
c.Stream(func(w io.Writer) bool {
c.SSEvent("message", gin.H{
"type": "error",
"content": "[Error] OpenAI token is empty",
})
return false
})
return
}

config := openai.DefaultConfig(settings.OpenAISettings.Token)

Expand Down Expand Up @@ -104,9 +113,8 @@ func MakeChatCompletionRequest(c *gin.Context) {
return
}

// Send SSE to client
message := fmt.Sprintf("%s", response.Choices[0].Delta.Content)
fmt.Printf("%s", response.Choices[0].Delta.Content)
fmt.Printf("%s", message)
_ = os.Stdout.Sync()

msgChan <- message
Expand Down

0 comments on commit e11bc16

Please sign in to comment.