workouttest_util/lib/service/openai_service.dart
2023-02-19 23:22:36 +01:00

37 lines
1.0 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:workouttest_util/model/openai.dart';
import 'package:workouttest_util/service/api.dart';
import 'package:workouttest_util/util/logging.dart';
class OpenAIApi with Logging {
final APIClient _client = APIClient();
Future<String> getOpenAICompletion(String question) async {
String? response;
try {
final body = await _client.post("openai/completion", question);
response = body;
} on TimeoutException catch (_) {
log("Timeout from OpenAI");
} on Exception catch (e) {
log(e.toString());
}
return response ?? "";
}
Future<String> getOpenAICompletionWithModel(OpenAI openai) async {
String? response;
try {
String body = const JsonEncoder().convert(openai.toJson());
response = await _client.post("openai/completion_with_model", body);
} on TimeoutException catch (_) {
log("Timeout from OpenAI");
} on Exception catch (e) {
log(e.toString());
}
return response ?? "";
}
}