Last active
January 5, 2023 10:46
-
-
Save D3v0-wsc/2972b313d5a8d707782ba4aa8041c305 to your computer and use it in GitHub Desktop.
chatgpt-clone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
import 'dart:convert'; | |
///by Toan N. | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
final TextEditingController _promptController = TextEditingController(); | |
final TextEditingController _modelController = TextEditingController(); | |
final TextEditingController _keyController = TextEditingController(); | |
final ScrollController _scrollController = ScrollController(); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('Mon assistant virtuel'), | |
), | |
body: Column( | |
children: [ | |
Expanded( | |
child: ListView.builder( | |
controller: _scrollController, | |
itemCount: _completions.length, | |
itemBuilder: (BuildContext context, int index) { | |
return Text(_completions[index]); | |
}, | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: TextField( | |
controller: _promptController, | |
decoration: InputDecoration( | |
hintText: 'Quel est votre question ?', | |
), | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: TextField( | |
controller: _modelController, | |
decoration: InputDecoration( | |
hintText: 'Entrez un modèle (facultatif): text-davinci-003', | |
), | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: TextField( | |
controller: _keyController, | |
decoration: InputDecoration( | |
hintText: 'Entrez votre clé API', | |
), | |
), | |
), | |
ElevatedButton( | |
child: Text('Entrez votre question'), | |
onPressed: () { | |
_getCompletion(_promptController.text, _modelController.text, _keyController.text); | |
}, | |
), | |
], | |
), | |
), | |
); | |
} | |
List<String> _completions = []; | |
void _getCompletion(String prompt, String model, String key) { | |
_completions.add(prompt); | |
_scrollController.jumpTo(_scrollController.position.maxScrollExtent); | |
_promptController.clear(); | |
setState(() {}); | |
_fetchCompletion(prompt, model, key).then((response) { | |
setState(() { | |
_completions.add(response); | |
_scrollController.jumpTo(_scrollController.position.maxScrollExtent); | |
}); | |
}); | |
} | |
Future<String> _fetchCompletion(String prompt, String model, String key) async { | |
String apiKey = key; | |
final response = await http.post( | |
Uri.parse('https://api.openai.com/v1/completions'), | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': | |
'Bearer $apiKey', | |
}, | |
body: jsonEncode({ | |
'prompt': prompt, | |
'model': model.isEmpty ? 'text-davinci-003' : model, | |
"temperature": 0.9, | |
"max_tokens": 150, | |
"top_p": 1, | |
"frequency_penalty": 0.6, | |
"presence_penalty": 0 | |
}), | |
); | |
final responseJson = json.decode(utf8.decode(response.bodyBytes)); | |
return responseJson['choices'][0]['text']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment