diff --git a/libs/langchain-anthropic/package.json b/libs/langchain-anthropic/package.json index 65f158e73501..3e61dd9b456e 100644 --- a/libs/langchain-anthropic/package.json +++ b/libs/langchain-anthropic/package.json @@ -16,26 +16,21 @@ "scripts": { "build": "yarn turbo:command build:internal --filter=@langchain/anthropic", "build:internal": "yarn lc-build:v2 --create-entrypoints --pre --tree-shaking --gen-maps", - "build:deps": "yarn run turbo:command build --filter=@langchain/core", - "build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/", - "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && yarn move-cjs-to-dist && rimraf dist-cjs", - "build:watch": "yarn create-entrypoints && tsc --outDir dist/ --watch", - "build:scripts": "yarn create-entrypoints && yarn check-tree-shaking", "lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/", "lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts", "lint": "yarn lint:eslint && yarn lint:dpdm", "lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm", "clean": "rm -rf .turbo dist/", "prepack": "yarn build", - "test": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%", - "test:watch": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts", - "test:single": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000", - "test:int": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%", + "test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%", + "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts", + "test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000", + "test:int": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%", + "test:standard:unit": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.standard\\.test.ts --testTimeout 100000 --maxWorkers=50%", + "test:standard:int": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.standard\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%", + "test:standard": "yarn test:standard:unit && yarn test:standard:int", "format": "prettier --config .prettierrc --write \"src\"", - "format:check": "prettier --config .prettierrc --check \"src\"", - "move-cjs-to-dist": "yarn lc-build --config ./langchain.config.js --move-cjs-dist", - "create-entrypoints": "yarn lc-build --config ./langchain.config.js --create-entrypoints", - "check-tree-shaking": "yarn lc-build --config ./langchain.config.js --tree-shaking" + "format:check": "prettier --config .prettierrc --check \"src\"" }, "author": "LangChain", "license": "MIT", @@ -50,6 +45,7 @@ "@jest/globals": "^29.5.0", "@langchain/community": "workspace:*", "@langchain/scripts": "~0.0.14", + "@langchain/standard-tests": "workspace:*", "@swc/core": "^1.3.90", "@swc/jest": "^0.2.29", "dpdm": "^3.12.0", diff --git a/libs/langchain-anthropic/src/chat_models.ts b/libs/langchain-anthropic/src/chat_models.ts index ab1acebd0ca7..aba95cee3a00 100644 --- a/libs/langchain-anthropic/src/chat_models.ts +++ b/libs/langchain-anthropic/src/chat_models.ts @@ -67,7 +67,7 @@ type AnthropicToolChoice = } | "any" | "auto"; -interface ChatAnthropicCallOptions extends BaseLanguageModelCallOptions { +export interface ChatAnthropicCallOptions extends BaseLanguageModelCallOptions { tools?: (StructuredToolInterface | AnthropicTool)[]; /** * Whether or not to specify what tool the model should use diff --git a/libs/langchain-anthropic/src/tests/chat_models.standard.int.test.ts b/libs/langchain-anthropic/src/tests/chat_models.standard.int.test.ts new file mode 100644 index 000000000000..45eff821ce9c --- /dev/null +++ b/libs/langchain-anthropic/src/tests/chat_models.standard.int.test.ts @@ -0,0 +1,39 @@ +/* eslint-disable no-process-env */ +import { test, expect } from "@jest/globals"; +import { ChatModelIntegrationTests } from "@langchain/standard-tests"; +import { AIMessageChunk } from "@langchain/core/messages"; +import { ChatAnthropic, ChatAnthropicCallOptions } from "../chat_models.js"; + +class ChatAnthropicStandardIntegrationTests extends ChatModelIntegrationTests< + ChatAnthropicCallOptions, + AIMessageChunk +> { + constructor() { + if (!process.env.ANTHROPIC_API_KEY) { + throw new Error( + "ANTHROPIC_API_KEY must be set to run standard integration tests." + ); + } + super({ + Cls: ChatAnthropic, + chatModelHasToolCalling: true, + chatModelHasStructuredOutput: true, + constructorArgs: { + model: "claude-3-haiku-20240307", + }, + }); + } + + async testUsageMetadataStreaming() { + console.warn( + "Skipping testUsageMetadataStreaming, not implemented in ChatAnthropic." + ); + } +} + +const testClass = new ChatAnthropicStandardIntegrationTests(); + +test("ChatAnthropicStandardIntegrationTests", async () => { + const testResults = await testClass.runTests(); + expect(testResults).toBe(true); +}); diff --git a/libs/langchain-anthropic/src/tests/chat_models.standard.test.ts b/libs/langchain-anthropic/src/tests/chat_models.standard.test.ts new file mode 100644 index 000000000000..7ddefe42b8f4 --- /dev/null +++ b/libs/langchain-anthropic/src/tests/chat_models.standard.test.ts @@ -0,0 +1,39 @@ +/* eslint-disable no-process-env */ +import { test, expect } from "@jest/globals"; +import { ChatModelUnitTests } from "@langchain/standard-tests"; +import { AIMessageChunk } from "@langchain/core/messages"; +import { ChatAnthropic, ChatAnthropicCallOptions } from "../chat_models.js"; + +class ChatAnthropicStandardUnitTests extends ChatModelUnitTests< + ChatAnthropicCallOptions, + AIMessageChunk +> { + constructor() { + super({ + Cls: ChatAnthropic, + chatModelHasToolCalling: true, + chatModelHasStructuredOutput: true, + constructorArgs: {}, + }); + // This must be set so method like `.bindTools` or `.withStructuredOutput` + // which we call after instantiating the model will work. + // (constructor will throw if API key is not set) + process.env.ANTHROPIC_API_KEY = "test"; + } + + testChatModelInitApiKey() { + // Unset the API key env var here so this test can properly check + // the API key class arg. + process.env.ANTHROPIC_API_KEY = ""; + super.testChatModelInitApiKey(); + // Re-set the API key env var here so other tests can run properly. + process.env.ANTHROPIC_API_KEY = "test"; + } +} + +const testClass = new ChatAnthropicStandardUnitTests(); + +test("ChatAnthropicStandardUnitTests", () => { + const testResults = testClass.runTests(); + expect(testResults).toBe(true); +}); diff --git a/yarn.lock b/yarn.lock index b1e6fb935076..fd993483d5ca 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8931,6 +8931,7 @@ __metadata: "@langchain/community": "workspace:*" "@langchain/core": ">=0.2.5 <0.3.0" "@langchain/scripts": ~0.0.14 + "@langchain/standard-tests": "workspace:*" "@swc/core": ^1.3.90 "@swc/jest": ^0.2.29 dpdm: ^3.12.0