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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import 'dart:async';
|
2 |
+
import 'dart:math';
|
3 |
+
import 'package:chat_gpt_sdk/chat_gpt_sdk.dart';
|
4 |
+
import 'package:example/constants.dart';
|
5 |
+
import 'package:flutter/material.dart';
|
6 |
+
import 'package:material_buttonx/materialButtonX.dart';
|
7 |
+
|
8 |
+
void main() => runApp(const MyApp());
|
9 |
+
|
10 |
+
class MyApp extends StatelessWidget {
|
11 |
+
const MyApp({super.key});
|
12 |
+
|
13 |
+
@override
|
14 |
+
Widget build(BuildContext context) {
|
15 |
+
return const MaterialApp(
|
16 |
+
home: TranslateScreen(),
|
17 |
+
);
|
18 |
+
}
|
19 |
+
}
|
20 |
+
|
21 |
+
class TranslateScreen extends StatefulWidget {
|
22 |
+
const TranslateScreen({super.key});
|
23 |
+
@override
|
24 |
+
State<TranslateScreen> createState() => _TranslateScreenState();
|
25 |
+
}
|
26 |
+
|
27 |
+
class _TranslateScreenState extends State<TranslateScreen> {
|
28 |
+
/// text controller
|
29 |
+
final _txtWord = TextEditingController();
|
30 |
+
|
31 |
+
late OpenAI openAI;
|
32 |
+
|
33 |
+
Future<CompleteResponse?>? _translateFuture;
|
34 |
+
void _translateEngToThai() async {
|
35 |
+
final request = CompleteText(
|
36 |
+
prompt: translateEngToThai(word: _txtWord.text.toString()),
|
37 |
+
maxTokens: 200,
|
38 |
+
model: Gpt3TurboInstruct());
|
39 |
+
|
40 |
+
setState(() {
|
41 |
+
_translateFuture = openAI.onCompletion(request: request);
|
42 |
+
});
|
43 |
+
}
|
44 |
+
|
45 |
+
///parameter name is require
|
46 |
+
void gptFunctionCalling() async {
|
47 |
+
final request = ChatCompleteText(
|
48 |
+
messages: [
|
49 |
+
Messages(
|
50 |
+
role: Role.user,
|
51 |
+
content: "What is the weather like in Boston?",
|
52 |
+
name: "get_current_weather")
|
53 |
+
.toJson(),
|
54 |
+
],
|
55 |
+
maxToken: 200,
|
56 |
+
model: Gpt41106PreviewChatModel(),
|
57 |
+
tools: [
|
58 |
+
{
|
59 |
+
"type": "function",
|
60 |
+
"function": {
|
61 |
+
"name": "get_current_weather",
|
62 |
+
"description": "Get the current weather in a given location",
|
63 |
+
"parameters": {
|
64 |
+
"type": "object",
|
65 |
+
"properties": {
|
66 |
+
"location": {
|
67 |
+
"type": "string",
|
68 |
+
"description": "The city and state, e.g. San Francisco, CA"
|
69 |
+
},
|
70 |
+
"unit": {
|
71 |
+
"type": "string",
|
72 |
+
"enum": ["celsius", "fahrenheit"]
|
73 |
+
}
|
74 |
+
},
|
75 |
+
"required": ["location"]
|
76 |
+
}
|
77 |
+
}
|
78 |
+
}
|
79 |
+
],
|
80 |
+
toolChoice: 'auto',
|
81 |
+
);
|
82 |
+
|
83 |
+
ChatCTResponse? response = await openAI.onChatCompletion(request: request);
|
84 |
+
debugPrint("$response");
|
85 |
+
}
|
86 |
+
|
87 |
+
void imageInput() async {
|
88 |
+
final request = ChatCompleteText(
|
89 |
+
messages: [
|
90 |
+
{
|
91 |
+
"role": "user",
|
92 |
+
"content": [
|
93 |
+
{"type": "text", "text": "What’s in this image?"},
|
94 |
+
{
|
95 |
+
"type": "image_url",
|
96 |
+
"image_url": {
|
97 |
+
"url":
|
98 |
+
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
|
99 |
+
}
|
100 |
+
}
|
101 |
+
]
|
102 |
+
}
|
103 |
+
],
|
104 |
+
maxToken: 200,
|
105 |
+
model: Gpt4VisionPreviewChatModel(),
|
106 |
+
);
|
107 |
+
|
108 |
+
ChatCTResponse? response = await openAI.onChatCompletion(request: request);
|
109 |
+
debugPrint("$response");
|
110 |
+
}
|
111 |
+
|
112 |
+
void gpt4() async {
|
113 |
+
final request = ChatCompleteText(messages: [
|
114 |
+
Messages(role: Role.assistant, content: 'Hello!').toJson(),
|
115 |
+
], maxToken: 200, model: Gpt4ChatModel());
|
116 |
+
|
117 |
+
await openAI.onChatCompletion(request: request);
|
118 |
+
}
|
119 |
+
|
120 |
+
@override
|
121 |
+
void initState() {
|
122 |
+
openAI = OpenAI.instance.build(
|
123 |
+
token: kToken,
|
124 |
+
baseOption: HttpSetup(
|
125 |
+
receiveTimeout: const Duration(seconds: 20),
|
126 |
+
connectTimeout: const Duration(seconds: 20)),
|
127 |
+
enableLog: true);
|
128 |
+
super.initState();
|
129 |
+
}
|
130 |
+
|
131 |
+
@override
|
132 |
+
Widget build(BuildContext context) {
|
133 |
+
var size = MediaQuery.of(context).size;
|
134 |
+
return Scaffold(
|
135 |
+
backgroundColor: Colors.white,
|
136 |
+
body: SingleChildScrollView(
|
137 |
+
child: Center(
|
138 |
+
child: Padding(
|
139 |
+
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
140 |
+
child: Column(
|
141 |
+
crossAxisAlignment: CrossAxisAlignment.center,
|
142 |
+
children: [
|
143 |
+
/**
|
144 |
+
* title translate
|
145 |
+
*/
|
146 |
+
_titleCard(size),
|
147 |
+
/**
|
148 |
+
* input card
|
149 |
+
* insert your text for translate to th.com
|
150 |
+
*/
|
151 |
+
_inputCard(size),
|
152 |
+
|
153 |
+
/**
|
154 |
+
* card input translate
|
155 |
+
*/
|
156 |
+
_resultCard(size),
|
157 |
+
/**
|
158 |
+
* button translate
|
159 |
+
*/
|
160 |
+
_btnTranslate()
|
161 |
+
],
|
162 |
+
),
|
163 |
+
),
|
164 |
+
),
|
165 |
+
),
|
166 |
+
bottomNavigationBar: _navigation(size),
|
167 |
+
);
|
168 |
+
}
|
169 |
+
|
170 |
+
Widget _btnTranslate() {
|
171 |
+
return Row(
|
172 |
+
mainAxisAlignment: MainAxisAlignment.end,
|
173 |
+
children: [
|
174 |
+
Padding(
|
175 |
+
padding: const EdgeInsets.only(right: 16.0),
|
176 |
+
child: MaterialButtonX(
|
177 |
+
message: "Translate",
|
178 |
+
height: 40.0,
|
179 |
+
width: 130.0,
|
180 |
+
color: Colors.blueAccent,
|
181 |
+
icon: Icons.translate,
|
182 |
+
iconSize: 18.0,
|
183 |
+
radius: 46.0,
|
184 |
+
onClick: _translateEngToThai,
|
185 |
+
),
|
186 |
+
),
|
187 |
+
],
|
188 |
+
);
|
189 |
+
}
|
190 |
+
|
191 |
+
Widget _resultCard(Size size) {
|
192 |
+
return FutureBuilder<CompleteResponse?>(
|
193 |
+
future: _translateFuture,
|
194 |
+
builder: (context, snapshot) {
|
195 |
+
final text = snapshot.data?.choices.last.text;
|
196 |
+
return Container(
|
197 |
+
margin: const EdgeInsets.symmetric(vertical: 32.0),
|
198 |
+
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
199 |
+
alignment: Alignment.bottomCenter,
|
200 |
+
width: size.width * .86,
|
201 |
+
height: size.height * .3,
|
202 |
+
decoration: heroCard,
|
203 |
+
child: SingleChildScrollView(
|
204 |
+
child: Column(
|
205 |
+
children: [
|
206 |
+
Text(
|
207 |
+
text ?? 'Loading...',
|
208 |
+
style: const TextStyle(color: Colors.black, fontSize: 18.0),
|
209 |
+
),
|
210 |
+
SizedBox(
|
211 |
+
width: size.width,
|
212 |
+
child: const Divider(
|
213 |
+
color: Colors.grey,
|
214 |
+
thickness: 1,
|
215 |
+
),
|
216 |
+
),
|
217 |
+
const Padding(
|
218 |
+
padding: EdgeInsets.all(12.0),
|
219 |
+
child: Row(
|
220 |
+
mainAxisAlignment: MainAxisAlignment.end,
|
221 |
+
children: [
|
222 |
+
Icon(
|
223 |
+
Icons.copy_outlined,
|
224 |
+
color: Colors.grey,
|
225 |
+
size: 22.0,
|
226 |
+
),
|
227 |
+
Padding(
|
228 |
+
padding: EdgeInsets.symmetric(horizontal: 8.0),
|
229 |
+
child: Icon(
|
230 |
+
Icons.delete_forever,
|
231 |
+
color: Colors.grey,
|
232 |
+
size: 22.0,
|
233 |
+
),
|
234 |
+
)
|
235 |
+
],
|
236 |
+
),
|
237 |
+
)
|
238 |
+
],
|
239 |
+
),
|
240 |
+
),
|
241 |
+
);
|
242 |
+
});
|
243 |
+
}
|
244 |
+
|
245 |
+
Padding _navigation(Size size) {
|
246 |
+
return Padding(
|
247 |
+
padding: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 18.0),
|
248 |
+
child: Container(
|
249 |
+
width: size.width * .8,
|
250 |
+
height: size.height * .06,
|
251 |
+
decoration: heroNav,
|
252 |
+
child: Row(
|
253 |
+
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
254 |
+
children: [
|
255 |
+
Container(
|
256 |
+
padding: const EdgeInsets.all(4.0),
|
257 |
+
decoration: BoxDecoration(
|
258 |
+
color: Colors.indigoAccent,
|
259 |
+
borderRadius: BorderRadius.circular(50.0)),
|
260 |
+
child: const Icon(
|
261 |
+
Icons.translate,
|
262 |
+
color: Colors.white,
|
263 |
+
size: 22.0,
|
264 |
+
),
|
265 |
+
),
|
266 |
+
const Icon(
|
267 |
+
Icons.record_voice_over_outlined,
|
268 |
+
color: Colors.indigoAccent,
|
269 |
+
size: 22.0,
|
270 |
+
),
|
271 |
+
const Icon(
|
272 |
+
Icons.favorite,
|
273 |
+
color: Colors.indigoAccent,
|
274 |
+
size: 22.0,
|
275 |
+
),
|
276 |
+
const Icon(
|
277 |
+
Icons.person,
|
278 |
+
color: Colors.indigoAccent,
|
279 |
+
size: 22.0,
|
280 |
+
)
|
281 |
+
],
|
282 |
+
),
|
283 |
+
),
|
284 |
+
);
|
285 |
+
}
|
286 |
+
|
287 |
+
Widget _titleCard(Size size) {
|
288 |
+
return Container(
|
289 |
+
margin: const EdgeInsets.symmetric(vertical: 32.0),
|
290 |
+
width: size.width * .86,
|
291 |
+
height: size.height * .08,
|
292 |
+
decoration: heroCard,
|
293 |
+
child: Row(
|
294 |
+
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
295 |
+
children: [
|
296 |
+
Row(
|
297 |
+
children: [
|
298 |
+
ClipRRect(
|
299 |
+
borderRadius: BorderRadius.circular(30),
|
300 |
+
child: Image.network(
|
301 |
+
"https://www.clipartmax.com/png/middle/200-2009207_francais-english-italiano-english-flag-icon-flat.png",
|
302 |
+
fit: BoxFit.cover,
|
303 |
+
width: 30.0,
|
304 |
+
height: 30.0,
|
305 |
+
),
|
306 |
+
),
|
307 |
+
const Padding(
|
308 |
+
padding: EdgeInsets.symmetric(horizontal: 4.0),
|
309 |
+
child: Text(
|
310 |
+
"Eng",
|
311 |
+
style: TextStyle(color: Colors.grey, fontSize: 12.0),
|
312 |
+
),
|
313 |
+
),
|
314 |
+
Transform.rotate(
|
315 |
+
angle: -pi / 2,
|
316 |
+
child: const Icon(
|
317 |
+
Icons.arrow_back_ios_new,
|
318 |
+
size: 16.0,
|
319 |
+
color: Colors.grey,
|
320 |
+
),
|
321 |
+
)
|
322 |
+
],
|
323 |
+
),
|
324 |
+
const Padding(
|
325 |
+
padding: EdgeInsets.symmetric(horizontal: 12.0),
|
326 |
+
child: Icon(
|
327 |
+
Icons.swap_horiz_outlined,
|
328 |
+
color: Colors.grey,
|
329 |
+
size: 22.0,
|
330 |
+
),
|
331 |
+
),
|
332 |
+
Row(
|
333 |
+
children: [
|
334 |
+
ClipRRect(
|
335 |
+
borderRadius: BorderRadius.circular(30),
|
336 |
+
child: Image.network(
|
337 |
+
"https://cdn-icons-png.flaticon.com/512/197/197452.png",
|
338 |
+
fit: BoxFit.cover,
|
339 |
+
width: 30.0,
|
340 |
+
height: 30.0,
|
341 |
+
),
|
342 |
+
),
|
343 |
+
const Padding(
|
344 |
+
padding: EdgeInsets.symmetric(horizontal: 4.0),
|
345 |
+
child: Text(
|
346 |
+
"Thai",
|
347 |
+
style: TextStyle(color: Colors.grey, fontSize: 12.0),
|
348 |
+
),
|
349 |
+
),
|
350 |
+
Transform.rotate(
|
351 |
+
angle: -pi / 2,
|
352 |
+
child: const Icon(
|
353 |
+
Icons.arrow_back_ios_new,
|
354 |
+
size: 16.0,
|
355 |
+
color: Colors.grey,
|
356 |
+
),
|
357 |
+
)
|
358 |
+
],
|
359 |
+
)
|
360 |
+
],
|
361 |
+
),
|
362 |
+
);
|
363 |
+
}
|
364 |
+
|
365 |
+
Widget _inputCard(Size size) {
|
366 |
+
return Container(
|
367 |
+
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
368 |
+
alignment: Alignment.bottomCenter,
|
369 |
+
width: size.width * .86,
|
370 |
+
height: size.height * .25,
|
371 |
+
decoration: heroCard,
|
372 |
+
child: SingleChildScrollView(
|
373 |
+
child: Column(
|
374 |
+
children: [
|
375 |
+
TextField(
|
376 |
+
decoration: const InputDecoration(
|
377 |
+
border: InputBorder.none,
|
378 |
+
enabledBorder: InputBorder.none,
|
379 |
+
disabledBorder: InputBorder.none),
|
380 |
+
controller: _txtWord,
|
381 |
+
maxLines: 6,
|
382 |
+
textInputAction: TextInputAction.newline,
|
383 |
+
keyboardType: TextInputType.multiline,
|
384 |
+
),
|
385 |
+
SizedBox(
|
386 |
+
width: size.width,
|
387 |
+
child: const Divider(
|
388 |
+
color: Colors.grey,
|
389 |
+
thickness: 1,
|
390 |
+
),
|
391 |
+
),
|
392 |
+
const Padding(
|
393 |
+
padding: EdgeInsets.all(12.0),
|
394 |
+
child: Row(
|
395 |
+
mainAxisAlignment: MainAxisAlignment.end,
|
396 |
+
children: [
|
397 |
+
Icon(
|
398 |
+
Icons.copy_outlined,
|
399 |
+
color: Colors.grey,
|
400 |
+
size: 22.0,
|
401 |
+
),
|
402 |
+
Padding(
|
403 |
+
padding: EdgeInsets.symmetric(horizontal: 8.0),
|
404 |
+
child: Icon(
|
405 |
+
Icons.record_voice_over_outlined,
|
406 |
+
color: Colors.grey,
|
407 |
+
size: 22.0,
|
408 |
+
),
|
409 |
+
)
|
410 |
+
],
|
411 |
+
),
|
412 |
+
)
|
413 |
+
],
|
414 |
+
),
|
415 |
+
),
|
416 |
+
);
|
417 |
+
}
|
418 |
+
}
|