nic-6443 commented on code in PR #13181: URL: https://github.com/apache/apisix/pull/13181#discussion_r3049615871
########## apisix/plugins/ai-protocols/converters/anthropic-messages-to-openai-chat.lua: ########## @@ -0,0 +1,447 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +--- Converter: Anthropic Messages → OpenAI Chat Completions. +-- Converts client requests from Anthropic Messages API format to +-- OpenAI Chat Completions format, and converts provider responses +-- back from OpenAI to Anthropic format. +-- +-- Converters work DOWNSTREAM of adapters: the target adapter (openai-chat) +-- parses the provider's response, and this converter transforms the parsed +-- result into the client's format (Anthropic Messages). + +local core = require("apisix.core") +local table = table +local type = type +local ipairs = ipairs +local tostring = tostring +local setmetatable = setmetatable + +local _M = { + from = "anthropic-messages", + to = "openai-chat", +} + + +-- SSE event helpers +local function make_sse_event(event_type, data) + return { type = event_type, data = core.json.encode(data) } +end + +local function push_content_block_stop(events, idx) + table.insert(events, make_sse_event("content_block_stop", { + type = "content_block_stop", + index = idx, + })) +end + +local function push_content_block_start(events, idx, block) + table.insert(events, make_sse_event("content_block_start", { + type = "content_block_start", + index = idx, + content_block = block, + })) +end + +local function push_content_block_delta(events, idx, delta) + table.insert(events, make_sse_event("content_block_delta", { + type = "content_block_delta", + index = idx, + delta = delta, + })) +end + +local openai_stop_reason_map = { + stop = "end_turn", + length = "max_tokens", + content_filter = "end_turn", + tool_calls = "tool_use", +} + + +--- Convert an incoming Anthropic request to OpenAI Chat format. +function _M.convert_request(request_table, ctx) + if type(request_table) ~= "table" then + return nil, "request body must be a table" + end + + if type(request_table.messages) ~= "table" or + #request_table.messages == 0 then + return nil, "missing messages" + end + + local openai_body = core.table.clone(request_table) + + -- 1. Handle System Prompt + local messages = {} + if request_table.system then + local system_content = "" + if type(request_table.system) == "string" then + system_content = request_table.system + elseif type(request_table.system) == "table" then + for _, block in ipairs(request_table.system) do + if type(block) == "table" and block.type == "text" + and type(block.text) == "string" then + system_content = system_content .. block.text + end + end + end + + if system_content ~= "" then + table.insert(messages, { + role = "system", + content = system_content + }) + end + openai_body.system = nil + end + + -- 2. Convert Messages (including tool calls and results) + for i, msg in ipairs(request_table.messages) do + if type(msg) ~= "table" or type(msg.role) ~= "string" then + return nil, "invalid message at index " .. i + end + if type(msg.content) ~= "string" and type(msg.content) ~= "table" then + return nil, "invalid message content at index " .. i + end + + local new_msg = { + role = msg.role, + content = "" + } + if type(msg.content) == "string" then + new_msg.content = msg.content + elseif type(msg.content) == "table" then + local tool_calls = {} + local tool_results = {} + + for _, block in ipairs(msg.content) do + if type(block) ~= "table" then + core.log.warn("unexpected non-table content block in Anthropic ", + "response, skipping: ", tostring(block)) + goto CONTINUE_BLOCK + end + + if block.type == "text" and type(block.text) == "string" then + new_msg.content = (new_msg.content or "") .. block.text + elseif block.type == "tool_use" then + if type(block.id) == "string" and type(block.name) == "string" then + table.insert(tool_calls, { + id = block.id, + type = "function", + ["function"] = { + name = block.name, + arguments = core.json.encode(block.input or {}) + } + }) + end + elseif block.type == "tool_result" then + if type(block.tool_use_id) == "string" then + table.insert(tool_results, { + role = "tool", + tool_call_id = block.tool_use_id, + content = type(block.content) == "table" + and core.json.encode(block.content) + or tostring(block.content or "") + }) + end + end + + ::CONTINUE_BLOCK:: + end + + if #tool_calls > 0 then + new_msg.tool_calls = tool_calls + new_msg.content = new_msg.content ~= "" and new_msg.content or nil + end + + if #tool_results > 0 then + if new_msg.content and new_msg.content ~= "" then + table.insert(messages, { role = msg.role, content = new_msg.content }) + end + for _, tr in ipairs(tool_results) do + table.insert(messages, tr) + end + goto CONTINUE + end + end + + table.insert(messages, new_msg) + ::CONTINUE:: + end + openai_body.messages = messages + + -- 3. Convert Tools Definition + if type(request_table.tools) == "table" then + local openai_tools = {} + for i, tool in ipairs(request_table.tools) do + if type(tool) ~= "table" or type(tool.name) ~= "string" or tool.name == "" then + return nil, "invalid tool definition at index " .. i + end + table.insert(openai_tools, { + type = "function", + ["function"] = { + name = tool.name, + description = tool.description, + parameters = tool.input_schema + } + }) + end + openai_body.tools = openai_tools + end + + -- 4. Map Parameters + if openai_body.max_tokens then + openai_body.max_completion_tokens = openai_body.max_tokens + end + + if openai_body.stop_sequences then + openai_body.stop = openai_body.stop_sequences + openai_body.stop_sequences = nil + end + Review Comment: Good catch! Fixed in 09ad2ff — now injecting `stream_options = { include_usage = true }` in the converter when `openai_body.stream` is true. This ensures OpenAI-compatible providers include usage data in streaming responses. ########## apisix/plugins/ai-protocols/converters/anthropic-messages-to-openai-chat.lua: ########## @@ -0,0 +1,447 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +--- Converter: Anthropic Messages → OpenAI Chat Completions. +-- Converts client requests from Anthropic Messages API format to +-- OpenAI Chat Completions format, and converts provider responses +-- back from OpenAI to Anthropic format. +-- +-- Converters work DOWNSTREAM of adapters: the target adapter (openai-chat) +-- parses the provider's response, and this converter transforms the parsed +-- result into the client's format (Anthropic Messages). + +local core = require("apisix.core") +local table = table +local type = type +local ipairs = ipairs +local tostring = tostring +local setmetatable = setmetatable + +local _M = { + from = "anthropic-messages", + to = "openai-chat", +} + + +-- SSE event helpers +local function make_sse_event(event_type, data) + return { type = event_type, data = core.json.encode(data) } +end + +local function push_content_block_stop(events, idx) + table.insert(events, make_sse_event("content_block_stop", { + type = "content_block_stop", + index = idx, + })) +end + +local function push_content_block_start(events, idx, block) + table.insert(events, make_sse_event("content_block_start", { + type = "content_block_start", + index = idx, + content_block = block, + })) +end + +local function push_content_block_delta(events, idx, delta) + table.insert(events, make_sse_event("content_block_delta", { + type = "content_block_delta", + index = idx, + delta = delta, + })) +end + +local openai_stop_reason_map = { + stop = "end_turn", + length = "max_tokens", + content_filter = "end_turn", + tool_calls = "tool_use", +} + + +--- Convert an incoming Anthropic request to OpenAI Chat format. +function _M.convert_request(request_table, ctx) + if type(request_table) ~= "table" then + return nil, "request body must be a table" + end + + if type(request_table.messages) ~= "table" or + #request_table.messages == 0 then + return nil, "missing messages" + end + + local openai_body = core.table.clone(request_table) + + -- 1. Handle System Prompt + local messages = {} + if request_table.system then + local system_content = "" + if type(request_table.system) == "string" then + system_content = request_table.system + elseif type(request_table.system) == "table" then + for _, block in ipairs(request_table.system) do + if type(block) == "table" and block.type == "text" + and type(block.text) == "string" then + system_content = system_content .. block.text + end + end + end + + if system_content ~= "" then + table.insert(messages, { + role = "system", + content = system_content + }) + end + openai_body.system = nil + end + + -- 2. Convert Messages (including tool calls and results) + for i, msg in ipairs(request_table.messages) do + if type(msg) ~= "table" or type(msg.role) ~= "string" then + return nil, "invalid message at index " .. i + end + if type(msg.content) ~= "string" and type(msg.content) ~= "table" then + return nil, "invalid message content at index " .. i + end + + local new_msg = { + role = msg.role, + content = "" + } + if type(msg.content) == "string" then + new_msg.content = msg.content + elseif type(msg.content) == "table" then + local tool_calls = {} + local tool_results = {} + + for _, block in ipairs(msg.content) do + if type(block) ~= "table" then + core.log.warn("unexpected non-table content block in Anthropic ", + "response, skipping: ", tostring(block)) Review Comment: Good catch. Fixed in 09ad2ff — updated the log message from "response" to "request" since this is in `convert_request()`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
