Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
danemadsen committed Feb 10, 2025
1 parent 097ca1e commit d5d0b0e
Show file tree
Hide file tree
Showing 10 changed files with 399 additions and 422 deletions.
46 changes: 18 additions & 28 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ class _LlamaAppState extends State<LlamaApp> {

void _loadModel() async {
final result = await FilePicker.platform.pickFiles(
dialogTitle: "Load Model File",
type: FileType.any,
allowMultiple: false,
allowCompression: false
);

if (result == null || result.files.isEmpty || result.files.single.path == null) {
dialogTitle: "Load Model File",
type: FileType.any,
allowMultiple: false,
allowCompression: false);

if (result == null ||
result.files.isEmpty ||
result.files.single.path == null) {
throw Exception('No file selected');
}

Expand All @@ -42,17 +43,9 @@ class _LlamaAppState extends State<LlamaApp> {
}

final llamaCpp = LlamaIsolated(
modelParams: ModelParams(
path: result.files.single.path!
),
contextParams: const ContextParams(
nCtx: 2048,
nBatch: 2048
),
samplingParams: const SamplingParams(
greedy: true
)
);
modelParams: ModelParams(path: result.files.single.path!),
contextParams: const ContextParams(nCtx: 2048, nBatch: 2048),
samplingParams: const SamplingParams(greedy: true));

setState(() {
_model = llamaCpp;
Expand Down Expand Up @@ -83,9 +76,7 @@ class _LlamaAppState extends State<LlamaApp> {

@override
Widget build(BuildContext context) {
return MaterialApp(
home: buildHome()
);
return MaterialApp(home: buildHome());
}

Widget buildHome() {
Expand All @@ -97,12 +88,11 @@ class _LlamaAppState extends State<LlamaApp> {

PreferredSizeWidget buildAppBar() {
return AppBar(
title: Text(_modelPath ?? 'No model loaded'),
leading: IconButton(
icon: const Icon(Icons.folder_open),
onPressed: _loadModel,
)
);
title: Text(_modelPath ?? 'No model loaded'),
leading: IconButton(
icon: const Icon(Icons.folder_open),
onPressed: _loadModel,
));
}

Widget buildBody() {
Expand Down Expand Up @@ -150,4 +140,4 @@ class _LlamaAppState extends State<LlamaApp> {
),
);
}
}
}
2 changes: 1 addition & 1 deletion lib/lcpp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ part 'src/llama_native.dart';
part 'src/params/model_params.dart';
part 'src/chat_message.dart';
part 'src/params/context_params.dart';
part 'src/params/sampling_params.dart';
part 'src/params/sampling_params.dart';
63 changes: 28 additions & 35 deletions lib/src/chat_message.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
part of '../lcpp.dart';

typedef _ChatMessageRecord = (
String role,
String content
);
typedef _ChatMessageRecord = (String role, String content);

/// An abstract class representing a chat message.
///
Expand All @@ -28,7 +25,7 @@ typedef _ChatMessageRecord = (
/// - `_ChatMessageRecord _toRecord()`: Converts the chat message to a record.
abstract class ChatMessage {
/// The role of the chat message sender.
///
///
/// This property represents the role of the entity that sent the chat message,
/// such as 'user', 'assistant', or any other defined role.
String get role;
Expand All @@ -37,7 +34,7 @@ abstract class ChatMessage {
String content;

/// Creates a new instance of [ChatMessage] with the given content.
///
///
/// The [content] parameter represents the message content.
ChatMessage(this.content);

Expand Down Expand Up @@ -69,10 +66,8 @@ abstract class ChatMessage {
}
}

factory ChatMessage._fromRecord(_ChatMessageRecord record) => ChatMessage.withRole(
role: record.$1,
content: record.$2
);
factory ChatMessage._fromRecord(_ChatMessageRecord record) =>
ChatMessage.withRole(role: record.$1, content: record.$2);

/// Creates a [ChatMessage] instance from a native [llama_chat_message].
///
Expand All @@ -89,10 +84,10 @@ abstract class ChatMessage {
/// - [message]: The native [llama_chat_message] object to be converted.
///
/// Returns a new [ChatMessage] instance with the role and content from the native message.
factory ChatMessage.fromNative(llama_chat_message message) => ChatMessage.withRole(
role: message.role.cast<Utf8>().toDartString(),
content: message.content.cast<Utf8>().toDartString()
);
factory ChatMessage.fromNative(llama_chat_message message) =>
ChatMessage.withRole(
role: message.role.cast<Utf8>().toDartString(),
content: message.content.cast<Utf8>().toDartString());

/// Converts the current chat message instance to its native representation.
///
Expand All @@ -113,10 +108,7 @@ abstract class ChatMessage {
return message.ref;
}

_ChatMessageRecord _toRecord() => (
role,
content
);
_ChatMessageRecord _toRecord() => (role, content);
}

/// A class representing a chat message from a user.
Expand All @@ -137,15 +129,15 @@ class UserChatMessage extends ChatMessage {
String get role => 'user';

/// A class representing a user chat message.
///
///
/// This class extends a base class and takes the content of the message as a parameter.
///
///
/// Example usage:
/// ```dart
/// var message = UserChatMessage('Hello, world!');
/// print(message.content); // Outputs: Hello, world!
/// ```
///
///
/// Parameters:
/// - `content`: The content of the chat message.
UserChatMessage(super.content);
Expand All @@ -170,50 +162,50 @@ class AssistantChatMessage extends ChatMessage {
String get role => 'assistant';

/// Represents a chat message from the assistant.
///
/// The [AssistantChatMessage] class extends a base class with the provided
///
/// The [AssistantChatMessage] class extends a base class with the provided
/// content of the message.
///
///
/// Example usage:
/// ```dart
/// var message = AssistantChatMessage('Hello, how can I assist you?');
/// print(message.content); // Output: Hello, how can I assist you?
/// ```
///
///
/// Parameters:
/// - `content`: The content of the chat message.
AssistantChatMessage(super.content);
}

/// A class representing a system-generated chat message.
///
///
/// This class extends the [ChatMessage] class and overrides the `role`
/// property to return 'system', indicating that the message is generated
/// by the system.
///
///
/// Example usage:
/// ```dart
/// var systemMessage = SystemChatMessage('System maintenance scheduled.');
/// print(systemMessage.role); // Outputs: system
/// print(systemMessage.content); // Outputs: System maintenance scheduled.
/// ```
///
///
/// The [SystemChatMessage] constructor takes a single parameter, `content`,
/// which represents the content of the chat message.
class SystemChatMessage extends ChatMessage {
@override
String get role => 'system';

/// Represents a system-generated chat message.
///
///
/// This class extends the base class and takes the content of the message as a parameter.
///
///
/// Example usage:
/// ```dart
/// var message = SystemChatMessage('System maintenance scheduled.');
/// print(message.content); // Outputs: System maintenance scheduled.
/// ```
///
///
/// Parameters:
/// - `content`: The content of the chat message.
SystemChatMessage(super.content);
Expand Down Expand Up @@ -261,18 +253,19 @@ extension ChatMessages on List<ChatMessage> {
}

/// Creates a copy of the list of `ChatMessage` objects.
///
///
/// This method iterates over the current list of `ChatMessage` instances,
/// creates a new `ChatMessage` for each one with the same role and content,
/// and returns a new list containing these copied messages.
///
///
/// Returns:
/// A new list of `ChatMessage` objects with the same role and content as the original list.
List<ChatMessage> copy() {
final List<ChatMessage> messages = [];

for (var message in this) {
messages.add(ChatMessage.withRole(role: message.role, content: message.content));
messages.add(
ChatMessage.withRole(role: message.role, content: message.content));
}

return messages;
Expand All @@ -288,4 +281,4 @@ extension _LlamaChatMessagePtrExtension on ffi.Pointer<llama_chat_message> {

calloc.free(this);
}
}
}
44 changes: 22 additions & 22 deletions lib/src/llama.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
part of '../lcpp.dart';

/// An abstract interface class representing a Llama library.
///
///
/// This class provides a factory constructor to create instances of either
/// `LlamaIsolated` or `LlamaNative` based on the `isolate` parameter. It also
/// provides a static getter to load the appropriate dynamic library based on
/// the platform.
///
///
/// The `Llama` class has the following members:
///
///
/// - `lib`: A static getter that returns an instance of the `llama` library,
/// loading the appropriate dynamic library based on the platform if it has
/// not been loaded already.
Expand All @@ -18,7 +18,7 @@ part of '../lcpp.dart';
/// a stream of strings.
/// - `stop`: A method to stop the Llama instance.
/// - `free`: A method to free the resources used by the Llama instance.
///
///
/// Throws an `LlamaException` if the platform is unsupported.
abstract interface class Llama {
static llama? _lib;
Expand All @@ -27,7 +27,7 @@ abstract interface class Llama {
///
/// This getter initializes the `_lib` field if it is `null` by loading the
/// appropriate dynamic library based on the current platform:
///
///
/// - On Windows, it loads `llama.dll`.
/// - On Linux or Android, it loads `libllama.so`.
/// - On macOS or iOS, it loads `llama.framework/llama`.
Expand All @@ -37,14 +37,11 @@ abstract interface class Llama {
if (_lib == null) {
if (Platform.isWindows) {
_lib = llama(ffi.DynamicLibrary.open('llama.dll'));
}
else if (Platform.isLinux || Platform.isAndroid) {
} else if (Platform.isLinux || Platform.isAndroid) {
_lib = llama(ffi.DynamicLibrary.open('libllama.so'));
}
else if (Platform.isMacOS || Platform.isIOS) {
} else if (Platform.isMacOS || Platform.isIOS) {
_lib = llama(ffi.DynamicLibrary.open('llama.framework/llama'));
}
else {
} else {
throw LlamaException('Unsupported platform');
}
}
Expand All @@ -64,19 +61,22 @@ abstract interface class Llama {
/// - [isolate]: If true, creates an instance of [LlamaIsolated], otherwise
/// creates an instance of [LlamaNative].
factory Llama({
required ModelParams modelParams,
required ModelParams modelParams,
ContextParams contextParams = const ContextParams(),
SamplingParams samplingParams = const SamplingParams(),
bool isolate = false,
}) => isolate ? LlamaIsolated(
modelParams: modelParams,
contextParams: contextParams,
samplingParams: samplingParams,
) : LlamaNative(
modelParams: modelParams,
contextParams: contextParams,
samplingParams: samplingParams,
);
}) =>
isolate
? LlamaIsolated(
modelParams: modelParams,
contextParams: contextParams,
samplingParams: samplingParams,
)
: LlamaNative(
modelParams: modelParams,
contextParams: contextParams,
samplingParams: samplingParams,
);

/// Generates a stream of responses based on the provided list of chat messages.
///
Expand All @@ -102,4 +102,4 @@ abstract interface class Llama {
/// This method should be called when the object is no longer needed
/// to release any allocated resources and avoid memory leaks.
void free();
}
}
2 changes: 1 addition & 1 deletion lib/src/llama_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ class LlamaException implements Exception {

@override
String toString() => message;
}
}
Loading

0 comments on commit d5d0b0e

Please sign in to comment.