Browse Source

补充调试日志

newdev_shunjiawei
liwei1dao 1 year ago
parent
commit
fd1868be74
  1. 15
      local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/OpenAIService.kt

15
local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/OpenAIService.kt

@ -514,10 +514,9 @@ class OpenAIService(private val context: Context? = null) : CoroutineScope {
if (tools.length() > 0) { if (tools.length() > 0) {
put("tools", tools) put("tools", tools)
} }
Log.i(TAG, "请求Ai对话: 携带工具数据 ${tools.length()}") Log.i(TAG, "liwei------------------- 请求Ai对话: context:${messages.getJSONObject(messages.length() - 1).get("content").toString()} 携带工具数据 ${tools.length()}")
} }
val mediaType = "application/json".toMediaTypeOrNull() val mediaType = "application/json".toMediaTypeOrNull()
val request = Request.Builder() val request = Request.Builder()
.url(baseUrl) .url(baseUrl)
@ -678,6 +677,8 @@ class OpenAIService(private val context: Context? = null) : CoroutineScope {
if (function.has("arguments")) { if (function.has("arguments")) {
toolCallInfo.arguments += function.getString("arguments") toolCallInfo.arguments += function.getString("arguments")
} }
Log.d(TAG, "liwei---------------------- AI发起工具调用: tool=$function")
} }
} }
} }
@ -706,7 +707,7 @@ class OpenAIService(private val context: Context? = null) : CoroutineScope {
put("arguments", firstToolCall.arguments) put("arguments", firstToolCall.arguments)
put("id", firstToolCall.id) put("id", firstToolCall.id)
} }
Log.d(TAG, "工具调用: id=${firstToolCall.id}, name=${firstToolCall.name} arguments=${firstToolCall.arguments}") Log.d(TAG, "liwei---------------------- 工具调用: id=${firstToolCall.id}, name=${firstToolCall.name} arguments=${firstToolCall.arguments}")
// 通知上层工具调用事件 // 通知上层工具调用事件
callback.onFunctionCall(functionCall) callback.onFunctionCall(functionCall)
@ -721,7 +722,7 @@ class OpenAIService(private val context: Context? = null) : CoroutineScope {
val args = mcpClient?.parseJsonArguments(firstToolCall.arguments) ?: mapOf() val args = mcpClient?.parseJsonArguments(firstToolCall.arguments) ?: mapOf()
mcpClient?.callTool(firstToolCall.name, args) mcpClient?.callTool(firstToolCall.name, args)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "工具调用错误: ${e.message}") Log.e(TAG, "liwei----------------------- 工具调用错误: ${e.message}")
JSONObject().put("context", "工具调用失败: ${e.message}") JSONObject().put("context", "工具调用失败: ${e.message}")
} }
@ -733,7 +734,7 @@ class OpenAIService(private val context: Context? = null) : CoroutineScope {
val context = resultObj.optString("context", "工具调用失败").let { val context = resultObj.optString("context", "工具调用失败").let {
if (it.isNullOrEmpty()) "工具调用失败" else it if (it.isNullOrEmpty()) "工具调用失败" else it
} }
Log.d(TAG, "mcp工具调用完成: name=${firstToolCall.name}, context=$context") Log.d(TAG, "liwei----------------------- mcp工具调用完成: name=${firstToolCall.name}, context=$context")
// 将结果发送回OpenAI继续对话 // 将结果发送回OpenAI继续对话
sendFunctionCallResult( sendFunctionCallResult(
@ -746,7 +747,7 @@ class OpenAIService(private val context: Context? = null) : CoroutineScope {
} }
} catch (e: Exception) { } catch (e: Exception) {
if (!isCanceled) { if (!isCanceled) {
Log.e(TAG, "处理工具调用异常: ${e.message}") Log.e(TAG, "liwei----------------------- 处理工具调用异常: ${e.message}")
try { try {
val errorMessage = "工具调用处理失败: ${e.message}" val errorMessage = "工具调用处理失败: ${e.message}"
sendFunctionCallResult( sendFunctionCallResult(

Loading…
Cancel
Save