Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
F
flutter-chat
Project
Project
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
Graph
比较
统计图
议题
0
议题
0
列表
看板
标记
Milestones
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
songchuancai
flutter-chat
Commits
f21dc626
提交
f21dc626
authored
11月 08, 2024
作者:
songchuancai
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
调整聊天接口
上级
6fcbd5a6
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
89 行增加
和
127 行删除
+89
-127
home_page.dart
lib/home_page.dart
+10
-5
openai_service.dart
lib/openai_service.dart
+0
-90
chat_service.dart
lib/services/chat_service.dart
+79
-32
没有找到文件。
lib/home_page.dart
浏览文件 @
f21dc626
import
'package:allen/openai_service.dart'
;
import
'package:allen/pallete.dart'
;
import
'package:animate_do/animate_do.dart'
;
import
'package:flutter/material.dart'
;
...
...
@@ -9,9 +8,10 @@ import 'package:flutter/foundation.dart' show kIsWeb;
import
'package:uuid/uuid.dart'
;
import
'models/conversation.dart'
;
import
'services/storage_service.dart'
;
import
'
package:shared_preferences/shared_preferences
.dart'
;
import
'
services/chat_service
.dart'
;
import
'models/chat_message.dart'
;
import
'models/user.dart'
;
import
'package:shared_preferences/shared_preferences.dart'
;
class
HomePage
extends
StatefulWidget
{
const
HomePage
({
super
.
key
});
...
...
@@ -37,6 +37,7 @@ class _HomePageState extends State<HomePage> {
bool
_isListeningPressed
=
false
;
String
_currentVoiceText
=
''
;
late
StorageService
_storageService
;
late
SharedPreferences
_prefs
;
late
Conversation
_currentConversation
;
List
<
Conversation
>
_conversations
=
[];
...
...
@@ -49,8 +50,8 @@ class _HomePageState extends State<HomePage> {
}
Future
<
void
>
_initializeStorage
()
async
{
final
prefs
=
await
SharedPreferences
.
getInstance
();
_storageService
=
StorageService
(
prefs
);
_
prefs
=
await
SharedPreferences
.
getInstance
();
_storageService
=
StorageService
(
_
prefs
);
_conversations
=
await
_storageService
.
getConversations
();
if
(
_conversations
.
isEmpty
)
{
_createNewConversation
();
...
...
@@ -120,6 +121,10 @@ class _HomePageState extends State<HomePage> {
Future
<
void
>
systemSpeak
(
String
content
)
async
{
try
{
if
(
kIsWeb
)
{
// 设置语速
await
flutterTts
.
setSpeechRate
(
3
);
// 音调
await
flutterTts
.
setPitch
(
0.8
);
await
flutterTts
.
speak
(
content
);
}
else
{
await
flutterTts
.
setSharedInstance
(
true
);
...
...
@@ -502,7 +507,7 @@ class _HomePageState extends State<HomePage> {
],
),
);
if
(
confirmed
==
true
&&
context
.
mounted
)
{
await
StorageService
.
clearUser
();
Navigator
.
of
(
context
).
pushReplacementNamed
(
'/login'
);
...
...
lib/openai_service.dart
deleted
100644 → 0
浏览文件 @
6fcbd5a6
import
'dart:convert'
;
import
'package:http/http.dart'
as
http
;
class
OpenAIService
{
final
String
baseUrl
=
'https://portal.apps.iytcloud.com/console/api/openapi/chat'
;
final
String
apiKey
=
'sk-VEug2XmXEmHyFLtSbRBagBgrwMVhCXks'
;
final
String
appKey
=
'app-UtEKZmmxId1HIjQP'
;
String
?
conversationId
;
Stream
<
String
>
chatGPTAPI
(
String
message
)
async
*
{
final
client
=
http
.
Client
();
var
buffer
=
StringBuffer
();
try
{
final
request
=
http
.
Request
(
'POST'
,
Uri
.
parse
(
baseUrl
));
request
.
headers
.
addAll
({
'Content-Type'
:
'application/json'
,
'Authorization'
:
'Bearer
$apiKey
'
,
});
Map
<
String
,
dynamic
>
requestBody
=
{
'app_key'
:
appKey
,
'query'
:
message
,
'stream'
:
true
,
};
if
(
conversationId
!=
null
)
{
requestBody
[
'conversation_id'
]
=
conversationId
as
String
;
}
request
.
body
=
jsonEncode
(
requestBody
);
final
streamedResponse
=
await
client
.
send
(
request
);
if
(
streamedResponse
.
statusCode
==
200
)
{
DateTime
startTime
=
DateTime
.
now
();
// 直接处理字节流
await
for
(
var
bytes
in
streamedResponse
.
stream
)
{
if
(
DateTime
.
now
().
difference
(
startTime
).
inMinutes
>=
2
)
{
throw
Exception
(
'请求超时,请稍后重试'
);
}
// 将字节转换为字符串并添加到缓冲区
String
chunk
=
utf8
.
decode
(
bytes
);
buffer
.
write
(
chunk
);
// 处理缓冲区中的每一行
while
(
buffer
.
toString
().
contains
(
'
\n
'
))
{
int
newlineIndex
=
buffer
.
toString
().
indexOf
(
'
\n
'
);
String
line
=
buffer
.
toString
().
substring
(
0
,
newlineIndex
).
trim
();
buffer
=
StringBuffer
(
buffer
.
toString
().
substring
(
newlineIndex
+
1
));
if
(
line
.
startsWith
(
'data: '
))
{
try
{
final
jsonStr
=
line
.
substring
(
6
);
final
Map
<
String
,
dynamic
>
data
=
jsonDecode
(
jsonStr
);
if
(
data
[
'conversation_id'
]
!=
null
)
{
conversationId
=
data
[
'conversation_id'
]
as
String
;
}
if
(
data
[
'event'
]
==
'message'
&&
data
[
'answer'
]
!=
null
)
{
yield
data
[
'answer'
]
as
String
;
}
else
if
(
data
[
'event'
]
==
'message_end'
)
{
return
;
}
}
catch
(
e
)
{
print
(
'JSON解析错误:
$e
'
);
continue
;
}
}
}
}
}
else
{
throw
Exception
(
'请求失败,状态码:
${streamedResponse.statusCode}
'
);
}
}
catch
(
e
)
{
throw
Exception
(
e
.
toString
());
}
finally
{
client
.
close
();
}
}
void
clearConversation
()
{
conversationId
=
null
;
}
}
lib/services/chat_service.dart
浏览文件 @
f21dc626
import
'dart:convert'
;
import
'package:http/http.dart'
as
http
;
class
ChatService
{
static
const
String
baseUrl
=
'https://portal.apps.iytcloud.com/console/api/openapi/chat'
;
Stream
<
String
>
streamChat
(
String
query
)
async
*
{
final
response
=
await
http
.
post
(
Uri
.
parse
(
baseUrl
),
headers:
{
'Accept'
:
'*/*'
,
'Accept-Language'
:
'zh-CN,zh;q=0.9'
,
'Authorization'
:
'Bearer sk-VEug2XmXEmHyFLtSbRBagBgrwMVhCXks'
,
'Content-Type'
:
'application/json; charset=utf-8'
,
},
body:
jsonEncode
({
'app_key'
:
'app-UtEKZmmxId1HIjQP'
,
'query'
:
query
,
class
OpenAIService
{
final
String
baseUrl
=
'https://knowledge-web.apps.iytcloud.com/console/api/openapi/chat'
;
final
String
apiKey
=
'sk-OVjS7VE9mT68Uvg7kSFoMnbU6EU836FO'
;
final
String
appKey
=
'app-FRP2s2wSx01rsE67'
;
String
?
conversationId
;
Stream
<
String
>
chatGPTAPI
(
String
message
)
async
*
{
final
client
=
http
.
Client
();
var
buffer
=
StringBuffer
();
try
{
final
request
=
http
.
Request
(
'POST'
,
Uri
.
parse
(
baseUrl
));
request
.
headers
.
addAll
({
'Content-Type'
:
'application/json'
,
'Authorization'
:
'Bearer
$apiKey
'
,
});
Map
<
String
,
dynamic
>
requestBody
=
{
'app_key'
:
appKey
,
'query'
:
message
,
'stream'
:
true
,
'conversation_id'
:
'f2a71524-53cd-4cd8-99be-abb6598b52db'
,
}),
);
final
stream
=
response
.
body
.
split
(
'
\n
'
);
for
(
var
line
in
stream
)
{
if
(
line
.
startsWith
(
'data: '
))
{
final
jsonStr
=
line
.
substring
(
6
);
if
(
jsonStr
.
isNotEmpty
)
{
try
{
final
data
=
jsonDecode
(
jsonStr
);
if
(
data
[
'event'
]
==
'message'
&&
data
[
'answer'
]
!=
null
)
{
yield
data
[
'answer'
];
};
if
(
conversationId
!=
null
)
{
requestBody
[
'conversation_id'
]
=
conversationId
as
String
;
}
request
.
body
=
jsonEncode
(
requestBody
);
final
streamedResponse
=
await
client
.
send
(
request
);
if
(
streamedResponse
.
statusCode
==
200
)
{
DateTime
startTime
=
DateTime
.
now
();
// 直接处理字节流
await
for
(
var
bytes
in
streamedResponse
.
stream
)
{
if
(
DateTime
.
now
().
difference
(
startTime
).
inMinutes
>=
2
)
{
throw
Exception
(
'请求超时,请稍后重试'
);
}
// 将字节转换为字符串并添加到缓冲区
String
chunk
=
utf8
.
decode
(
bytes
);
buffer
.
write
(
chunk
);
// 处理缓冲区中的每一行
while
(
buffer
.
toString
().
contains
(
'
\n
'
))
{
int
newlineIndex
=
buffer
.
toString
().
indexOf
(
'
\n
'
);
String
line
=
buffer
.
toString
().
substring
(
0
,
newlineIndex
).
trim
();
buffer
=
StringBuffer
(
buffer
.
toString
().
substring
(
newlineIndex
+
1
));
if
(
line
.
startsWith
(
'data: '
))
{
try
{
final
jsonStr
=
line
.
substring
(
6
);
final
Map
<
String
,
dynamic
>
data
=
jsonDecode
(
jsonStr
);
if
(
data
[
'conversation_id'
]
!=
null
)
{
conversationId
=
data
[
'conversation_id'
]
as
String
;
}
if
(
data
[
'event'
]
==
'message'
&&
data
[
'answer'
]
!=
null
)
{
yield
data
[
'answer'
]
as
String
;
}
else
if
(
data
[
'event'
]
==
'message_end'
)
{
return
;
}
}
catch
(
e
)
{
print
(
'JSON解析错误:
$e
'
);
continue
;
}
}
}
catch
(
e
)
{
print
(
'解析错误:
$e
'
);
}
}
}
else
{
throw
Exception
(
'请求失败,状态码:
${streamedResponse.statusCode}
'
);
}
}
catch
(
e
)
{
throw
Exception
(
e
.
toString
());
}
finally
{
client
.
close
();
}
}
void
clearConversation
()
{
conversationId
=
null
;
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论