From 78066befecae926ef679fb44c6c32c5c3bff32cd Mon Sep 17 00:00:00 2001 From: Jacob Lee Date: Tue, 24 Sep 2024 13:33:15 -0700 Subject: [PATCH] chore(openai,anthropic): Expose OpenAI and Anthropic payload formatters (#6872) --- libs/langchain-anthropic/src/chat_models.ts | 6 +++--- libs/langchain-anthropic/src/index.ts | 1 + libs/langchain-anthropic/src/tests/chat_models.test.ts | 4 ++-- libs/langchain-anthropic/src/utils/message_inputs.ts | 3 ++- libs/langchain-openai/src/chat_models.ts | 7 ++++--- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/libs/langchain-anthropic/src/chat_models.ts b/libs/langchain-anthropic/src/chat_models.ts index 3ae88586d079..294b84c26f88 100644 --- a/libs/langchain-anthropic/src/chat_models.ts +++ b/libs/langchain-anthropic/src/chat_models.ts @@ -33,7 +33,7 @@ import type { import { isLangChainTool } from "@langchain/core/utils/function_calling"; import { AnthropicToolsOutputParser } from "./output_parsers.js"; import { extractToolCallChunk, handleToolChoice } from "./utils/tools.js"; -import { _formatMessagesForAnthropic } from "./utils/message_inputs.js"; +import { _convertMessagesToAnthropicPayload } from "./utils/message_inputs.js"; import { _makeMessageChunkFromAnthropicEvent, anthropicResponseToChatMessages, @@ -782,7 +782,7 @@ export class ChatAnthropicMessages< runManager?: CallbackManagerForLLMRun ): AsyncGenerator { const params = this.invocationParams(options); - const formattedMessages = _formatMessagesForAnthropic(messages); + const formattedMessages = _convertMessagesToAnthropicPayload(messages); const coerceContentToString = !_toolsInParams({ ...params, ...formattedMessages, @@ -852,7 +852,7 @@ export class ChatAnthropicMessages< { ...params, stream: false, - ..._formatMessagesForAnthropic(messages), + ..._convertMessagesToAnthropicPayload(messages), }, requestOptions ); diff --git a/libs/langchain-anthropic/src/index.ts b/libs/langchain-anthropic/src/index.ts index 38c7cea7f478..b45ffe6fb26b 100644 --- a/libs/langchain-anthropic/src/index.ts +++ b/libs/langchain-anthropic/src/index.ts @@ -1 +1,2 @@ export * from "./chat_models.js"; +export { _convertMessagesToAnthropicPayload } from "./utils/message_inputs.js"; diff --git a/libs/langchain-anthropic/src/tests/chat_models.test.ts b/libs/langchain-anthropic/src/tests/chat_models.test.ts index bc6308c75770..68644d453187 100644 --- a/libs/langchain-anthropic/src/tests/chat_models.test.ts +++ b/libs/langchain-anthropic/src/tests/chat_models.test.ts @@ -3,7 +3,7 @@ import { AIMessage, HumanMessage, ToolMessage } from "@langchain/core/messages"; import { z } from "zod"; import { OutputParserException } from "@langchain/core/output_parsers"; import { ChatAnthropic } from "../chat_models.js"; -import { _formatMessagesForAnthropic } from "../utils/message_inputs.js"; +import { _convertMessagesToAnthropicPayload } from "../index.js"; test("withStructuredOutput with output validation", async () => { const model = new ChatAnthropic({ @@ -143,7 +143,7 @@ test("Can properly format anthropic messages when given two tool results", async }), ]; - const formattedMessages = _formatMessagesForAnthropic(messageHistory); + const formattedMessages = _convertMessagesToAnthropicPayload(messageHistory); expect(formattedMessages).toEqual({ messages: [ diff --git a/libs/langchain-anthropic/src/utils/message_inputs.ts b/libs/langchain-anthropic/src/utils/message_inputs.ts index b32786e43f02..8f810b231f97 100644 --- a/libs/langchain-anthropic/src/utils/message_inputs.ts +++ b/libs/langchain-anthropic/src/utils/message_inputs.ts @@ -185,10 +185,11 @@ function _formatContent(content: MessageContent) { /** * Formats messages as a prompt for the model. + * Used in LangSmith, export is important here. * @param messages The base messages to format as a prompt. * @returns The formatted prompt. */ -export function _formatMessagesForAnthropic( +export function _convertMessagesToAnthropicPayload( messages: BaseMessage[] ): AnthropicMessageCreateParams { const mergedMessages = _mergeMessages(messages); diff --git a/libs/langchain-openai/src/chat_models.ts b/libs/langchain-openai/src/chat_models.ts index 3a0cccf13d04..ac67fef13903 100644 --- a/libs/langchain-openai/src/chat_models.ts +++ b/libs/langchain-openai/src/chat_models.ts @@ -250,7 +250,8 @@ function _convertDeltaToMessageChunk( } } -function convertMessagesToOpenAIParams(messages: BaseMessage[]) { +// Used in LangSmith, export is important here +export function _convertMessagesToOpenAIParams(messages: BaseMessage[]) { // TODO: Function messages do not support array content, fix cast return messages.map((message) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -1200,7 +1201,7 @@ export class ChatOpenAI< return; } const messagesMapped: OpenAICompletionParam[] = - convertMessagesToOpenAIParams(messages); + _convertMessagesToOpenAIParams(messages); const params = { ...this.invocationParams(options, { streaming: true, @@ -1329,7 +1330,7 @@ export class ChatOpenAI< const tokenUsage: TokenUsage = {}; const params = this.invocationParams(options); const messagesMapped: OpenAICompletionParam[] = - convertMessagesToOpenAIParams(messages); + _convertMessagesToOpenAIParams(messages); if (params.stream) { const stream = this._streamResponseChunks(messages, options, runManager);