Skip to content

Commit

Permalink
Added Logic to use keys from users profile if available
Browse files Browse the repository at this point in the history
  • Loading branch information
Ketan Choyal committed Mar 15, 2024
1 parent 4645e8b commit f4992e2
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 12 deletions.
4 changes: 3 additions & 1 deletion lib/app/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class App extends StatelessWidget {
create: (context) => OpenFoodAPIServiceImpl(),
),
RepositoryProvider<OpenAIService>(
create: (context) => OpenAIServiceImpl()..init(),
create: (context) => OpenAIServiceImpl(
RepositoryProvider.of<FirebaseService>(context),
)..init(),
),
RepositoryProvider<FirebaseAuthService>(
create: (context) => FirebaseAuthServiceImpl(),
Expand Down
22 changes: 22 additions & 0 deletions lib/core/models/profile/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ part 'profile.g.dart';
class Profile with _$Profile {
factory Profile({
Goals? goals,
Keys? keys,
}) = _Profile;

Profile._();
Expand Down Expand Up @@ -46,3 +47,24 @@ class Goals with _$Goals {

Map<String, dynamic> toJson() => _$GoalsToJson(this);
}

@JsonSerializable(
createToJson: true,
explicitToJson: true,
)
@Freezed(
fromJson: false,
toJson: false,
)
class Keys with _$Keys {
factory Keys({
required String openAiToken,
required String orgId,
}) = _Keys;

Keys._();

factory Keys.fromJson(Map<String, dynamic> json) => _$KeysFromJson(json);

Map<String, dynamic> toJson() => _$KeysToJson(this);
}
178 changes: 171 additions & 7 deletions lib/core/models/profile/profile.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final _privateConstructorUsedError = UnsupportedError(
/// @nodoc
mixin _$Profile {
Goals? get goals => throw _privateConstructorUsedError;
Keys? get keys => throw _privateConstructorUsedError;

@JsonKey(ignore: true)
$ProfileCopyWith<Profile> get copyWith => throw _privateConstructorUsedError;
Expand All @@ -27,9 +28,10 @@ abstract class $ProfileCopyWith<$Res> {
factory $ProfileCopyWith(Profile value, $Res Function(Profile) then) =
_$ProfileCopyWithImpl<$Res, Profile>;
@useResult
$Res call({Goals? goals});
$Res call({Goals? goals, Keys? keys});

$GoalsCopyWith<$Res>? get goals;
$KeysCopyWith<$Res>? get keys;
}

/// @nodoc
Expand All @@ -46,12 +48,17 @@ class _$ProfileCopyWithImpl<$Res, $Val extends Profile>
@override
$Res call({
Object? goals = freezed,
Object? keys = freezed,
}) {
return _then(_value.copyWith(
goals: freezed == goals
? _value.goals
: goals // ignore: cast_nullable_to_non_nullable
as Goals?,
keys: freezed == keys
? _value.keys
: keys // ignore: cast_nullable_to_non_nullable
as Keys?,
) as $Val);
}

Expand All @@ -66,6 +73,18 @@ class _$ProfileCopyWithImpl<$Res, $Val extends Profile>
return _then(_value.copyWith(goals: value) as $Val);
});
}

@override
@pragma('vm:prefer-inline')
$KeysCopyWith<$Res>? get keys {
if (_value.keys == null) {
return null;
}

return $KeysCopyWith<$Res>(_value.keys!, (value) {
return _then(_value.copyWith(keys: value) as $Val);
});
}
}

/// @nodoc
Expand All @@ -75,10 +94,12 @@ abstract class _$$ProfileImplCopyWith<$Res> implements $ProfileCopyWith<$Res> {
__$$ProfileImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({Goals? goals});
$Res call({Goals? goals, Keys? keys});

@override
$GoalsCopyWith<$Res>? get goals;
@override
$KeysCopyWith<$Res>? get keys;
}

/// @nodoc
Expand All @@ -93,39 +114,47 @@ class __$$ProfileImplCopyWithImpl<$Res>
@override
$Res call({
Object? goals = freezed,
Object? keys = freezed,
}) {
return _then(_$ProfileImpl(
goals: freezed == goals
? _value.goals
: goals // ignore: cast_nullable_to_non_nullable
as Goals?,
keys: freezed == keys
? _value.keys
: keys // ignore: cast_nullable_to_non_nullable
as Keys?,
));
}
}

/// @nodoc
class _$ProfileImpl extends _Profile {
_$ProfileImpl({this.goals}) : super._();
_$ProfileImpl({this.goals, this.keys}) : super._();

@override
final Goals? goals;
@override
final Keys? keys;

@override
String toString() {
return 'Profile(goals: $goals)';
return 'Profile(goals: $goals, keys: $keys)';
}

@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$ProfileImpl &&
(identical(other.goals, goals) || other.goals == goals));
(identical(other.goals, goals) || other.goals == goals) &&
(identical(other.keys, keys) || other.keys == keys));
}

@override
int get hashCode => Object.hash(runtimeType, goals);
int get hashCode => Object.hash(runtimeType, goals, keys);

@JsonKey(ignore: true)
@override
Expand All @@ -135,12 +164,14 @@ class _$ProfileImpl extends _Profile {
}

