Skip to content

Commit

Permalink
Migrate function tools repository to data source (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
banghuazhao authored Oct 17, 2024
1 parent df7ae81 commit 495313e
Show file tree
Hide file tree
Showing 42 changed files with 42 additions and 111 deletions.
5 changes: 2 additions & 3 deletions data/lib/data.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export 'utils/network_exceptions.dart';
export 'data_sources/chat_completion_data_source.dart';
export 'data_sources/open_ai_data_source.dart';
export 'repositories/chat_repository_impl.dart';
export 'repositories/chat_session_repository_imp.dart';
export "repositories/function_tools_repository_imp.dart";
export 'repositories/chat_session_repository_imp.dart';
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'package:domain/entities/function_tool.dart';
import 'package:domain/repositories_abstract/function_tools_repository.dart';

class FunctionToolsRepositoryImp extends FunctionToolsRepository {
abstract class FunctionToolsDataSource {
List<FunctionTool> getAllFunctionTools();
}

class FunctionToolsDataSourceImp implements FunctionToolsDataSource {
String get commonDescription =>
"Display the default input parameters first. Allow the user to either proceed with these default values or modify them as needed. Based on the user’s decision to either keep or change the parameters, continue with the appropriate calculation process, ensure that all modifications are used in the calculation to reflect the user’s preferences accurately.";

Expand All @@ -10,7 +13,7 @@ class FunctionToolsRepositoryImp extends FunctionToolsRepository {
FunctionTool calculateLaminaEngineeringConstantsTool = FunctionTool(
name: "calculate_lamina_engineering_constants",
description:
"Calculates the engineering constants for a lamina. $commonDescription",
"Calculates the engineering constants for a lamina. $commonDescription",
parameters: {
"type": "object",
"required": ["E1", "E2", "G12", "nu12", "layup_angle"],
Expand Down Expand Up @@ -92,7 +95,7 @@ class FunctionToolsRepositoryImp extends FunctionToolsRepository {
FunctionTool calculateLaminatePlatePropertiesTool = FunctionTool(
name: "calculate_laminate_plate_properties",
description:
"Calculates the laminate plate properties. $commonDescription",
"Calculates the laminate plate properties. $commonDescription",
parameters: {
"type": "object",
"required": [
Expand Down Expand Up @@ -124,7 +127,7 @@ class FunctionToolsRepositoryImp extends FunctionToolsRepository {
FunctionTool calculateLaminate3DPropertiesTool = FunctionTool(
name: "calculate_laminate_3d_properties",
description:
"Calculates the laminate plate properties. $commonDescription",
"Calculates the laminate plate properties. $commonDescription",
parameters: {
"type": "object",
"required": [
Expand Down Expand Up @@ -163,7 +166,7 @@ class FunctionToolsRepositoryImp extends FunctionToolsRepository {
FunctionTool calculateLaminarStrainTool = FunctionTool(
name: "calculate_laminar_strain",
description:
"Calculates the strains for a laminar/laminate. $commonDescription",
"Calculates the strains for a laminar/laminate. $commonDescription",
parameters: {
"type": "object",
"required": [
Expand Down Expand Up @@ -207,7 +210,7 @@ class FunctionToolsRepositoryImp extends FunctionToolsRepository {
FunctionTool calculateLaminarStressTool = FunctionTool(
name: "calculate_laminar_stress",
description:
"Calculates the stress for a laminar/laminate. $commonDescription",
"Calculates the stress for a laminar/laminate. $commonDescription",
parameters: {
"type": "object",
"required": [
Expand Down Expand Up @@ -251,7 +254,7 @@ class FunctionToolsRepositoryImp extends FunctionToolsRepository {
FunctionTool calculateUDFRCRulesOfMixtureTool = FunctionTool(
name: "calculate_UDFRC_rules_of_mixture",
description:
"Calculates the UDFRC (Unidirectional fibre-reinforced composites) properties by rules of mixture. $commonDescription",
"Calculates the UDFRC (Unidirectional fibre-reinforced composites) properties by rules of mixture. $commonDescription",
parameters: {
"type": "object",
"required": [
Expand Down Expand Up @@ -322,4 +325,4 @@ class FunctionToolsRepositoryImp extends FunctionToolsRepository {
calculateUDFRCRulesOfMixtureTool
];
}
}
}
3 changes: 3 additions & 0 deletions data/lib/data_sources/local_data_source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class LocalDataSource {

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import '../models/chat_chunk.dart';
import '../utils/api_constants.dart';
import '../utils/network_exceptions.dart';

abstract class ChatCompletionsDataSource {
abstract class OpenAIDataSource {
Stream<ChatChunk> sendMessages(List<Message> messages, List<FunctionTool> functionTools);
}

class ChatRemoteDataSourceImpl implements ChatCompletionsDataSource {
class ChatRemoteDataSourceImpl implements OpenAIDataSource {
final http.Client client;

ChatRemoteDataSourceImpl({required this.client});
Expand Down
18 changes: 10 additions & 8 deletions data/lib/repositories/chat_repository_impl.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import 'dart:async';

import 'package:domain/entities/function_tool.dart';
import 'package:domain/entities/message.dart';
import 'package:domain/repositories_abstract/chat_repository.dart';
import '../data_sources/chat_completion_data_source.dart';
import '../data_sources/function_tools_data_source.dart';
import '../data_sources/open_ai_data_source.dart';
import '../models/chat_chunk.dart';

class ChatRepositoryImp implements ChatRepository {
final ChatCompletionsDataSource chatCompletionsDataSource;
final OpenAIDataSource openAIDataSource;
final FunctionToolsDataSource functionToolsDataSource;

ChatRepositoryImp({required this.chatCompletionsDataSource});
ChatRepositoryImp(
{required this.openAIDataSource, required this.functionToolsDataSource});

@override
Stream<Message> sendMessages(
List<Message> messages, List<FunctionTool> functionTools) {
Stream<Message> sendMessages(List<Message> messages) {
// Create a StreamController to accumulate and emit the content as strings
final StreamController<Message> controller = StreamController<Message>();

final functionTools = functionToolsDataSource.getAllFunctionTools();

// Call the original sendMessages method that returns Stream<ChatChunk>
final chatChunks =
chatCompletionsDataSource.sendMessages(messages, functionTools);
final chatChunks = openAIDataSource.sendMessages(messages, functionTools);

Message buffer = Message(role: "assistant");
// Listen to the incoming stream of ChatChunks
Expand Down
4 changes: 1 addition & 3 deletions domain/lib/repositories_abstract/chat_repository.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import '../entities/function_tool.dart';
import '../entities/message.dart';

abstract class ChatRepository {
Stream<Message> sendMessages(List<Message> messages,
List<FunctionTool> functionTools);
Stream<Message> sendMessages(List<Message> messages);
}
14 changes: 5 additions & 9 deletions domain/lib/usecases/chat_usecase.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import '../entities/function_tool.dart';
import '../entities/message.dart';
import '../repositories_abstract/chat_repository.dart';
import '../repositories_abstract/function_tools_repository.dart';

class ChatUseCase {
final ChatRepository chatRepository;
final FunctionToolsRepository functionToolsRepository;

ChatUseCase({required this.chatRepository, required this.functionToolsRepository});
ChatUseCase(
{required this.chatRepository});

final Message systemMessage = Message(
role: "system",
content:
"You are an expert in composite materials and structures. Please answer questions related to composites design and manufacturing.");
"You are an expert in composite materials and structures. Please answer questions related to composites design and manufacturing.");

Stream<Message> sendMessages(List<Message> messages) {
final chatHistory = [systemMessage] + messages;
final functionTools = functionToolsRepository.getAllFunctionTools();
return chatRepository.sendMessages(
chatHistory, functionTools);
return chatRepository.sendMessages(chatHistory);
}
}
}
19 changes: 7 additions & 12 deletions lib/injection_container.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import 'package:data/data_sources/chat_completion_data_source.dart';
import 'package:data/data_sources/function_tools_data_source.dart';
import 'package:data/data_sources/open_ai_data_source.dart';
import 'package:data/repositories/chat_repository_impl.dart';
import 'package:data/repositories/chat_session_repository_imp.dart';
import 'package:data/repositories/function_tools_repository_imp.dart';
import 'package:domain/domain.dart';
import 'package:domain/repositories_abstract/chat_repository.dart';
import 'package:domain/repositories_abstract/chat_session_repository.dart';
import 'package:domain/repositories_abstract/function_tools_repository.dart';
import 'package:domain/usecases/chat_session_usecase.dart';
import 'package:domain/usecases/chat_usecase.dart';
import 'package:get_it/get_it.dart';
import 'package:http/http.dart' as http;
import 'presentation/chat/viewModels/chat_view_model.dart';
Expand All @@ -21,22 +16,22 @@ void initInjection() {

// Use Cases
sl.registerLazySingleton<ChatUseCase>(
() => ChatUseCase(chatRepository: sl(), functionToolsRepository: sl()));
() => ChatUseCase(chatRepository: sl()));

sl.registerLazySingleton<ChatSessionUseCase>(
() => ChatSessionUseCase(repository: sl()));

// Repositories
sl.registerLazySingleton<ChatRepository>(
() => ChatRepositoryImp(chatCompletionsDataSource: sl()));
() => ChatRepositoryImp(openAIDataSource: sl(), functionToolsDataSource: sl()));
sl.registerLazySingleton<ChatSessionRepository>(
() => ChatSessionRepositoryImpl());
sl.registerLazySingleton<FunctionToolsRepository>(
() => FunctionToolsRepositoryImp());

// Data Sources
sl.registerLazySingleton<ChatCompletionsDataSource>(
sl.registerLazySingleton<OpenAIDataSource>(
() => ChatRemoteDataSourceImpl(client: sl()));
sl.registerLazySingleton<FunctionToolsDataSource>(
() => FunctionToolsDataSourceImp());

// External
sl.registerLazySingleton(() => http.Client());
Expand Down
1 change: 0 additions & 1 deletion lib/presentation/bottom_navigator.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:swiftcomp/presentation/more/feature_flag_provider.dart';
Expand Down
19 changes: 0 additions & 19 deletions lib/presentation/chat/viewModels/chat_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
import 'dart:convert';

import 'package:composite_calculator/calculators/lamina_engineering_constants_calculator.dart';
import 'package:composite_calculator/calculators/lamina_stress_strain_calculator.dart';
import 'package:composite_calculator/calculators/laminar_stress_strain_calculator.dart';
import 'package:composite_calculator/calculators/laminate_3d_properties_calculator.dart';
import 'package:composite_calculator/calculators/laminate_plate_properties_calculator.dart';
import 'package:composite_calculator/composite_calculator.dart';

import 'package:composite_calculator/models/lamina_engineering_constants_input.dart';
import 'package:composite_calculator/models/lamina_engineering_constants_output.dart';
import 'package:composite_calculator/models/lamina_stress_strain_input.dart';
import 'package:composite_calculator/models/lamina_stress_strain_output.dart';
import 'package:composite_calculator/models/laminar_stress_strain_input.dart';
import 'package:composite_calculator/models/laminar_stress_strain_output.dart';
import 'package:composite_calculator/models/laminate_3d_properties_input.dart';
import 'package:composite_calculator/models/laminate_3d_properties_output.dart';
import 'package:composite_calculator/models/laminate_plate_properties_input.dart';
import 'package:composite_calculator/models/laminate_plate_properties_output.dart';
import 'package:composite_calculator/models/tensor_type.dart';
import 'package:domain/domain.dart';
import 'package:domain/entities/chat_session.dart';
import 'package:domain/entities/message.dart';
import 'package:domain/usecases/chat_session_usecase.dart';
import 'package:domain/usecases/chat_usecase.dart';
import 'package:flutter/cupertino.dart';

class ChatViewModel extends ChangeNotifier {
Expand Down
1 change: 0 additions & 1 deletion lib/presentation/chat/views/chat_message_list.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

Expand Down
1 change: 0 additions & 1 deletion lib/presentation/chat/views/chat_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

Expand Down
1 change: 0 additions & 1 deletion lib/presentation/chat/views/chat_session_drawer.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

Expand Down
1 change: 0 additions & 1 deletion lib/presentation/more/login/forget_password_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
Expand Down
2 changes: 0 additions & 2 deletions lib/presentation/more/more_page.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'dart:io';

import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:device_info/device_info.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:fluttertoast/fluttertoast.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/presentation/tools/model/DescriptionModels.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_math_fork/flutter_math.dart';

Expand Down
2 changes: 0 additions & 2 deletions lib/presentation/tools/page/UDFRC_rules_of_mixture_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'package:composite_calculator/composite_calculator.dart';
import 'package:composite_calculator/models/UDFRC_rules_of_mixture_input.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:swiftcomp/generated/l10n.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:composite_calculator/composite_calculator.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:swiftcomp/generated/l10n.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:composite_calculator/composite_calculator.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:swiftcomp/generated/l10n.dart';
Expand Down
4 changes: 0 additions & 4 deletions lib/presentation/tools/page/lamina_stress_strain_page.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import 'package:composite_calculator/calculators/lamina_stress_strain_calculator.dart';
import 'package:composite_calculator/composite_calculator.dart';
import 'package:composite_calculator/models/lamina_stress_strain_input.dart';
import 'package:composite_calculator/models/lamina_stress_strain_output.dart';
import 'package:composite_calculator/models/tensor_type.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:swiftcomp/generated/l10n.dart';
Expand Down
5 changes: 0 additions & 5 deletions lib/presentation/tools/page/laminate_3d_properties_page.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import 'dart:math';

import 'package:composite_calculator/composite_calculator.dart';
import 'package:composite_calculator/models/laminate_3d_properties_input.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:linalg/linalg.dart';
import 'package:swiftcomp/generated/l10n.dart';
import 'package:swiftcomp/presentation/tools/model/layer_thickness.dart';
import 'package:swiftcomp/presentation/tools/model/layup_sequence_model.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:composite_calculator/composite_calculator.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:swiftcomp/generated/l10n.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:composite_calculator/composite_calculator.dart';
import 'package:composite_calculator/models/analysis_type.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:swiftcomp/generated/l10n.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:math';

import 'package:composite_calculator/composite_calculator.dart';
import 'package:composite_calculator/models/in-plane-properties.dart';
import 'package:flutter/material.dart';
Expand Down
3 changes: 0 additions & 3 deletions lib/presentation/tools/page/laminate_stress_strain_page.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import 'dart:math';

import 'package:composite_calculator/composite_calculator.dart';
import 'package:composite_calculator/models/laminar_stress_strain_input.dart';
import 'package:composite_calculator/models/tensor_type.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:linalg/linalg.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:math';
import 'package:composite_calculator/composite_calculator.dart';
import 'package:composite_calculator/models/tensor_type.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:linalg/matrix.dart';
Expand Down
2 changes: 0 additions & 2 deletions lib/presentation/tools/page/tool_page.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:swiftcomp/generated/l10n.dart';
import 'package:swiftcomp/presentation/tools/model/tool_model.dart';
import 'package:swiftcomp/presentation/tools/page/UDFRC_rules_of_mixture_page.dart';
import 'package:swiftcomp/presentation/tools/page/lamina_stress_strain_page.dart';
import 'package:swiftcomp/presentation/tools/model/DescriptionModels.dart';
import 'package:swiftcomp/presentation/more/more_page.dart';

import 'lamina_engineering_constants_page.dart';
import 'laminate_3d_properties_page.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/presentation/tools/widget/description.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:swiftcomp/generated/l10n.dart';

Expand Down
1 change: 0 additions & 1 deletion lib/presentation/tools/widget/isotropic_material_row.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:swiftcomp/presentation/tools/model/material_model.dart';
import 'package:swiftcomp/presentation/tools/model/explain.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:swiftcomp/presentation/tools/model/explain.dart';
import 'package:swiftcomp/presentation/tools/model/validate.dart';
Expand Down
Loading

0 comments on commit 495313e

Please sign in to comment.