langchain 0.0.164 → 0.0.166

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.
Files changed (84) hide show
  1. package/chat_models/portkey.cjs +1 -0
  2. package/chat_models/portkey.d.ts +1 -0
  3. package/chat_models/portkey.js +1 -0
  4. package/dist/cache/momento.cjs +7 -7
  5. package/dist/cache/momento.d.ts +1 -1
  6. package/dist/cache/momento.js +1 -1
  7. package/dist/chat_models/portkey.cjs +159 -0
  8. package/dist/chat_models/portkey.d.ts +17 -0
  9. package/dist/chat_models/portkey.js +155 -0
  10. package/dist/document_loaders/fs/unstructured.cjs +40 -0
  11. package/dist/document_loaders/fs/unstructured.d.ts +27 -0
  12. package/dist/document_loaders/fs/unstructured.js +40 -0
  13. package/dist/embeddings/minimax.cjs +1 -1
  14. package/dist/embeddings/minimax.js +1 -1
  15. package/dist/experimental/chat_models/anthropic_functions.cjs +34 -2
  16. package/dist/experimental/chat_models/anthropic_functions.d.ts +15 -5
  17. package/dist/experimental/chat_models/anthropic_functions.js +34 -2
  18. package/dist/llms/portkey.cjs +147 -0
  19. package/dist/llms/portkey.d.ts +33 -0
  20. package/dist/llms/portkey.js +138 -0
  21. package/dist/llms/sagemaker_endpoint.cjs +76 -14
  22. package/dist/llms/sagemaker_endpoint.d.ts +39 -20
  23. package/dist/llms/sagemaker_endpoint.js +77 -15
  24. package/dist/load/import_constants.cjs +4 -0
  25. package/dist/load/import_constants.js +4 -0
  26. package/dist/load/import_map.cjs +3 -1
  27. package/dist/load/import_map.d.ts +2 -0
  28. package/dist/load/import_map.js +2 -0
  29. package/dist/output_parsers/list.cjs +1 -1
  30. package/dist/output_parsers/list.js +1 -1
  31. package/dist/prompts/chat.cjs +19 -2
  32. package/dist/prompts/chat.d.ts +1 -0
  33. package/dist/prompts/chat.js +19 -2
  34. package/dist/retrievers/self_query/base.cjs +4 -1
  35. package/dist/retrievers/self_query/base.d.ts +3 -2
  36. package/dist/retrievers/self_query/base.js +4 -1
  37. package/dist/retrievers/self_query/index.cjs +2 -2
  38. package/dist/retrievers/self_query/index.d.ts +2 -0
  39. package/dist/retrievers/self_query/index.js +2 -2
  40. package/dist/runnables/remote.cjs +225 -0
  41. package/dist/runnables/remote.d.ts +28 -0
  42. package/dist/runnables/remote.js +221 -0
  43. package/dist/schema/index.cjs +1 -1
  44. package/dist/schema/index.d.ts +1 -1
  45. package/dist/schema/index.js +1 -1
  46. package/dist/schema/runnable/base.cjs +4 -4
  47. package/dist/schema/runnable/base.d.ts +9 -7
  48. package/dist/schema/runnable/base.js +4 -4
  49. package/dist/schema/runnable/remote.cjs +225 -0
  50. package/dist/schema/runnable/remote.d.ts +28 -0
  51. package/dist/schema/runnable/remote.js +221 -0
  52. package/dist/stores/message/momento.cjs +11 -11
  53. package/dist/stores/message/momento.d.ts +1 -1
  54. package/dist/stores/message/momento.js +1 -1
  55. package/dist/util/time.cjs +14 -0
  56. package/dist/util/time.d.ts +6 -0
  57. package/dist/util/time.js +10 -0
  58. package/dist/vectorstores/cassandra.cjs +212 -0
  59. package/dist/vectorstores/cassandra.d.ts +98 -0
  60. package/dist/vectorstores/cassandra.js +208 -0
  61. package/dist/vectorstores/momento_vector_index.cjs +292 -0
  62. package/dist/vectorstores/momento_vector_index.d.ts +135 -0
  63. package/dist/vectorstores/momento_vector_index.js +265 -0
  64. package/dist/vectorstores/mongodb_atlas.cjs +29 -39
  65. package/dist/vectorstores/mongodb_atlas.js +29 -39
  66. package/dist/vectorstores/supabase.cjs +37 -8
  67. package/dist/vectorstores/supabase.d.ts +28 -1
  68. package/dist/vectorstores/supabase.js +37 -8
  69. package/llms/portkey.cjs +1 -0
  70. package/llms/portkey.d.ts +1 -0
  71. package/llms/portkey.js +1 -0
  72. package/package.json +77 -4
  73. package/runnables/remote.cjs +1 -0
  74. package/runnables/remote.d.ts +1 -0
  75. package/runnables/remote.js +1 -0
  76. package/util/time.cjs +1 -0
  77. package/util/time.d.ts +1 -0
  78. package/util/time.js +1 -0
  79. package/vectorstores/cassandra.cjs +1 -0
  80. package/vectorstores/cassandra.d.ts +1 -0
  81. package/vectorstores/cassandra.js +1 -0
  82. package/vectorstores/momento_vector_index.cjs +1 -0
  83. package/vectorstores/momento_vector_index.d.ts +1 -0
  84. package/vectorstores/momento_vector_index.js +1 -0
