workouttest_util/lib/service/openai_service.dart
2023-05-11 08:10:17 +02:00

55 lines
1.6 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:workouttest_util/model/openai.dart';
import 'package:workouttest_util/model/openai_chat.dart';
import 'package:workouttest_util/service/api.dart';
import 'package:workouttest_util/util/logging.dart';
class OpenAIApi with Logging {
final String modelDavinci = "text-davinci-003";
final String modelGpt35 = "gpt-3.5-turbo";
final String modelGpt4 = "gpt-4";
final String modelAda = "text-embedding-ada-002";
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 ?? "";
}
Future<String> getOpenAIChatCompletion(OpenAIChat openai) async {
String? response;
try {
String body = const JsonEncoder().convert(openai.toJson());
response = await _client.post("openai/chat_completion", body);
} on TimeoutException catch (_) {
log("Timeout from OpenAI");
} on Exception catch (e) {
log(e.toString());
}
return response ?? "";
}
}