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

Add stream_options property to get token usage. #180

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion lib/src/core/models/chat/stream/chat.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:collection/collection.dart';

import 'sub_models/choices/choices.dart';
import 'sub_models/usage.dart';

export 'sub_models/choices/choices.dart';
export 'sub_models/usage.dart';
Expand All @@ -21,6 +21,9 @@ final class OpenAIStreamChatCompletionModel {
/// This fingerprint represents the backend configuration that the model runs with.
final String? systemFingerprint;

/// An optional field that will only be present when you set `streamOptions: {"include_usage": true}` in your request. When present, it contains a null value except for the last chunk which contains the token usage statistics for the entire request.
final OpenAIStreamChatCompletionUsageModel? usage;

/// Wether the chat completion have at least one choice in [choices].
bool get haveChoices => choices.isNotEmpty;

Expand All @@ -41,6 +44,7 @@ final class OpenAIStreamChatCompletionModel {
required this.created,
required this.choices,
required this.systemFingerprint,
this.usage,
});

/// {@macro openai_stream_chat_completion}
Expand All @@ -55,6 +59,9 @@ final class OpenAIStreamChatCompletionModel {
)
.toList(),
systemFingerprint: json['system_fingerprint'],
usage: json['usage'] != null
? OpenAIStreamChatCompletionUsageModel.fromMap(json['usage'])
: null,
);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/src/instance/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ interface class OpenAIChat implements OpenAIChatBase {
int? seed,
String? user,
http.Client? client,
Map<String, dynamic>? streamOptions,
}) {
return OpenAINetworkingClient.postStream<OpenAIStreamChatCompletionModel>(
to: BaseApiUrlBuilder.build(endpoint),
Expand All @@ -201,6 +202,7 @@ interface class OpenAIChat implements OpenAIChatBase {
if (user != null) "user": user,
if (seed != null) "seed": seed,
if (responseFormat != null) "response_format": responseFormat,
if (streamOptions != null && streamOptions.isNotEmpty) "stream_options": streamOptions,
},
onSuccess: (Map<String, dynamic> response) {
return OpenAIStreamChatCompletionModel.fromMap(response);
Expand Down
10 changes: 10 additions & 0 deletions test/openai_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ void main() async {
test('create with a stream', () async {
final chatStream = OpenAI.instance.chat.createStream(
model: "gpt-3.5-turbo",
streamOptions: {
"include_usage": true,
},
messages: [
OpenAIChatCompletionChoiceMessageModel(
content: [
Expand All @@ -352,6 +355,13 @@ void main() async {
isA<List<OpenAIChatCompletionChoiceMessageContentItemModel>?>());
expect(streamEvent.choices.first.delta.content?.first?.text,
isA<String?>());
if (streamEvent.usage != null) {
final u = streamEvent.usage!;
expect(u, isA<OpenAIStreamChatCompletionUsageModel>());
expect(u.promptTokens, greaterThan(0));
expect(u.completionTokens, greaterThan(0));
expect(u.totalTokens, equals(u.promptTokens + u.completionTokens));
}
},
onDone: () {
completer.complete(true);
Expand Down