@langchain/anthropic 0.1.14 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -188,24 +188,28 @@ function _formatMessagesForAnthropic(messages) {
188
188
  throw new Error(`Message type "${message._getType()}" is not supported.`);
189
189
  }
190
190
  if ((0, messages_1.isAIMessage)(message) && !!message.tool_calls?.length) {
191
- if (message.content === "") {
192
- return {
193
- role,
194
- content: message.tool_calls.map(_convertLangChainToolCallToAnthropic),
195
- };
196
- }
197
- else if (typeof message.content === "string") {
198
- console.warn(`The "tool_calls" field on a message is only respected if content is an empty string.`);
199
- return {
200
- role,
201
- content: _formatContent(message.content),
202
- };
191
+ if (typeof message.content === "string") {
192
+ if (message.content === "") {
193
+ return {
194
+ role,
195
+ content: message.tool_calls.map(_convertLangChainToolCallToAnthropic),
196
+ };
197
+ }
198
+ else {
199
+ return {
200
+ role,
201
+ content: [
202
+ { type: "text", text: message.content },
203
+ ...message.tool_calls.map(_convertLangChainToolCallToAnthropic),
204
+ ],
205
+ };
206
+ }
203
207
  }
204
208
  else {
205
209
  const { content } = message;
206
210
  const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) => content.find((contentPart) => contentPart.type === "tool_use" && contentPart.id === toolCall.id));
207
211
  if (hasMismatchedToolCalls) {
208
- console.warn(`The "tool_calls" field on a message is only respected if content is an empty string.`);
212
+ console.warn(`The "tool_calls" field on a message is only respected if content is a string.`);
209
213
  }
210
214
  return {
211
215
  role,
@@ -455,7 +459,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
455
459
  const params = this.invocationParams(options);
456
460
  const formattedMessages = _formatMessagesForAnthropic(messages);
457
461
  if (options.tools !== undefined && options.tools.length > 0) {
458
- const generations = await this._generateNonStreaming(messages, params, {
462
+ const { generations } = await this._generateNonStreaming(messages, params, {
459
463
  signal: options.signal,
460
464
  });
461
465
  const result = generations[0].message;
@@ -558,7 +562,8 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
558
562
  }, options);
559
563
  const { content, ...additionalKwargs } = response;
560
564
  const generations = anthropicResponseToChatMessages(content, additionalKwargs);
561
- return generations;
565
+ const { role: _role, type: _type, ...rest } = additionalKwargs;
566
+ return { generations, llmOutput: rest };
562
567
  }
563
568
  /** @ignore */
564
569
  async _generate(messages, options, runManager) {
@@ -590,12 +595,9 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
590
595
  };
591
596
  }
592
597
  else {
593
- const generations = await this._generateNonStreaming(messages, params, {
598
+ return this._generateNonStreaming(messages, params, {
594
599
  signal: options.signal,
595
600
  });
596
- return {
597
- generations,
598
- };
599
601
  }
600
602
  }
601
603
  /**
@@ -176,7 +176,16 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
176
176
  };
177
177
  _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
178
178
  /** @ignore */
179
- _generateNonStreaming(messages: BaseMessage[], params: Omit<Anthropic.Messages.MessageCreateParamsNonStreaming | Anthropic.Messages.MessageCreateParamsStreaming, "messages"> & Kwargs, requestOptions: AnthropicRequestOptions): Promise<ChatGeneration[]>;
179
+ _generateNonStreaming(messages: BaseMessage[], params: Omit<Anthropic.Messages.MessageCreateParamsNonStreaming | Anthropic.Messages.MessageCreateParamsStreaming, "messages"> & Kwargs, requestOptions: AnthropicRequestOptions): Promise<{
180
+ generations: ChatGeneration[];
181
+ llmOutput: {
182
+ id: string;
183
+ model: string;
184
+ stop_reason: "max_tokens" | "stop_sequence" | "end_turn" | null;
185
+ stop_sequence: string | null;
186
+ usage: Anthropic.Messages.Usage;
187
+ };
188
+ }>;
180
189
  /** @ignore */
181
190
  _generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
182
191
  /**
@@ -184,24 +184,28 @@ function _formatMessagesForAnthropic(messages) {
184
184
  throw new Error(`Message type "${message._getType()}" is not supported.`);
185
185
  }
186
186
  if (isAIMessage(message) && !!message.tool_calls?.length) {
187
- if (message.content === "") {
188
- return {
189
- role,
190
- content: message.tool_calls.map(_convertLangChainToolCallToAnthropic),
191
- };
192
- }
193
- else if (typeof message.content === "string") {
194
- console.warn(`The "tool_calls" field on a message is only respected if content is an empty string.`);
195
- return {
196
- role,
197
- content: _formatContent(message.content),
198
- };
187
+ if (typeof message.content === "string") {
188
+ if (message.content === "") {
189
+ return {
190
+ role,
191
+ content: message.tool_calls.map(_convertLangChainToolCallToAnthropic),
192
+ };
193
+ }
194
+ else {
195
+ return {
196
+ role,
197
+ content: [
198
+ { type: "text", text: message.content },
199
+ ...message.tool_calls.map(_convertLangChainToolCallToAnthropic),
200
+ ],
201
+ };
202
+ }
199
203
  }
200
204
  else {
201
205
  const { content } = message;
202
206
  const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) => content.find((contentPart) => contentPart.type === "tool_use" && contentPart.id === toolCall.id));
203
207
  if (hasMismatchedToolCalls) {
204
- console.warn(`The "tool_calls" field on a message is only respected if content is an empty string.`);
208
+ console.warn(`The "tool_calls" field on a message is only respected if content is a string.`);
205
209
  }
206
210
  return {
207
211
  role,
@@ -451,7 +455,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
451
455
  const params = this.invocationParams(options);
452
456
  const formattedMessages = _formatMessagesForAnthropic(messages);
453
457
  if (options.tools !== undefined && options.tools.length > 0) {
454
- const generations = await this._generateNonStreaming(messages, params, {
458
+ const { generations } = await this._generateNonStreaming(messages, params, {
455
459
  signal: options.signal,
456
460
  });
457
461
  const result = generations[0].message;
@@ -554,7 +558,8 @@ export class ChatAnthropicMessages extends BaseChatModel {
554
558
  }, options);
555
559
  const { content, ...additionalKwargs } = response;
556
560
  const generations = anthropicResponseToChatMessages(content, additionalKwargs);
557
- return generations;
561
+ const { role: _role, type: _type, ...rest } = additionalKwargs;
562
+ return { generations, llmOutput: rest };
558
563
  }
559
564
  /** @ignore */
560
565
  async _generate(messages, options, runManager) {
@@ -586,12 +591,9 @@ export class ChatAnthropicMessages extends BaseChatModel {
586
591
  };
587
592
  }
588
593
  else {
589
- const generations = await this._generateNonStreaming(messages, params, {
594
+ return this._generateNonStreaming(messages, params, {
590
595
  signal: options.signal,
591
596
  });
592
- return {
593
- generations,
594
- };
595
597
  }
596
598
  }
597
599
  /**
@@ -62,7 +62,7 @@ test("Few shotting with tool calls", async () => {
62
62
  const res = await chat.invoke([
63
63
  new HumanMessage("What is the weather in SF?"),
64
64
  new AIMessage({
65
- content: "",
65
+ content: "Let me look up the current weather.",
66
66
  tool_calls: [
67
67
  {
68
68
  id: "toolu_feiwjf9u98r389u498",
@@ -5,7 +5,7 @@ import { ChatPromptValue } from "@langchain/core/prompt_values";
5
5
  import { PromptTemplate, ChatPromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, } from "@langchain/core/prompts";
6
6
  import { CallbackManager } from "@langchain/core/callbacks/manager";
7
7
  import { ChatAnthropic } from "../chat_models.js";
8
- test.skip("Test ChatAnthropic", async () => {
8
+ test("Test ChatAnthropic", async () => {
9
9
  const chat = new ChatAnthropic({
10
10
  modelName: "claude-3-sonnet-20240229",
11
11
  maxRetries: 0,
@@ -13,6 +13,7 @@ test.skip("Test ChatAnthropic", async () => {
13
13
  const message = new HumanMessage("Hello!");
14
14
  const res = await chat.invoke([message]);
15
15
  console.log({ res });
16
+ expect(res.response_metadata.usage).toBeDefined();
16
17
  });
17
18
  test("Test ChatAnthropic Generate", async () => {
18
19
  const chat = new ChatAnthropic({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/anthropic",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
4
4
  "description": "Anthropic integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {