ConstThe converter performs several key transformations:
Array of LangChain BaseMessages to convert. Can include any message type: HumanMessage, AIMessage, SystemMessage, ToolMessage, FunctionMessage, etc.
Optional model name used to determine if special role mapping is needed. For reasoning models (o1, o3, etc.), "system" role is converted to "developer" role.
Array of ChatCompletionMessageParam objects formatted for OpenAI's Chat Completions API. Some messages may be split into multiple parameters (e.g., audio messages).
Basic message conversion:
const messages = [
new HumanMessage("What's the weather like?"),
new AIMessage("Let me check that for you.")
];
const params = convertMessagesToCompletionsMessageParams({
messages,
model: "gpt-4"
});
// Returns:
// [
// { role: "user", content: "What's the weather like?" },
// { role: "assistant", content: "Let me check that for you." }
// ]
Message with tool calls:
const messages = [
new AIMessage({
content: "",
tool_calls: [{
id: "call_123",
name: "get_weather",
args: { location: "San Francisco" }
}]
})
];
const params = convertMessagesToCompletionsMessageParams({ messages });
// Returns:
// [{
// role: "assistant",
// content: "",
// tool_calls: [{
// id: "call_123",
// type: "function",
// function: { name: "get_weather", arguments: '{"location":"San Francisco"}' }
// }]
// }]
Converts an array of LangChain BaseMessages to OpenAI Chat Completions API message parameters.
This converter transforms LangChain's internal message representation into the format required by OpenAI's Chat Completions API. It handles various message types, roles, content formats, tool calls, function calls, audio messages, and special model-specific requirements.