From 0ed43ccda227720dd2e6d1a6b18c73f875065abd Mon Sep 17 00:00:00 2001 From: poka Date: Sat, 7 Jan 2023 13:02:53 +0100 Subject: [PATCH] completion is working --- lib/global.dart | 7 +++ lib/main.dart | 80 +++++---------------------------- lib/riverpods/openai.dart | 23 ++++++++++ lib/screens/home.dart | 94 +++++++++++++++++++++++++++++++++++++++ pubspec.lock | 29 ++++++++++++ pubspec.yaml | 2 + 6 files changed, 166 insertions(+), 69 deletions(-) create mode 100644 lib/global.dart create mode 100644 lib/riverpods/openai.dart create mode 100644 lib/screens/home.dart diff --git a/lib/global.dart b/lib/global.dart new file mode 100644 index 0000000..966f842 --- /dev/null +++ b/lib/global.dart @@ -0,0 +1,7 @@ +import 'package:flutter/material.dart'; + +// Colors +const Color orangeC = Color(0xffd07316); +const Color yellowC = Color(0xffFFD68E); +const Color floattingYellow = Color(0xffEFEFBF); +const Color backgroundColor = Color(0xFFF5F5F5); diff --git a/lib/main.dart b/lib/main.dart index 8cfeee9..f971c87 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,8 +1,10 @@ +import 'package:bogui/global.dart'; +import 'package:bogui/screens/home.dart'; import 'package:flutter/material.dart'; -import 'package:openai_gpt3_api/openai_gpt3_api.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; void main() { - runApp(const MyApp()); + runApp(const ProviderScope(child: MyApp())); } class MyApp extends StatelessWidget { @@ -13,75 +15,15 @@ class MyApp extends StatelessWidget { return MaterialApp( title: 'bogui', theme: ThemeData( - primarySwatch: Colors.blue, - ), - home: const MyHomePage(title: 'power AI'), - ); - } -} - -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); - - final String title; - - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - int _counter = 0; - - Future _incrementCounter() async { - final gpt = GPT3('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'); - // gpt.answer( ,'Connais tu la monnaie libre Ğ1 et son dividend universel ?', [], ''); - // final anwser = await gpt.search( - // 'Connais-tu la monnaie libre Ğ1 et son dividend universel ?', - // documents: [ - // 'abc', - // 'abc', - // 'abc', - // 'abc', - // 'abc', - // ], - // engine: Engine.davinci3); - - final anwser = await gpt.completion( - "Connais tu la monnaie libre Ğ1 et son dividend universel ?", - engine: Engine.davinci3); - for (final choice in anwser.choices) { - print(choice.text); - } - setState(() { - _counter++; - }); - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: Text(widget.title), - ), - body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text( - 'You have pushed the button this many times:...', - ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headline4, - ), - ], + appBarTheme: const AppBarTheme( + color: orangeC, + foregroundColor: Color.fromRGBO(33, 33, 33, 1), ), + brightness: Brightness.dark, + primaryColor: orangeC, + fontFamily: 'Georgia', ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: const Icon(Icons.add), - ), + home: const Bogui(title: 'power AI'), ); } } diff --git a/lib/riverpods/openai.dart b/lib/riverpods/openai.dart new file mode 100644 index 0000000..eb7ba0d --- /dev/null +++ b/lib/riverpods/openai.dart @@ -0,0 +1,23 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:openai_gpt3_api/openai_gpt3_api.dart'; + +class OpenAI extends GPT3 { + OpenAI() : super('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'); + // final prompt = TextEditingController(); + final prompt = + StateProvider((_) => TextEditingController()); + + Future completionEasy(WidgetRef ref) async { + final anwser = await OpenAI().completion(ref.read(prompt).text, + maxTokens: 250, engine: Engine.davinci3, temperature: 0.1); + + String anwserString = ''; + for (final choice in anwser.choices) { + anwserString += choice.text; + } + + ref.read(prompt).text += anwserString; + return anwserString; + } +} diff --git a/lib/screens/home.dart b/lib/screens/home.dart new file mode 100644 index 0000000..4dc60f4 --- /dev/null +++ b/lib/screens/home.dart @@ -0,0 +1,94 @@ +import 'package:bogui/global.dart'; +import 'package:bogui/riverpods/openai.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +class Bogui extends ConsumerStatefulWidget { + const Bogui({super.key, required this.title}); + + final String title; + + @override + ConsumerState createState() => _BoguiState(); +} + +class _BoguiState extends ConsumerState { + late OpenAI gpt; + + @override + void initState() { + gpt = OpenAI(); + super.initState(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text(widget.title), + ), + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + SizedBox( + width: 800, + child: TextField( + controller: ref.watch(gpt.prompt), + autofocus: true, + textInputAction: TextInputAction.search, + minLines: 3, + maxLines: 9, + cursorColor: orangeC, + style: TextStyle(color: Colors.grey[400], fontSize: 18), + decoration: InputDecoration( + filled: true, + // fillColor: Colors.white, + prefixIconConstraints: const BoxConstraints( + minHeight: 32, + ), + prefixIcon: const Padding( + padding: EdgeInsets.symmetric(horizontal: 17), + child: Icon( + Icons.fireplace_rounded, + color: orangeC, + size: 30, + )), + border: OutlineInputBorder( + borderSide: + BorderSide(color: Colors.grey[500]!, width: 2), + borderRadius: BorderRadius.circular(8)), + focusedBorder: OutlineInputBorder( + borderSide: + BorderSide(color: Colors.grey[500]!, width: 2.5), + borderRadius: BorderRadius.circular(8), + ), + contentPadding: const EdgeInsets.all(20), + ), + onSubmitted: (value) => gpt.completionEasy(ref), + ), + ), + const SizedBox(height: 40), + SizedBox( + width: 250, + height: 50, + child: ElevatedButton( + style: ElevatedButton.styleFrom( + foregroundColor: Colors.white, elevation: 4, + backgroundColor: orangeC, // foreground + ), + onPressed: () { + gpt.completionEasy(ref); + }, + child: const Text( + 'Valider', + style: TextStyle(fontSize: 21, fontWeight: FontWeight.w500), + ), + ), + ), + ], + ), + ), + ); + } +} diff --git a/pubspec.lock b/pubspec.lock index 5a279d9..ec0b7a0 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -55,6 +55,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_hooks: + dependency: "direct main" + description: + name: flutter_hooks + url: "https://pub.dartlang.org" + source: hosted + version: "0.18.5+1" flutter_lints: dependency: "direct dev" description: @@ -62,6 +69,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.1" + flutter_riverpod: + dependency: "direct main" + description: + name: flutter_riverpod + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.3" flutter_test: dependency: "direct dev" description: flutter @@ -132,6 +146,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.2" + riverpod: + dependency: transitive + description: + name: riverpod + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.3" sky_engine: dependency: transitive description: flutter @@ -151,6 +172,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.10.0" + state_notifier: + dependency: transitive + description: + name: state_notifier + url: "https://pub.dartlang.org" + source: hosted + version: "0.7.2+1" stream_channel: dependency: transitive description: @@ -195,3 +223,4 @@ packages: version: "2.1.2" sdks: dart: ">=2.18.6 <3.0.0" + flutter: ">=3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 2a253af..ef9344a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -18,6 +18,8 @@ dependencies: git: url: https://github.com/morriskurz/openai_gpt3_dart_api.git ref: main + flutter_hooks: ^0.18.0 + flutter_riverpod: ^2.1.3 dev_dependencies: flutter_test: