提交 c2c63647 authored 作者: songchuancai's avatar songchuancai

增加会话名称自动生成

上级 0f950dc7
差异被折叠。
......@@ -6,6 +6,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import '../models/app_item.dart';
import '../services/chat_service.dart';
import '../services/storage_service.dart';
import '../home_page.dart';
......@@ -22,6 +23,8 @@ class ChatPage extends ConsumerStatefulWidget {
}
class _ChatPageState extends ConsumerState<ChatPage> {
final OpenAIService openAIService = OpenAIService();
@override
void initState() {
super.initState();
......@@ -46,6 +49,12 @@ class _ChatPageState extends ConsumerState<ChatPage> {
}
@override
void dispose() {
openAIService.clearConversation();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
......@@ -78,6 +87,7 @@ class _ChatPageState extends ConsumerState<ChatPage> {
customDescription: widget.app.description,
customImageUrl: widget.app.iconUrl,
hideNavigation: true,
openAIService: openAIService,
),
);
}
......
......@@ -8,6 +8,32 @@ class OpenAIService {
// final String apiKey = 'sk-OVjS7VE9mT68Uvg7kSFoMnbU6EU836FO';
// final String appKey = 'app-FRP2s2wSx01rsE67';
String? conversationId;
bool _isFirstMessage = true;
Future<void> _generateConversationName() async {
if (conversationId == null) return;
var prefs = await SharedPreferences.getInstance();
var storageService = StorageService(prefs);
String appKey = storageService.getWorkbenchToken() ?? '';
try {
final response = await http.post(
Uri.parse('$baseUrl/api/conversations/$conversationId/name'),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $appKey',
},
body: jsonEncode({"auto_generate": true}),
);
if (response.statusCode != 200) {
print('生成会话名称失败: ${response.statusCode}');
}
} catch (e) {
print('生成会话名称异常: $e');
}
}
Stream<String> chatGPTAPI(String message) async* {
final client = http.Client();
......@@ -85,11 +111,16 @@ class OpenAIService {
} catch (e) {
throw Exception(e.toString());
} finally {
if (_isFirstMessage && conversationId != null) {
await _generateConversationName();
_isFirstMessage = false;
}
client.close();
}
}
void clearConversation() {
conversationId = null;
_isFirstMessage = true;
}
}
......@@ -9,12 +9,12 @@ import audioplayers_darwin
import flutter_tts
import path_provider_foundation
import shared_preferences_foundation
import speech_to_text_macos
import speech_to_text
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
FlutterTtsPlugin.register(with: registry.registrar(forPlugin: "FlutterTtsPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
SpeechToTextMacosPlugin.register(with: registry.registrar(forPlugin: "SpeechToTextMacosPlugin"))
SpeechToTextPlugin.register(with: registry.registrar(forPlugin: "SpeechToTextPlugin"))
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论