diff --git a/README.md b/README.md index e2f2b34..a099a9d 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,11 @@ A Chatbot chat app built using the Flutter framework and OpenAI's GPT-3 language ## Getting Started -1. Clone the repository -2. Run `flutter pub get` to install dependencies -3. Replace the placeholder API key with your own in the `chat_screen.dart` file -4. Run the app on an emulator or physical device +1. Clone the repository. +2. Run `flutter pub get` to install dependencies. +3. Create a '.env' file in your lib folder, and assign the variable API_KEY in it to your own API key. +4. Specify the path to your '.env' file in the assets in the 'pubspec.yaml' file, and in the main function when loading the '.env' file. +5. Run the app on an emulator or a physical device. The chatbot can be further customized to fit your use-case by modifying the code in the `main.dart` file and other files. The source code for the chatbot is available on GitHub. diff --git a/lib/chat_screen.dart b/lib/chat_screen.dart index d504742..a98c525 100644 --- a/lib/chat_screen.dart +++ b/lib/chat_screen.dart @@ -25,16 +25,16 @@ class _ChatScreenState extends State { void initState() { chatGPT = OpenAI.instance.build( token: dotenv.env["API_KEY"], - baseOption: HttpSetup(receiveTimeout: 60000)); + baseOption: HttpSetup(receiveTimeout: const Duration(seconds: 6000))); super.initState(); } - @override - void dispose() { - chatGPT?.close(); - chatGPT?.genImgClose(); - super.dispose(); - } + // @override + // void dispose() { + // chatGPT?.close(); + // chatGPT?.genImgClose(); + // super.dispose(); + // } // Link for api - https://beta.openai.com/account/api-keys @@ -54,19 +54,37 @@ class _ChatScreenState extends State { _controller.clear(); if (_isImageSearch) { - final request = GenerateImage(message.text, 1, size: "256x256"); + final request = GenerateImage(message.text, 1, size: ImageSize.size256); final response = await chatGPT!.generateImage(request); Vx.log(response!.data!.last!.url!); insertNewData(response.data!.last!.url!, isImage: true); } else { - final request = - CompleteText(prompt: message.text, model: kTranslateModelV3); + final response = await _chooseModel(message.text, Model.textDavinci3); + Vx.log(response); + insertNewData(response, isImage: false); + } + } - final response = await chatGPT!.onCompleteText(request: request); - Vx.log(response!.choices[0].text); - insertNewData(response.choices[0].text, isImage: false); + Future _chooseModel(String message, model) async { + final String resultMessage; + if (model is Model) { + final request = CompleteText(prompt: message, model: model); + + final response = await chatGPT!.onCompletion(request: request); + resultMessage = response!.choices[0].text; + } else if (model is ChatModel) { + final request = ChatCompleteText(messages: [ + Map.of({"role": "user", "content": message}) + ], model: model); + + final response = await chatGPT!.onChatCompletion(request: request); + resultMessage = response!.choices[0].message!.content; + } else { + throw ArgumentError('Invalid model type. Must be Model or ChatModel.'); } + Vx.log('Used the model: $model'); + return resultMessage; } void insertNewData(String response, {bool isImage = false}) { diff --git a/lib/main.dart b/lib/main.dart index 4467bc7..d344f25 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -5,7 +5,7 @@ import 'chat_screen.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); - await dotenv.load(fileName: ".env"); + await dotenv.load(fileName: "lib/.env"); runApp(const MyApp()); } diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 724bb2a..cccf817 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,8 +5,6 @@ import FlutterMacOS import Foundation -import shared_preferences_foundation func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { - SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) } diff --git a/pubspec.lock b/pubspec.lock index fab8e62..a8b9988 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -36,12 +36,11 @@ packages: chat_gpt_sdk: dependency: "direct main" description: - path: "." - ref: HEAD - resolved-ref: "7e6565831cd9b8b74a7b376042f952d6c54bea60" - url: "https://github.com/iampawan/Flutter-ChatGPT.git" - source: git - version: "1.0.2+4" + name: chat_gpt_sdk + sha256: "64bc89a7ff086e2b814643003d37de617c777780239539b00da58810afa2337e" + url: "https://pub.dev" + source: hosted + version: "2.1.2" clock: dependency: transitive description: @@ -70,10 +69,10 @@ packages: dependency: transitive description: name: dio - sha256: "7d328c4d898a61efc3cd93655a0955858e29a0aa647f0f9e02d59b3bb275e2e8" + sha256: "0894a098594263fe1caaba3520e3016d8a855caeb010a882273189cca10f11e9" url: "https://pub.dev" source: hosted - version: "4.0.6" + version: "5.1.1" fake_async: dependency: transitive description: @@ -82,22 +81,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.1" - ffi: - dependency: transitive - description: - name: ffi - sha256: a38574032c5f1dd06c4aee541789906c12ccaab8ba01446e800d9c5b79c4a978 - url: "https://pub.dev" - source: hosted - version: "2.0.1" - file: - dependency: transitive - description: - name: file - sha256: "1b92bec4fc2a72f59a8e15af5f52cd441e4a7860b49499d69dfa817af20e925d" - url: "https://pub.dev" - source: hosted - version: "6.1.4" flutter: dependency: "direct main" description: flutter @@ -193,110 +176,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.2" - path_provider_linux: - dependency: transitive - description: - name: path_provider_linux - sha256: "2e32f1640f07caef0d3cb993680f181c79e54a3827b997d5ee221490d131fbd9" - url: "https://pub.dev" - source: hosted - version: "2.1.8" - path_provider_platform_interface: - dependency: transitive - description: - name: path_provider_platform_interface - sha256: f0abc8ebd7253741f05488b4813d936b4d07c6bae3e86148a09e342ee4b08e76 - url: "https://pub.dev" - source: hosted - version: "2.0.5" - path_provider_windows: - dependency: transitive - description: - name: path_provider_windows - sha256: bcabbe399d4042b8ee687e17548d5d3f527255253b4a639f5f8d2094a9c2b45c - url: "https://pub.dev" - source: hosted - version: "2.1.3" - platform: - dependency: transitive - description: - name: platform - sha256: "4a451831508d7d6ca779f7ac6e212b4023dd5a7d08a27a63da33756410e32b76" - url: "https://pub.dev" - source: hosted - version: "3.1.0" - plugin_platform_interface: - dependency: transitive - description: - name: plugin_platform_interface - sha256: dbf0f707c78beedc9200146ad3cb0ab4d5da13c246336987be6940f026500d3a - url: "https://pub.dev" - source: hosted - version: "2.1.3" - process: - dependency: transitive - description: - name: process - sha256: "53fd8db9cec1d37b0574e12f07520d582019cb6c44abf5479a01505099a34a09" - url: "https://pub.dev" - source: hosted - version: "4.2.4" - shared_preferences: - dependency: transitive - description: - name: shared_preferences - sha256: "5949029e70abe87f75cfe59d17bf5c397619c4b74a099b10116baeb34786fad9" - url: "https://pub.dev" - source: hosted - version: "2.0.17" - shared_preferences_android: - dependency: transitive - description: - name: shared_preferences_android - sha256: "955e9736a12ba776bdd261cf030232b30eadfcd9c79b32a3250dd4a494e8c8f7" - url: "https://pub.dev" - source: hosted - version: "2.0.15" - shared_preferences_foundation: - dependency: transitive - description: - name: shared_preferences_foundation - sha256: "2b55c18636a4edc529fa5cd44c03d3f3100c00513f518c5127c951978efcccd0" - url: "https://pub.dev" - source: hosted - version: "2.1.3" - shared_preferences_linux: - dependency: transitive - description: - name: shared_preferences_linux - sha256: f8ea038aa6da37090093974ebdcf4397010605fd2ff65c37a66f9d28394cb874 - url: "https://pub.dev" - source: hosted - version: "2.1.3" - shared_preferences_platform_interface: - dependency: transitive - description: - name: shared_preferences_platform_interface - sha256: da9431745ede5ece47bc26d5d73a9d3c6936ef6945c101a5aca46f62e52c1cf3 - url: "https://pub.dev" - source: hosted - version: "2.1.0" - shared_preferences_web: - dependency: transitive - description: - name: shared_preferences_web - sha256: a4b5bc37fe1b368bbc81f953197d55e12f49d0296e7e412dfe2d2d77d6929958 - url: "https://pub.dev" - source: hosted - version: "2.0.4" - shared_preferences_windows: - dependency: transitive - description: - name: shared_preferences_windows - sha256: "5eaf05ae77658d3521d0e993ede1af962d4b326cd2153d312df716dc250f00c9" - url: "https://pub.dev" - source: hosted - version: "2.1.3" sky_engine: dependency: transitive description: flutter @@ -382,22 +261,6 @@ packages: url: "https://pub.dev" source: hosted version: "2.3.0" - win32: - dependency: transitive - description: - name: win32 - sha256: c9ebe7ee4ab0c2194e65d3a07d8c54c5d00bb001b76081c4a04cdb8448b59e46 - url: "https://pub.dev" - source: hosted - version: "3.1.3" - xdg_directories: - dependency: transitive - description: - name: xdg_directories - sha256: ee1505df1426458f7f60aac270645098d318a8b4766d85fde75f76f2e21807d1 - url: "https://pub.dev" - source: hosted - version: "1.0.0" sdks: - dart: ">=2.18.6 <3.0.0" - flutter: ">=3.0.0" + dart: ">=2.19.6 <3.0.0" + flutter: ">=2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index c274256..1f81e5e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -29,9 +29,7 @@ environment: # the latest version available on pub.dev. To see which dependencies have newer # versions available, run `flutter pub outdated`. dependencies: - chat_gpt_sdk: - git: - url: https://github.com/iampawan/Flutter-ChatGPT.git + chat_gpt_sdk: ^2.1.2 cupertino_icons: ^1.0.2 flutter: sdk: flutter @@ -58,7 +56,7 @@ flutter: uses-material-design: true # To add assets to your application, add an assets section, like this: assets: - - .env + - lib/.env # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg # An image asset can refer to one or more resolution-specific "variants", see