Skip to content

Commit

Permalink
[azopenai] Add test for different formats in chat completions streami…
Browse files Browse the repository at this point in the history
…ng (Azure#21374)

Also, updating CODEOWNERS to add @jhendrixMSFT to the ai folder.
  • Loading branch information
richardpark-msft authored Aug 15, 2023
1 parent ea9085f commit 59af847
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
# PRLabel: %Azure.Identity
/sdk/azidentity/ @chlowell @jhendrixMSFT @rickwinter @Azure/azure-sdk-write-identity

# PRLable: %OpenAI
/sdk/ai @richardpark-msft @jhendrixMSFT

# PRLabel: %Internal
/sdk/internal/ @chlowell @jhendrixMSFT @richardpark-msft @rickwinter

Expand All @@ -36,6 +39,9 @@
# PRLabel: %Service Bus
/sdk/messaging/azservicebus/ @richardpark-msft @jhendrixMSFT

# PRLabel: %Event Grid
/sdk/messaging/azeventgrid/ @richardpark-msft @jhendrixMSFT

# PRLabel: %Event Hubs
/sdk/messaging/azeventhubs/ @richardpark-msft @jhendrixMSFT

Expand Down
22 changes: 21 additions & 1 deletion sdk/ai/azopenai/event_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func TestEventReader_InvalidType(t *testing.T) {
data := []string{
"invaliddata: {\u0022name\u0022:\u0022chatcmpl-7Z4kUpXX6HN85cWY28IXM4EwemLU3\u0022,\u0022object\u0022:\u0022chat.completion.chunk\u0022,\u0022created\u0022:1688594090,\u0022model\u0022:\u0022gpt-4-0613\u0022,\u0022choices\u0022:[{\u0022index\u0022:0,\u0022delta\u0022:{\u0022role\u0022:\u0022assistant\u0022,\u0022content\u0022:\u0022\u0022},\u0022finish_reason\u0022:null}]}\n\n",
"invaliddata: {\"name\":\"chatcmpl-7Z4kUpXX6HN85cWY28IXM4EwemLU3\",\"object\":\"chat.completion.chunk\",\"created\":1688594090,\"model\":\"gpt-4-0613\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\"},\"finish_reason\":null}]}\n\n",
}

text := strings.NewReader(strings.Join(data, "\n"))
Expand Down Expand Up @@ -55,3 +55,23 @@ func TestEventReader_StreamIsClosedBeforeDone(t *testing.T) {
require.Empty(t, evt)
require.EqualError(t, err, "incomplete stream")
}

func TestEventReader_SpacesAroundAreas(t *testing.T) {
buff := strings.NewReader(
// spaces between data
"data: {\"name\":\"chatcmpl-7Z4kUpXX6HN85cWY28IXM4EwemLU3\",\"object\":\"chat.completion.chunk\",\"created\":1688594090,\"model\":\"gpt-4-0613\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"with-spaces\"},\"finish_reason\":null}]}\n" +
// no spaces
"data:{\"name\":\"chatcmpl-7Z4kUpXX6HN85cWY28IXM4EwemLU3\",\"object\":\"chat.completion.chunk\",\"created\":1688594090,\"model\":\"gpt-4-0613\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"without-spaces\"},\"finish_reason\":null}]}\n",
)

eventReader := newEventReader[ChatCompletions](io.NopCloser(buff))

evt, err := eventReader.Read()
require.NoError(t, err)
require.Equal(t, "with-spaces", *evt.Choices[0].Delta.Content)

evt, err = eventReader.Read()
require.NoError(t, err)
require.NotEmpty(t, evt)
require.Equal(t, "without-spaces", *evt.Choices[0].Delta.Content)
}

0 comments on commit 59af847

Please sign in to comment.