mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-20 22:41:23 -04:00
chore: add opus 4.1 and fix prompt caching (#2155)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@llamaindex/anthropic": patch
|
||||
---
|
||||
|
||||
Fix: prompt caching (moved from beta)
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@llamaindex/anthropic": patch
|
||||
---
|
||||
|
||||
Feat: add claude opus 4.1
|
||||
@@ -0,0 +1,14 @@
|
||||
import { anthropic } from "@llamaindex/anthropic";
|
||||
import { agent } from "@llamaindex/workflow";
|
||||
|
||||
(async function () {
|
||||
const workflow = agent({
|
||||
llm: anthropic({
|
||||
model: "claude-4-1-opus",
|
||||
}),
|
||||
});
|
||||
const result = await workflow.run(
|
||||
"What are three compounds we should consider investigating to advance research into new antibiotics? Why should we consider them?",
|
||||
);
|
||||
console.log(result.data.result);
|
||||
})();
|
||||
@@ -28,19 +28,20 @@
|
||||
"scripts": {
|
||||
"build": "bunchee",
|
||||
"dev": "bunchee --watch",
|
||||
"test": "vitest run"
|
||||
"test": "vitest run",
|
||||
"type-check": "tsc -b --diagnostics"
|
||||
},
|
||||
"devDependencies": {
|
||||
"vitest": "^2.1.5",
|
||||
"@llamaindex/core": "workspace:*",
|
||||
"@llamaindex/env": "workspace:*"
|
||||
"@llamaindex/env": "workspace:*",
|
||||
"vitest": "^2.1.5"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@llamaindex/core": "workspace:*",
|
||||
"@llamaindex/env": "workspace:*"
|
||||
},
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sdk": "0.37.0",
|
||||
"@anthropic-ai/sdk": "0.58.0",
|
||||
"remeda": "^2.17.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
import type { ClientOptions } from "@anthropic-ai/sdk";
|
||||
import { Anthropic as SDKAnthropic } from "@anthropic-ai/sdk";
|
||||
import type {
|
||||
BetaCacheControlEphemeral,
|
||||
BetaTextBlockParam,
|
||||
} from "@anthropic-ai/sdk/resources/beta/index";
|
||||
import type { TextBlock } from "@anthropic-ai/sdk/resources/index";
|
||||
CacheControlEphemeral,
|
||||
Message,
|
||||
TextBlock,
|
||||
} from "@anthropic-ai/sdk/resources/index";
|
||||
import type {
|
||||
MessageCreateParams,
|
||||
MessageCreateParamsBase,
|
||||
MessageParam,
|
||||
Model,
|
||||
TextBlockParam,
|
||||
ThinkingBlock,
|
||||
Tool,
|
||||
ToolUseBlock,
|
||||
@@ -116,7 +117,15 @@ export const ALL_AVAILABLE_V4_MODELS = {
|
||||
"claude-4-0-sonnet": { contextWindow: 200000 },
|
||||
"claude-4-sonnet-20240514": { contextWindow: 200000 },
|
||||
"claude-4-0-opus": { contextWindow: 200000 },
|
||||
"claude-4-1-opus": { contextWindow: 200000 },
|
||||
"claude-4-opus-20240514": { contextWindow: 200000 },
|
||||
"claude-sonnet-4-0": { contextWindow: 200000 },
|
||||
"claude-sonnet-4-20250514": { contextWindow: 200000 },
|
||||
"claude-opus-4-0": { contextWindow: 200000 },
|
||||
"claude-opus-4-20250514": { contextWindow: 200000 },
|
||||
"claude-4-sonnet-20250514": { contextWindow: 200000 },
|
||||
"claude-4-opus-20250514": { contextWindow: 200000 },
|
||||
"claude-opus-4-1-20250805": { contextWindow: 200000 },
|
||||
};
|
||||
|
||||
export const ALL_AVAILABLE_ANTHROPIC_MODELS = {
|
||||
@@ -137,6 +146,7 @@ const AVAILABLE_ANTHROPIC_MODELS_WITHOUT_DATE: { [key: string]: string } = {
|
||||
"claude-3-7-sonnet": "claude-3-7-sonnet-20250219",
|
||||
"claude-4-0-sonnet": "claude-sonnet-4-20250514",
|
||||
"claude-4-0-opus": "claude-opus-4-20250514",
|
||||
"claude-4-1-opus": "claude-opus-4-1-20250805",
|
||||
} as { [key in keyof typeof ALL_AVAILABLE_ANTHROPIC_MODELS]: string };
|
||||
|
||||
export type AnthropicAdditionalChatOptions = Pick<
|
||||
@@ -144,7 +154,7 @@ export type AnthropicAdditionalChatOptions = Pick<
|
||||
"thinking"
|
||||
>;
|
||||
export type AnthropicToolCallLLMMessageOptions = ToolCallLLMMessageOptions & {
|
||||
cache_control?: BetaCacheControlEphemeral | null;
|
||||
cache_control?: CacheControlEphemeral | null;
|
||||
thinking?: string | undefined;
|
||||
thinking_signature?: string | undefined;
|
||||
};
|
||||
@@ -462,20 +472,20 @@ export class Anthropic extends ToolCallLLM<
|
||||
const { messages, stream, tools } = params;
|
||||
|
||||
// Handle system messages
|
||||
let systemPrompt: string | BetaTextBlockParam[] | null = null;
|
||||
let systemPrompt: string | TextBlockParam[] | null = null;
|
||||
const systemMessages = messages.filter(
|
||||
(message) => message.role === "system",
|
||||
);
|
||||
|
||||
if (systemMessages.length > 0) {
|
||||
systemPrompt = systemMessages.map((message): BetaTextBlockParam => {
|
||||
systemPrompt = systemMessages.map((message): TextBlockParam => {
|
||||
const textContent = extractText(message.content);
|
||||
if (message.options && "cache_control" in message.options) {
|
||||
return {
|
||||
type: "text" as const,
|
||||
text: textContent,
|
||||
cache_control: message.options
|
||||
.cache_control as BetaCacheControlEphemeral,
|
||||
.cache_control as CacheControlEphemeral,
|
||||
};
|
||||
}
|
||||
return {
|
||||
@@ -485,17 +495,9 @@ export class Anthropic extends ToolCallLLM<
|
||||
});
|
||||
}
|
||||
|
||||
const beta =
|
||||
Array.isArray(systemPrompt) &&
|
||||
systemPrompt.some((message) => "cache_control" in message);
|
||||
const anthropic = this.session.anthropic;
|
||||
|
||||
let anthropic = this.session.anthropic;
|
||||
if (beta) {
|
||||
// @ts-expect-error type casting
|
||||
anthropic = anthropic.beta.promptCaching;
|
||||
}
|
||||
|
||||
const apiParams: MessageCreateParams = {
|
||||
const apiParams: MessageCreateParamsBase = {
|
||||
model: this.getModelName(this.model),
|
||||
messages: this.mergeConsecutiveMessages(
|
||||
this.formatMessages(messages.filter((m) => m.role !== "system")),
|
||||
@@ -521,7 +523,7 @@ export class Anthropic extends ToolCallLLM<
|
||||
return this.streamChat(anthropic, apiParams);
|
||||
}
|
||||
|
||||
const response = await anthropic.messages.create(apiParams);
|
||||
const response = (await anthropic.messages.create(apiParams)) as Message;
|
||||
|
||||
const thinkingBlock = response.content.find(
|
||||
(content): content is ThinkingBlock => content.type === "thinking",
|
||||
|
||||
Generated
+6
-15
@@ -1164,8 +1164,8 @@ importers:
|
||||
packages/providers/anthropic:
|
||||
dependencies:
|
||||
'@anthropic-ai/sdk':
|
||||
specifier: 0.37.0
|
||||
version: 0.37.0
|
||||
specifier: 0.58.0
|
||||
version: 0.58.0
|
||||
remeda:
|
||||
specifier: ^2.17.3
|
||||
version: 2.21.3
|
||||
@@ -2057,8 +2057,9 @@ packages:
|
||||
resolution: {integrity: sha512-30iZtAPgz+LTIYoeivqYo853f02jBYSd5uGnGpkFV0M3xOt9aN73erkgYAmZU43x4VfqcnLxW9Kpg3R5LC4YYw==}
|
||||
engines: {node: '>=6.0.0'}
|
||||
|
||||
'@anthropic-ai/sdk@0.37.0':
|
||||
resolution: {integrity: sha512-tHjX2YbkUBwEgg0JZU3EFSSAQPoK4qQR/NFYa8Vtzd5UAyXzZksCw2In69Rml4R/TyHPBfRYaLK35XiOe33pjw==}
|
||||
'@anthropic-ai/sdk@0.58.0':
|
||||
resolution: {integrity: sha512-wF8w+LB0AlKVLYUQho0nbK0uDv0K5Cgb92dbh8213t4roilnQ9Tm6zndheIaUxQdoEAeb0uoOT+GkkoIkqD8+A==}
|
||||
hasBin: true
|
||||
|
||||
'@apidevtools/json-schema-ref-parser@11.7.2':
|
||||
resolution: {integrity: sha512-4gY54eEGEstClvEkGnwVkTkrx0sqwemEFG5OSRRn3tD91XH0+Q8XIkYIfo7IwEWPpJZwILb9GUXeShtplRc/eA==}
|
||||
@@ -14177,17 +14178,7 @@ snapshots:
|
||||
'@jridgewell/gen-mapping': 0.3.8
|
||||
'@jridgewell/trace-mapping': 0.3.25
|
||||
|
||||
'@anthropic-ai/sdk@0.37.0':
|
||||
dependencies:
|
||||
'@types/node': 18.19.118
|
||||
'@types/node-fetch': 2.6.12
|
||||
abort-controller: 3.0.0
|
||||
agentkeepalive: 4.6.0
|
||||
form-data-encoder: 1.7.2
|
||||
formdata-node: 4.4.1
|
||||
node-fetch: 2.7.0
|
||||
transitivePeerDependencies:
|
||||
- encoding
|
||||
'@anthropic-ai/sdk@0.58.0': {}
|
||||
|
||||
'@apidevtools/json-schema-ref-parser@11.7.2':
|
||||
dependencies:
|
||||
|
||||
Reference in New Issue
Block a user