Skip to content
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

feat: Allow to pass a custom model in OpenAI RealtimeClient #654

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/openai_realtime_dart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ client.on(RealtimeEventType.conversationUpdated, (event) {
// Connect to Realtime API
await client.connect();

// You can optionally specify a custom model
// await client.connect(model: 'gpt-4o-mini-realtime-preview');

// Send a item and triggers a generation
await client.sendUserMessageContent([
const ContentPart.inputText(text: 'How are you?'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Future<void> main() async {
});

// Connect to Realtime API
// You can specify a custom model if needed
// await client.connect(model: 'gpt-4o-mini-realtime-preview');
await client.connect();

// Send a item and triggers a generation
Expand Down
5 changes: 4 additions & 1 deletion packages/openai_realtime_dart/lib/src/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ class RealtimeAPI extends RealtimeEventHandler {
bool isConnected() => _ws != null;

/// Connects to Realtime API Websocket Server.
///
/// [model] specifies which model to use. You can find the list of available
/// models [here](https://platform.openai.com/docs/models).
Future<bool> connect({
final String model = 'gpt-4o-realtime-preview-2024-10-01',
final String model = RealtimeUtils.defaultModel,
}) async {
if (isConnected()) {
throw Exception('Already connected');
Expand Down
9 changes: 7 additions & 2 deletions packages/openai_realtime_dart/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,16 @@ class RealtimeClient extends RealtimeEventHandler {

/// Connects to the Realtime WebSocket API.
/// Updates session config and conversation config.
Future<bool> connect() async {
///
/// [model] specifies which model to use. You can find the list of available
/// models [here](https://platform.openai.com/docs/models).
Future<bool> connect({
final String model = RealtimeUtils.defaultModel,
}) async {
if (isConnected()) {
throw Exception('Already connected, use .disconnect() first');
}
final connected = await realtime.connect();
final connected = await realtime.connect(model: model);
if (connected) {
await updateSession();
}
Expand Down
3 changes: 3 additions & 0 deletions packages/openai_realtime_dart/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import 'dart:math';
import 'dart:typed_data';

class RealtimeUtils {
/// Default model for OpenAI Realtime API.
static const String defaultModel = 'gpt-4o-realtime-preview';

static Uint8List mergeUint8Lists(Uint8List left, Uint8List right) {
final result = Uint8List(left.length + right.length);
result.setRange(0, left.length, left);
Expand Down
2 changes: 1 addition & 1 deletion packages/openai_realtime_dart/test/api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void main() {
});

test('Should handle multiple connections and disconnections', () async {
for (int i = 0; i < 3; i++) {
for (var i = 0; i < 3; i++) {
final isConnected = await realtime.connect();
expect(isConnected, isTrue, reason: 'Connection $i failed');
expect(realtime.isConnected(), isTrue, reason: 'Connection $i failed');
Expand Down
18 changes: 18 additions & 0 deletions packages/openai_realtime_dart/test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ import 'package:test/test.dart';

void main() {
group('RealtimeClient Tests', () {
test('RealtimeClient connect with custom model', () async {
final client = RealtimeClient(
apiKey: Platform.environment['OPENAI_API_KEY'],
debug: true,
);

// Connect with a custom model
const customModel = 'gpt-4o-mini-realtime-preview';
final isConnected = await client.connect(model: customModel);

expect(isConnected, isTrue);
expect(client.isConnected(), isTrue);

// Clean up
await client.disconnect();
expect(client.isConnected(), isFalse);
});

test('RealtimeClient test', () async {
final realtimeEvents = <RealtimeEvent>[];
final client = RealtimeClient(
Expand Down
Loading