improve env token management
This commit is contained in:
parent
0a78158c17
commit
d6ff18a769
|
@ -42,3 +42,6 @@ app.*.map.json
|
||||||
/android/app/debug
|
/android/app/debug
|
||||||
/android/app/profile
|
/android/app/profile
|
||||||
/android/app/release
|
/android/app/release
|
||||||
|
|
||||||
|
# ignore .env
|
||||||
|
.env
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:logger/logger.dart';
|
||||||
|
|
||||||
// Colors
|
// Colors
|
||||||
const Color orangeC = Color(0xffd07316);
|
const Color orangeC = Color(0xffd07316);
|
||||||
|
@ -8,3 +9,6 @@ const Color backgroundColor = Color(0xFFF5F5F5);
|
||||||
|
|
||||||
late double screenWidth;
|
late double screenWidth;
|
||||||
late double screenHight;
|
late double screenHight;
|
||||||
|
|
||||||
|
// Logger
|
||||||
|
final log = Logger();
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
|
Future<Map<String, String>> loadEnv({String assetsFileName = '.env'}) async {
|
||||||
|
Map<String, String> environment = {};
|
||||||
|
try {
|
||||||
|
final lines = await rootBundle.loadString(assetsFileName);
|
||||||
|
for (String line in lines.split('\n')) {
|
||||||
|
line = line.trim();
|
||||||
|
if (line.contains('=') //Set Key Value Pairs on lines separated by =
|
||||||
|
&&
|
||||||
|
!line.startsWith(RegExp(r'=|#'))) {
|
||||||
|
//No need to add empty keys and remove comments
|
||||||
|
List<String> contents = line.split('=');
|
||||||
|
environment[contents[0]] = contents.sublist(1).join('=');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return {'': ''};
|
||||||
|
}
|
||||||
|
|
||||||
|
return environment;
|
||||||
|
}
|
|
@ -1,24 +1,49 @@
|
||||||
|
import 'package:bogui/global.dart';
|
||||||
|
import 'package:bogui/load_env.dart';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:openai_gpt3_api/completion.dart';
|
||||||
import 'package:openai_gpt3_api/openai_gpt3_api.dart';
|
import 'package:openai_gpt3_api/openai_gpt3_api.dart';
|
||||||
|
|
||||||
class OpenAI extends GPT3 {
|
class OpenAI {
|
||||||
OpenAI() : super(String.fromEnvironment('OPENAPI_SECRET_KEY'));
|
// final openaiSecret;
|
||||||
|
late GPT3 gpt;
|
||||||
|
|
||||||
// final prompt = TextEditingController();
|
// final prompt = TextEditingController();
|
||||||
final prompt =
|
final prompt =
|
||||||
StateProvider<TextEditingController>((ref) => TextEditingController());
|
StateProvider<TextEditingController>((ref) => TextEditingController());
|
||||||
final isLoading = StateProvider<bool>((ref) => false);
|
final isLoading = StateProvider<bool>((ref) => false);
|
||||||
final temperature = StateProvider<double>((ref) => 0.7);
|
final temperature = StateProvider<double>((ref) => 0.7);
|
||||||
|
|
||||||
|
Future init() async {
|
||||||
|
final env = await loadEnv();
|
||||||
|
String openaiSecretFile = '';
|
||||||
|
if (env.containsKey('OPENAPI_SECRET_KEY')) {
|
||||||
|
openaiSecretFile = env['OPENAPI_SECRET_KEY']!;
|
||||||
|
}
|
||||||
|
const openaiSecretEnv = String.fromEnvironment('OPENAPI_SECRET_KEY');
|
||||||
|
gpt = GPT3(kDebugMode ? openaiSecretFile : openaiSecretEnv);
|
||||||
|
}
|
||||||
|
|
||||||
Future<String> completionEasy(WidgetRef ref) async {
|
Future<String> completionEasy(WidgetRef ref) async {
|
||||||
if (ref.read(isLoading) || ref.read(prompt).text.length < 2) return '';
|
if (ref.read(isLoading) || ref.read(prompt).text.length < 2) return '';
|
||||||
ref.read(isLoading.notifier).state = true;
|
ref.read(isLoading.notifier).state = true;
|
||||||
final anwser = await OpenAI().completion(ref.read(prompt).text,
|
late CompletionApiResult anwser;
|
||||||
|
try {
|
||||||
|
anwser = await gpt.completion(ref.read(prompt).text,
|
||||||
maxTokens: 250,
|
maxTokens: 250,
|
||||||
engine: Engine.davinci3,
|
engine: Engine.davinci3,
|
||||||
temperature: ref.read(temperature),
|
temperature: ref.read(temperature),
|
||||||
echo: false,
|
echo: false,
|
||||||
stream: false);
|
stream: false);
|
||||||
|
} catch (e) {
|
||||||
|
log.d(e);
|
||||||
|
ref.read(prompt).text +=
|
||||||
|
"\nJe n'ai pas la bonne clé API secret pour OpenAI, connard.";
|
||||||
|
ref.read(isLoading.notifier).state = false;
|
||||||
|
return "\nJe n'ai pas la bonne clé API secret pour OpenAI, connard.";
|
||||||
|
}
|
||||||
|
|
||||||
String anwserString = '';
|
String anwserString = '';
|
||||||
for (final choice in anwser.choices) {
|
for (final choice in anwser.choices) {
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
import 'package:bogui/global.dart';
|
import 'package:bogui/global.dart';
|
||||||
import 'package:bogui/riverpods/openai.dart';
|
import 'package:bogui/riverpods/openai.dart';
|
||||||
import 'package:bogui/widgets/paramters_sliders.dart';
|
import 'package:bogui/widgets/parameters_sliders.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
// import 'package:flutter/gestures.dart';
|
||||||
|
// import 'dart:math';
|
||||||
|
|
||||||
class Bogui extends ConsumerStatefulWidget {
|
class Bogui extends ConsumerStatefulWidget {
|
||||||
const Bogui({super.key, required this.title});
|
const Bogui({super.key, required this.title});
|
||||||
|
@ -21,6 +23,7 @@ class _BoguiState extends ConsumerState<Bogui> {
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
gpt = OpenAI();
|
gpt = OpenAI();
|
||||||
|
gpt.init();
|
||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +34,7 @@ class _BoguiState extends ConsumerState<Bogui> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
final scrollController = ScrollController();
|
||||||
screenWidth = MediaQuery.of(context).size.width;
|
screenWidth = MediaQuery.of(context).size.width;
|
||||||
// screenHight = MediaQuery.of(context).size.height;
|
// screenHight = MediaQuery.of(context).size.height;
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
@ -40,8 +44,24 @@ class _BoguiState extends ConsumerState<Bogui> {
|
||||||
body: Center(
|
body: Center(
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
width: screenWidth,
|
width: screenWidth,
|
||||||
|
child: Listener(
|
||||||
|
onPointerSignal: (_) {
|
||||||
|
// if (_ is PointerScrollEvent) {
|
||||||
|
// scrollController.jumpTo(
|
||||||
|
// max(
|
||||||
|
// min(
|
||||||
|
// scrollController.position.maxScrollExtent,
|
||||||
|
// scrollController.offset + _.scrollDelta.dx,
|
||||||
|
// ),
|
||||||
|
// scrollController.position.minScrollExtent,
|
||||||
|
// ),
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
},
|
||||||
child: SingleChildScrollView(
|
child: SingleChildScrollView(
|
||||||
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
|
controller: scrollController,
|
||||||
|
child:
|
||||||
|
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
|
||||||
const Spacer(flex: 3),
|
const Spacer(flex: 3),
|
||||||
Column(children: <Widget>[
|
Column(children: <Widget>[
|
||||||
const SizedBox(height: 40),
|
const SizedBox(height: 40),
|
||||||
|
@ -55,6 +75,7 @@ class _BoguiState extends ConsumerState<Bogui> {
|
||||||
(() => _handleValidation()),
|
(() => _handleValidation()),
|
||||||
},
|
},
|
||||||
child: TextField(
|
child: TextField(
|
||||||
|
scrollPhysics: const NeverScrollableScrollPhysics(),
|
||||||
focusNode: promptFocus,
|
focusNode: promptFocus,
|
||||||
controller: ref.read(gpt.prompt),
|
controller: ref.read(gpt.prompt),
|
||||||
autofocus: true,
|
autofocus: true,
|
||||||
|
@ -79,12 +100,12 @@ class _BoguiState extends ConsumerState<Bogui> {
|
||||||
size: 30,
|
size: 30,
|
||||||
)),
|
)),
|
||||||
border: OutlineInputBorder(
|
border: OutlineInputBorder(
|
||||||
borderSide:
|
borderSide: BorderSide(
|
||||||
BorderSide(color: Colors.grey[500]!, width: 2),
|
color: Colors.grey[500]!, width: 2),
|
||||||
borderRadius: BorderRadius.circular(8)),
|
borderRadius: BorderRadius.circular(8)),
|
||||||
focusedBorder: OutlineInputBorder(
|
focusedBorder: OutlineInputBorder(
|
||||||
borderSide:
|
borderSide: BorderSide(
|
||||||
BorderSide(color: Colors.grey[500]!, width: 2.5),
|
color: Colors.grey[500]!, width: 2.5),
|
||||||
borderRadius: BorderRadius.circular(8),
|
borderRadius: BorderRadius.circular(8),
|
||||||
),
|
),
|
||||||
contentPadding: const EdgeInsets.all(20),
|
contentPadding: const EdgeInsets.all(20),
|
||||||
|
@ -102,7 +123,8 @@ class _BoguiState extends ConsumerState<Bogui> {
|
||||||
foregroundColor: Colors.white, elevation: 4,
|
foregroundColor: Colors.white, elevation: 4,
|
||||||
backgroundColor: orangeC, // foreground
|
backgroundColor: orangeC, // foreground
|
||||||
),
|
),
|
||||||
onPressed: ref.watch(gpt.prompt).text.length > 100000000000
|
onPressed:
|
||||||
|
ref.watch(gpt.prompt).text.length > 100000000000
|
||||||
? null
|
? null
|
||||||
: () {
|
: () {
|
||||||
_handleValidation();
|
_handleValidation();
|
||||||
|
@ -139,6 +161,7 @@ class _BoguiState extends ConsumerState<Bogui> {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,6 +109,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.1"
|
version: "2.0.1"
|
||||||
|
logger:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: logger
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.1.0"
|
||||||
matcher:
|
matcher:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|
|
@ -20,6 +20,7 @@ dependencies:
|
||||||
ref: main
|
ref: main
|
||||||
flutter_hooks: ^0.18.0
|
flutter_hooks: ^0.18.0
|
||||||
flutter_riverpod: ^2.1.3
|
flutter_riverpod: ^2.1.3
|
||||||
|
logger: ^1.1.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
@ -32,9 +33,8 @@ flutter:
|
||||||
uses-material-design: true
|
uses-material-design: true
|
||||||
|
|
||||||
# To add assets to your application, add an assets section, like this:
|
# To add assets to your application, add an assets section, like this:
|
||||||
# assets:
|
assets:
|
||||||
# - images/a_dot_burr.jpeg
|
- .env
|
||||||
# - images/a_dot_ham.jpeg
|
|
||||||
|
|
||||||
# An image asset can refer to one or more resolution-specific "variants", see
|
# An image asset can refer to one or more resolution-specific "variants", see
|
||||||
# https://flutter.dev/assets-and-images/#resolution-aware
|
# https://flutter.dev/assets-and-images/#resolution-aware
|
||||||
|
|
|
@ -2,14 +2,17 @@
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
|
OPENAI_SECRET_KEY=$1
|
||||||
|
[[ ! $OPENAI_SECRET_KEY ]] && echo "Please pass a valid OpenAI token API key" && exit 1
|
||||||
|
|
||||||
flutter clean && flutter pub get
|
flutter clean && flutter pub get
|
||||||
|
|
||||||
if [[ $1 == "html" ]]; then
|
if [[ $1 == "html" ]]; then
|
||||||
rm -rf /home/poka/dev/bogui/build/web && flutter build web --web-renderer html
|
rm -rf /home/poka/dev/bogui/build/web && flutter build web --web-renderer html --dart-define=OPENAPI_SECRET_KEY=${OPENAI_SECRET_KEY}
|
||||||
ssh -p 10322 poka@p2p.legal 'rm -rf /home/poka/bogui-html && mkdir /home/poka/bogui-html'
|
ssh -p 10322 poka@p2p.legal 'rm -rf /home/poka/bogui-html && mkdir /home/poka/bogui-html'
|
||||||
rs /home/poka/dev/bogui/build/web poka@p2p.legal:/home/poka/bogui-html/ 10322
|
rs /home/poka/dev/bogui/build/web poka@p2p.legal:/home/poka/bogui-html/ 10322
|
||||||
else
|
else
|
||||||
rm -rf /home/poka/dev/bogui/build/web && flutter build web --web-renderer canvaskit
|
rm -rf /home/poka/dev/bogui/build/web && flutter build web --web-renderer canvaskit --dart-define=OPENAPI_SECRET_KEY=${OPENAI_SECRET_KEY}
|
||||||
ssh -p 10322 poka@p2p.legal 'rm -rf /home/poka/bogui-web && mkdir /home/poka/bogui-web'
|
ssh -p 10322 poka@p2p.legal 'rm -rf /home/poka/bogui-web && mkdir /home/poka/bogui-web'
|
||||||
rs /home/poka/dev/bogui/build/web poka@p2p.legal:/home/poka/bogui/ 10322
|
rs /home/poka/dev/bogui/build/web poka@p2p.legal:/home/poka/bogui/ 10322
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in New Issue