Skip to content

Commit

Permalink
Merge pull request #43 from sunist-c/main
Browse files Browse the repository at this point in the history
fix(openai): 修复 OpenAI 在流式传输时,判定流式传输结束失效的问题
  • Loading branch information
sunist-c authored Nov 12, 2024
2 parents 6fce560 + 5fcf081 commit 8e9aab2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
9 changes: 2 additions & 7 deletions thirdparty/openai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package openai

import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"strings"

"github.com/alioth-center/infrastructure/logger"
"github.com/alioth-center/infrastructure/network/http"
Expand Down Expand Up @@ -171,13 +171,8 @@ func (c client) CompleteStreamingChat(ctx context.Context, req CompleteChatReque
reply := StreamingReplyObject{}
payload := event.Data

// attempt to decode base64 encoded payload
if decoded, decodeErr := base64.StdEncoding.DecodeString(string(payload)); decodeErr == nil {
payload = decoded
}

// end of the conversation
if string(payload) == "[DONE]" {
if strings.TrimSpace(string(payload)) == "[DONE]" {
break
}

Expand Down
44 changes: 41 additions & 3 deletions thirdparty/openai/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package openai
import (
"bytes"
"encoding/json"
"fmt"
"github.com/alioth-center/infrastructure/logger"
"github.com/alioth-center/infrastructure/network/http"
"github.com/alioth-center/infrastructure/trace"
"io"
h "net/http"
"os"
"strings"
"testing"

"github.com/alioth-center/infrastructure/logger"
"github.com/alioth-center/infrastructure/network/http"
)

func TestOpenAiClient(t *testing.T) {
Expand Down Expand Up @@ -110,3 +110,41 @@ func initMockingClient(t *testing.T) Client {
client := http.NewMockClientWithLogger(logger.Default(), chatCompletionsOpts, embeddingOpts)
return NewCustomClient(Config{}, client, logger.Default())
}

func TestCompleteStreamingChat(t *testing.T) {
apiKey, baseUrl := os.Getenv("OPENAI_API_KEY"), os.Getenv("OPENAI_BASE_URL")
if apiKey == "" || baseUrl == "" {
t.Skip("OPENAI_API_KEY or OPENAI_BASE_URL is empty")
}

cli := NewClient(Config{ApiKey: apiKey, BaseUrl: baseUrl}, logger.Default())

for i := 0; i < 10; i++ {
input := "Hello, world! Please echo the input content, without any changes"
slices, err := cli.CompleteStreamingChat(trace.NewContext(), CompleteChatRequest{
Body: CompleteChatRequestBody{
Model: "gpt-4o-mini",
Messages: []ChatMessageObject{
{Role: ChatRoleEnumSystem, Content: json.RawMessage(`"Please echo the input content, without any changes"`)},
{Role: ChatRoleEnumUser, Content: json.RawMessage(fmt.Sprintf(`"%s"`, input))},
},
N: 1,
Stream: true,
},
})
if err != nil {
t.Error(err)
}

output := ""
for slice := range slices {
output += slice.Choices[0].Delta.Content
}

if output == input {
return
}
}

t.Error("failed to complete streaming chat")
}

0 comments on commit 8e9aab2

Please sign in to comment.