abstract class _Profile extends Profile {
factory _Profile({final Goals? goals}) = _$ProfileImpl;
factory _Profile({final Goals? goals, final Keys? keys}) = _$ProfileImpl;
_Profile._() : super._();

@override
Goals? get goals;
@override
Keys? get keys;
@override
@JsonKey(ignore: true)
_$$ProfileImplCopyWith<_$ProfileImpl> get copyWith =>
throw _privateConstructorUsedError;
Expand Down Expand Up @@ -318,3 +349,136 @@ abstract class _Goals extends Goals {
_$$GoalsImplCopyWith<_$GoalsImpl> get copyWith =>
throw _privateConstructorUsedError;
}

/// @nodoc
mixin _$Keys {
String get openAiToken => throw _privateConstructorUsedError;
String get orgId => throw _privateConstructorUsedError;

@JsonKey(ignore: true)
$KeysCopyWith<Keys> get copyWith => throw _privateConstructorUsedError;
}

/// @nodoc
abstract class $KeysCopyWith<$Res> {
factory $KeysCopyWith(Keys value, $Res Function(Keys) then) =
_$KeysCopyWithImpl<$Res, Keys>;
@useResult
$Res call({String openAiToken, String orgId});
}

/// @nodoc
class _$KeysCopyWithImpl<$Res, $Val extends Keys>
implements $KeysCopyWith<$Res> {
_$KeysCopyWithImpl(this._value, this._then);

// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;

@pragma('vm:prefer-inline')
@override
$Res call({
Object? openAiToken = null,
Object? orgId = null,
}) {
return _then(_value.copyWith(
openAiToken: null == openAiToken
? _value.openAiToken
: openAiToken // ignore: cast_nullable_to_non_nullable
as String,
orgId: null == orgId
? _value.orgId
: orgId // ignore: cast_nullable_to_non_nullable
as String,
) as $Val);
}
}

/// @nodoc
abstract class _$$KeysImplCopyWith<$Res> implements $KeysCopyWith<$Res> {
factory _$$KeysImplCopyWith(
_$KeysImpl value, $Res Function(_$KeysImpl) then) =
__$$KeysImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({String openAiToken, String orgId});
}

/// @nodoc
class __$$KeysImplCopyWithImpl<$Res>
extends _$KeysCopyWithImpl<$Res, _$KeysImpl>
implements _$$KeysImplCopyWith<$Res> {
__$$KeysImplCopyWithImpl(_$KeysImpl _value, $Res Function(_$KeysImpl) _then)
: super(_value, _then);

@pragma('vm:prefer-inline')
@override
$Res call({
Object? openAiToken = null,
Object? orgId = null,
}) {
return _then(_$KeysImpl(
openAiToken: null == openAiToken
? _value.openAiToken
: openAiToken // ignore: cast_nullable_to_non_nullable
as String,
orgId: null == orgId
? _value.orgId
: orgId // ignore: cast_nullable_to_non_nullable
as String,
));
}
}

/// @nodoc
class _$KeysImpl extends _Keys {
_$KeysImpl({required this.openAiToken, required this.orgId}) : super._();

@override
final String openAiToken;
@override
final String orgId;

@override
String toString() {
return 'Keys(openAiToken: $openAiToken, orgId: $orgId)';
}

@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$KeysImpl &&
(identical(other.openAiToken, openAiToken) ||
other.openAiToken == openAiToken) &&
(identical(other.orgId, orgId) || other.orgId == orgId));
}

@override
int get hashCode => Object.hash(runtimeType, openAiToken, orgId);

@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$KeysImplCopyWith<_$KeysImpl> get copyWith =>
__$$KeysImplCopyWithImpl<_$KeysImpl>(this, _$identity);
}

abstract class _Keys extends Keys {
factory _Keys(
{required final String openAiToken,
required final String orgId}) = _$KeysImpl;
_Keys._() : super._();

@override
String get openAiToken;
@override
String get orgId;
@override
@JsonKey(ignore: true)
_$$KeysImplCopyWith<_$KeysImpl> get copyWith =>
throw _privateConstructorUsedError;
}
14 changes: 14 additions & 0 deletions lib/core/models/profile/profile.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions lib/core/services/open_ai/open_ai_serice.impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ import 'dart:convert';

import 'package:calorie_tracker/core/enums/serving_type.enum.dart';
import 'package:calorie_tracker/core/models/food/food.dart';
import 'package:calorie_tracker/core/services/firebase/firebase_service.dart';
import 'package:calorie_tracker/core/services/open_ai/open_ai_service.dart';
import 'package:calorie_tracker/keys.dart';
import 'package:dart_openai/dart_openai.dart';

class OpenAIServiceImpl extends OpenAIService {
OpenAIServiceImpl();
final FirebaseService _firebaseService;
OpenAIServiceImpl(this._firebaseService);

init() {
_firebaseService.getProfileData().then((profile) {
if (profile != null && profile.keys != null) {
OpenAI.organization = profile.keys!.orgId;
OpenAI.apiKey = profile.keys!.openAiToken;
}
});
OpenAI.showLogs = true;
OpenAI.showResponsesLogs = true;
OpenAI.organization = orogId;
OpenAI.apiKey = openAiToken;
}

// the system message that will be sent to the request.
Expand Down

0 comments on commit f4992e2

Please sign in to comment.