completion is working

This commit is contained in:
poka 2023-01-07 13:02:53 +01:00
parent 61b36ccef7
commit 0ed43ccda2
6 changed files with 166 additions and 69 deletions

7
lib/global.dart Normal file
View File

@ -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);

View File

@ -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<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Future<void> _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: <Widget>[
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'),
);
}
}

23
lib/riverpods/openai.dart Normal file
View File

@ -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>((_) => TextEditingController());
Future<String> 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;
}
}

94
lib/screens/home.dart Normal file
View File

@ -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<Bogui> createState() => _BoguiState();
}
class _BoguiState extends ConsumerState<Bogui> {
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: <Widget>[
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),
),
),
),
],
),
),
);
}
}

View File

@ -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"

View File

@ -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: