-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connect to local Ollama instance (#2)
- 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
Showing
13 changed files
with
773 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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,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(); | ||
} |
This file contains 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,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); | ||
|
||
} | ||
|
This file contains 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,7 @@ | ||
import 'package:logger/logger.dart'; | ||
|
||
final logger = Logger( | ||
filter: null, | ||
printer: PrettyPrinter(), | ||
output: null, | ||
); |
This file contains 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 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,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())); | ||
} |
Oops, something went wrong.