diff --git a/src/agentCore.jl b/src/agentCore.jl index dd27893..18defc9 100644 --- a/src/agentCore.jl +++ b/src/agentCore.jl @@ -179,7 +179,7 @@ function _process_message(agent::yiemAgent)::assistantMessage # call agent.prepareContext() preparedContext = agent.prepareContext(agent._state) - #WORKING Call agent.formatMsgForLLM(agent._state) to format for LLM + # Call agent.formatMsgForLLM(agent._state) to format for LLM formatted_messages = agent.formatMsgForLLM(preparedContext) # Call llmCall() (blocking — the task waits here) diff --git a/src/utils.jl b/src/utils.jl index 911eee2..38729f4 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -115,60 +115,47 @@ end """ convert preparedcontext into openai message format ready to be used by LLM """ -function formatMsgForLLM(messages::Vector{agentMessage})::Vector{Dict{String, Any}} - - 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), +function formatMsgForLLM(preparedContext::Vector{agentMessage})::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 ) -end + """ -function _toOpenAIAssistantMsg(msg::assistantMessage)::Dict{String, Any} - return Dict{String, Any}( - "role" => "assistant", - "content" => _toOpenAIBlocks(msg.content), - ) -end + # convert various messages to openai message format -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 + return openai_message end