Skip to content

Commit

Permalink
Convert local tools into native facts, and making heart rate measurem…
Browse files Browse the repository at this point in the history
…ent optional #53

This removes ugly bubbling through the function calling facilities of latlon and HR
  • Loading branch information
MrCsabaToth committed Sep 22, 2024
1 parent 85c4e55 commit 3933939
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 138 deletions.
38 changes: 25 additions & 13 deletions lib/ai/service/ai_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,28 +166,44 @@ class AiService with FirebaseMixin, ToolsMixin {

final nearestP13ns =
await database.getNearestPersonalization(userEmbedding);
final p13Stuffing = StringBuffer();
if (nearestP13ns.isNotEmpty) {
log('P13n ANN Peers ${nearestP13ns.map((p) => p.score)}');
final annThreshold = preferences.personalizationRagThreshold;
final p13Stuffing = StringBuffer();
for (final personalization
in nearestP13ns.where((p) => p.score < annThreshold)) {
p13Stuffing.writeln(
'<personalFact>${personalization.object.content}</personalFact>',
);
}
}

if (p13Stuffing.isNotEmpty) {
stuffedPrompt.writeln(
p13nStuffingTemplate.replaceAll(
p13nRagStuffingVariable,
p13Stuffing.toString(),
),
);
}
final gpsLocation = await GetIt.I.get<LocationService>().obtain();
p13Stuffing
..write(
"<personalFact>User's current immediate location: ",
)
..write(
'{"lat": ${gpsLocation.latitude}, ',
)
..writeln(
'"lon": ${gpsLocation.longitude}}</personalFact>',
);

if (preferences.measureHeartRate) {
final heartRate = GetIt.I.get<HeartRateService>().heartRate;
p13Stuffing.writeln(
"<personalFact>User's current heart rate: $heartRate</personalFact>",
);
}

stuffedPrompt
..writeln(
p13nStuffingTemplate.replaceAll(
p13nRagStuffingVariable,
p13Stuffing.toString(),
),
)
..write(
requestInstructionTemplate.replaceAll(
requestInstructionVariable,
Expand Down Expand Up @@ -229,12 +245,8 @@ class AiService with FirebaseMixin, ToolsMixin {
debugPrint('Function call ${functionCall.name}, '
'params: ${functionCall.args}');
try {
final gpsLocation = await GetIt.I.get<LocationService>().obtain();
final heartRate = GetIt.I.get<HeartRateService>().heartRate;
final response = await dispatchFunctionCall(
functionCall,
gpsLocation,
heartRate,
preferences,
);
debugPrint('Function call result ${response?.response}');
Expand Down
3 changes: 0 additions & 3 deletions lib/ai/tools/alpha_vantage_tool.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:dart_helper_utils/dart_helper_utils.dart';
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:fl_location/fl_location.dart';
import 'package:http/http.dart' as http;
import 'package:inspector_gadget/ai/tools/function_tool.dart';
import 'package:inspector_gadget/preferences/service/preferences.dart';
Expand Down Expand Up @@ -110,8 +109,6 @@ and technology''',
@override
Future<FunctionResponse> dispatchFunctionCall(
FunctionCall call,
Location? location,
int hr,
PreferencesService preferences,
) async {
alphaVantageAccessKey = preferences.alphaVantageAccessKey;
Expand Down
3 changes: 0 additions & 3 deletions lib/ai/tools/exchange_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:convert';

import 'package:dart_helper_utils/dart_helper_utils.dart';
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:fl_location/fl_location.dart';
import 'package:http/http.dart' as http;
import 'package:inspector_gadget/ai/tools/currency_request.dart';
import 'package:inspector_gadget/ai/tools/function_tool.dart';
Expand Down Expand Up @@ -80,8 +79,6 @@ class ExchangeTool implements FunctionTool {
@override
Future<FunctionResponse> dispatchFunctionCall(
FunctionCall call,
Location? location,
int hr,
PreferencesService preferences,
) async {
final result = switch (call.name) {
Expand Down
3 changes: 0 additions & 3 deletions lib/ai/tools/function_tool.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:fl_location/fl_location.dart';
import 'package:inspector_gadget/preferences/service/preferences.dart';

abstract class FunctionTool {
Expand All @@ -15,8 +14,6 @@ abstract class FunctionTool {

Future<FunctionResponse?> dispatchFunctionCall(
FunctionCall call,
Location? location,
int hr,
PreferencesService preferences,
);
}
79 changes: 0 additions & 79 deletions lib/ai/tools/local_tool.dart

This file was deleted.

3 changes: 0 additions & 3 deletions lib/ai/tools/lyrics_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:convert';

import 'package:dart_helper_utils/dart_helper_utils.dart';
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:fl_location/fl_location.dart';
import 'package:http/http.dart' as http;
import 'package:inspector_gadget/ai/tools/function_tool.dart';
import 'package:inspector_gadget/preferences/service/preferences.dart';
Expand Down Expand Up @@ -52,8 +51,6 @@ class LyricsTool implements FunctionTool {
@override
Future<FunctionResponse> dispatchFunctionCall(
FunctionCall call,
Location? location,
int hr,
PreferencesService preferences,
) async {
final result = switch (call.name) {
Expand Down
3 changes: 0 additions & 3 deletions lib/ai/tools/sun_time_tool.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:dart_helper_utils/dart_helper_utils.dart';
import 'package:daylight/daylight.dart';
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:fl_location/fl_location.dart';
import 'package:inspector_gadget/ai/tools/function_tool.dart';
import 'package:inspector_gadget/ai/tools/geo_request.dart';
import 'package:inspector_gadget/preferences/service/preferences.dart';
Expand Down Expand Up @@ -73,8 +72,6 @@ class SunTimeTool implements FunctionTool {
@override
Future<FunctionResponse?> dispatchFunctionCall(
FunctionCall call,
Location? location,
int hr,
PreferencesService preferences,
) async {
final result = switch (call.name) {
Expand Down
15 changes: 4 additions & 11 deletions lib/ai/tools/tools_mixin.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'package:dart_helper_utils/dart_helper_utils.dart';
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:fl_location/fl_location.dart';
// import 'package:inspector_gadget/ai/tools/alpha_vantage_tool.dart';
import 'package:inspector_gadget/ai/tools/alpha_vantage_tool.dart';
import 'package:inspector_gadget/ai/tools/exchange_tool.dart';
import 'package:inspector_gadget/ai/tools/function_tool.dart';
import 'package:inspector_gadget/ai/tools/local_tool.dart';
import 'package:inspector_gadget/ai/tools/sun_time_tool.dart';
import 'package:inspector_gadget/ai/tools/weather_tool.dart';
import 'package:inspector_gadget/ai/tools/web_research_tool.dart';
Expand All @@ -23,17 +21,16 @@ mixin ToolsMixin {
}

functionTools.addAll([
LocalTool(),
WeatherTool(),
SunTimeTool(),
// WebSearchTool(),
WebResearchTool(),
ExchangeTool(),
]);

// if (!(preferences?.alphaVantageAccessKey.isNullOrWhiteSpace ?? false)) {
// functionTools.add(AlphaVantageTool());
// }
if (!preferences.alphaVantageAccessKey.isNullOrWhiteSpace) {
functionTools.add(AlphaVantageTool());
}

return functionTools;
}
Expand Down Expand Up @@ -67,16 +64,12 @@ mixin ToolsMixin {

Future<FunctionResponse?> dispatchFunctionCall(
FunctionCall call,
Location? location,
int hr,
PreferencesService preferences,
) async {
for (final functionTool in functionTools) {
if (functionTool.canDispatchFunctionCall(call)) {
final futureResponse = functionTool.dispatchFunctionCall(
call,
location,
hr,
preferences,
);

Expand Down
3 changes: 0 additions & 3 deletions lib/ai/tools/weather_tool.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:fl_location/fl_location.dart';
import 'package:http/http.dart' as http;
import 'package:inspector_gadget/ai/tools/function_tool.dart';
import 'package:inspector_gadget/ai/tools/geo_request.dart';
Expand Down Expand Up @@ -50,8 +49,6 @@ class WeatherTool implements FunctionTool {
@override
Future<FunctionResponse> dispatchFunctionCall(
FunctionCall call,
Location? location,
int hr,
PreferencesService preferences,
) async {
final isMetric = preferences.unitSystem;
Expand Down
3 changes: 0 additions & 3 deletions lib/ai/tools/web_research_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:convert';

import 'package:dart_helper_utils/dart_helper_utils.dart';
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:fl_location/fl_location.dart';
import 'package:http/http.dart' as http;
import 'package:inspector_gadget/ai/tools/function_tool.dart';
import 'package:inspector_gadget/preferences/service/preferences.dart';
Expand Down Expand Up @@ -54,8 +53,6 @@ class WebResearchTool implements FunctionTool {
@override
Future<FunctionResponse> dispatchFunctionCall(
FunctionCall call,
Location? location,
int hr,
PreferencesService preferences,
) async {
final result = switch (call.name) {
Expand Down
3 changes: 0 additions & 3 deletions lib/ai/tools/web_search_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:convert';

import 'package:dart_helper_utils/dart_helper_utils.dart';
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:fl_location/fl_location.dart';
import 'package:http/http.dart' as http;
import 'package:inspector_gadget/ai/tools/function_tool.dart';
import 'package:inspector_gadget/preferences/service/preferences.dart';
Expand Down Expand Up @@ -51,8 +50,6 @@ class WebSearchTool implements FunctionTool {
@override
Future<FunctionResponse> dispatchFunctionCall(
FunctionCall call,
Location? location,
int hr,
PreferencesService preferences,
) async {
final result = switch (call.name) {
Expand Down
19 changes: 9 additions & 10 deletions lib/heart_rate/service/heart_rate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@ import 'package:flutter/foundation.dart';
import 'package:heart_rate_flutter/heart_rate_flutter.dart';

class HeartRateService with ChangeNotifier {
HeartRateService() {
final heartBeatFlutter = HeartRateFlutter();
heartBeatFlutter
.getPlatformVersion()
.then((platformVersion) => debugPrint('HR $platformVersion'));
_heartRateStream = heartBeatFlutter.heartBeatStream;
}

int heartRate = 0;

late Stream<double> _heartRateStream;
HeartRateFlutter? _heartBeatFlutter;
Stream<double>? _heartRateStream;
StreamSubscription<double>? _heartRateSubscription;

void listenToHeartRate() {
if (_heartRateSubscription != null) {
return;
}

_heartRateSubscription = _heartRateStream.listen((hr) {
_heartBeatFlutter = HeartRateFlutter();
_heartBeatFlutter!
.getPlatformVersion()
.then((platformVersion) => debugPrint('HR $platformVersion'));
_heartRateStream = _heartBeatFlutter?.heartBeatStream;

_heartRateSubscription = _heartRateStream?.listen((hr) {
if (hr > 0) {
heartRate = hr.toInt();
}
Expand Down
4 changes: 3 additions & 1 deletion lib/interaction/view/interaction_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ class InteractionPageState extends State<InteractionPage>
GetIt.I.get<InteractionState>().setState(StateBase.waitingStateLabel);
database = GetIt.I.get<DatabaseService>();
preferences = GetIt.I.get<PreferencesService>();
GetIt.I.get<HeartRateService>().listenToHeartRate();
if (preferences.measureHeartRate) {
GetIt.I.get<HeartRateService>().listenToHeartRate();
}

deferredActionQueue.add(DeferredAction(ActionKind.initialize));
}
Expand Down
4 changes: 4 additions & 0 deletions lib/l10n/arb/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,9 @@
"cameraResolutionVeryHighLabel": "Very High (1080p)",
"@cameraResolutionVeryHighLabel": {
"description": "Text shown in the preferences settings Very High Camera Resolution selection"
},
"preferencesMeasureHeartRateLabel": "Measure Heart Rate?",
"@preferencesMeasureHeartRateLabel": {
"description": "Text shown in the preferences settings for Measure Heart Rate?"
}
}
4 changes: 4 additions & 0 deletions lib/l10n/arb/app_es.arb
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,9 @@
"cameraResolutionVeryHighLabel": "Muy alta (1080p)",
"@cameraResolutionVeryHighLabel": {
"description": "Texto que se muestra en la configuración de preferencias Selección de resolución de cámara muy alta"
},
"preferencesMeasureHeartRateLabel": "¿Medir la frecuencia cardíaca?",
"@preferencesMeasureHeartRateLabel": {
"description": "Texto que se muestra en la configuración de preferencias para ¿Medir la frecuencia cardíaca?"
}
}
Loading

0 comments on commit 3933939

Please sign in to comment.