-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
73 lines (67 loc) · 2.05 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _openAIresponse = "";
void _getOpenAIresponse(String message) async {
String openAIUrl = "https://api.openai.com/v1/engines/davinci-codex/completions";
String openAIKey = "YOUR_API_KEY_HERE";
String prompt = "Please generate a response to this message: " + message;
var response = await http.post(openAIUrl,
headers: {"Content-Type": "application/json", "Authorization": "Bearer $openAIKey"},
body: jsonEncode({
"prompt": prompt,
"max_tokens": 100,
}));
setState(() {
_openAIresponse = jsonDecode(response.body)["choices"][0]["text"];
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("OpenAI Chat App"),
),
body: Column(
children: [
Expanded(
child: Container(
padding: EdgeInsets.all(10),
child: TextField(
onSubmitted: (value) {
_getOpenAIresponse(value);
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Enter your message",
),
),
),
),
Expanded(
child: Container(
padding: EdgeInsets.all(10),
child: Text(_openAIresponse),
),
),
Expanded(
child: Container(
padding: EdgeInsets.all(10),
child: RaisedButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => DALLENImageGenerator()));
},
child: Text("DALL-E Image Generator"),
),
),
),
],
),
);
}
}