Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e2f5c1eb8c | ||
|
|
d68aa4c96f | ||
|
|
47cb77de53 | ||
|
|
61912f5e2c |
@@ -55,10 +55,14 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errors.New("invalid status code: " + strconv.Itoa(resp.StatusCode))
|
||||
// Prinnt the body in string
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(resp.Body)
|
||||
return errors.New("error response: " + strconv.Itoa(resp.StatusCode) + " " + buf.String())
|
||||
}
|
||||
|
||||
var streamResponseText string
|
||||
var done = false
|
||||
var streamResponseText = ""
|
||||
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||
@@ -82,24 +86,52 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
|
||||
common.SysError("invalid stream response: " + data)
|
||||
continue
|
||||
}
|
||||
// If data has event: event content inside, remove it, it can be prefix or inside the data
|
||||
if strings.HasPrefix(data, "event:") || strings.Contains(data, "event:") {
|
||||
// Remove event: event in the front or back
|
||||
data = strings.TrimPrefix(data, "event: event")
|
||||
data = strings.TrimSuffix(data, "event: event")
|
||||
// Remove everything, only keep `data: {...}` <--- this is the json
|
||||
// Find the start and end indices of `data: {...}` substring
|
||||
startIndex := strings.Index(data, "data:")
|
||||
endIndex := strings.LastIndex(data, "}")
|
||||
|
||||
// If both indices are found and end index is greater than start index
|
||||
if startIndex != -1 && endIndex != -1 && endIndex > startIndex {
|
||||
// Extract the `data: {...}` substring
|
||||
data = data[startIndex : endIndex+1]
|
||||
}
|
||||
|
||||
// Trim whitespace and newlines from the modified data string
|
||||
data = strings.TrimSpace(data)
|
||||
}
|
||||
if !strings.HasPrefix(data, "data:") {
|
||||
continue
|
||||
}
|
||||
data = data[6:]
|
||||
if !strings.HasPrefix(data, "[DONE]") {
|
||||
var streamResponse ChatCompletionsStreamResponse
|
||||
err = json.Unmarshal([]byte(data), &streamResponse)
|
||||
if err != nil {
|
||||
common.SysError("error unmarshalling stream response: " + err.Error())
|
||||
// Prinnt the body in string
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(resp.Body)
|
||||
common.SysError("error unmarshalling stream response: " + err.Error() + " " + buf.String())
|
||||
return err
|
||||
}
|
||||
for _, choice := range streamResponse.Choices {
|
||||
streamResponseText += choice.Delta.Content
|
||||
}
|
||||
} else {
|
||||
done = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Check if streaming is complete and streamResponseText is populated
|
||||
if streamResponseText == "" {
|
||||
if streamResponseText == "" || !done {
|
||||
return errors.New("Streaming not complete")
|
||||
}
|
||||
|
||||
@@ -109,12 +141,11 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
|
||||
func buildTestRequest() *ChatRequest {
|
||||
testRequest := &ChatRequest{
|
||||
Model: "", // this will be set later
|
||||
MaxTokens: 1,
|
||||
Stream: true,
|
||||
}
|
||||
testMessage := Message{
|
||||
Role: "user",
|
||||
Content: "hi",
|
||||
Content: "say hi word only",
|
||||
}
|
||||
testRequest.Messages = append(testRequest.Messages, testMessage)
|
||||
return testRequest
|
||||
|
||||
@@ -278,6 +278,28 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
||||
common.SysError("invalid stream response: " + data)
|
||||
continue
|
||||
}
|
||||
// If data has event: event content inside, remove it, it can be prefix or inside the data
|
||||
if strings.HasPrefix(data, "event:") || strings.Contains(data, "event:") {
|
||||
// Remove event: event in the front or back
|
||||
data = strings.TrimPrefix(data, "event: event")
|
||||
data = strings.TrimSuffix(data, "event: event")
|
||||
// Remove everything, only keep `data: {...}` <--- this is the json
|
||||
// Find the start and end indices of `data: {...}` substring
|
||||
startIndex := strings.Index(data, "data:")
|
||||
endIndex := strings.LastIndex(data, "}")
|
||||
|
||||
// If both indices are found and end index is greater than start index
|
||||
if startIndex != -1 && endIndex != -1 && endIndex > startIndex {
|
||||
// Extract the `data: {...}` substring
|
||||
data = data[startIndex : endIndex+1]
|
||||
}
|
||||
|
||||
// Trim whitespace and newlines from the modified data string
|
||||
data = strings.TrimSpace(data)
|
||||
}
|
||||
if !strings.HasPrefix(data, "data:") {
|
||||
continue
|
||||
}
|
||||
dataChan <- data
|
||||
data = data[6:]
|
||||
if !strings.HasPrefix(data, "[DONE]") {
|
||||
|
||||
@@ -44,7 +44,7 @@ type GeneralOpenAIRequest struct {
|
||||
type ChatRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []Message `json:"messages"`
|
||||
MaxTokens int `json:"max_tokens"`
|
||||
MaxTokens *int `json:"max_tokens,omitempty"`
|
||||
Stream bool `json:"stream"`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user