-
Notifications
You must be signed in to change notification settings - Fork 0
/
15_StreamBuilder_test.dart
206 lines (175 loc) · 5.5 KB
/
15_StreamBuilder_test.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:jp_flutter_demo/JPLog.dart';
class JPStreamBuilderExample extends StatelessWidget {
static String title = "15.StreamBuilder Example";
final GlobalKey<_JPStreamBuilderDemoState> gbKey = GlobalKey();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
JPStreamBuilderExample.title,
textAlign: TextAlign.center,
maxLines: 2,
),
),
backgroundColor: Colors.white,
body: JPStreamBuilderDemo(gbKey),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.ac_unit),
onPressed: () {
var state = gbKey.currentState;
state?.doSomeThing();
},
),
);
}
}
class JPStreamBuilderDemo extends StatefulWidget {
@override
_JPStreamBuilderDemoState createState() => _JPStreamBuilderDemoState();
JPStreamBuilderDemo(Key key) : super(key: key);
}
class _JPStreamBuilderDemoState extends State<JPStreamBuilderDemo> {
final apiKey = "Your OpenAI API Key";
final apiURL = "https://api.openai.com/v1/chat/completions";
final apiModel = "gpt-3.5-turbo";
StreamController<String> _streamController = StreamController<String>();
String myProblem = "请帮我用Dart写一个快速排序算法";
bool isAsking = false;
@override
void initState() {
super.initState();
_streamController.add("点击右下角按钮开始发问:$myProblem");
}
@override
Widget build(BuildContext context) {
return Center(
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(16),
child: StreamBuilder(
stream: _streamController.stream,
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
if (snapshot.hasError) return Text("发生错误: ${snapshot.error}");
if (snapshot.hasData) {
String content = snapshot.data ?? "";
if (content.length > 0) {
return Text(content);
}
}
return CircularProgressIndicator();
},
),
),
),
);
}
@override
dispose() {
super.dispose();
_streamController.close();
}
void doSomeThing() async {
if (isAsking) return;
isAsking = true;
_streamController.add("");
askGPT(myProblem);
// reqGPT(myProblem);
}
/// 流式请求
void askGPT(String problem) {
final messages = [
{
"role": "user",
"content": problem,
},
];
final requestBody = {
"model": apiModel,
"messages": messages,
"temperature": 0.5,
"stream": true
};
var request = http.Request("POST", Uri.parse(apiURL));
request.headers["Authorization"] = "Bearer $apiKey";
request.headers["Content-Type"] = "application/json; charset=UTF-8";
request.body = jsonEncode(requestBody);
JPrint("开始请求");
http.Client().send(request).then((response) {
String showContent = "";
final stream = response.stream.transform(utf8.decoder);
stream.listen((data) {
final dataLines = data.split("\n").where((element) => element.isNotEmpty).toList();
// JPrint("dataLines ${dataLines.length}");
for (String line in dataLines) {
if (!line.startsWith("data: ")) continue;
final data = line.substring(6);
// JPrint("data $data");
if (data == "[DONE]") break;
Map<String, dynamic> responseData = json.decode(data);
// JPrint("responseData $responseData");
List<dynamic> choices = responseData["choices"];
Map<String, dynamic> choice = choices[0];
Map<String, dynamic> delta = choice["delta"];
String content = delta["content"] ?? "";
showContent += content;
_streamController.add(showContent);
String finishReason = choice["finish_reason"] ?? "";
if (finishReason.length > 0) {
JPrint("finish_reason:$finishReason");
break;
}
}
},
onDone: () {
_streamController.add(showContent);
isAsking = false;
},
onError: (error) {
_streamController.addError(error);
isAsking = false;
},
);
});
}
/// 一次性请求
Future<void> reqGPT(String problem) async {
final headers = {
"Authorization": "Bearer $apiKey",
"Content-Type": "application/json; charset=UTF-8",
};
final messages = [
{
"role": "user",
"content": problem,
},
];
final requestBody = {
"model": apiModel,
"messages": messages,
};
JPrint("开始请求");
final response = await http.post(
Uri.parse(apiURL),
headers: headers,
body: jsonEncode(requestBody),
);
Map<String, dynamic> responseData = json.decode(utf8.decode(response.bodyBytes));
if (response.statusCode == 200) {
JPrint("请求成功");
List<dynamic> choices = responseData["choices"];
Map<String, dynamic> choice = choices[0];
Map<String, dynamic> message = choice["message"];
String content = message["content"];
// 将数据添加到流中
_streamController.add(content);
} else {
_streamController.addError("请求失败");
}
isAsking = false;
}
}