Skip to content

Commit

Permalink
feat: Add support for listing running Ollama models (#451)
Browse files Browse the repository at this point in the history
Co-authored-by: David Miguel <me@davidmiguel.com>
  • Loading branch information
HavenDV and davidmigloz authored Jun 13, 2024
1 parent 255f34c commit cfaa31f
Show file tree
Hide file tree
Showing 9 changed files with 742 additions and 5 deletions.
10 changes: 10 additions & 0 deletions packages/ollama_dart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Unofficial Dart client for [Ollama](https://ollama.ai/) API.
* [Models](#models)
+ [Create model](#create-model)
+ [List models](#list-models)
+ [List running models](#list-running-models)
+ [Show Model Information](#show-model-information)
+ [Pull a Model](#pull-a-model)
+ [Push a Model](#push-a-model)
Expand Down Expand Up @@ -192,6 +193,15 @@ final res = await client.listModels();
print(res.models);
```

#### List running models

Lists models currently loaded and their memory footprint.

```dart
final res = await client.listRunningModels();
print(res.models);
```

#### Show Model Information

Show details about a model including modelfile, template, parameters, license, and system prompt.
Expand Down
23 changes: 21 additions & 2 deletions packages/ollama_dart/lib/src/generated/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,25 @@ class OllamaClient {
return ModelsResponse.fromJson(_jsonDecode(r));
}

// ------------------------------------------
// METHOD: listRunningModels
// ------------------------------------------

/// List models that are running.
///
/// `GET` `http://localhost:11434/api/ps`
Future<ProcessResponse> listRunningModels() async {
final r = await makeRequest(
baseUrl: 'http://localhost:11434/api',
path: '/ps',
method: HttpMethod.get,
isMultipart: false,
requestType: '',
responseType: 'application/json',
);
return ProcessResponse.fromJson(_jsonDecode(r));
}

// ------------------------------------------
// METHOD: showModelInfo
// ------------------------------------------
Expand Down Expand Up @@ -567,7 +586,7 @@ class OllamaClient {
method: HttpMethod.post,
isMultipart: false,
requestType: 'application/json',
responseType: 'application/json',
responseType: 'application/x-ndjson',
body: request,
);
return PullModelResponse.fromJson(_jsonDecode(r));
Expand All @@ -593,7 +612,7 @@ class OllamaClient {
method: HttpMethod.post,
isMultipart: false,
requestType: 'application/json',
responseType: 'application/json',
responseType: 'application/x-ndjson',
body: request,
);
return PushModelResponse.fromJson(_jsonDecode(r));
Expand Down
69 changes: 69 additions & 0 deletions packages/ollama_dart/lib/src/generated/schema/process_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: invalid_annotation_target
part of ollama_schema;

// ==========================================
// CLASS: ProcessModel
// ==========================================

/// A model that is currently loaded.
@freezed
class ProcessModel with _$ProcessModel {
const ProcessModel._();

/// Factory constructor for ProcessModel
const factory ProcessModel({
/// The model name.
///
/// Model names follow a `model:tag` format. Some examples are `orca-mini:3b-q4_1` and `llama3:70b`. The tag is optional and, if not provided, will default to `latest`. The tag is used to identify a specific version.
@JsonKey(includeIfNull: false) String? model,

/// Size of the model on disk.
@JsonKey(includeIfNull: false) int? size,

/// The model's digest.
@JsonKey(includeIfNull: false) String? digest,

/// Details about a model.
@JsonKey(includeIfNull: false) ModelDetails? details,

/// No Description
@JsonKey(name: 'expires_at', includeIfNull: false) String? expiresAt,

/// Size of the model on disk.
@JsonKey(name: 'size_vram', includeIfNull: false) int? sizeVram,
}) = _ProcessModel;

/// Object construction from a JSON representation
factory ProcessModel.fromJson(Map<String, dynamic> json) =>
_$ProcessModelFromJson(json);

/// List of all property names of schema
static const List<String> propertyNames = [
'model',
'size',
'digest',
'details',
'expires_at',
'size_vram'
];

/// Perform validations on the schema property values
String? validateSchema() {
return null;
}

/// Map representation of object (not serialized)
Map<String, dynamic> toMap() {
return {
'model': model,
'size': size,
'digest': digest,
'details': details,
'expires_at': expiresAt,
'size_vram': sizeVram,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: invalid_annotation_target
part of ollama_schema;

// ==========================================
// CLASS: ProcessResponse
// ==========================================

/// Response class for the list running models endpoint.
@freezed
class ProcessResponse with _$ProcessResponse {
const ProcessResponse._();

/// Factory constructor for ProcessResponse
const factory ProcessResponse({
/// List of running models.
@JsonKey(includeIfNull: false) List<ProcessModel>? models,
}) = _ProcessResponse;

/// Object construction from a JSON representation
factory ProcessResponse.fromJson(Map<String, dynamic> json) =>
_$ProcessResponseFromJson(json);

/// List of all property names of schema
static const List<String> propertyNames = ['models'];

/// Perform validations on the schema property values
String? validateSchema() {
return null;
}

/// Map representation of object (not serialized)
Map<String, dynamic> toMap() {
return {
'models': models,
};
}
}
2 changes: 2 additions & 0 deletions packages/ollama_dart/lib/src/generated/schema/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ part 'create_model_status.dart';
part 'models_response.dart';
part 'model.dart';
part 'model_details.dart';
part 'process_response.dart';
part 'process_model.dart';
part 'model_info_request.dart';
part 'model_info.dart';
part 'copy_model_request.dart';
Expand Down
Loading

0 comments on commit cfaa31f

Please sign in to comment.