Browse Source

上传用户画像逻辑代码

newdev_shunjiawei
liwei1dao 1 year ago
parent
commit
1726148a0d
  1. 50
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/ChatApiService.kt

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() 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,17 +398,17 @@ 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}")
// 添加超时控制,10秒超时 // 添加超时控制,10秒超时
val flow = withTimeout(10.seconds) { val flow = withTimeout(10.seconds) {
openAI!!.chatCompletions(chatCompletionRequest) 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)
@ -425,7 +425,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){ //发现流式不返回这歌数据
@ -436,19 +436,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")
if (delta.toolCalls == null){ if (delta.toolCalls == null){
// 处理普通文本内容 // 处理普通文本内容
delta.content?.let { content -> 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 -> delta.toolCalls?.forEach { toolCall ->
try { try {
@ -513,10 +513,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) {
@ -528,7 +528,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")
@ -652,7 +652,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
mapOf("context" to "Tool not found: $functionName") mapOf("context" to "Tool not found: $functionName")
} }
if (sessionid == currSessionId) { if (sessionid == currSessionId) {
if (functionName != "set_user_profile_field"){ // if (functionName != "set_user_profile_field"){
// 处理结果 // 处理结果
try { try {
if(!isError){ if(!isError){
@ -674,14 +674,14 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
functionCall = functionCall, functionCall = functionCall,
functionResult = gson.toJson(result) functionResult = gson.toJson(result)
) )
}else{ // }else{
try { // try {
getSessionCallback(sessionid)?.onComplete(sessionid) // getSessionCallback(sessionid)?.onComplete(sessionid)
} catch (ex: Exception) { // } catch (ex: Exception) {
Log.e(TAG, "onComplete回调异常: ${ex.message}", ex) // Log.e(TAG, "onComplete回调异常: ${ex.message}", ex)
} // }
clearCurrentSession() // clearCurrentSession()
} // }
} }
} }
} catch (e: Exception) { } catch (e: Exception) {

Loading…
Cancel
Save