50 lines
1.6 KiB
Dart
50 lines
1.6 KiB
Dart
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(String.fromEnvironment('OPENAPI_SECRET_KEY'));
|
|
// final prompt = TextEditingController();
|
|
final prompt =
|
|
StateProvider<TextEditingController>((ref) => TextEditingController());
|
|
final isLoading = StateProvider<bool>((ref) => false);
|
|
final temperature = StateProvider<double>((ref) => 0.7);
|
|
|
|
Future<String> completionEasy(WidgetRef ref) async {
|
|
if (ref.read(isLoading) || ref.read(prompt).text.length < 2) return '';
|
|
ref.read(isLoading.notifier).state = true;
|
|
final anwser = await OpenAI().completion(ref.read(prompt).text,
|
|
maxTokens: 250,
|
|
engine: Engine.davinci3,
|
|
temperature: ref.read(temperature),
|
|
echo: false,
|
|
stream: false);
|
|
|
|
String anwserString = '';
|
|
for (final choice in anwser.choices) {
|
|
anwserString += choice.text;
|
|
}
|
|
|
|
ref.read(prompt).text += anwserString;
|
|
ref.read(isLoading.notifier).state = false;
|
|
ref.read(prompt).selection = TextSelection.fromPosition(
|
|
TextPosition(offset: ref.read(prompt).text.length));
|
|
return anwserString;
|
|
}
|
|
}
|
|
|
|
double truncateDouble(double val, int decimals) {
|
|
String valString = val.toString();
|
|
int dotIndex = valString.indexOf('.');
|
|
|
|
// not enough decimals
|
|
int totalDecimals = valString.length - dotIndex - 1;
|
|
if (totalDecimals < decimals) {
|
|
decimals = totalDecimals;
|
|
}
|
|
|
|
valString = valString.substring(0, dotIndex + decimals + 1);
|
|
|
|
return double.parse(valString);
|
|
}
|