convertMessagesToCompletionsMessageParams: Converter<__type, ChatCompletionMessageParam[]>Basic message conversion:
Message with tool calls:
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.
The converter performs several key transformations:
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." }
// ]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"}' }
// }]
// }]