Skip to content

Update text_generation.dart #213

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions samples/dart/bin/text_generation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,28 @@ import 'dart:io';
import 'package:google_generative_ai/google_generative_ai.dart';

final apiKey = () {
final apiKey = Platform.environment['GEMINI_API_KEY'];
final apiKey = Platform.environment['GOOGLE_API_KEY'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that GEMINI_API_KEY is the standard across the SDKS.

#188 (comment)

if (apiKey == null) {
stderr.writeln(r'No $GEMINI_API_KEY environment variable');
stderr.writeln(r'No $GOOGLE_API_KEY environment variable');
exit(1);
}
return apiKey;
}();

Future<void> textGenTextOnlyPrompt() async {
// [START text_gen_text_only_prompt]
import 'package:google_generative_ai/google_generative_ai.dart';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not valid Dart. Imports may only be at the beginning of a library.


final apiKey = Platform.environment['GOOGLE_API_KEY'];
if (apiKey == null) {
print('No \$GOOGLE_API_KEY environment variable');
exit(1);
}
final model = GenerativeModel(
model: 'gemini-1.5-flash',
apiKey: apiKey,
);
final prompt = 'Write a story about a magic backpack.';
final prompt = 'Explain how AI works';

final response = await model.generateContent([Content.text(prompt)]);
print(response.text);
Expand All @@ -44,7 +51,7 @@ Future<void> textGenTextOnlyPromptStreaming() async {
model: 'gemini-1.5-flash',
apiKey: apiKey,
);
final prompt = 'Write a story about a magic backpack.';
final prompt = 'Explain how AI works';

final responses = model.generateContentStream([Content.text(prompt)]);
await for (final response in responses) {
Expand Down
Loading