From ebd79aa6866ad06d47f0748c838da38b8fbe307e Mon Sep 17 00:00:00 2001 From: narawat Date: Thu, 6 Aug 2026 18:59:39 +0700 Subject: [PATCH] update --- src/utils.jl | 87 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index de364ed..911eee2 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -116,46 +116,59 @@ end """ convert preparedcontext into openai message format ready to be used by LLM """ function formatMsgForLLM(messages::Vector{agentMessage})::Vector{Dict{String, Any}} - """ openai message format example - msg = Dict( - "model" => "gemma-4-E4B-it-UD-Q4_K_XL", - "messages" => [ - Dict( - "role" => "system", - "content" => [ - Dict("type" => "text", "text" => systemmsg), - ] - ), - Dict( - "role" => "user", - "content" => [ - Dict("type" => "text", "text" => "Do you have something similar to the one in the image?"), - Dict( - "type" => "image_url", - "image_url" => Dict("url" => data1_uri) - ) - ] - ), - Dict( - "role" => "assistant", - "content" => [ - Dict("type" => "text", "text" => "let me check."), - ] - ), - Dict( - "role" => "toolResult", - "content" => [ - Dict("type" => "text", "text" => "name: Chateau Montelena ..."), - ] - ), - ], - "temperature" => 0.7 + + openai_messages = Vector{Dict{String, Any}}() + + for msg in messages + if msg isa userMessage + push!(openai_messages, _toOpenAIUserMsg(msg)) + elseif msg isa assistantMessage + push!(openai_messages, _toOpenAIAssistantMsg(msg)) + elseif msg isa toolResultMessage + push!(openai_messages, _toOpenAIToolResultMsg(msg)) + end + end + + return openai_messages +end + +function _toOpenAIUserMsg(msg::userMessage)::Dict{String, Any} + return Dict{String, Any}( + "role" => "user", + "content" => _toOpenAIBlocks(msg.content), ) - """ +end - # convert various messages to openai message format +function _toOpenAIAssistantMsg(msg::assistantMessage)::Dict{String, Any} + return Dict{String, Any}( + "role" => "assistant", + "content" => _toOpenAIBlocks(msg.content), + ) +end - return openai_message +function _toOpenAIToolResultMsg(msg::toolResultMessage)::Dict{String, Any} + return Dict{String, Any}( + "role" => "tool", + "tool_call_id" => msg.toolCallId, + "name" => msg.toolName, + "content" => _toOpenAIBlocks(msg.content), + ) +end + +function _toOpenAIBlocks(contents::Vector{messageContent})::Vector{Dict{String, Any}} + blocks = Vector{Dict{String, Any}}() + for c in contents + if c isa textContent + push!(blocks, Dict("type" => "text", "text" => c.text)) + elseif c isa imageContent + data_uri = "data:$(c.mimeType);base64,$(c.data)" + push!(blocks, Dict( + "type" => "image_url", + "image_url" => Dict("url" => data_uri), + )) + end + end + return blocks end