|
|
@ -36,6 +36,10 @@ class ChatApiException(message: String) : Exception("ChatApiException: $message" |
|
|
* 与 iOS 版本 StreamCallback 协议保持完全一致 |
|
|
* 与 iOS 版本 StreamCallback 协议保持完全一致 |
|
|
*/ |
|
|
*/ |
|
|
interface StreamCallback { |
|
|
interface StreamCallback { |
|
|
|
|
|
/** |
|
|
|
|
|
* AI使用量 |
|
|
|
|
|
* */ |
|
|
|
|
|
fun onUsage(sessionid: String,prompt_tokens: Int?,completion_tokens: Int?,total_tokens: Int?) |
|
|
/** |
|
|
/** |
|
|
* 接收到新的token |
|
|
* 接收到新的token |
|
|
*/ |
|
|
*/ |
|
|
@ -407,8 +411,14 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor |
|
|
Log.e("ChatApiService", "创建ChatCompletionRequest或调用chatCompletions失败: ${e.message}", e) |
|
|
Log.e("ChatApiService", "创建ChatCompletionRequest或调用chatCompletions失败: ${e.message}", e) |
|
|
throw e |
|
|
throw e |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var replyContext = ""; |
|
|
// Log.d(TAG, "liwei-------------------------开始AI 对话 7") |
|
|
// Log.d(TAG, "liwei-------------------------开始AI 对话 7") |
|
|
chatsFlow.collect { result -> |
|
|
chatsFlow.collect { result -> |
|
|
|
|
|
// val usage = result.usage |
|
|
|
|
|
// if (usage != null){ //发现流式不返回这歌数据 |
|
|
|
|
|
// getSessionCallback(sessionid)?.onUsage(sessionid,usage.promptTokens,usage.completionTokens,usage.totalTokens) |
|
|
|
|
|
// } |
|
|
// 每次处理前检查是否仍是当前会话 |
|
|
// 每次处理前检查是否仍是当前会话 |
|
|
if (sessionid != currSessionId) { |
|
|
if (sessionid != currSessionId) { |
|
|
// throw CancellationException("Session cancelled") |
|
|
// throw CancellationException("Session cancelled") |
|
|
@ -430,6 +440,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor |
|
|
// 处理普通文本内容 |
|
|
// 处理普通文本内容 |
|
|
delta.content?.let { content -> |
|
|
delta.content?.let { content -> |
|
|
try { |
|
|
try { |
|
|
|
|
|
replyContext += content |
|
|
getSessionCallback(sessionid)?.onToken(sessionid,content) |
|
|
getSessionCallback(sessionid)?.onToken(sessionid,content) |
|
|
} catch (e: Exception) { |
|
|
} catch (e: Exception) { |
|
|
Log.e(TAG, "onToken回调异常: ${e.message}", e) |
|
|
Log.e(TAG, "onToken回调异常: ${e.message}", e) |
|
|
@ -454,6 +465,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor |
|
|
val functionName = function.name |
|
|
val functionName = function.name |
|
|
if (!functionName.isNullOrEmpty()) { |
|
|
if (!functionName.isNullOrEmpty()) { |
|
|
toolCallInfo.name = functionName |
|
|
toolCallInfo.name = functionName |
|
|
|
|
|
replyContext += functionName |
|
|
} |
|
|
} |
|
|
} catch (e: Exception) { |
|
|
} catch (e: Exception) { |
|
|
// 在流式响应中,name 可能在后续的 delta 中为 null,这是正常的 |
|
|
// 在流式响应中,name 可能在后续的 delta 中为 null,这是正常的 |
|
|
@ -465,6 +477,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor |
|
|
val functionArgs = function.arguments |
|
|
val functionArgs = function.arguments |
|
|
if (!functionArgs.isNullOrEmpty()) { |
|
|
if (!functionArgs.isNullOrEmpty()) { |
|
|
toolCallInfo.arguments += functionArgs |
|
|
toolCallInfo.arguments += functionArgs |
|
|
|
|
|
replyContext += functionArgs |
|
|
} |
|
|
} |
|
|
} catch (e: Exception) { |
|
|
} catch (e: Exception) { |
|
|
// 在流式响应中,arguments 可能在后续的 delta 中为 null,这是正常的 |
|
|
// 在流式响应中,arguments 可能在后续的 delta 中为 null,这是正常的 |
|
|
@ -483,6 +496,9 @@ 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) { |
|
|
if (sessionid == currSessionId) { |
|
|
|