Skip to content

Commit

Permalink
Update Anthropic SDK to 0.5.7 (langchain-ai#2023)
Browse files Browse the repository at this point in the history
* Update Anthropic SDK to 0.5.7

* Remove previous workaround
  • Loading branch information
dqbd authored Jul 20, 2023
1 parent 9302454 commit 48ac23f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 36 deletions.
2 changes: 1 addition & 1 deletion langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@
}
},
"dependencies": {
"@anthropic-ai/sdk": "^0.5.3",
"@anthropic-ai/sdk": "^0.5.7",
"ansi-styles": "^5.0.0",
"binary-extensions": "^2.2.0",
"camelcase": "6",
Expand Down
31 changes: 13 additions & 18 deletions langchain/src/chat_models/anthropic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as AnthropicApi from "@anthropic-ai/sdk";
import { Anthropic, AI_PROMPT, HUMAN_PROMPT } from "@anthropic-ai/sdk";
import type { CompletionCreateParams } from "@anthropic-ai/sdk/resources/completions";

import { BaseLanguageModelCallOptions } from "../base_language/index.js";
Expand All @@ -13,25 +13,20 @@ import {
import { getEnvironmentVariable } from "../util/env.js";
import { BaseChatModel, BaseChatModelParams } from "./base.js";

// Anthropic's 0.5.3 SDK currently has a collision with the default exported class
// and an exported namespace that causes issues when transpiling to CommonJS
const AnthropicClientConstructor =
AnthropicApi.Anthropic ?? AnthropicApi.default;

function getAnthropicPromptFromMessage(type: MessageType): string {
switch (type) {
case "ai":
return AnthropicApi.AI_PROMPT;
return AI_PROMPT;
case "human":
return AnthropicApi.HUMAN_PROMPT;
return HUMAN_PROMPT;
case "system":
return "";
default:
throw new Error(`Unknown message type: ${type}`);
}
}

const DEFAULT_STOP_SEQUENCES = [AnthropicApi.HUMAN_PROMPT];
const DEFAULT_STOP_SEQUENCES = [HUMAN_PROMPT];

/**
* Input to AnthropicChat class.
Expand Down Expand Up @@ -142,10 +137,10 @@ export class ChatAnthropic extends BaseChatModel implements AnthropicInput {
streaming = false;

// Used for non-streaming requests
private batchClient: AnthropicApi.Anthropic;
private batchClient: Anthropic;

// Used for streaming requests
private streamingClient: AnthropicApi.Anthropic;
private streamingClient: Anthropic;

constructor(fields?: Partial<AnthropicInput> & BaseChatModelParams) {
super(fields ?? {});
Expand Down Expand Up @@ -220,7 +215,7 @@ export class ChatAnthropic extends BaseChatModel implements AnthropicInput {
);
return `${messagePrompt} ${message.content}`;
})
.join("") + AnthropicApi.AI_PROMPT
.join("") + AI_PROMPT
);
}

Expand All @@ -247,7 +242,7 @@ export class ChatAnthropic extends BaseChatModel implements AnthropicInput {
);

const generations: ChatGeneration[] = response.completion
.split(AnthropicApi.AI_PROMPT)
.split(AI_PROMPT)
.map((message) => ({
text: message,
message: new AIMessage(message),
Expand All @@ -263,17 +258,17 @@ export class ChatAnthropic extends BaseChatModel implements AnthropicInput {
request: CompletionCreateParams & Kwargs,
options: { signal?: AbortSignal },
runManager?: CallbackManagerForLLMRun
): Promise<AnthropicApi.Anthropic.Completions.Completion> {
): Promise<Anthropic.Completions.Completion> {
if (!this.anthropicApiKey) {
throw new Error("Missing Anthropic API key.");
}
let makeCompletionRequest: () => Promise<AnthropicApi.Anthropic.Completions.Completion>;
let makeCompletionRequest: () => Promise<Anthropic.Completions.Completion>;

let asyncCallerOptions = {};
if (request.stream) {
if (!this.streamingClient) {
const options = this.apiUrl ? { apiUrl: this.apiUrl } : undefined;
this.streamingClient = new AnthropicClientConstructor({
this.streamingClient = new Anthropic({
...options,
apiKey: this.anthropicApiKey,
});
Expand All @@ -283,7 +278,7 @@ export class ChatAnthropic extends BaseChatModel implements AnthropicInput {
...request,
});

const completion: AnthropicApi.Anthropic.Completion = {
const completion: Anthropic.Completion = {
completion: "",
model: "",
stop_reason: "",
Expand Down Expand Up @@ -314,7 +309,7 @@ export class ChatAnthropic extends BaseChatModel implements AnthropicInput {
} else {
if (!this.batchClient) {
const options = this.apiUrl ? { apiUrl: this.apiUrl } : undefined;
this.batchClient = new AnthropicClientConstructor({
this.batchClient = new Anthropic({
...options,
apiKey: this.anthropicApiKey,
});
Expand Down
23 changes: 6 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@ __metadata:
languageName: node
linkType: hard

"@anthropic-ai/sdk@npm:^0.5.3":
version: 0.5.4
resolution: "@anthropic-ai/sdk@npm:0.5.4"
"@anthropic-ai/sdk@npm:^0.5.7":
version: 0.5.7
resolution: "@anthropic-ai/sdk@npm:0.5.7"
dependencies:
"@types/node": ^18.11.18
"@types/node-fetch": ^2.6.4
"@types/qs": ^6.9.7
abort-controller: ^3.0.0
agentkeepalive: ^4.2.1
digest-fetch: ^1.3.0
form-data-encoder: 1.7.2
formdata-node: ^4.3.2
node-fetch: ^2.6.7
qs: ^6.10.3
checksum: 2baee795eebd852d13368c67f857d039901f362ac740cd97dc748c8170e0aa19ccccefd57356c2c7b134ace2fb22ff7d95a2d43b6957524056f0803a6610fac8
checksum: b4c55d8c962438516a035e8d08c4e6bc1f34f93df6254dc0e12db3d21cf265df3416cbfe976e9fad9ecc22f15aa1f58048465f9a298f80b71efcb6c872a35583
languageName: node
linkType: hard

Expand Down Expand Up @@ -6114,7 +6112,7 @@ __metadata:
languageName: node
linkType: hard

"@types/qs@npm:*, @types/qs@npm:^6.9.7":
"@types/qs@npm:*":
version: 6.9.7
resolution: "@types/qs@npm:6.9.7"
checksum: 7fd6f9c25053e9b5bb6bc9f9f76c1d89e6c04f7707a7ba0e44cc01f17ef5284adb82f230f542c2d5557d69407c9a40f0f3515e8319afd14e1e16b5543ac6cdba
Expand Down Expand Up @@ -12947,7 +12945,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "langchain@workspace:langchain"
dependencies:
"@anthropic-ai/sdk": ^0.5.3
"@anthropic-ai/sdk": ^0.5.7
"@aws-sdk/client-dynamodb": ^3.310.0
"@aws-sdk/client-kendra": ^3.352.0
"@aws-sdk/client-lambda": ^3.310.0
Expand Down Expand Up @@ -16046,15 +16044,6 @@ __metadata:
languageName: node
linkType: hard

"qs@npm:^6.10.3":
version: 6.11.2
resolution: "qs@npm:6.11.2"
dependencies:
side-channel: ^1.0.4
checksum: e812f3c590b2262548647d62f1637b6989cc56656dc960b893fe2098d96e1bd633f36576f4cd7564dfbff9db42e17775884db96d846bebe4f37420d073ecdc0b
languageName: node
linkType: hard

"queue-microtask@npm:^1.2.2":
version: 1.2.3
resolution: "queue-microtask@npm:1.2.3"
Expand Down

0 comments on commit 48ac23f

Please sign in to comment.