Browse Source

Merge commit 'ef4a26e95a22e70a475fe7a1b85ea8158eaf000e' into new_dev

weicu
lxm 1 year ago
parent
commit
5df2367ded
  1. 50
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/ChatApiService.kt
  2. 10
      local_plugins/chat_api/ios/chat_api/Sources/chat_api/ChatApiService.swift

50
local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/ChatApiService.kt

@ -364,7 +364,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
toolCalls.clear()
// 检查是否包含图片,决定使用哪个模型
val currentModel = if (containsImage(messages)) visionModel else model
Log.d(TAG, "liwei-------------------------开始AI 对话 0")
// Log.d(TAG, "liwei-------------------------开始AI 对话 0")
currentStreamJob = launch {
try {
// 转换消息格式
@ -374,22 +374,22 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
Log.e("ChatApiService", "转换消息格式失败: ${e.message}", e)
throw e
}
Log.d(TAG, "liwei-------------------------开始AI 对话 1")
// Log.d(TAG, "liwei-------------------------开始AI 对话 1")
// 直接获取工具列表
val tools = getOpenAiTools()
Log.d(TAG, "liwei-------------------------开始AI 对话 2")
// Log.d(TAG, "liwei-------------------------开始AI 对话 2")
// 构建请求
if (currentModel.isEmpty()) {
Log.e("ChatApiService", "模型名称为空")
throw IllegalArgumentException("模型名称不能为空")
}
Log.d(TAG, "liwei-------------------------开始AI 对话 3")
// Log.d(TAG, "liwei-------------------------开始AI 对话 3")
if (chatMessages.isEmpty()) {
Log.e("ChatApiService", "消息列表为空")
throw IllegalArgumentException("消息列表不能为空")
}
// Log.e("ChatApiService", "AI解析工具列表: ${tools}")
Log.d(TAG, "liwei-------------------------开始AI 对话 4")
// Log.d(TAG, "liwei-------------------------开始AI 对话 4")
val chatsFlow = try {
val chatCompletionRequest = ChatCompletionRequest(
model = ModelId(currentModel),
@ -398,17 +398,17 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
temperature = 0.7,
tools = if (tools.isNotEmpty()) tools else null
)
Log.d(TAG, "liwei-------------------------开始AI 对话 5")
// Log.d(TAG, "liwei-------------------------开始AI 对话 5")
if (openAI == null) {
Log.e("ChatApiService", "openAI对象为null")
throw IllegalStateException("OpenAI客户端未初始化")
}
Log.e("ChatApiService", "liwei---------,发送AI请求 model:${currentModel} messages:${chatMessages}")
// Log.e("ChatApiService", "liwei---------,发送AI请求 model:${currentModel} messages:${chatMessages}")
// 添加超时控制,10秒超时
val flow = withTimeout(10.seconds) {
openAI!!.chatCompletions(chatCompletionRequest)
}
Log.d(TAG, "liwei-------------------------开始AI 对话 6")
// Log.d(TAG, "liwei-------------------------开始AI 对话 6")
flow
} catch (e: Exception) {
Log.e("ChatApiService", "创建ChatCompletionRequest或调用chatCompletions失败: ${e.message}", e)
@ -425,7 +425,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
}
var replyContext = "";
Log.d(TAG, "liwei-------------------------开始AI 对话 7")
// Log.d(TAG, "liwei-------------------------开始AI 对话 7")
chatsFlow.collect { result ->
// val usage = result.usage
// if (usage != null){ //发现流式不返回这歌数据
@ -436,19 +436,19 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
// throw CancellationException("Session cancelled")
return@collect
}
Log.d(TAG, "liwei-------------------------开始AI 对话 7-1")
// Log.d(TAG, "liwei-------------------------开始AI 对话 7-1")
val choice = result.choices.firstOrNull()
if (choice == null) {
Log.w("ChatApiService", "[Session: $sessionid] choice为null")
return@collect
}
Log.d(TAG, "liwei-------------------------开始AI 对话 7-2")
// Log.d(TAG, "liwei-------------------------开始AI 对话 7-2")
val delta = choice.delta
if (delta == null) {
Log.w("ChatApiService", "[Session: $sessionid] delta为null")
return@collect
}
Log.d(TAG, "liwei-------------------------开始AI 对话 7-3")
// Log.d(TAG, "liwei-------------------------开始AI 对话 7-3")
if (delta.toolCalls == null){
// 处理普通文本内容
delta.content?.let { content ->
@ -461,7 +461,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
}
}
}
Log.d(TAG, "liwei-------------------------开始AI 对话 7-4")
// Log.d(TAG, "liwei-------------------------开始AI 对话 7-4")
// 收集工具调用信息
delta.toolCalls?.forEach { toolCall ->
try {
@ -513,10 +513,10 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
val promptTokens = estimatePromptTokens(chatMessages); //计算上传token
val completionTokens = roughTokenCount(replyContext); //计算上传token
getSessionCallback(sessionid)?.onUsage(sessionid,promptTokens,completionTokens,promptTokens+completionTokens)
Log.d(TAG, "liwei-------------------------开始AI 对话 8")
// Log.d(TAG, "liwei-------------------------开始AI 对话 8")
// 完成处理
if (sessionid == currSessionId) {
Log.d(TAG, "liwei-------------------------开始AI 对话 9")
// Log.d(TAG, "liwei-------------------------开始AI 对话 9")
// 检查是否有工具调用需要处理
val hasToolCalls = processToolCalls(sessionid)
if (!hasToolCalls) {
@ -528,7 +528,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
clearCurrentSession()
}
}
Log.d(TAG, "liwei-------------------------开始AI 对话 10")
// Log.d(TAG, "liwei-------------------------开始AI 对话 10")
} catch (e: CancellationException) {
// 会话被取消,不需要处理
Log.d(TAG, "Session $sessionid cancelled")
@ -652,7 +652,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
mapOf("context" to "Tool not found: $functionName")
}
if (sessionid == currSessionId) {
if (functionName != "set_user_profile_field"){
// if (functionName != "set_user_profile_field"){
// 处理结果
try {
if(!isError){
@ -674,14 +674,14 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
functionCall = functionCall,
functionResult = gson.toJson(result)
)
}else{
try {
getSessionCallback(sessionid)?.onComplete(sessionid)
} catch (ex: Exception) {
Log.e(TAG, "onComplete回调异常: ${ex.message}", ex)
}
clearCurrentSession()
}
// }else{
// try {
// getSessionCallback(sessionid)?.onComplete(sessionid)
// } catch (ex: Exception) {
// Log.e(TAG, "onComplete回调异常: ${ex.message}", ex)
// }
// clearCurrentSession()
// }
}
}
} catch (e: Exception) {

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

@ -520,7 +520,7 @@ private func processToolCalls(_ sessionId:String) async -> Bool {
}
if sessionId == self.currSessionId {
if (firstToolCall.name != "set_user_profile_field"){
// if (firstToolCall.name != "set_user_profile_field"){
// 处理结果
if (!isError){
self.getSessionCallback(sessionId)?.onFunctionCallResult(
@ -541,10 +541,10 @@ private func processToolCalls(_ sessionId:String) async -> Bool {
functionCall: functionCall,
functionResult: self.jsonToString(result) ?? "{}"
)
}else{
self.getSessionCallback(sessionId)?.onComplete(sessionId)
self.clearCurrentSession()
}
// }else{
// self.getSessionCallback(sessionId)?.onComplete(sessionId)
// self.clearCurrentSession()
// }
}
}
} catch {

Loading…
Cancel
Save