Skip to content

Commit

Permalink
anthropic[minor]: Add standard chat model tests to anthropic (#5659)
Browse files Browse the repository at this point in the history
* anthropic[minor]: Add standard chat model tests to anthropic

* format
  • Loading branch information
bracesproul authored Jun 4, 2024
1 parent 840e787 commit 7d7b5ad
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 14 deletions.
22 changes: 9 additions & 13 deletions libs/langchain-anthropic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion libs/langchain-anthropic/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
39 changes: 39 additions & 0 deletions libs/langchain-anthropic/src/tests/chat_models.standard.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7d7b5ad

Please sign in to comment.