@librechat/agents 3.0.78 → 3.0.79
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.
- package/dist/cjs/llm/bedrock/index.cjs +135 -23
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/utils/message_inputs.cjs +465 -0
- package/dist/cjs/llm/bedrock/utils/message_inputs.cjs.map +1 -0
- package/dist/cjs/llm/bedrock/utils/message_outputs.cjs +238 -0
- package/dist/cjs/llm/bedrock/utils/message_outputs.cjs.map +1 -0
- package/dist/cjs/messages/core.cjs +1 -1
- package/dist/cjs/messages/core.cjs.map +1 -1
- package/dist/cjs/stream.cjs +4 -2
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +134 -22
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/llm/bedrock/utils/message_inputs.mjs +460 -0
- package/dist/esm/llm/bedrock/utils/message_inputs.mjs.map +1 -0
- package/dist/esm/llm/bedrock/utils/message_outputs.mjs +231 -0
- package/dist/esm/llm/bedrock/utils/message_outputs.mjs.map +1 -0
- package/dist/esm/messages/core.mjs +1 -1
- package/dist/esm/messages/core.mjs.map +1 -1
- package/dist/esm/stream.mjs +4 -2
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/types/llm/bedrock/index.d.ts +83 -7
- package/dist/types/llm/bedrock/types.d.ts +27 -0
- package/dist/types/llm/bedrock/utils/index.d.ts +5 -0
- package/dist/types/llm/bedrock/utils/message_inputs.d.ts +31 -0
- package/dist/types/llm/bedrock/utils/message_outputs.d.ts +33 -0
- package/package.json +4 -3
- package/src/llm/bedrock/index.ts +232 -41
- package/src/llm/bedrock/llm.spec.ts +616 -0
- package/src/llm/bedrock/types.ts +51 -0
- package/src/llm/bedrock/utils/index.ts +18 -0
- package/src/llm/bedrock/utils/message_inputs.ts +563 -0
- package/src/llm/bedrock/utils/message_outputs.ts +296 -0
- package/src/messages/core.ts +1 -1
- package/src/scripts/thinking.ts +39 -18
- package/src/scripts/tools.ts +7 -3
- package/src/stream.ts +4 -2
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts
|
|
3
|
+
* and adds support for latest @langchain/aws features:
|
|
4
|
+
*
|
|
5
|
+
* - Application Inference Profiles (PR #9129)
|
|
6
|
+
* - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+
|
|
3
7
|
*
|
|
4
8
|
* Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,
|
|
5
9
|
* causing LangChain's merge logic to fail with "field[contentBlockIndex] already exists"
|
|
@@ -11,19 +15,91 @@
|
|
|
11
15
|
* the purpose of tracking tool call ordering.
|
|
12
16
|
*/
|
|
13
17
|
import { ChatBedrockConverse } from '@langchain/aws';
|
|
18
|
+
import { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';
|
|
19
|
+
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
14
20
|
import type { ChatBedrockConverseInput } from '@langchain/aws';
|
|
15
21
|
import type { BaseMessage } from '@langchain/core/messages';
|
|
16
|
-
|
|
17
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Service tier type for Bedrock invocations.
|
|
24
|
+
* Requires AWS SDK >= 3.966.0 to actually work.
|
|
25
|
+
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html
|
|
26
|
+
*/
|
|
27
|
+
export type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';
|
|
28
|
+
/**
|
|
29
|
+
* Extended input interface with additional features:
|
|
30
|
+
* - applicationInferenceProfile: Use an inference profile ARN instead of model ID
|
|
31
|
+
* - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)
|
|
32
|
+
*/
|
|
33
|
+
export interface CustomChatBedrockConverseInput extends ChatBedrockConverseInput {
|
|
34
|
+
/**
|
|
35
|
+
* Application Inference Profile ARN to use for the model.
|
|
36
|
+
* For example, "arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx"
|
|
37
|
+
* When provided, this ARN will be used for the actual inference calls instead of the model ID.
|
|
38
|
+
* Must still provide `model` as normal modelId to benefit from all the metadata.
|
|
39
|
+
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html
|
|
40
|
+
*/
|
|
41
|
+
applicationInferenceProfile?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Service tier for model invocation.
|
|
44
|
+
* Specifies the processing tier type used for serving the request.
|
|
45
|
+
* Supported values are 'priority', 'default', 'flex', and 'reserved'.
|
|
46
|
+
*
|
|
47
|
+
* - 'priority': Prioritized processing for lower latency
|
|
48
|
+
* - 'default': Standard processing tier
|
|
49
|
+
* - 'flex': Flexible processing tier with lower cost
|
|
50
|
+
* - 'reserved': Reserved capacity for consistent performance
|
|
51
|
+
*
|
|
52
|
+
* If not provided, AWS uses the default tier.
|
|
53
|
+
* Note: Requires AWS SDK >= 3.966.0 to work.
|
|
54
|
+
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html
|
|
55
|
+
*/
|
|
56
|
+
serviceTier?: ServiceTierType;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Extended call options with serviceTier override support.
|
|
60
|
+
*/
|
|
61
|
+
export interface CustomChatBedrockConverseCallOptions {
|
|
62
|
+
serviceTier?: ServiceTierType;
|
|
63
|
+
}
|
|
18
64
|
export declare class CustomChatBedrockConverse extends ChatBedrockConverse {
|
|
19
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Application Inference Profile ARN to use instead of model ID.
|
|
67
|
+
*/
|
|
68
|
+
applicationInferenceProfile?: string;
|
|
69
|
+
/**
|
|
70
|
+
* Service tier for model invocation.
|
|
71
|
+
*/
|
|
72
|
+
serviceTier?: ServiceTierType;
|
|
73
|
+
constructor(fields?: CustomChatBedrockConverseInput);
|
|
20
74
|
static lc_name(): string;
|
|
21
75
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
|
|
76
|
+
* Get the model ID to use for API calls.
|
|
77
|
+
* Returns applicationInferenceProfile if set, otherwise returns this.model.
|
|
78
|
+
*/
|
|
79
|
+
protected getModelId(): string;
|
|
80
|
+
/**
|
|
81
|
+
* Override invocationParams to add serviceTier support.
|
|
82
|
+
*/
|
|
83
|
+
invocationParams(options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions): ReturnType<ChatBedrockConverse['invocationParams']> & {
|
|
84
|
+
serviceTier?: {
|
|
85
|
+
type: ServiceTierType;
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Override _generateNonStreaming to use applicationInferenceProfile as modelId.
|
|
90
|
+
*/
|
|
91
|
+
_generateNonStreaming(messages: BaseMessage[], options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions, _runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
92
|
+
/**
|
|
93
|
+
* Override _streamResponseChunks to:
|
|
94
|
+
* 1. Use applicationInferenceProfile as modelId
|
|
95
|
+
* 2. Include serviceTier in request
|
|
96
|
+
* 3. Strip contentBlockIndex from response_metadata to prevent merge conflicts
|
|
97
|
+
*/
|
|
98
|
+
_streamResponseChunks(messages: BaseMessage[], options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions, runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
|
|
99
|
+
/**
|
|
100
|
+
* Clean a chunk by removing contentBlockIndex from response_metadata.
|
|
25
101
|
*/
|
|
26
|
-
|
|
102
|
+
private cleanChunk;
|
|
27
103
|
/**
|
|
28
104
|
* Check if contentBlockIndex exists at any level in the object
|
|
29
105
|
*/
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for Bedrock Converse utilities.
|
|
3
|
+
*/
|
|
4
|
+
import type { Message as BedrockMessage, SystemContentBlock as BedrockSystemContentBlock, ContentBlock as BedrockContentBlock, ConverseResponse, ContentBlockDeltaEvent, ConverseStreamMetadataEvent, ContentBlockStartEvent, ReasoningContentBlock, ReasoningContentBlockDelta } from '@aws-sdk/client-bedrock-runtime';
|
|
5
|
+
/**
|
|
6
|
+
* Reasoning content block type for LangChain messages.
|
|
7
|
+
*/
|
|
8
|
+
export interface MessageContentReasoningBlock {
|
|
9
|
+
type: 'reasoning_content';
|
|
10
|
+
reasoningText?: {
|
|
11
|
+
text?: string;
|
|
12
|
+
signature?: string;
|
|
13
|
+
};
|
|
14
|
+
redactedContent?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface MessageContentReasoningBlockReasoningTextPartial {
|
|
17
|
+
type: 'reasoning_content';
|
|
18
|
+
reasoningText: {
|
|
19
|
+
text?: string;
|
|
20
|
+
signature?: string;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export interface MessageContentReasoningBlockRedacted {
|
|
24
|
+
type: 'reasoning_content';
|
|
25
|
+
redactedContent: string;
|
|
26
|
+
}
|
|
27
|
+
export type { BedrockMessage, BedrockSystemContentBlock, BedrockContentBlock, ConverseResponse, ContentBlockDeltaEvent, ConverseStreamMetadataEvent, ContentBlockStartEvent, ReasoningContentBlock, ReasoningContentBlockDelta, };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bedrock Converse utility exports.
|
|
3
|
+
*/
|
|
4
|
+
export { convertToConverseMessages, extractImageInfo, langchainReasoningBlockToBedrockReasoningBlock, concatenateLangchainReasoningBlocks, } from './message_inputs';
|
|
5
|
+
export { convertConverseMessageToLangChainMessage, handleConverseStreamContentBlockStart, handleConverseStreamContentBlockDelta, handleConverseStreamMetadata, bedrockReasoningBlockToLangchainReasoningBlock, bedrockReasoningDeltaToLangchainPartialReasoningBlock, } from './message_outputs';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for converting LangChain messages to Bedrock Converse messages.
|
|
3
|
+
* Ported from @langchain/aws common.js
|
|
4
|
+
*/
|
|
5
|
+
import { type BaseMessage, MessageContentComplex } from '@langchain/core/messages';
|
|
6
|
+
import type { BedrockMessage, BedrockSystemContentBlock, BedrockContentBlock, MessageContentReasoningBlock } from '../types';
|
|
7
|
+
/**
|
|
8
|
+
* Convert a LangChain reasoning block to a Bedrock reasoning block.
|
|
9
|
+
*/
|
|
10
|
+
export declare function langchainReasoningBlockToBedrockReasoningBlock(content: MessageContentReasoningBlock): {
|
|
11
|
+
reasoningText?: {
|
|
12
|
+
text?: string;
|
|
13
|
+
signature?: string;
|
|
14
|
+
};
|
|
15
|
+
redactedContent?: Uint8Array;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Concatenate consecutive reasoning blocks in content array.
|
|
19
|
+
*/
|
|
20
|
+
export declare function concatenateLangchainReasoningBlocks(content: Array<MessageContentComplex | MessageContentReasoningBlock>): Array<MessageContentComplex | MessageContentReasoningBlock>;
|
|
21
|
+
/**
|
|
22
|
+
* Extract image info from a base64 string or URL.
|
|
23
|
+
*/
|
|
24
|
+
export declare function extractImageInfo(base64: string): BedrockContentBlock;
|
|
25
|
+
/**
|
|
26
|
+
* Convert LangChain messages to Bedrock Converse messages.
|
|
27
|
+
*/
|
|
28
|
+
export declare function convertToConverseMessages(messages: BaseMessage[]): {
|
|
29
|
+
converseMessages: BedrockMessage[];
|
|
30
|
+
converseSystem: BedrockSystemContentBlock[];
|
|
31
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for converting Bedrock Converse responses to LangChain messages.
|
|
3
|
+
* Ported from @langchain/aws common.js
|
|
4
|
+
*/
|
|
5
|
+
import { AIMessage } from '@langchain/core/messages';
|
|
6
|
+
import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
7
|
+
import type { BedrockMessage, ConverseResponse, ContentBlockDeltaEvent, ConverseStreamMetadataEvent, ContentBlockStartEvent, ReasoningContentBlock, ReasoningContentBlockDelta, MessageContentReasoningBlock, MessageContentReasoningBlockReasoningTextPartial, MessageContentReasoningBlockRedacted } from '../types';
|
|
8
|
+
/**
|
|
9
|
+
* Convert a Bedrock reasoning block delta to a LangChain partial reasoning block.
|
|
10
|
+
*/
|
|
11
|
+
export declare function bedrockReasoningDeltaToLangchainPartialReasoningBlock(reasoningContent: ReasoningContentBlockDelta): MessageContentReasoningBlockReasoningTextPartial | MessageContentReasoningBlockRedacted;
|
|
12
|
+
/**
|
|
13
|
+
* Convert a Bedrock reasoning block to a LangChain reasoning block.
|
|
14
|
+
*/
|
|
15
|
+
export declare function bedrockReasoningBlockToLangchainReasoningBlock(reasoningContent: ReasoningContentBlock): MessageContentReasoningBlock;
|
|
16
|
+
/**
|
|
17
|
+
* Convert a Bedrock Converse message to a LangChain message.
|
|
18
|
+
*/
|
|
19
|
+
export declare function convertConverseMessageToLangChainMessage(message: BedrockMessage, responseMetadata: Omit<ConverseResponse, 'output'>): AIMessage;
|
|
20
|
+
/**
|
|
21
|
+
* Handle a content block delta event from Bedrock Converse stream.
|
|
22
|
+
*/
|
|
23
|
+
export declare function handleConverseStreamContentBlockDelta(contentBlockDelta: ContentBlockDeltaEvent): ChatGenerationChunk;
|
|
24
|
+
/**
|
|
25
|
+
* Handle a content block start event from Bedrock Converse stream.
|
|
26
|
+
*/
|
|
27
|
+
export declare function handleConverseStreamContentBlockStart(contentBlockStart: ContentBlockStartEvent): ChatGenerationChunk | null;
|
|
28
|
+
/**
|
|
29
|
+
* Handle a metadata event from Bedrock Converse stream.
|
|
30
|
+
*/
|
|
31
|
+
export declare function handleConverseStreamMetadata(metadata: ConverseStreamMetadataEvent, extra: {
|
|
32
|
+
streamUsage: boolean;
|
|
33
|
+
}): ChatGenerationChunk;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@librechat/agents",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.79",
|
|
4
4
|
"main": "./dist/cjs/main.cjs",
|
|
5
5
|
"module": "./dist/esm/main.mjs",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
@@ -53,11 +53,11 @@
|
|
|
53
53
|
"code_exec_session": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_session.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
54
54
|
"code_exec_multi_session": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_multi_session.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
55
55
|
"code_exec_simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_simple.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
56
|
-
"simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider '
|
|
56
|
+
"simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider 'bedrock' --name 'Jo' --location 'New York, NY'",
|
|
57
57
|
"caching": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/caching.ts --name 'Jo' --location 'New York, NY'",
|
|
58
58
|
"thinking": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/thinking.ts --name 'Jo' --location 'New York, NY'",
|
|
59
59
|
"memory": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/memory.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
60
|
-
"tool": "node --trace-warnings -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/tools.ts --provider '
|
|
60
|
+
"tool": "node --trace-warnings -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/tools.ts --provider 'bedrock' --name 'Jo' --location 'New York, NY'",
|
|
61
61
|
"search": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/search.ts --provider 'bedrock' --name 'Jo' --location 'New York, NY'",
|
|
62
62
|
"tool_search": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/tool_search.ts",
|
|
63
63
|
"programmatic_exec": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/programmatic_exec.ts",
|
|
@@ -111,6 +111,7 @@
|
|
|
111
111
|
}
|
|
112
112
|
},
|
|
113
113
|
"dependencies": {
|
|
114
|
+
"@aws-sdk/client-bedrock-runtime": "^3.970.0",
|
|
114
115
|
"@langchain/anthropic": "^0.3.26",
|
|
115
116
|
"@langchain/aws": "^0.1.15",
|
|
116
117
|
"@langchain/core": "^0.3.80",
|
package/src/llm/bedrock/index.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts
|
|
3
|
+
* and adds support for latest @langchain/aws features:
|
|
4
|
+
*
|
|
5
|
+
* - Application Inference Profiles (PR #9129)
|
|
6
|
+
* - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+
|
|
3
7
|
*
|
|
4
8
|
* Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,
|
|
5
9
|
* causing LangChain's merge logic to fail with "field[contentBlockIndex] already exists"
|
|
@@ -12,15 +16,85 @@
|
|
|
12
16
|
*/
|
|
13
17
|
|
|
14
18
|
import { ChatBedrockConverse } from '@langchain/aws';
|
|
15
|
-
import type { ChatBedrockConverseInput } from '@langchain/aws';
|
|
16
19
|
import { AIMessageChunk } from '@langchain/core/messages';
|
|
17
|
-
import
|
|
18
|
-
import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
20
|
+
import { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';
|
|
19
21
|
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
22
|
+
import type { ChatBedrockConverseInput } from '@langchain/aws';
|
|
23
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
24
|
+
import {
|
|
25
|
+
ConverseCommand,
|
|
26
|
+
ConverseStreamCommand,
|
|
27
|
+
} from '@aws-sdk/client-bedrock-runtime';
|
|
28
|
+
import {
|
|
29
|
+
convertToConverseMessages,
|
|
30
|
+
convertConverseMessageToLangChainMessage,
|
|
31
|
+
handleConverseStreamContentBlockStart,
|
|
32
|
+
handleConverseStreamContentBlockDelta,
|
|
33
|
+
handleConverseStreamMetadata,
|
|
34
|
+
} from './utils';
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Service tier type for Bedrock invocations.
|
|
38
|
+
* Requires AWS SDK >= 3.966.0 to actually work.
|
|
39
|
+
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html
|
|
40
|
+
*/
|
|
41
|
+
export type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Extended input interface with additional features:
|
|
45
|
+
* - applicationInferenceProfile: Use an inference profile ARN instead of model ID
|
|
46
|
+
* - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)
|
|
47
|
+
*/
|
|
48
|
+
export interface CustomChatBedrockConverseInput
|
|
49
|
+
extends ChatBedrockConverseInput {
|
|
50
|
+
/**
|
|
51
|
+
* Application Inference Profile ARN to use for the model.
|
|
52
|
+
* For example, "arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx"
|
|
53
|
+
* When provided, this ARN will be used for the actual inference calls instead of the model ID.
|
|
54
|
+
* Must still provide `model` as normal modelId to benefit from all the metadata.
|
|
55
|
+
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html
|
|
56
|
+
*/
|
|
57
|
+
applicationInferenceProfile?: string;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Service tier for model invocation.
|
|
61
|
+
* Specifies the processing tier type used for serving the request.
|
|
62
|
+
* Supported values are 'priority', 'default', 'flex', and 'reserved'.
|
|
63
|
+
*
|
|
64
|
+
* - 'priority': Prioritized processing for lower latency
|
|
65
|
+
* - 'default': Standard processing tier
|
|
66
|
+
* - 'flex': Flexible processing tier with lower cost
|
|
67
|
+
* - 'reserved': Reserved capacity for consistent performance
|
|
68
|
+
*
|
|
69
|
+
* If not provided, AWS uses the default tier.
|
|
70
|
+
* Note: Requires AWS SDK >= 3.966.0 to work.
|
|
71
|
+
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html
|
|
72
|
+
*/
|
|
73
|
+
serviceTier?: ServiceTierType;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Extended call options with serviceTier override support.
|
|
78
|
+
*/
|
|
79
|
+
export interface CustomChatBedrockConverseCallOptions {
|
|
80
|
+
serviceTier?: ServiceTierType;
|
|
81
|
+
}
|
|
20
82
|
|
|
21
83
|
export class CustomChatBedrockConverse extends ChatBedrockConverse {
|
|
22
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Application Inference Profile ARN to use instead of model ID.
|
|
86
|
+
*/
|
|
87
|
+
applicationInferenceProfile?: string;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Service tier for model invocation.
|
|
91
|
+
*/
|
|
92
|
+
serviceTier?: ServiceTierType;
|
|
93
|
+
|
|
94
|
+
constructor(fields?: CustomChatBedrockConverseInput) {
|
|
23
95
|
super(fields);
|
|
96
|
+
this.applicationInferenceProfile = fields?.applicationInferenceProfile;
|
|
97
|
+
this.serviceTier = fields?.serviceTier;
|
|
24
98
|
}
|
|
25
99
|
|
|
26
100
|
static lc_name(): string {
|
|
@@ -28,52 +102,169 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
|
|
|
28
102
|
}
|
|
29
103
|
|
|
30
104
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
|
|
105
|
+
* Get the model ID to use for API calls.
|
|
106
|
+
* Returns applicationInferenceProfile if set, otherwise returns this.model.
|
|
107
|
+
*/
|
|
108
|
+
protected getModelId(): string {
|
|
109
|
+
return this.applicationInferenceProfile ?? this.model;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Override invocationParams to add serviceTier support.
|
|
34
114
|
*/
|
|
35
|
-
|
|
115
|
+
override invocationParams(
|
|
116
|
+
options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions
|
|
117
|
+
): ReturnType<ChatBedrockConverse['invocationParams']> & {
|
|
118
|
+
serviceTier?: { type: ServiceTierType };
|
|
119
|
+
} {
|
|
120
|
+
const baseParams = super.invocationParams(options);
|
|
121
|
+
|
|
122
|
+
// Get serviceTier from options or fall back to class-level setting
|
|
123
|
+
const serviceTierType = options?.serviceTier ?? this.serviceTier;
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
...baseParams,
|
|
127
|
+
serviceTier: serviceTierType ? { type: serviceTierType } : undefined,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Override _generateNonStreaming to use applicationInferenceProfile as modelId.
|
|
133
|
+
*/
|
|
134
|
+
override async _generateNonStreaming(
|
|
36
135
|
messages: BaseMessage[],
|
|
37
|
-
options: this['ParsedCallOptions'],
|
|
136
|
+
options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,
|
|
137
|
+
_runManager?: CallbackManagerForLLMRun
|
|
138
|
+
): Promise<ChatResult> {
|
|
139
|
+
const { converseMessages, converseSystem } =
|
|
140
|
+
convertToConverseMessages(messages);
|
|
141
|
+
const params = this.invocationParams(options);
|
|
142
|
+
|
|
143
|
+
const command = new ConverseCommand({
|
|
144
|
+
modelId: this.getModelId(),
|
|
145
|
+
messages: converseMessages,
|
|
146
|
+
system: converseSystem,
|
|
147
|
+
requestMetadata: options.requestMetadata,
|
|
148
|
+
...params,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const response = await this.client.send(command, {
|
|
152
|
+
abortSignal: options.signal,
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const { output, ...responseMetadata } = response;
|
|
156
|
+
if (!output?.message) {
|
|
157
|
+
throw new Error('No message found in Bedrock response.');
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const message = convertConverseMessageToLangChainMessage(
|
|
161
|
+
output.message,
|
|
162
|
+
responseMetadata
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
generations: [
|
|
167
|
+
{
|
|
168
|
+
text: typeof message.content === 'string' ? message.content : '',
|
|
169
|
+
message,
|
|
170
|
+
},
|
|
171
|
+
],
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Override _streamResponseChunks to:
|
|
177
|
+
* 1. Use applicationInferenceProfile as modelId
|
|
178
|
+
* 2. Include serviceTier in request
|
|
179
|
+
* 3. Strip contentBlockIndex from response_metadata to prevent merge conflicts
|
|
180
|
+
*/
|
|
181
|
+
override async *_streamResponseChunks(
|
|
182
|
+
messages: BaseMessage[],
|
|
183
|
+
options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,
|
|
38
184
|
runManager?: CallbackManagerForLLMRun
|
|
39
185
|
): AsyncGenerator<ChatGenerationChunk> {
|
|
40
|
-
const
|
|
41
|
-
messages
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
186
|
+
const { converseMessages, converseSystem } =
|
|
187
|
+
convertToConverseMessages(messages);
|
|
188
|
+
const params = this.invocationParams(options);
|
|
189
|
+
|
|
190
|
+
let { streamUsage } = this;
|
|
191
|
+
if (options.streamUsage !== undefined) {
|
|
192
|
+
streamUsage = options.streamUsage;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const command = new ConverseStreamCommand({
|
|
196
|
+
modelId: this.getModelId(),
|
|
197
|
+
messages: converseMessages,
|
|
198
|
+
system: converseSystem,
|
|
199
|
+
requestMetadata: options.requestMetadata,
|
|
200
|
+
...params,
|
|
201
|
+
});
|
|
45
202
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
203
|
+
const response = await this.client.send(command, {
|
|
204
|
+
abortSignal: options.signal,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
if (response.stream) {
|
|
208
|
+
for await (const event of response.stream) {
|
|
209
|
+
if (event.contentBlockStart != null) {
|
|
210
|
+
const chunk = handleConverseStreamContentBlockStart(
|
|
211
|
+
event.contentBlockStart
|
|
212
|
+
) as ChatGenerationChunk | undefined;
|
|
213
|
+
if (chunk !== undefined) {
|
|
214
|
+
const cleanedChunk = this.cleanChunk(chunk);
|
|
215
|
+
yield cleanedChunk;
|
|
216
|
+
await runManager?.handleLLMNewToken(cleanedChunk.text || '');
|
|
217
|
+
}
|
|
218
|
+
} else if (event.contentBlockDelta != null) {
|
|
219
|
+
const chunk = handleConverseStreamContentBlockDelta(
|
|
220
|
+
event.contentBlockDelta
|
|
221
|
+
) as ChatGenerationChunk | undefined;
|
|
222
|
+
if (chunk !== undefined) {
|
|
223
|
+
const cleanedChunk = this.cleanChunk(chunk);
|
|
224
|
+
yield cleanedChunk;
|
|
225
|
+
await runManager?.handleLLMNewToken(cleanedChunk.text || '');
|
|
226
|
+
}
|
|
227
|
+
} else if (event.metadata != null) {
|
|
228
|
+
const chunk = handleConverseStreamMetadata(event.metadata, {
|
|
229
|
+
streamUsage,
|
|
230
|
+
}) as ChatGenerationChunk | undefined;
|
|
231
|
+
if (chunk !== undefined) {
|
|
232
|
+
const cleanedChunk = this.cleanChunk(chunk);
|
|
233
|
+
yield cleanedChunk;
|
|
234
|
+
}
|
|
72
235
|
}
|
|
73
236
|
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
74
239
|
|
|
75
|
-
|
|
240
|
+
/**
|
|
241
|
+
* Clean a chunk by removing contentBlockIndex from response_metadata.
|
|
242
|
+
*/
|
|
243
|
+
private cleanChunk(chunk: ChatGenerationChunk): ChatGenerationChunk {
|
|
244
|
+
const message = chunk.message;
|
|
245
|
+
if (!(message instanceof AIMessageChunk)) {
|
|
246
|
+
return chunk;
|
|
76
247
|
}
|
|
248
|
+
|
|
249
|
+
const metadata = message.response_metadata as Record<string, unknown>;
|
|
250
|
+
const hasContentBlockIndex = this.hasContentBlockIndex(metadata);
|
|
251
|
+
if (!hasContentBlockIndex) {
|
|
252
|
+
return chunk;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const cleanedMetadata = this.removeContentBlockIndex(metadata) as Record<
|
|
256
|
+
string,
|
|
257
|
+
unknown
|
|
258
|
+
>;
|
|
259
|
+
|
|
260
|
+
return new ChatGenerationChunk({
|
|
261
|
+
text: chunk.text,
|
|
262
|
+
message: new AIMessageChunk({
|
|
263
|
+
...message,
|
|
264
|
+
response_metadata: cleanedMetadata,
|
|
265
|
+
}),
|
|
266
|
+
generationInfo: chunk.generationInfo,
|
|
267
|
+
});
|
|
77
268
|
}
|
|
78
269
|
|
|
79
270
|
/**
|