Skip to content

chore(firebaseai): add token count tests for different scenarios #17521

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
152 changes: 142 additions & 10 deletions packages/firebase_ai/firebase_ai/example/lib/pages/token_count_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:firebase_ai/firebase_ai.dart';
import '../widgets/message_widget.dart';

Expand Down Expand Up @@ -58,17 +60,27 @@ class _TokenCountPageState extends State<TokenCountPage> {
vertical: 25,
horizontal: 15,
),
child: Row(
child: Column(
children: [
Expanded(
child: ElevatedButton(
onPressed: !_loading
? () async {
await _testCountToken();
}
: null,
child: const Text('Count Tokens'),
),
ElevatedButton(
onPressed: !_loading ? _testCountToken : null,
child: const Text('Count text tokens'),
),
ElevatedButton(
onPressed: !_loading ? _testCountTokenChat : null,
child: const Text('Count chat tokens'),
),
ElevatedButton(
onPressed: !_loading ? _testCountTokenImage : null,
child: const Text('Count image tokens'),
),
ElevatedButton(
onPressed: !_loading ? _testCountTokenVideo : null,
child: const Text('Count video tokens'),
),
ElevatedButton(
onPressed: !_loading ? _testCountTokenPdf : null,
child: const Text('Count PDF tokens'),
),
],
),
Expand Down Expand Up @@ -102,4 +114,124 @@ class _TokenCountPageState extends State<TokenCountPage> {
_loading = false;
});
}

Future<void> _testCountTokenPdf() async {
setState(() {
_loading = true;
});

final pdfBytes =
(await rootBundle.load('assets/documents/gemini_summary.pdf'))
.buffer
.asUint8List();
const text = 'what is in the document?';
final content = Content.multi(
[InlineDataPart('application/pdf', pdfBytes), TextPart(text)]);

final tokenResponse = await widget.model.countTokens([content]);
final tokenResult = 'Count token from video: ${tokenResponse.totalTokens}';
_messages.add(MessageData(text: tokenResult, fromUser: false));

final contentResponse = await widget.model.generateContent([content]);
final contentMetaData = 'result metadata, promptTokenCount:'
'${contentResponse.usageMetadata!.promptTokenCount}, '
'candidatesTokenCount:'
'${contentResponse.usageMetadata!.candidatesTokenCount}, '
'totalTokenCount:'
'${contentResponse.usageMetadata!.totalTokenCount}';
_messages.add(MessageData(text: contentMetaData, fromUser: false));

setState(() {
_loading = false;
});
}

Future<void> _testCountTokenVideo() async {
setState(() {
_loading = true;
});

final busyCatBytes = (await rootBundle.load('assets/videos/landscape.mp4'))
.buffer
.asUint8List();
const text = 'what is in the video?';
final content = Content.multi(
[InlineDataPart('video/mp4', busyCatBytes), TextPart(text)]);

final tokenResponse = await widget.model.countTokens([content]);
final tokenResult = 'Count token from video: ${tokenResponse.totalTokens}';
_messages.add(MessageData(text: tokenResult, fromUser: false));

final contentResponse = await widget.model.generateContent([content]);
final contentMetaData = 'result metadata, promptTokenCount:'
'${contentResponse.usageMetadata!.promptTokenCount}, '
'candidatesTokenCount:'
'${contentResponse.usageMetadata!.candidatesTokenCount}, '
'totalTokenCount:'
'${contentResponse.usageMetadata!.totalTokenCount}';
_messages.add(MessageData(text: contentMetaData, fromUser: false));

setState(() {
_loading = false;
});
}

Future<void> _testCountTokenImage() async {
setState(() {
_loading = true;
});

final catBytes =
(await rootBundle.load('assets/images/cat.jpg')).buffer.asUint8List();
const text = 'what is in the image?';
final content =
Content.multi([InlineDataPart('image/jpeg', catBytes), TextPart(text)]);

final tokenResponse = await widget.model.countTokens([content]);
final tokenResult = 'Count token from image: ${tokenResponse.totalTokens}';
_messages.add(MessageData(text: tokenResult, fromUser: false));

final contentResponse = await widget.model.generateContent([content]);
final contentMetaData = 'result metadata, promptTokenCount:'
'${contentResponse.usageMetadata!.promptTokenCount}, '
'candidatesTokenCount:'
'${contentResponse.usageMetadata!.candidatesTokenCount}, '
'totalTokenCount:'
'${contentResponse.usageMetadata!.totalTokenCount}';
_messages.add(MessageData(text: contentMetaData, fromUser: false));

setState(() {
_loading = false;
});
}

Future<void> _testCountTokenChat() async {
setState(() {
_loading = true;
});

final chat = widget.model.startChat(history: [
Content.text('hello'),
Content.model([TextPart('great to meet you, how can I help?')]),
]);
final tokenResponse = await widget.model.countTokens(chat.history);
final tokenResult = 'Count token from chat history: '
'${tokenResponse.totalTokens}';
_messages.add(MessageData(text: tokenResult, fromUser: false));

const prompt = 'tell a short story';
final content = Content.text(prompt);
final contentResponse = await chat.sendMessage(content);
final contentMetaData = 'result metadata, promptTokenCount:'
'${contentResponse.usageMetadata!.promptTokenCount}, '
'candidatesTokenCount:'
'${contentResponse.usageMetadata!.candidatesTokenCount}, '
'totalTokenCount:'
'${contentResponse.usageMetadata!.totalTokenCount}';
_messages.add(MessageData(text: contentMetaData, fromUser: false));

setState(() {
_loading = false;
});
}
}
Loading