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