@@ -2,6 +2,7 @@ package adapter
22
33import (
44 "bytes"
5+ "fmt"
56 "github.com/luispater/anyAIProxyAPI/internal/proxy/model"
67 "github.com/luispater/anyAIProxyAPI/internal/proxy/utils"
78 log "github.com/sirupsen/logrus"
@@ -82,12 +83,14 @@ outLoop:
8283 }
8384 responseBody = responseBody [lengthCrlfIdx + 2 + int (length )+ 2 :]
8485 dataBuffer = append (dataBuffer , chunkedData ... )
86+ log .Infof ("Buffer length: %d" , len (dataBuffer ))
8587 result , err := g .createResponse (dataBuffer , false )
8688 if err == nil {
8789 if * sniffing {
8890 queue .Enqueue (result )
8991 }
9092 }
93+ log .Infof ("result: %v, %v, %v" , result , err == nil , * sniffing )
9194 }
9295 }
9396 }
@@ -96,25 +99,38 @@ outLoop:
9699 }
97100 }
98101
99- result , errDecompressGzip := g .createResponse (dataBuffer , true )
100- if errDecompressGzip == nil {
102+ result , err := g .createResponse (dataBuffer , true )
103+ if err == nil {
101104 if * sniffing {
102105 queue .Enqueue (result )
103106 }
104107 }
108+ log .Infof ("result: %v, %v, %v" , result , err == nil , * sniffing )
105109}
106110
107111func (g * ChatGPTAdapter ) createResponse (dataBuffer []byte , done bool ) (* model.ProxyResponse , error ) {
108112 content := ""
109113
110- pattern := `(?m)^ data\:\s(\{ .*?\})$ `
114+ pattern := `data:( .*?)\n\n `
111115 re := regexp .MustCompile (pattern )
112116 matches := re .FindAllStringSubmatch (string (dataBuffer ), - 1 )
117+ if len (matches ) == 0 {
118+ return nil , fmt .Errorf ("no match data" )
119+ }
120+ log .Infof ("All matches: %v" , matches )
113121 for i := 0 ; i < len (matches ); i ++ {
114122 match := matches [i ]
123+ log .Infof ("Buffer 1: %s" , match [0 ])
124+ log .Infof ("Buffer 2: %s" , match [1 ])
115125 if len (match ) == 2 {
116- c := g .getDataContent (match [1 ])
117- content = content + c
126+ log .Info (match [1 ])
127+ c , d := g .getDataContent (match [1 ])
128+ if ! d {
129+ content = content + c
130+ } else {
131+ done = true
132+ break
133+ }
118134 }
119135 }
120136
@@ -126,11 +142,17 @@ func (g *ChatGPTAdapter) createResponse(dataBuffer []byte, done bool) (*model.Pr
126142 }, nil
127143}
128144
129- func (g * ChatGPTAdapter ) getDataContent (jsonData string ) string {
145+ func (g * ChatGPTAdapter ) getDataContent (jsonData string ) (string , bool ) {
146+ if jsonData == "[DONE]" {
147+ return "" , true
148+ }
130149 result := ""
131150 operationResult := gjson .Get (jsonData , "o" )
132151 if operationResult .Type == gjson .Null {
133- result = result + gjson .Get (jsonData , "v" ).String ()
152+ vResult := gjson .Get (jsonData , "v" )
153+ if vResult .Type == gjson .String {
154+ result = result + gjson .Get (jsonData , "v" ).String ()
155+ }
134156 } else if operationResult .Type == gjson .String {
135157 operation := operationResult .String ()
136158 if operation == "add" {
@@ -156,7 +178,12 @@ func (g *ChatGPTAdapter) getDataContent(jsonData string) string {
156178 }
157179 }
158180 }
181+ } else if operation == "append" {
182+ vResult := gjson .Get (jsonData , "v" )
183+ if vResult .Type == gjson .String {
184+ result = result + gjson .Get (jsonData , "v" ).String ()
185+ }
159186 }
160187 }
161- return result
188+ return result , false
162189}
0 commit comments