From cb90c3d68a8af11ccabb90cf3743bb10cee927a0 Mon Sep 17 00:00:00 2001 From: Tibor Bossanyi Date: Sun, 19 Feb 2023 23:22:36 +0100 Subject: [PATCH] v1.0.7 openai with model name and temperature --- README.md | 3 +++ lib/model/membership.dart | 8 ++++---- lib/model/openai.dart | 22 ++++++++++++++++++++++ lib/model/product.dart | 18 +++++++++--------- lib/model/property.dart | 6 +++--- lib/model/training_plan.dart | 6 +++--- lib/service/openai_service.dart | 15 +++++++++++++++ pubspec.yaml | 2 +- test/openai_test.dart | 12 ++++++++++-- 9 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 lib/model/openai.dart diff --git a/README.md b/README.md index 71475bd..46f40ba 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ Workout Test and Diet 4 You Common Util Functions +Version 1.0.7 + openai with model name and temperature + Version 1.0.6 membership, customer_membership diff --git a/lib/model/membership.dart b/lib/model/membership.dart index d8c7372..b46f417 100644 --- a/lib/model/membership.dart +++ b/lib/model/membership.dart @@ -1,7 +1,7 @@ class Membership { late int membershipId; late String name; - late String description; + String? description; late int duration; late String durationType; late String durationUnit; @@ -12,12 +12,12 @@ class Membership { Membership.fromJson(Map json) { membershipId = json['membershipId']; name = json['name']; - description = json['description']; + description = json['description'] ?? ""; duration = json['duration']; durationUnit = json['durationUnit']; durationType = json['durationType']; - trainingPlanId = json['trainingPlanId']; - trainingPlanDayIds = json['trainingPlanDayIds']; + trainingPlanId = json['trainingPlanId'] ?? 0; + trainingPlanDayIds = json['trainingPlanDayIds'] ?? 0; } Map toJson() => { diff --git a/lib/model/openai.dart b/lib/model/openai.dart new file mode 100644 index 0000000..19fa1b5 --- /dev/null +++ b/lib/model/openai.dart @@ -0,0 +1,22 @@ +class OpenAI { + late String question; + late String modelName; + late double temperature; + + OpenAI(this.question, this.modelName, this.temperature); + + OpenAI.fromJson(Map json) { + question = json['question']; + modelName = json['modelName']; + temperature = json['temperature']; + } + +Map toJson() => { + "question": question, + "modelName": modelName, + "temperature": temperature, + }; + + @override + String toString() => toJson().toString(); +} \ No newline at end of file diff --git a/lib/model/product.dart b/lib/model/product.dart index 27b9950..09519b0 100644 --- a/lib/model/product.dart +++ b/lib/model/product.dart @@ -7,11 +7,11 @@ class Product { late int sort; late int productSet; late DateTime validFrom; - late DateTime? validTo; - late String? productIdIos; - late String? productIdAndroid; - late double? priceIos; - late double? priceAndroid; + DateTime? validTo; + String? productIdIos; + String? productIdAndroid; + double? priceIos; + double? priceAndroid; String? localizedPrice; Product.fromJson(Map json) { @@ -24,10 +24,10 @@ class Product { productSet = json['productSet']; validFrom = (json['validFrom'] == null ? null : DateTime.parse(json['validFrom']))!; validTo = json['validTo'] == null ? null : DateTime.parse(json['validTo']); - productIdIos = json['productIdIos']; - productIdAndroid = json['productIdAndroid']; - priceIos = json['priceIos']; - priceAndroid = json['priceAndroid']; + productIdIos = json['productIdIos'] ?? 0.0; + productIdAndroid = json['productIdAndroid'] ?? 0.0; + priceIos = json['priceIos'] ?? 0.0; + priceAndroid = json['priceAndroid'] ?? 0.0; } @override diff --git a/lib/model/property.dart b/lib/model/property.dart index 280397c..051934a 100644 --- a/lib/model/property.dart +++ b/lib/model/property.dart @@ -1,8 +1,8 @@ class Property { late int propertyId; late String propertyName; - late String propertyUnit; - late String propertyNameTranslation; + String? propertyUnit; + String? propertyNameTranslation; int? top; int? left; double? value; @@ -10,7 +10,7 @@ class Property { Property.fromJson(Map json) { propertyId = json['propertyId']; propertyName = json['propertyName']; - propertyUnit = json['propertyUnit']; + propertyUnit = json['propertyUnit'] ?? ""; propertyNameTranslation = json['translations'] != null && (json['translations']).length > 0 ? json['translations'][0]['propertyName'] diff --git a/lib/model/training_plan.dart b/lib/model/training_plan.dart index 48288c0..0f861e0 100644 --- a/lib/model/training_plan.dart +++ b/lib/model/training_plan.dart @@ -20,9 +20,9 @@ class TrainingPlan { TrainingPlan.fromJson(Map json) { trainingPlanId = json['trainingPlanId']; name = json['name']; - type = json['type']; - internalName = json['internalName']; - description = json['description']; + type = json['type'] ?? ""; + internalName = json['internalName'] ?? ""; + description = json['description'] ?? ""; free = json['free']; active = json['active']; treeId = json['treeId']; diff --git a/lib/service/openai_service.dart b/lib/service/openai_service.dart index 2621447..3231f8f 100644 --- a/lib/service/openai_service.dart +++ b/lib/service/openai_service.dart @@ -1,5 +1,7 @@ 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'; @@ -19,4 +21,17 @@ class OpenAIApi with Logging { } return response ?? ""; } + + Future 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 ?? ""; + } } \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 9495f99..785e1ae 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: workouttest_util description: Workout Test app and web functions. -version: 1.0.6 +version: 1.0.7 homepage: environment: diff --git a/test/openai_test.dart b/test/openai_test.dart index a47172a..e835925 100644 --- a/test/openai_test.dart +++ b/test/openai_test.dart @@ -1,5 +1,6 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:workouttest_util/model/cache.dart'; +import 'package:workouttest_util/model/openai.dart'; import 'package:workouttest_util/service/openai_service.dart'; @@ -9,11 +10,18 @@ void main() { }); test('openai response succesful', () async { - - var api = OpenAIApi(); String response = await api.getOpenAICompletion("Who wrote the song 'yellow submarine'?"); print(response); expect(response, matches(RegExp(r'Beatles') )); }); + + test('openai with model response succesful', () async { + var api = OpenAIApi(); + String question = "Készíts egy heti egészséges és változatos étrendet egy új felhasználónak az alábbi adatok alapján: férfi, 51 éves. Célja: Le szeretnék fogyni, heti mozgás: Hetente 3-4 alkalom, BMI: 24.784257517393772, BMR: 1723.75. A neve Tibi. Az egyes étkezések különbözőek legyenek, és add meg hozzájuk a mennyiséget és a kalóriatartalmat is. Vedd figyelembe, hogy a napi összes kalóriaérték 200-400 kCal-val kevesebb legyen, mint 1723.75 kCal"; + var openai = OpenAI(question, "text-davinci-003", 0.5); + String response = await api.getOpenAICompletionWithModel(openai); + print(response); + expect(response, matches(RegExp(r'Tibi') )); + }); }