-
Notifications
You must be signed in to change notification settings - Fork 94
initial port of dartantic_interface types #619
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
csells
wants to merge
14
commits into
flutter:main
Choose a base branch
from
csells:initial_primitives
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,625
−52
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4ad0ecd
initial port
csells 86b9f84
port to json_schema_builder
csells 2033059
updated tests
csells 9a41d31
changelog, pubspec
csells dbeae4a
pubspec
csells f301f5d
pana feedback
csells 48b1401
pana feedback
csells ae9bf1b
copyrights
csells c7cdf9a
Update packages/genai_primitives/lib/src/chat_message.dart
csells 4c6c8e2
Update packages/genai_primitives/lib/src/message_parts.dart
csells a95f613
formatting
csells d2007d5
Update packages/genai_primitives/lib/src/message_parts.dart
csells 4f3b62f
Update packages/genai_primitives/lib/src/chat_message.dart
csells 3f1169d
double quotes
csells File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| TODO: describe initial version here | ||
| # `genai_primitives` Changelog | ||
|
|
||
| ## 0.1.0 | ||
|
|
||
| - Initial release |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // Copyright 2025 The Flutter Authors. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'dart:convert'; | ||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:genai_primitives/genai_primitives.dart'; | ||
| import 'package:json_schema_builder/json_schema_builder.dart'; | ||
|
|
||
| void main() { | ||
| print('--- GenAI Primitives Example ---'); | ||
|
|
||
| // 1. Define a Tool | ||
| final ToolDefinition<Object> getWeatherTool = ToolDefinition( | ||
| name: 'get_weather', | ||
| description: 'Get the current weather for a location', | ||
| inputSchema: Schema.object( | ||
| properties: { | ||
| 'location': Schema.string( | ||
| description: 'The city and state, e.g. San Francisco, CA', | ||
| ), | ||
| 'unit': Schema.string( | ||
| enumValues: ['celsius', 'fahrenheit'], | ||
| description: 'The unit of temperature', | ||
| ), | ||
| }, | ||
| required: ['location'], | ||
| ), | ||
| ); | ||
|
|
||
| print('\n[Tool Definition]'); | ||
| print(const JsonEncoder.withIndent(' ').convert(getWeatherTool.toJson())); | ||
|
|
||
| // 2. Create a conversation history | ||
| final history = <ChatMessage>[ | ||
| // System message | ||
| ChatMessage.system( | ||
| 'You are a helpful weather assistant. ' | ||
| 'Use the get_weather tool when needed.', | ||
| ), | ||
|
|
||
| // User message asking for weather | ||
| ChatMessage.user('What is the weather in London?'), | ||
| ]; | ||
|
|
||
| print('\n[Initial Conversation]'); | ||
| for (final msg in history) { | ||
| print('${msg.role.name}: ${msg.text}'); | ||
| } | ||
|
|
||
| // 3. Simulate Model Response with Tool Call | ||
| final modelResponse = ChatMessage.model( | ||
| '', // Empty text for tool call | ||
| parts: [ | ||
| const TextPart('Thinking: User wants weather for London...'), | ||
| const ToolPart.call( | ||
| id: 'call_123', | ||
| name: 'get_weather', | ||
| arguments: {'location': 'London', 'unit': 'celsius'}, | ||
| ), | ||
| ], | ||
| ); | ||
| history.add(modelResponse); | ||
|
|
||
| print('\n[Model Response with Tool Call]'); | ||
| if (modelResponse.hasToolCalls) { | ||
| for (final ToolPart call in modelResponse.toolCalls) { | ||
| print('Tool Call: ${call.name}(${call.arguments})'); | ||
| } | ||
| } | ||
|
|
||
| // 4. Simulate Tool Execution & Result | ||
| final toolResult = ChatMessage.user( | ||
| '', // User role is typically used for tool results in many APIs | ||
| parts: [ | ||
| const ToolPart.result( | ||
| id: 'call_123', | ||
| name: 'get_weather', | ||
| result: {'temperature': 15, 'condition': 'Cloudy'}, | ||
| ), | ||
| ], | ||
| ); | ||
| history.add(toolResult); | ||
|
|
||
| print('\n[Tool Result]'); | ||
| print('Result: ${toolResult.toolResults.first.result}'); | ||
|
|
||
| // 5. Simulate Final Model Response with Data (e.g. an image generated or | ||
| // returned) | ||
| final finalResponse = ChatMessage.model( | ||
| 'Here is a chart of the weather trend:', | ||
| parts: [ | ||
| DataPart( | ||
| Uint8List.fromList([0x89, 0x50, 0x4E, 0x47]), // Fake PNG header | ||
| mimeType: 'image/png', | ||
| name: 'weather_chart.png', | ||
| ), | ||
| ], | ||
| ); | ||
| history.add(finalResponse); | ||
|
|
||
| print('\n[Final Model Response with Data]'); | ||
| print('Text: ${finalResponse.text}'); | ||
| if (finalResponse.parts.any((p) => p is DataPart)) { | ||
| final DataPart dataPart = finalResponse.parts.whereType<DataPart>().first; | ||
| print( | ||
| 'Attachment: ${dataPart.name} ' | ||
| '(${dataPart.mimeType}, ${dataPart.bytes.length} bytes)', | ||
| ); | ||
| } | ||
|
|
||
| // 6. Demonstrate JSON serialization of the whole history | ||
| print('\n[Full History JSON]'); | ||
| print( | ||
| const JsonEncoder.withIndent( | ||
| ' ', | ||
| ).convert(history.map((m) => m.toJson()).toList()), | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // Copyright 2025 The Flutter Authors. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
|
|
||
| import 'message_parts.dart'; | ||
| import 'utils.dart'; | ||
|
|
||
| /// A message in a conversation between a user and a model. | ||
| @immutable | ||
| class ChatMessage { | ||
| /// Creates a new message. | ||
| const ChatMessage({ | ||
| required this.role, | ||
| required this.parts, | ||
| this.metadata = const {}, | ||
| }); | ||
|
|
||
| /// Creates a message from a JSON-compatible map. | ||
| factory ChatMessage.fromJson(Map<String, dynamic> json) => ChatMessage( | ||
| role: ChatMessageRole.values.byName(json['role'] as String), | ||
| parts: (json['parts'] as List<dynamic>) | ||
| .map((p) => Part.fromJson(p as Map<String, dynamic>)) | ||
| .toList(), | ||
| metadata: (json['metadata'] as Map<String, dynamic>?) ?? const {}, | ||
| ); | ||
|
|
||
| /// Creates a system message. | ||
| factory ChatMessage.system( | ||
| String text, { | ||
| List<Part> parts = const [], | ||
| Map<String, dynamic>? metadata, | ||
| }) => ChatMessage( | ||
| role: ChatMessageRole.system, | ||
| parts: [TextPart(text), ...parts], | ||
| metadata: metadata ?? const {}, | ||
| ); | ||
|
|
||
| /// Creates a user message with text. | ||
| factory ChatMessage.user( | ||
| String text, { | ||
| List<Part> parts = const [], | ||
| Map<String, dynamic>? metadata, | ||
| }) => ChatMessage( | ||
| role: ChatMessageRole.user, | ||
| parts: [TextPart(text), ...parts], | ||
| metadata: metadata ?? const {}, | ||
| ); | ||
|
|
||
| /// Creates a model message with text. | ||
| factory ChatMessage.model( | ||
| String text, { | ||
| List<Part> parts = const [], | ||
| Map<String, dynamic>? metadata, | ||
| }) => ChatMessage( | ||
| role: ChatMessageRole.model, | ||
| parts: [TextPart(text), ...parts], | ||
| metadata: metadata ?? const {}, | ||
| ); | ||
|
|
||
| /// The role of the message author. | ||
| final ChatMessageRole role; | ||
|
|
||
| /// The content parts of the message. | ||
| final List<Part> parts; | ||
|
|
||
| /// Optional metadata associated with this message. | ||
| /// Can include information like suppressed content, warnings, etc. | ||
| final Map<String, dynamic> metadata; | ||
|
|
||
| /// Gets the text content of the message by concatenating all text parts. | ||
| String get text => parts.whereType<TextPart>().map((p) => p.text).join(); | ||
|
|
||
| /// Checks if this message contains any tool calls. | ||
| bool get hasToolCalls => | ||
| parts.whereType<ToolPart>().any((p) => p.kind == ToolPartKind.call); | ||
|
|
||
| /// Gets all tool calls in this message. | ||
| List<ToolPart> get toolCalls => parts | ||
| .whereType<ToolPart>() | ||
| .where((p) => p.kind == ToolPartKind.call) | ||
| .toList(); | ||
|
|
||
| /// Checks if this message contains any tool results. | ||
| bool get hasToolResults => | ||
| parts.whereType<ToolPart>().any((p) => p.kind == ToolPartKind.result); | ||
|
|
||
| /// Gets all tool results in this message. | ||
| List<ToolPart> get toolResults => parts | ||
| .whereType<ToolPart>() | ||
| .where((p) => p.kind == ToolPartKind.result) | ||
| .toList(); | ||
|
|
||
| /// Converts the message to a JSON-compatible map. | ||
| Map<String, dynamic> toJson() => { | ||
| 'role': role.name, | ||
| 'parts': parts.map((p) => p.toJson()).toList(), | ||
| 'metadata': metadata, | ||
| }; | ||
|
|
||
| @override | ||
| bool operator ==(Object other) => | ||
| identical(this, other) || | ||
| other is ChatMessage && | ||
| runtimeType == other.runtimeType && | ||
| role == other.role && | ||
| listEquals(parts, other.parts) && | ||
| mapEquals(metadata, other.metadata); | ||
|
|
||
| @override | ||
| int get hashCode => Object.hash( | ||
| role, | ||
| Object.hashAll(parts), | ||
| Object.hashAll(metadata.entries), | ||
| ); | ||
|
|
||
| @override | ||
| String toString() => | ||
| 'Message(role: $role, parts: $parts, metadata: $metadata)'; | ||
| } | ||
|
|
||
| /// The role of a message author. | ||
| enum ChatMessageRole { | ||
| /// A message from the system that sets context or instructions. | ||
| system, | ||
|
|
||
| /// A message from the end user. | ||
| user, | ||
|
|
||
| /// A message from the model. | ||
| model, | ||
| } | ||
10 changes: 0 additions & 10 deletions
10
packages/genai_primitives/lib/src/genai_primitives_base.dart
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add more details to doc strings for enum and its items?
I can represent a confused user here:
doc for enum is saying that each item is a role of a message author, while doc for each item is describing type of message, not author
can you elaborate what is 'system'?
documentation is saying 'from', but not 'to'. Is it correct that system always send messages to model? If yes, should we specify it here? And the same about model: is it correct that model's messages are intended to be handled by system and then system will show to the user whatever is intended for user?
or my assumptions above are completely wrong and the enum relates to difference between 'user prompt' and 'system prompt' somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe in this library instead of ChatMessage having field
ChatMessageRole role, it make sense for it to have the field 'String type', so that every dependent can define their set of message types.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option:
Then users will be able to pass their enums for the message type. Maybe some of them will have more than one model and more than one system.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, we can remove type completely, so that users just derive from ChatMessage and add whatever fields they want to add. Or users can compose ChatMessage into another object, that contains envelope information.
This seems to be cleanest option for me.