Browse Source

添加自己的ai回复

newdev_shunjiawei
tanlongsheng 2 years ago
parent
commit
b78354277e
  1. 71
      lib/data/services/deapsound_ai_service.dart
  2. 20
      lib/modules/chat/controllers/chat_controller.dart

71
lib/data/services/deapsound_ai_service.dart

@ -0,0 +1,71 @@
import 'dart:convert';
import 'package:get_storage/get_storage.dart';
import 'package:http/http.dart' as http;
import '../../core/utils/logger.dart';
import 'ai_service.dart';
class DeapsoundAIService implements AiService {
late final String apiKey;
DeapsoundAIService() {
final GetStorage storage = GetStorage();
apiKey = storage.read('logintoken');
}
@override
Stream<String> sendMessageStream({
required List<Map<String, String>> messages,
required String systemPrompt,
}) async* {
final client = http.Client();
final url = Uri.parse('https://www.deapsound.com/ai/chat');
final headers = {
'Content-Type': 'application/json',
'Authorization': apiKey,
};
final requestBody = {
"msg": messages.last['content'],
};
try {
final request = http.Request('POST', url)
..headers.addAll(headers)
..body = jsonEncode(requestBody);
final response = await client.send(request);
//流式处理响应数据
await for (final chunk in response.stream
.transform(utf8.decoder)
.transform(const LineSplitter())) {
if (chunk.isEmpty) continue;
if (chunk.startsWith('data:')) {
final jsonStr = chunk.substring(5).trim();
try {
final data = jsonDecode(jsonStr);
if (data['event'] == 'workflow_finished') {
break;
}
if (data['event'] == 'text_chunk') {
String content = data['data']['text'] ?? '';
if (content.isNotEmpty) {
yield content;
}
}
} catch (e) {
Logger.error('JSON解析错误: $e');
}
}
}
} catch (e) {
Logger.error('请求异常: $e');
} finally {
client.close();
}
}
}

20
lib/modules/chat/controllers/chat_controller.dart

@ -3,6 +3,7 @@ import 'package:get/get.dart';
import 'package:flutter/widgets.dart';
import 'package:get_storage/get_storage.dart';
import '../../../data/services/ai_service.dart';
import '../../../data/services/deapsound_ai_service.dart';
import '../../../data/services/kouzi_ai_service.dart';
import '../../../data/services/speech_impl/voice_clone_tts_service.dart';
import '../models/message_model.dart';
@ -91,12 +92,19 @@ class ChatController extends GetxController {
agent = foundAgent;
// 亲子陪伴
if (agent.id != 'cyber_girlfriend') {
_aiService = Get.find<VolcanoAIService>();
} else {
Get.put(KouziAIService());
_aiService = Get.find<KouziAIService>();
switch (agent.id) {
case 'personal_assistant':
// 全能助手
Get.put(DeapsoundAIService());
_aiService = Get.find<DeapsoundAIService>();
break;
case 'cyber_girlfriend':
// 亲子陪伴
Get.put(KouziAIService());
_aiService = Get.find<KouziAIService>();
break;
default:
_aiService = Get.find<VolcanoAIService>();
}
// 使用克隆音色语音合成

Loading…
Cancel
Save