@@ -1,15 +1,25 @@
1
- import { BaseChatModelParams } from "../../chat_models/base.js";
1
+ import { BaseChatModel, BaseChatModelParams } from "../../chat_models/base.js";
2
2
  import { CallbackManagerForLLMRun } from "../../callbacks/manager.js";
3
- import { BaseMessage, ChatResult } from "../../schema/index.js";
4
- import { ChatAnthropic, type AnthropicInput } from "../../chat_models/anthropic.js";
3
+ import { BaseMessage, ChatGenerationChunk, ChatResult } from "../../schema/index.js";
4
+ import { type AnthropicInput } from "../../chat_models/anthropic.js";
5
5
  import { BaseFunctionCallOptions } from "../../base_language/index.js";
6
6
  import { StructuredTool } from "../../tools/base.js";
7
7
  export interface ChatAnthropicFunctionsCallOptions extends BaseFunctionCallOptions {
8
8
  tools?: StructuredTool[];
9
9
  }
10
- export declare class AnthropicFunctions extends ChatAnthropic<ChatAnthropicFunctionsCallOptions> {
10
+ export type AnthropicFunctionsInput = Partial<AnthropicInput> & BaseChatModelParams & {
11
+ llm?: BaseChatModel;
12
+ };
13
+ export declare class AnthropicFunctions extends BaseChatModel<ChatAnthropicFunctionsCallOptions> {
14
+ llm: BaseChatModel;
15
+ stopSequences?: string[];
16
+ lc_namespace: string[];
11
17
  static lc_name(): string;
12
- constructor(fields?: Partial<AnthropicInput> & BaseChatModelParams);
18
+ constructor(fields?: AnthropicFunctionsInput);
19
+ invocationParams(): any;
20
+ /** @ignore */
21
+ _identifyingParams(): Record<string, any>;
22
+ _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
13
23
  _generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun | undefined): Promise<ChatResult>;
14
24
  _llmType(): string;
15
25
  /** @ignore */
@@ -1,4 +1,5 @@
1
1
  import { XMLParser } from "fast-xml-parser";
2
+ import { BaseChatModel } from "../../chat_models/base.js";
2
3
  import { AIMessage, SystemMessage, } from "../../schema/index.js";
3
4
  import { ChatAnthropic, DEFAULT_STOP_SEQUENCES, } from "../../chat_models/anthropic.js";
4
5
  import { PromptTemplate } from "../../prompts/prompt.js";
@@ -23,12 +24,43 @@ for the weather in SF you would respond:
23
24
 
24
25
  <tool>search</tool><tool_input><query>weather in SF</query></tool_input>
25
26
  <observation>64 degrees</observation>`);
26
- export class AnthropicFunctions extends ChatAnthropic {
27
+ export class AnthropicFunctions extends BaseChatModel {
27
28
  static lc_name() {
28
29
  return "AnthropicFunctions";
29
30
  }
30
31
  constructor(fields) {
31
32
  super(fields ?? {});
33
+ Object.defineProperty(this, "llm", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ Object.defineProperty(this, "stopSequences", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ Object.defineProperty(this, "lc_namespace", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: ["langchain", "experimental", "chat_models"]
50
+ });
51
+ this.llm = fields?.llm ?? new ChatAnthropic(fields);
52
+ this.stopSequences =
53
+ fields?.stopSequences ?? this.llm.stopSequences;
54
+ }
55
+ invocationParams() {
56
+ return this.llm.invocationParams();
57
+ }
58
+ /** @ignore */
59
+ _identifyingParams() {
60
+ return this.llm._identifyingParams();
61
+ }
62
+ async *_streamResponseChunks(messages, options, runManager) {
63
+ yield* this.llm._streamResponseChunks(messages, options, runManager);
32
64
  }
33
65
  async _generate(messages, options, runManager) {
34
66
  let promptMessages = messages;
@@ -75,7 +107,7 @@ export class AnthropicFunctions extends ChatAnthropic {
75
107
  else if (options.function_call !== undefined) {
76
108
  throw new Error(`If "function_call" is provided, "functions" must also be.`);
77
109
  }
78
- const chatResult = await super._generate(promptMessages, options, runManager);
110
+ const chatResult = await this.llm._generate(promptMessages, options, runManager);
79
111
  const chatGenerationContent = chatResult.generations[0].message.content;
80
112
  if (forced) {
81
113
  const parser = new XMLParser();
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Portkey = exports.getPortkeySession = exports.PortkeySession = void 0;
7
+ const lodash_1 = __importDefault(require("lodash"));
8
+ const portkey_ai_1 = require("portkey-ai");
9
+ const index_js_1 = require("../schema/index.cjs");
10
+ const env_js_1 = require("../util/env.cjs");
11
+ const base_js_1 = require("./base.cjs");
12
+ const readEnv = (env, default_val) => (0, env_js_1.getEnvironmentVariable)(env) ?? default_val;
13
+ class PortkeySession {
14
+ constructor(options = {}) {
15
+ Object.defineProperty(this, "portkey", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ if (!options.apiKey) {
22
+ /* eslint-disable no-param-reassign */
23
+ options.apiKey = readEnv("PORTKEY_API_KEY");
24
+ }
25
+ if (!options.baseURL) {
26
+ /* eslint-disable no-param-reassign */
27
+ options.baseURL = readEnv("PORTKEY_BASE_URL", "https://api.portkey.ai");
28
+ }
29
+ this.portkey = new portkey_ai_1.Portkey({});
30
+ this.portkey.llms = [{}];
31
+ if (!options.apiKey) {
32
+ throw new Error("Set Portkey ApiKey in PORTKEY_API_KEY env variable");
33
+ }
34
+ this.portkey = new portkey_ai_1.Portkey(options);
35
+ }
36
+ }
37
+ exports.PortkeySession = PortkeySession;
38
+ const defaultPortkeySession = [];
39
+ /**
40
+ * Get a session for the Portkey API. If one already exists with the same options,
41
+ * it will be returned. Otherwise, a new session will be created.
42
+ * @param options
43
+ * @returns
44
+ */
45
+ function getPortkeySession(options = {}) {
46
+ let session = defaultPortkeySession.find((session) => lodash_1.default.isEqual(session.options, options))?.session;
47
+ if (!session) {
48
+ session = new PortkeySession(options);
49
+ defaultPortkeySession.push({ session, options });
50
+ }
51
+ return session;
52
+ }
53
+ exports.getPortkeySession = getPortkeySession;
54
+ class Portkey extends base_js_1.BaseLLM {
55
+ constructor(init) {
56
+ super(init ?? {});
57
+ Object.defineProperty(this, "apiKey", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: undefined
62
+ });
63
+ Object.defineProperty(this, "baseURL", {
64
+ enumerable: true,
65
+ configurable: true,
66
+ writable: true,
67
+ value: undefined
68
+ });
69
+ Object.defineProperty(this, "mode", {
70
+ enumerable: true,
71
+ configurable: true,
72
+ writable: true,
73
+ value: undefined
74
+ });
75
+ Object.defineProperty(this, "llms", {
76
+ enumerable: true,
77
+ configurable: true,
78
+ writable: true,
79
+ value: undefined
80
+ });
81
+ Object.defineProperty(this, "session", {
82
+ enumerable: true,
83
+ configurable: true,
84
+ writable: true,
85
+ value: void 0
86
+ });
87
+ this.apiKey = init?.apiKey;
88
+ this.baseURL = init?.baseURL;
89
+ this.mode = init?.mode;
90
+ this.llms = init?.llms;
91
+ this.session = getPortkeySession({
92
+ apiKey: this.apiKey,
93
+ baseURL: this.baseURL,
94
+ llms: this.llms,
95
+ mode: this.mode,
96
+ });
97
+ }
98
+ _llmType() {
99
+ return "portkey";
100
+ }
101
+ async _generate(prompts, options, _) {
102
+ const choices = [];
103
+ for (let i = 0; i < prompts.length; i += 1) {
104
+ const response = await this.session.portkey.completions.create({
105
+ prompt: prompts[i],
106
+ ...options,
107
+ stream: false,
108
+ });
109
+ choices.push(response.choices);
110
+ }
111
+ const generations = choices.map((promptChoices) => promptChoices.map((choice) => ({
112
+ text: choice.text ?? "",
113
+ generationInfo: {
114
+ finishReason: choice.finish_reason,
115
+ logprobs: choice.logprobs,
116
+ },
117
+ })));
118
+ return {
119
+ generations,
120
+ };
121
+ }
122
+ async *_streamResponseChunks(input, options, runManager) {
123
+ const response = await this.session.portkey.completions.create({
124
+ prompt: input,
125
+ ...options,
126
+ stream: true,
127
+ });
128
+ for await (const data of response) {
129
+ const choice = data?.choices[0];
130
+ if (!choice) {
131
+ continue;
132
+ }
133
+ const chunk = new index_js_1.GenerationChunk({
134
+ text: choice.text ?? "",
135
+ generationInfo: {
136
+ finishReason: choice.finish_reason,
137
+ },
138
+ });
139
+ yield chunk;
140
+ void runManager?.handleLLMNewToken(chunk.text ?? "");
141
+ }
142
+ if (options.signal?.aborted) {
143
+ throw new Error("AbortError");
144
+ }
145
+ }
146
+ }
147
+ exports.Portkey = Portkey;
@@ -0,0 +1,33 @@
1
+ import { LLMOptions, Portkey as _Portkey } from "portkey-ai";
2
+ import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
3
+ import { GenerationChunk, LLMResult } from "../schema/index.js";
4
+ import { BaseLLM } from "./base.js";
5
+ interface PortkeyOptions {
6
+ apiKey?: string;
7
+ baseURL?: string;
8
+ mode?: string;
9
+ llms?: [LLMOptions] | null;
10
+ }
11
+ export declare class PortkeySession {
12
+ portkey: _Portkey;
13
+ constructor(options?: PortkeyOptions);
14
+ }
15
+ /**
16
+ * Get a session for the Portkey API. If one already exists with the same options,
17
+ * it will be returned. Otherwise, a new session will be created.
18
+ * @param options
19
+ * @returns
20
+ */
21
+ export declare function getPortkeySession(options?: PortkeyOptions): PortkeySession;
22
+ export declare class Portkey extends BaseLLM {
23
+ apiKey?: string;
24
+ baseURL?: string;
25
+ mode?: string;
26
+ llms?: [LLMOptions] | null;
27
+ session: PortkeySession;
28
+ constructor(init?: Partial<Portkey>);
29
+ _llmType(): string;
30
+ _generate(prompts: string[], options: this["ParsedCallOptions"], _?: CallbackManagerForLLMRun): Promise<LLMResult>;
31
+ _streamResponseChunks(input: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<GenerationChunk>;
32
+ }
33
+ export {};
@@ -0,0 +1,138 @@
1
+ import _ from "lodash";
2
+ import { Portkey as _Portkey } from "portkey-ai";
3
+ import { GenerationChunk } from "../schema/index.js";
4
+ import { getEnvironmentVariable } from "../util/env.js";
5
+ import { BaseLLM } from "./base.js";
6
+ const readEnv = (env, default_val) => getEnvironmentVariable(env) ?? default_val;
7
+ export class PortkeySession {
8
+ constructor(options = {}) {
9
+ Object.defineProperty(this, "portkey", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: void 0
14
+ });
15
+ if (!options.apiKey) {
16
+ /* eslint-disable no-param-reassign */
17
+ options.apiKey = readEnv("PORTKEY_API_KEY");
18
+ }
19
+ if (!options.baseURL) {
20
+ /* eslint-disable no-param-reassign */
21
+ options.baseURL = readEnv("PORTKEY_BASE_URL", "https://api.portkey.ai");
22
+ }
23
+ this.portkey = new _Portkey({});
24
+ this.portkey.llms = [{}];
25
+ if (!options.apiKey) {
26
+ throw new Error("Set Portkey ApiKey in PORTKEY_API_KEY env variable");
27
+ }
28
+ this.portkey = new _Portkey(options);
29
+ }
30
+ }
31
+ const defaultPortkeySession = [];
32
+ /**
33
+ * Get a session for the Portkey API. If one already exists with the same options,
34
+ * it will be returned. Otherwise, a new session will be created.
35
+ * @param options
36
+ * @returns
37
+ */
38
+ export function getPortkeySession(options = {}) {
39
+ let session = defaultPortkeySession.find((session) => _.isEqual(session.options, options))?.session;
40
+ if (!session) {
41
+ session = new PortkeySession(options);
42
+ defaultPortkeySession.push({ session, options });
43
+ }
44
+ return session;
45
+ }
46
+ export class Portkey extends BaseLLM {
47
+ constructor(init) {
48
+ super(init ?? {});
49
+ Object.defineProperty(this, "apiKey", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: undefined
54
+ });
55
+ Object.defineProperty(this, "baseURL", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: undefined
60
+ });
61
+ Object.defineProperty(this, "mode", {
62
+ enumerable: true,
63
+ configurable: true,
64
+ writable: true,
65
+ value: undefined
66
+ });
67
+ Object.defineProperty(this, "llms", {
68
+ enumerable: true,
69
+ configurable: true,
70
+ writable: true,
71
+ value: undefined
72
+ });
73
+ Object.defineProperty(this, "session", {
74
+ enumerable: true,
75
+ configurable: true,
76
+ writable: true,
77
+ value: void 0
78
+ });
79
+ this.apiKey = init?.apiKey;
80
+ this.baseURL = init?.baseURL;
81
+ this.mode = init?.mode;
82
+ this.llms = init?.llms;
83
+ this.session = getPortkeySession({
84
+ apiKey: this.apiKey,
85
+ baseURL: this.baseURL,
86
+ llms: this.llms,
87
+ mode: this.mode,
88
+ });
89
+ }
90
+ _llmType() {
91
+ return "portkey";
92
+ }
93
+ async _generate(prompts, options, _) {
94
+ const choices = [];
95
+ for (let i = 0; i < prompts.length; i += 1) {
96
+ const response = await this.session.portkey.completions.create({
97
+ prompt: prompts[i],
98
+ ...options,
99
+ stream: false,
100
+ });
101
+ choices.push(response.choices);
102
+ }
103
+ const generations = choices.map((promptChoices) => promptChoices.map((choice) => ({
104
+ text: choice.text ?? "",
105
+ generationInfo: {
106
+ finishReason: choice.finish_reason,
107
+ logprobs: choice.logprobs,
108
+ },
109
+ })));
110
+ return {
111
+ generations,
112
+ };
113
+ }
114
+ async *_streamResponseChunks(input, options, runManager) {
115
+ const response = await this.session.portkey.completions.create({
116
+ prompt: input,
117
+ ...options,
118
+ stream: true,
119
+ });
120
+ for await (const data of response) {
121
+ const choice = data?.choices[0];
122
+ if (!choice) {
123
+ continue;
124
+ }
125
+ const chunk = new GenerationChunk({
126
+ text: choice.text ?? "",
127
+ generationInfo: {
128
+ finishReason: choice.finish_reason,
129
+ },
130
+ });
131
+ yield chunk;
132
+ void runManager?.handleLLMNewToken(chunk.text ?? "");
133
+ }
134
+ if (options.signal?.aborted) {
135
+ throw new Error("AbortError");
136
+ }
137
+ }
138
+ }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SageMakerEndpoint = exports.BaseSageMakerContentHandler = void 0;
4
4
  const client_sagemaker_runtime_1 = require("@aws-sdk/client-sagemaker-runtime");
5
+ const index_js_1 = require("../schema/index.cjs");
5
6
  const base_js_1 = require("./base.cjs");
6
7
  /**
7
8
  * A handler class to transform input from LLM to a format that SageMaker
@@ -32,14 +33,12 @@ const base_js_1 = require("./base.cjs");
32
33
  */
33
34
  class BaseSageMakerContentHandler {
34
35
  constructor() {
35
- /** The MIME type of the input data passed to endpoint */
36
36
  Object.defineProperty(this, "contentType", {
37
37
  enumerable: true,
38
38
  configurable: true,
39
39
  writable: true,
40
40
  value: "text/plain"
41
41
  });
42
- /** The MIME type of the response data returned from endpoint */
43
42
  Object.defineProperty(this, "accepts", {
44
43
  enumerable: true,
45
44
  configurable: true,
@@ -51,16 +50,17 @@ class BaseSageMakerContentHandler {
51
50
  exports.BaseSageMakerContentHandler = BaseSageMakerContentHandler;
52
51
  /**
53
52
  * The SageMakerEndpoint class is used to interact with SageMaker
54
- * Inference Endpoint models. It extends the LLM class and overrides the
55
- * _call method to transform the input and output between the LLM and the
56
- * SageMaker endpoint using the provided content handler. The class uses
57
- * AWS client for authentication, which automatically loads credentials.
53
+ * Inference Endpoint models. It uses the AWS client for authentication,
54
+ * which automatically loads credentials.
58
55
  * If a specific credential profile is to be used, the name of the profile
59
56
  * from the ~/.aws/credentials file must be passed. The credentials or
60
57
  * roles used should have the required policies to access the SageMaker
61
58
  * endpoint.
62
59
  */
63
60
  class SageMakerEndpoint extends base_js_1.LLM {
61
+ static lc_name() {
62
+ return "SageMakerEndpoint";
63
+ }
64
64
  get lc_secrets() {
65
65
  return {
66
66
  "clientOptions.credentials.accessKeyId": "AWS_ACCESS_KEY_ID",
@@ -69,39 +69,44 @@ class SageMakerEndpoint extends base_js_1.LLM {
69
69
  };
70
70
  }
71
71
  constructor(fields) {
72
- super(fields ?? {});
72
+ super(fields);
73
73
  Object.defineProperty(this, "endpointName", {
74
74
  enumerable: true,
75
75
  configurable: true,
76
76
  writable: true,
77
77
  value: void 0
78
78
  });
79
- Object.defineProperty(this, "contentHandler", {
79
+ Object.defineProperty(this, "modelKwargs", {
80
80
  enumerable: true,
81
81
  configurable: true,
82
82
  writable: true,
83
83
  value: void 0
84
84
  });
85
- Object.defineProperty(this, "modelKwargs", {
85
+ Object.defineProperty(this, "endpointKwargs", {
86
86
  enumerable: true,
87
87
  configurable: true,
88
88
  writable: true,
89
89
  value: void 0
90
90
  });
91
- Object.defineProperty(this, "endpointKwargs", {
91
+ Object.defineProperty(this, "client", {
92
92
  enumerable: true,
93
93
  configurable: true,
94
94
  writable: true,
95
95
  value: void 0
96
96
  });
97
- Object.defineProperty(this, "client", {
97
+ Object.defineProperty(this, "contentHandler", {
98
98
  enumerable: true,
99
99
  configurable: true,
100
100
  writable: true,
101
101
  value: void 0
102
102
  });
103
- const regionName = fields.clientOptions.region;
104
- if (!regionName) {
103
+ Object.defineProperty(this, "streaming", {
104
+ enumerable: true,
105
+ configurable: true,
106
+ writable: true,
107
+ value: void 0
108
+ });
109
+ if (!fields.clientOptions.region) {
105
110
  throw new Error(`Please pass a "clientOptions" object with a "region" field to the constructor`);
106
111
  }
107
112
  const endpointName = fields?.endpointName;
@@ -116,13 +121,33 @@ class SageMakerEndpoint extends base_js_1.LLM {
116
121
  this.contentHandler = fields.contentHandler;
117
122
  this.endpointKwargs = fields.endpointKwargs;
118
123
  this.modelKwargs = fields.modelKwargs;
124
+ this.streaming = fields.streaming ?? false;
119
125
  this.client = new client_sagemaker_runtime_1.SageMakerRuntimeClient(fields.clientOptions);
120
126
  }
121
127
  _llmType() {
122
128
  return "sagemaker_endpoint";
123
129
  }
130
+ /**
131
+ * Calls the SageMaker endpoint and retrieves the result.
132
+ * @param {string} prompt The input prompt.
133
+ * @param {this["ParsedCallOptions"]} options Parsed call options.
134
+ * @param {CallbackManagerForLLMRun} _runManager Optional run manager.
135
+ * @returns {Promise<string>} A promise that resolves to the generated string.
136
+ */
124
137
  /** @ignore */
125
- async _call(prompt, options) {
138
+ async _call(prompt, options, _runManager) {
139
+ return this.streaming
140
+ ? await this.streamingCall(prompt, options)
141
+ : await this.noStreamingCall(prompt, options);
142
+ }
143
+ async streamingCall(prompt, options) {
144
+ const chunks = [];
145
+ for await (const chunk of this._streamResponseChunks(prompt, options)) {
146
+ chunks.push(chunk.text);
147
+ }
148
+ return chunks.join("");
149
+ }
150
+ async noStreamingCall(prompt, options) {
126
151
  const body = await this.contentHandler.transformInput(prompt, this.modelKwargs ?? {});
127
152
  const { contentType, accepts } = this.contentHandler;
128
153
  const response = await this.caller.call(() => this.client.send(new client_sagemaker_runtime_1.InvokeEndpointCommand({
@@ -137,5 +162,42 @@ class SageMakerEndpoint extends base_js_1.LLM {
137
162
  }
138
163
  return this.contentHandler.transformOutput(response.Body);
139
164
  }
165
+ /**
166
+ * Streams response chunks from the SageMaker endpoint.
167
+ * @param {string} prompt The input prompt.
168
+ * @param {this["ParsedCallOptions"]} options Parsed call options.
169
+ * @returns {AsyncGenerator<GenerationChunk>} An asynchronous generator yielding generation chunks.
170
+ */
171
+ async *_streamResponseChunks(prompt, options) {
172
+ const body = await this.contentHandler.transformInput(prompt, this.modelKwargs ?? {});
173
+ const { contentType, accepts } = this.contentHandler;
174
+ const stream = await this.caller.call(() => this.client.send(new client_sagemaker_runtime_1.InvokeEndpointWithResponseStreamCommand({
175
+ EndpointName: this.endpointName,
176
+ Body: body,
177
+ ContentType: contentType,
178
+ Accept: accepts,
179
+ ...this.endpointKwargs,
180
+ }), { abortSignal: options.signal }));
181
+ if (!stream.Body) {
182
+ throw new Error("Inference result missing Body");
183
+ }
184
+ for await (const chunk of stream.Body) {
185
+ if (chunk.PayloadPart && chunk.PayloadPart.Bytes) {
186
+ yield new index_js_1.GenerationChunk({
187
+ text: await this.contentHandler.transformOutput(chunk.PayloadPart.Bytes),
188
+ generationInfo: {
189
+ ...chunk,
190
+ response: undefined,
191
+ },
192
+ });
193
+ }
194
+ else if (chunk.InternalStreamFailure) {
195
+ throw new Error(chunk.InternalStreamFailure.message);
196
+ }
197
+ else if (chunk.ModelStreamError) {
198
+ throw new Error(chunk.ModelStreamError.message);
199
+ }
200
+ }
201
+ }
140
202
  }
141
203
  exports.SageMakerEndpoint = SageMakerEndpoint;