Skip to content

Commit

Permalink
Connect to local Ollama instance (#2)
Browse files Browse the repository at this point in the history
- Connect to Ollama local instance
- Check for Ollama connection and models
- Retrieve models
- Pick mode
- Generate from a text prompt
- Chat capabilities
  • Loading branch information
Otacon authored Apr 30, 2024
1 parent 90036cb commit b276168
Show file tree
Hide file tree
Showing 13 changed files with 773 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/create_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
flutter pub global run intl_utils:generate
- name: Build artifact
run: flutter build web --base-href "/olpaka/"
run: flutter build web --release --base-href "/olpaka/"

- name: Upload static content
uses: actions/upload-pages-artifact@v3
Expand Down
8 changes: 5 additions & 3 deletions lib/app/dependency_injection.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

import 'package:get_it/get_it.dart';

void registerModules() {
var l = GetIt.instance;
import 'package:olpaka/chat/di.dart';
import 'package:olpaka/ollama/di.dart';

void registerModules() {
registerOllama();
registerChat();
}
75 changes: 75 additions & 0 deletions lib/app/http_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'package:dio/dio.dart';

class HttpClient {
final Dio _client;

HttpClient(this._client);

Future<HttpResponse> post(String endpoint, {required Object? data}) async {
Response<String> response;
try {
response = await _client.post(endpoint, data: data);
} on DioException catch (e) {
return _handleException(e);
}
return _handleResponse(response);
}

Future<HttpResponse> get(String endpoint) async {
Response<String> response;
try {
response = await _client.get(endpoint);
} on DioException catch (e) {
return _handleException(e);
}
return _handleResponse(response);
}

HttpResponse _handleResponse(Response<String> response) {
final int? statusCode = response.statusCode;
final String? data = response.data;
final String? message = response.statusMessage;
if (statusCode == null || data == null) {
return HttpResponseUnknownError();
}
if (statusCode < 200 && statusCode > 299) {
return HttpResponseError(statusCode, message);
}
return HttpResponseSuccess(data);
}

HttpResponse _handleException(DioException exception) {
if (exception.type == DioExceptionType.connectionError) {
var baseUrl = _client.options.baseUrl;
var isLocalhost =
baseUrl.contains("localhost") || baseUrl.contains("127.0.0.1");
if (isLocalhost) {
return HttpResponseConnectionError();
}
return HttpResponseUnknownError();
} else {
return HttpResponseUnknownError();
}
}
}

sealed class HttpResponse {}

class HttpResponseSuccess extends HttpResponse {
final String body;

HttpResponseSuccess(this.body);
}

class HttpResponseConnectionError extends HttpResponse {}

class HttpResponseUnknownError extends HttpResponse {}

class HttpResponseError extends HttpResponse {
final int code;
final String? message;

HttpResponseError(this.code, this.message);

}

7 changes: 7 additions & 0 deletions lib/app/logger.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:logger/logger.dart';

final logger = Logger(
filter: null,
printer: PrettyPrinter(),
output: null,
);
8 changes: 4 additions & 4 deletions lib/app/olpaka_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ class OlpakaApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'Olpaka',
themeMode: ThemeMode.system,
themeMode: ThemeMode.light,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.indigo,
brightness: Brightness.dark,
seedColor: Colors.blue,
brightness: Brightness.light,
),
useMaterial3: true,
),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.indigo,
seedColor: Colors.red,
brightness: Brightness.dark,
),
useMaterial3: true,
Expand Down
7 changes: 7 additions & 0 deletions lib/chat/di.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:get_it/get_it.dart';
import 'package:olpaka/chat/view_model.dart';

registerChat() {
final l = GetIt.instance;
l.registerFactory(() => ChatViewModel(l.get()));
}
Loading

0 comments on commit b276168

Please sign in to comment.