Skip to content

Commit

Permalink
Full request is in a nested field
Browse files Browse the repository at this point in the history
Also add `model` to the general `GenerateContentRequest`. It is required
when using that pattern for counting tokens.
  • Loading branch information
natebosch committed May 22, 2024
1 parent 8acc0f2 commit 9a79968
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
19 changes: 10 additions & 9 deletions pkgs/google_generative_ai/lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@ final class GenerativeModel {
List<Tool>? tools,
ToolConfig? toolConfig,
}) async {
final response = await _client.makeRequest(
_taskUri(Task.countTokens),
_generateContentRequest(
contents,
safetySettings: safetySettings,
generationConfig: generationConfig,
tools: tools,
toolConfig: toolConfig,
));
final response = await _client.makeRequest(_taskUri(Task.countTokens), {
'generateContentRequest': _generateContentRequest(
contents,
safetySettings: safetySettings,
generationConfig: generationConfig,
tools: tools,
toolConfig: toolConfig,
)
});
return parseCountTokensResponse(response);
}

Expand Down Expand Up @@ -319,6 +319,7 @@ final class GenerativeModel {
tools ??= _tools;
toolConfig ??= _toolConfig;
return {
'model': '${_model.prefix}/${_model.name}',
'contents': contents.map((c) => c.toJson()).toList(),
if (safetySettings.isNotEmpty)
'safetySettings': safetySettings.map((s) => s.toJson()).toList(),
Expand Down
8 changes: 7 additions & 1 deletion samples/dart/bin/simple_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ void main() async {
stderr.writeln(r'No $GOOGLE_API_KEY environment variable');
exit(1);
}
final model = GenerativeModel(model: 'gemini-pro', apiKey: apiKey);
final model = GenerativeModel(
model: 'gemini-pro',
apiKey: apiKey,
safetySettings: [
SafetySetting(HarmCategory.dangerousContent, HarmBlockThreshold.high)
],
generationConfig: GenerationConfig(maxOutputTokens: 200));
final prompt = 'Write a story about a magic backpack.';
print('Prompt: $prompt');
final content = [Content.text(prompt)];
Expand Down

0 comments on commit 9a79968

Please sign in to comment.