-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add dependency injection to ChatCompletionStream for improved testability #1011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rjcorwin
wants to merge
1
commit into
sashabaranov:master
Choose a base branch
from
rjcorwin:mock-chat-streams
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
package openai_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"testing" | ||
|
||
"github.com/sashabaranov/go-openai" | ||
) | ||
|
||
// This file demonstrates how to create mock clients for go-openai streaming | ||
// functionality. This pattern is useful when testing code that depends on | ||
// go-openai streaming but you want to control the responses for testing. | ||
|
||
// MockOpenAIStreamClient demonstrates how to create a full mock client for go-openai. | ||
type MockOpenAIStreamClient struct { | ||
// Configure canned responses | ||
ChatCompletionResponse openai.ChatCompletionResponse | ||
ChatCompletionStreamErr error | ||
|
||
// Allow function overrides for more complex scenarios | ||
CreateChatCompletionStreamFn func( | ||
ctx context.Context, req openai.ChatCompletionRequest) (*openai.ChatCompletionStream, error) | ||
} | ||
|
||
func (m *MockOpenAIStreamClient) CreateChatCompletionStream( | ||
ctx context.Context, | ||
req openai.ChatCompletionRequest, | ||
) (*openai.ChatCompletionStream, error) { | ||
if m.CreateChatCompletionStreamFn != nil { | ||
return m.CreateChatCompletionStreamFn(ctx, req) | ||
} | ||
return nil, m.ChatCompletionStreamErr | ||
} | ||
|
||
// mockStreamReader creates specific responses for testing. | ||
type mockStreamReader struct { | ||
responses []openai.ChatCompletionStreamResponse | ||
index int | ||
} | ||
|
||
func (m *mockStreamReader) Recv() (openai.ChatCompletionStreamResponse, error) { | ||
if m.index >= len(m.responses) { | ||
return openai.ChatCompletionStreamResponse{}, io.EOF | ||
} | ||
resp := m.responses[m.index] | ||
m.index++ | ||
return resp, nil | ||
} | ||
|
||
func (m *mockStreamReader) Close() error { | ||
return nil | ||
} | ||
|
||
func TestMockOpenAIStreamClient_Demo(t *testing.T) { | ||
// Create expected responses that our mock stream will return | ||
expectedResponses := []openai.ChatCompletionStreamResponse{ | ||
{ | ||
ID: "test-1", | ||
Object: "chat.completion.chunk", | ||
Model: "gpt-3.5-turbo", | ||
Choices: []openai.ChatCompletionStreamChoice{ | ||
{ | ||
Index: 0, | ||
Delta: openai.ChatCompletionStreamChoiceDelta{ | ||
Role: "assistant", | ||
Content: "Hello", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
ID: "test-2", | ||
Object: "chat.completion.chunk", | ||
Model: "gpt-3.5-turbo", | ||
Choices: []openai.ChatCompletionStreamChoice{ | ||
{ | ||
Index: 0, | ||
Delta: openai.ChatCompletionStreamChoiceDelta{ | ||
Content: " World", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
ID: "test-3", | ||
Object: "chat.completion.chunk", | ||
Model: "gpt-3.5-turbo", | ||
Choices: []openai.ChatCompletionStreamChoice{ | ||
{ | ||
Index: 0, | ||
Delta: openai.ChatCompletionStreamChoiceDelta{}, | ||
FinishReason: "stop", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// Create mock client with custom stream function | ||
mockClient := &MockOpenAIStreamClient{ | ||
CreateChatCompletionStreamFn: func( | ||
_ context.Context, _ openai.ChatCompletionRequest, | ||
) (*openai.ChatCompletionStream, error) { | ||
// Create a mock stream reader with our expected responses | ||
mockStreamReader := &mockStreamReader{ | ||
responses: expectedResponses, | ||
index: 0, | ||
} | ||
// Return a new ChatCompletionStream with our mock reader | ||
return openai.NewChatCompletionStream(mockStreamReader), nil | ||
}, | ||
} | ||
|
||
// Test the mock client | ||
stream, err := mockClient.CreateChatCompletionStream( | ||
context.Background(), | ||
openai.ChatCompletionRequest{ | ||
Model: openai.GPT3Dot5Turbo, | ||
Messages: []openai.ChatCompletionMessage{ | ||
{ | ||
Role: openai.ChatMessageRoleUser, | ||
Content: "Hello!", | ||
}, | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
t.Fatalf("CreateChatCompletionStream returned error: %v", err) | ||
} | ||
defer stream.Close() | ||
|
||
// Verify we get back exactly the responses we configured | ||
fullResponse := "" | ||
for i, expectedResponse := range expectedResponses { | ||
receivedResponse, streamErr := stream.Recv() | ||
if streamErr != nil { | ||
t.Fatalf("stream.Recv() failed at index %d: %v", i, streamErr) | ||
} | ||
|
||
// Additional specific checks | ||
if receivedResponse.ID != expectedResponse.ID { | ||
t.Errorf("Response %d ID mismatch. Expected: %s, Got: %s", | ||
i, expectedResponse.ID, receivedResponse.ID) | ||
} | ||
if len(receivedResponse.Choices) > 0 && len(expectedResponse.Choices) > 0 { | ||
expectedContent := expectedResponse.Choices[0].Delta.Content | ||
receivedContent := receivedResponse.Choices[0].Delta.Content | ||
if receivedContent != expectedContent { | ||
t.Errorf("Response %d content mismatch. Expected: %s, Got: %s", | ||
i, expectedContent, receivedContent) | ||
} | ||
fullResponse += receivedContent | ||
} | ||
} | ||
|
||
// Verify EOF at the end | ||
_, streamErr := stream.Recv() | ||
if !errors.Is(streamErr, io.EOF) { | ||
t.Errorf("Expected EOF at end of stream, got: %v", streamErr) | ||
} | ||
|
||
// Verify the full assembled response | ||
expectedFullResponse := "Hello World" | ||
if fullResponse != expectedFullResponse { | ||
t.Errorf("Full response mismatch. Expected: %s, Got: %s", expectedFullResponse, fullResponse) | ||
} | ||
|
||
t.Log("✅ Successfully demonstrated mock OpenAI client with streaming responses!") | ||
t.Logf(" Full response assembled: %q", fullResponse) | ||
} | ||
|
||
// TestMockOpenAIStreamClient_ErrorHandling demonstrates error handling. | ||
func TestMockOpenAIStreamClient_ErrorHandling(t *testing.T) { | ||
expectedError := errors.New("mock stream error") | ||
|
||
mockClient := &MockOpenAIStreamClient{ | ||
ChatCompletionStreamErr: expectedError, | ||
} | ||
|
||
_, err := mockClient.CreateChatCompletionStream( | ||
context.Background(), | ||
openai.ChatCompletionRequest{ | ||
Model: openai.GPT3Dot5Turbo, | ||
Messages: []openai.ChatCompletionMessage{ | ||
{ | ||
Role: openai.ChatMessageRoleUser, | ||
Content: "Hello!", | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
if !errors.Is(err, expectedError) { | ||
t.Errorf("Expected error %v, got %v", expectedError, err) | ||
} | ||
|
||
t.Log("✅ Successfully demonstrated mock OpenAI client error handling!") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sashabaranov Does this approach fulfill your intent in the
Note
comment?