Browse Source

Merge branch 'new_dev' of https://github.com/deepcloud2048/deep_voice into new_dev

newdev_shunjiawei
lxm 1 year ago
parent
commit
5e66c42862
  1. 10
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt
  2. 19
      local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift
  3. 24
      local_plugins/chat_api/ios/chat_api/Sources/chat_api/ChatApiService.swift
  4. 14
      local_plugins/chat_api/ios/chat_api/Sources/chat_api/MCPClient.swift
  5. 2
      local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/MCPClient.kt
  6. 2
      local_plugins/open_ai_service/ios/Classes/MCPClient.swift
  7. 2
      local_plugins/open_ai_service/ios/open_ai_service/Sources/open_ai_service/MCPClient.swift

10
local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt

@ -81,6 +81,7 @@ object AgentService : CoroutineScope {
// 服务端连接 // 服务端连接
private var serverurl = "" private var serverurl = ""
private var usertoken = "" private var usertoken = ""
private var language = ""
// 历史聊天消息缓存 // 历史聊天消息缓存
private val historyMessages = JSONArray() private val historyMessages = JSONArray()
@ -197,8 +198,8 @@ object AgentService : CoroutineScope {
//获取语音识别的语种 //获取语音识别的语种
val supportedLanguagesConfig = config["supportedLanguages"] as? List<String> val supportedLanguagesConfig = config["supportedLanguages"] as? List<String>
val supportedLanguages = supportedLanguagesConfig?.toTypedArray() val supportedLanguages = supportedLanguagesConfig?.toTypedArray()
?: arrayOf("zh-CN", "en-US") // 默认支持多语言 ?: arrayOf("zh-CN") // 默认支持多语言
language = supportedLanguages[0]
val asrInitSuccess = initialize( val asrInitSuccess = initialize(
subscriptionKey = config["azureSpeechKey"]?.toString() ?: "", subscriptionKey = config["azureSpeechKey"]?.toString() ?: "",
region = config["azureSpeechRegion"]?.toString() ?: "", region = config["azureSpeechRegion"]?.toString() ?: "",
@ -825,7 +826,8 @@ object AgentService : CoroutineScope {
// 先添加系统提示词 // 先添加系统提示词
if (systemPrompt.isNotEmpty()) { if (systemPrompt.isNotEmpty()) {
val mapdata = UserPortrait.getAll() val mapdata = UserPortrait.getAll().toMutableMap()
mapdata["language"] = language
// Log.d(TAG, "liwei--------- 用户画像: ${mapdata}") // Log.d(TAG, "liwei--------- 用户画像: ${mapdata}")
val _systemPrompt = Utils.fillTemplate(systemPrompt, mapdata) val _systemPrompt = Utils.fillTemplate(systemPrompt, mapdata)
messagesWithSystemPrompt.put(createSystemMessage(_systemPrompt)) messagesWithSystemPrompt.put(createSystemMessage(_systemPrompt))
@ -1127,7 +1129,7 @@ object AgentService : CoroutineScope {
sendEvent("error", mapOf("code" to "EMPTY_LANGUAGES", "message" to "语言列表不能为空")) sendEvent("error", mapOf("code" to "EMPTY_LANGUAGES", "message" to "语言列表不能为空"))
return false return false
} }
language = languages[0]
return try { return try {
Log.i(TAG, "设置支持的语言: ${languages.joinToString(", ")}") Log.i(TAG, "设置支持的语言: ${languages.joinToString(", ")}")

19
local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift

@ -59,6 +59,7 @@ class AgentServiceImpl: NSObject {
internal var isAiStreaming: Bool = false internal var isAiStreaming: Bool = false
var currsessionId: String = "" var currsessionId: String = ""
var language: String = ""
private var idleTimer: DispatchSourceTimer? private var idleTimer: DispatchSourceTimer?
private let maxIdleSeconds: TimeInterval = 10 private let maxIdleSeconds: TimeInterval = 10
@ -158,6 +159,9 @@ class AgentServiceImpl: NSObject {
if let openaivisionModel = config["openaivisionModel"] as? String { if let openaivisionModel = config["openaivisionModel"] as? String {
self.visionModel = openaivisionModel self.visionModel = openaivisionModel
} }
if let supportedLanguages = config["supportedLanguages"] as? [String] {
self.language = supportedLanguages[0]
}
if let customSystemPrompt = config["systemPrompt"] as? String, !customSystemPrompt.isEmpty { if let customSystemPrompt = config["systemPrompt"] as? String, !customSystemPrompt.isEmpty {
self.systemPrompt = customSystemPrompt self.systemPrompt = customSystemPrompt
} else { } else {
@ -208,7 +212,7 @@ class AgentServiceImpl: NSObject {
azureTtsHelper?.addListener(self) azureTtsHelper?.addListener(self)
// 获取支持的语言列表,支持从配置中读取 // 获取支持的语言列表,支持从配置中读取
let supportedLanguages = config["supportedLanguages"] as? [String] ?? ["zh-CN", "en-US"] let supportedLanguages = config["supportedLanguages"] as? [String] ?? ["zh-CN"]
guard let asrSuccess = azureAsrHelper?.initialize( guard let asrSuccess = azureAsrHelper?.initialize(
subscriptionKey: azureSpeechKey, subscriptionKey: azureSpeechKey,
@ -476,9 +480,16 @@ audioStream.saveAudioDataTo(data: audioData)
var messages: [[String: Any]] = [] var messages: [[String: Any]] = []
if !systemPrompt.isEmpty { if !systemPrompt.isEmpty {
var info = UserPortrait.shared.getAll()
let info: [String: String] = UserPortrait.shared.getAll() info["language"] = self.language
// 添加调试日志
print("系统提示词模板: \(systemPrompt)")
print("用户画像数据: \(info)")
let _systemPrompt = fillTemplate(systemPrompt, with: info) let _systemPrompt = fillTemplate(systemPrompt, with: info)
print("替换后的系统提示词: \(_systemPrompt)")
messages.append(chatApiService.createSystemMessage(content: _systemPrompt)) messages.append(chatApiService.createSystemMessage(content: _systemPrompt))
os_log("添加系统提示消息", log: logger, type: .info) os_log("添加系统提示消息", log: logger, type: .info)
} }
@ -898,6 +909,8 @@ private func jsonToString(_ json: [String: Any]) -> String? {
return false return false
} }
self.language = languages[0]
// 如果正在识别,需要先停止 // 如果正在识别,需要先停止
let wasRecognizing = isRecognizing let wasRecognizing = isRecognizing
if wasRecognizing { if wasRecognizing {

24
local_plugins/chat_api/ios/chat_api/Sources/chat_api/ChatApiService.swift

@ -68,18 +68,18 @@ public class ChatApiService: NSObject {
/// 设置流式回调 /// 设置流式回调
public func setStreamCallback(_ sessionId: String, _ callback: StreamCallback) { public func setStreamCallback(_ sessionId: String, _ callback: StreamCallback) {
print("[ChatApiService] 设置回调 for sessionId: \(sessionId)") // print("[ChatApiService] 设置回调 for sessionId: \(sessionId)")
print("[ChatApiService] 回调对象: \(callback)") // print("[ChatApiService] 回调对象: \(callback)")
self.sessionCallbacks[sessionId] = callback self.sessionCallbacks[sessionId] = callback
print("[ChatApiService] 回调设置完成,当前回调数量: \(sessionCallbacks.count)") print("[ChatApiService] 回调设置完成,当前回调数量: \(sessionCallbacks.count)")
} }
public func getSessionCallback(_ sessionId: String) -> StreamCallback? { public func getSessionCallback(_ sessionId: String) -> StreamCallback? {
let callback = self.sessionCallbacks[sessionId] let callback = self.sessionCallbacks[sessionId]
print("[ChatApiService] getSessionCallback for sessionId: \(sessionId)") // print("[ChatApiService] getSessionCallback for sessionId: \(sessionId)")
print("[ChatApiService] sessionCallbacks count: \(sessionCallbacks.count)") // print("[ChatApiService] sessionCallbacks count: \(sessionCallbacks.count)")
print("[ChatApiService] sessionCallbacks keys: \(Array(sessionCallbacks.keys))") // print("[ChatApiService] sessionCallbacks keys: \(Array(sessionCallbacks.keys))")
print("[ChatApiService] callback exists: \(callback != nil)") // print("[ChatApiService] callback exists: \(callback != nil)")
return callback return callback
} }
//移除回调函数 //移除回调函数
@ -311,7 +311,7 @@ public func sendMessageStream(_ sessionId: String, messages: [[String: Any]]) {
tools: chatTools tools: chatTools
) )
print("[ChatApiService] 发起AI问答 问题:\(messages[0]) 工具数量: \(chatTools.count)") // print("[ChatApiService] 发起AI问答 问题:\(messages[0]) 工具数量: \(chatTools.count)")
} else { } else {
query = ChatQuery( query = ChatQuery(
messages: chatMessages, messages: chatMessages,
@ -335,14 +335,14 @@ public func sendMessageStream(_ sessionId: String, messages: [[String: Any]]) {
// 处理文本内容 // 处理文本内容
if let content = delta.content { if let content = delta.content {
replyContext += content replyContext += content
print("[ChatApiService] 收到内容: \(content)") // print("[ChatApiService] 收到内容: \(content)")
print("[ChatApiService] 当前sessionId: \(sessionId)") // print("[ChatApiService] 当前sessionId: \(sessionId)")
let callback = self.getSessionCallback(sessionId) let callback = self.getSessionCallback(sessionId)
if let callback = callback { if let callback = callback {
print("[ChatApiService] 找到回调,准备调用 onToken") // print("[ChatApiService] 找到回调,准备调用 onToken")
callback.onToken(sessionId, content) callback.onToken(sessionId, content)
print("[ChatApiService] onToken 调用完成") // print("[ChatApiService] onToken 调用完成")
} else { } else {
print("[ChatApiService] 警告:未找到 sessionId \(sessionId) 的回调") print("[ChatApiService] 警告:未找到 sessionId \(sessionId) 的回调")
} }
@ -932,7 +932,7 @@ private func convertMapToJsonObject(_ map: [String: Any]) -> [String: Any] {
// 创建工具参数 // 创建工具参数
let toolParam = ChatQuery.ChatCompletionToolParam(function: functionDefinition) let toolParam = ChatQuery.ChatCompletionToolParam(function: functionDefinition)
print("解析工具 \(name):\(schema)") // print("解析工具 \(name):\(schema)")
chatTools.append(toolParam) chatTools.append(toolParam)
} }

14
local_plugins/chat_api/ios/chat_api/Sources/chat_api/MCPClient.swift

@ -479,20 +479,20 @@ public class MCPClient {
// 添加连接成功后的工具列表汇总输出 // 添加连接成功后的工具列表汇总输出
if connectedCount > 0 { if connectedCount > 0 {
print("[MCPClient] 🎉 MCP 连接完成,共连接 \(connectedCount) 个服务器") print("[MCPClient] 🎉 MCP 连接完成,共连接 \(connectedCount) 个服务器")
print("[MCPClient] 📋 所有可用工具汇总:") // print("[MCPClient] 📋 所有可用工具汇总:")
let allTools = getToolMaps() let allTools = getToolMaps()
for (index, toolMap) in allTools.enumerated() { for (index, toolMap) in allTools.enumerated() {
if let function = toolMap["function"] as? [String: Any], if let function = toolMap["function"] as? [String: Any],
let name = function["name"] as? String, let name = function["name"] as? String,
let description = function["description"] as? String { let description = function["description"] as? String {
print("[MCPClient] \(index + 1). \(name) - \(description)") // print("[MCPClient] \(index + 1). \(name) - \(description)")
} }
} }
print("[MCPClient] 📊 工具总数: \(allTools.count)") // print("[MCPClient] 📊 工具总数: \(allTools.count)")
print("[MCPClient] 🔧 本地函数: \(localFunctionDefs.count) 个") // print("[MCPClient] 🔧 本地函数: \(localFunctionDefs.count) 个")
print("[MCPClient] 🌐 远程工具: \(allTools.count - localFunctionDefs.count) 个") // print("[MCPClient] 🌐 远程工具: \(allTools.count - localFunctionDefs.count) 个")
} }
return connectedCount > 0 return connectedCount > 0
@ -508,7 +508,7 @@ public class MCPClient {
parameters: Any, parameters: Any,
handler: FunctionHandler handler: FunctionHandler
) -> Bool { ) -> Bool {
print("[MCPClient] 注册本地函数: \(name)") // print("[MCPClient] 注册本地函数: \(name)")
localFunctions[name] = handler localFunctions[name] = handler
let functionDef: [String: Any] = [ let functionDef: [String: Any] = [
@ -544,7 +544,7 @@ public class MCPClient {
for client in subClients.values { for client in subClients.values {
let tools = client.getToolMaps() let tools = client.getToolMaps()
print("[MCPClient] 读取工具列表: \(tools)") // print("[MCPClient] 读取工具列表: \(tools)")
allToolMaps.append(contentsOf: tools) allToolMaps.append(contentsOf: tools)
} }

2
local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/MCPClient.kt

@ -768,7 +768,7 @@ class MCPClient(private val context: Context? = null) : AutoCloseable {
// 保存函数定义 // 保存函数定义
localFunctionDefs[name] = ObjectMapper().writeValueAsString(functionDef) localFunctionDefs[name] = ObjectMapper().writeValueAsString(functionDef)
Log.d(TAG, "注册本地函数成功: $name") // Log.d(TAG, "注册本地函数成功: $name")
return true return true
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "注册本地函数失败: ${e.message}", e) Log.e(TAG, "注册本地函数失败: ${e.message}", e)

2
local_plugins/open_ai_service/ios/Classes/MCPClient.swift

@ -312,7 +312,7 @@ public class MCPClient {
let jsonString = String(data: jsonData, encoding: .utf8) ?? "" let jsonString = String(data: jsonData, encoding: .utf8) ?? ""
localFunctionDefs[name] = jsonString localFunctionDefs[name] = jsonString
print("\(tag) 成功注册本地函数: \(name)") // print("\(tag) 成功注册本地函数: \(name)")
return true return true
} catch { } catch {
print("\(tag) 注册本地函数失败: \(error.localizedDescription)") print("\(tag) 注册本地函数失败: \(error.localizedDescription)")

2
local_plugins/open_ai_service/ios/open_ai_service/Sources/open_ai_service/MCPClient.swift

@ -312,7 +312,7 @@ public class MCPClient {
let jsonString = String(data: jsonData, encoding: .utf8) ?? "" let jsonString = String(data: jsonData, encoding: .utf8) ?? ""
localFunctionDefs[name] = jsonString localFunctionDefs[name] = jsonString
print("\(tag) 成功注册本地函数: \(name)") // print("\(tag) 成功注册本地函数: \(name)")
return true return true
} catch { } catch {
print("\(tag) 注册本地函数失败: \(error.localizedDescription)") print("\(tag) 注册本地函数失败: \(error.localizedDescription)")

Loading…
Cancel
Save