Skip to content
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

Sync main and feature/changelog branches #87

Merged
merged 15 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions app/assets/graphics/logos/open_local_ui.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions app/assets/l10n/intl_en.arb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"@@locale": "en",
"deviceUnpluggedSnackBar": "Device unplugged. Power delivery reduced.",
"devicePluggedInSnackBar": "Device plugged in. Power delivery increased.",
"abortModelRemovalSnackBar": "Abort model removal",
"abortSessionRemovalSnackBar": "Abort session removal",
"aboutPageContributorsTitle": "Contributors",
Expand Down
10 changes: 10 additions & 0 deletions app/lib/backend/databases/chat_sessions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@ import 'package:hive_flutter/hive_flutter.dart';
import 'package:open_local_ui/backend/models/chat_session.dart';
import 'package:path_provider/path_provider.dart';

/// This class provides methods for saving, updating, deleting, and loading chat sessions.
///
/// The chat sessions are stored in a Hive database. This allows to easily save and load chat sessions in the JSON format.
///
/// The Hive Box used to store the chat sessions is named 'sessions'.
/// You can find the database files in the support directory of the app (see the output of [getApplicationSupportDirectory]).
class ChatSessionsDatabase {
/// Initializes the chat sessions database.
///
/// NOTE: This method must be called before any other methods in this class are called inside the current isolate.
static Future<void> init() async {
final dataDir = await getApplicationSupportDirectory();
Hive.init('${dataDir.path}/sessions');
}

/// Deinitializes the chat sessions database.
static Future<void> deinit() async {
await Hive.close();
}
Expand Down
41 changes: 40 additions & 1 deletion app/lib/backend/models/chat_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import 'package:freezed_annotation/freezed_annotation.dart';

part 'chat_message.g.dart';

/// Converts [Uint8List] to [Object] and vice versa for JSON serialization.
///
/// This class is used as a JSON converter for the [ChatUserMessageWrapper.imageBytes] property.
class ImageBytesJSONConverter implements JsonConverter<Uint8List?, Object?> {
const ImageBytesJSONConverter();

Expand All @@ -31,6 +34,9 @@ class ImageBytesJSONConverter implements JsonConverter<Uint8List?, Object?> {

enum ChatMessageSender { user, model, system }

/// Converts the [ChatMessageSender] object to JSON and vice versa.
///
/// This class is used as a JSON converter for the [ChatMessageWrapper.sender] property.
class ChatMessageSenderJSONConverter
implements JsonConverter<ChatMessageSender, String> {
const ChatMessageSenderJSONConverter();
Expand Down Expand Up @@ -62,7 +68,29 @@ class ChatMessageSenderJSONConverter
}
}

// NOTE: named with 'Wrapper' suffix to avoid conflict with LangChain
/// NOTE: named with 'Wrapper' suffix to avoid conflict with langchain.dart
///
/// This class is used to encapsulate the properties of a chat message.
///
/// The [ChatMessageWrapper] class is annotated with `@JsonSerializable` to enable JSON serialization and deserialization.
///
/// Properties:
/// - `text`: The text content of the chat message.
/// - `createdAt`: The date and time when the chat message was created.
/// - `uuid`: The unique identifier of the chat message.
/// - `senderName`: The name of the sender of the chat message (optional).
/// - `sender`: The sender of the chat message.
///
/// For metadata and usage statistics see [ChatResult.metadata] in langchain.dart.
/// - `totalDuration`: The total duration of the chat message.
/// - `loadDuration`: The duration it took to load the chat message.
/// - `promptEvalCount`: The number of prompt evaluations performed on the chat message.
/// - `promptEvalDuration`: The duration of prompt evaluations performed on the chat message.
/// - `evalCount`: The number of evaluations performed on the chat message.
/// - `evalDuration`: The duration of evaluations performed on the chat message.
/// - `promptTokens`: The number of prompt tokens in the chat message.
/// - `responseTokens`: The number of response tokens in the chat message.
/// - `totalTokens`: The total number of tokens in the chat message.
@JsonSerializable()
class ChatMessageWrapper {
String text;
Expand Down Expand Up @@ -120,6 +148,9 @@ class ChatMessageWrapper {
const ChatMessageSenderJSONConverter().toJson(object);
}

/// Represents a system message in the chat.
///
/// This class extends the [ChatMessageWrapper] class and sets the sender name and type to 'System'.
@JsonSerializable()
class ChatSystemMessageWrapper extends ChatMessageWrapper {
ChatSystemMessageWrapper(
Expand All @@ -141,6 +172,9 @@ class ChatSystemMessageWrapper extends ChatMessageWrapper {
Map<String, dynamic> toJson() => _$ChatSystemMessageWrapperToJson(this);
}

/// Represents a model message in the chat.
///
/// This class extends the [ChatMessageWrapper] class and sets the sender name and type to 'Model'.
@JsonSerializable()
class ChatModelMessageWrapper extends ChatMessageWrapper {
ChatModelMessageWrapper(
Expand All @@ -163,6 +197,11 @@ class ChatModelMessageWrapper extends ChatMessageWrapper {
Map<String, dynamic> toJson() => _$ChatModelMessageWrapperToJson(this);
}

/// Represents a user message in the chat.
///
/// This class extends the [ChatMessageWrapper] class and sets the sender name and type to 'User'.
///
/// The [ChatUserMessageWrapper] class also includes an optional [imageBytes] property to store image data for use with multimodal models.
@JsonSerializable()
class ChatUserMessageWrapper extends ChatMessageWrapper {
@JsonKey(
Expand Down
20 changes: 19 additions & 1 deletion app/lib/backend/models/chat_session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import 'package:open_local_ui/backend/models/chat_message.dart';

part 'chat_session.g.dart';

/// Converts JSON data to [ChatMessageWrapper] object and vice versa.
///
/// This class determines the type of [ChatMessageWrapper] object to be created based on the 'sender' property.
class ChatMessagesJSONConverter
implements
JsonConverter<List<ChatMessageWrapper>, List<Map<String, dynamic>>> {
Expand Down Expand Up @@ -37,7 +40,22 @@ enum ChatSessionStatus {
aborting,
}

// NOTE: named with 'Wrapper' suffix to avoid conflict with LangChain
/// NOTE: named with 'Wrapper' suffix to avoid conflict with lancghain.dart
///
/// This class is used to encapsulate the properties of a chat session.
///
/// The [ChatSessionWrapper] class is annotated with `@JsonSerializable` to enable JSON serialization and deserialization.
///
/// Properties:
/// - `title`: The title of the chat session.
/// - `createdAt`: The date and time when the chat session was created.
/// - `uuid`: The unique identifier of the chat session.
/// - `messages`: The list of chat messages associated with the chat session.
/// - `status`: The status of the chat session.
///
/// The [ChatSessionWrapper] class also contains a [memory] property of type [ConversationBufferMemory] for use by langchain.dart.
///
/// NOTE: In the future messages will be stored in an N-Ary tree structure to allow for branching conversations.
@JsonSerializable()
class ChatSessionWrapper {
String title;
Expand Down
10 changes: 10 additions & 0 deletions app/lib/backend/models/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import 'package:freezed_annotation/freezed_annotation.dart';

part 'model.g.dart';

/// This class is used to encapsulate the properties of an Ollama model.
///
/// The [Model] class is annotated with `@JsonSerializable` to enable JSON serialization and deserialization.
///
/// NOTE: The casing of the fields in the JSON data is forced to snake_case for interoperability with the Ollama REST API. This allows Ollama API responses to be converted to [Model] objects.
///
/// Model options are encapsulated in the [ModelSettings] class.
@JsonSerializable(fieldRename: FieldRename.snake)
class Model {
final String name;
Expand Down Expand Up @@ -45,6 +52,9 @@ class ModelDetails {
Map<String, dynamic> toJson() => _$ModelDetailsToJson(this);
}

/// This class is used to encapsulate the properties of the Ollama model settings.
///
/// For more information on the model settings, refer to the Ollama API documentation at https://github.com/ollama/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values.
@JsonSerializable()
class ModelSettings {
String? systemPrompt;
Expand Down
12 changes: 12 additions & 0 deletions app/lib/backend/models/ollama_responses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import 'package:open_local_ui/core/http.dart';

part 'ollama_responses.g.dart';

/// Represents a response from the Ollama API when pulling a model from registry.
///
/// This class extends the [HTTPStreamResponse] class and provides additional functionality specific to Ollama responses.

@JsonSerializable()
class OllamaPullResponse extends HTTPStreamResponse {
OllamaPullResponse({
Expand All @@ -20,6 +24,11 @@ class OllamaPullResponse extends HTTPStreamResponse {
Map<String, dynamic> toJson() => _$OllamaPullResponseToJson(this);
}

@JsonSerializable()

/// Represents a response from the Ollama API when pushing a model to the registry.
///
/// This class extends the [HTTPStreamResponse] class and provides additional functionality specific to OllamaPush responses.
@JsonSerializable()
class OllamaPushResponse extends HTTPStreamResponse {
OllamaPushResponse({
Expand All @@ -37,6 +46,9 @@ class OllamaPushResponse extends HTTPStreamResponse {
Map<String, dynamic> toJson() => _$OllamaPushResponseToJson(this);
}

/// Represents the response returned from the Ollama API when creating a new model locally.
///
/// This class extends the [HTTPStreamResponse] class and provides additional functionality specific to OllamaPush responses.
@JsonSerializable()
class OllamaCreateResponse extends HTTPStreamResponse {
OllamaCreateResponse({
Expand Down
Loading
Loading