@@ -4,14 +4,13 @@ import (
44 "context"
55 "fmt"
66 "github.com/0xJacky/Nginx-UI/internal/chatbot"
7- "github.com/0xJacky/Nginx-UI/internal/transport"
87 "github.com/0xJacky/Nginx-UI/settings"
98 "github.com/gin-gonic/gin"
109 "github.com/pkg/errors"
1110 "github.com/sashabaranov/go-openai"
1211 "github.com/uozi-tech/cosy"
12+ "github.com/uozi-tech/cosy/logger"
1313 "io"
14- "net/http"
1514)
1615
1716const ChatGPTInitPrompt = `You are a assistant who can help users write and optimise the configurations of Nginx,
@@ -49,30 +48,18 @@ func MakeChatCompletionRequest(c *gin.Context) {
4948 c .Writer .Header ().Set ("Connection" , "keep-alive" )
5049 c .Writer .Header ().Set ("Access-Control-Allow-Origin" , "*" )
5150
52- config := openai .DefaultConfig (settings .OpenAISettings .Token )
53-
54- if settings .OpenAISettings .Proxy != "" {
55- t , err := transport .NewTransport (transport .WithProxy (settings .OpenAISettings .Proxy ))
56- if err != nil {
57- c .Stream (func (w io.Writer ) bool {
58- c .SSEvent ("message" , gin.H {
59- "type" : "error" ,
60- "content" : err .Error (),
61- })
62- return false
51+ openaiClient , err := chatbot .GetClient ()
52+ if err != nil {
53+ c .Stream (func (w io.Writer ) bool {
54+ c .SSEvent ("message" , gin.H {
55+ "type" : "error" ,
56+ "content" : err .Error (),
6357 })
64- return
65- }
66- config .HTTPClient = & http.Client {
67- Transport : t ,
68- }
69- }
70-
71- if settings .OpenAISettings .BaseUrl != "" {
72- config .BaseURL = settings .OpenAISettings .BaseUrl
58+ return false
59+ })
60+ return
7361 }
7462
75- openaiClient := openai .NewClientWithConfig (config )
7663 ctx := context .Background ()
7764
7865 req := openai.ChatCompletionRequest {
@@ -82,7 +69,7 @@ func MakeChatCompletionRequest(c *gin.Context) {
8269 }
8370 stream , err := openaiClient .CreateChatCompletionStream (ctx , req )
8471 if err != nil {
85- fmt . Printf ("CompletionStream error: %v\n " , err )
72+ logger . Errorf ("CompletionStream error: %v\n " , err )
8673 c .Stream (func (w io.Writer ) bool {
8774 c .SSEvent ("message" , gin.H {
8875 "type" : "error" ,
@@ -99,12 +86,11 @@ func MakeChatCompletionRequest(c *gin.Context) {
9986 for {
10087 response , err := stream .Recv ()
10188 if errors .Is (err , io .EOF ) {
102- fmt .Println ()
10389 return
10490 }
10591
10692 if err != nil {
107- fmt . Printf ("Stream error: %v\n " , err )
93+ logger . Errorf ("Stream error: %v\n " , err )
10894 return
10995 }
11096
0 commit comments