langchain 0.0.178 → 0.0.180

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/chains/combine_documents/reduce.cjs +1 -0
  2. package/chains/combine_documents/reduce.d.ts +1 -0
  3. package/chains/combine_documents/reduce.js +1 -0
  4. package/chat_models/bedrock/web.cjs +1 -0
  5. package/chat_models/bedrock/web.d.ts +1 -0
  6. package/chat_models/bedrock/web.js +1 -0
  7. package/chat_models/bedrock.cjs +1 -1
  8. package/chat_models/bedrock.d.ts +1 -1
  9. package/chat_models/bedrock.js +1 -1
  10. package/dist/cache/base.d.ts +1 -1
  11. package/dist/callbacks/index.d.ts +1 -1
  12. package/dist/chains/combine_documents/reduce.cjs +69 -0
  13. package/dist/chains/combine_documents/reduce.d.ts +28 -0
  14. package/dist/chains/combine_documents/reduce.js +64 -0
  15. package/dist/chat_models/baiduwenxin.cjs +16 -2
  16. package/dist/chat_models/baiduwenxin.js +16 -2
  17. package/dist/chat_models/bedrock/index.cjs +24 -0
  18. package/dist/chat_models/bedrock/index.d.ts +12 -0
  19. package/dist/chat_models/bedrock/index.js +18 -0
  20. package/dist/chat_models/{bedrock.cjs → bedrock/web.cjs} +44 -15
  21. package/dist/chat_models/{bedrock.d.ts → bedrock/web.d.ts} +14 -5
  22. package/dist/chat_models/{bedrock.js → bedrock/web.js} +42 -13
  23. package/dist/chat_models/iflytek_xinghuo/index.cjs +1 -1
  24. package/dist/chat_models/iflytek_xinghuo/index.js +1 -1
  25. package/dist/llms/bedrock/index.cjs +17 -0
  26. package/dist/llms/bedrock/index.d.ts +7 -0
  27. package/dist/llms/bedrock/index.js +13 -0
  28. package/dist/llms/{bedrock.cjs → bedrock/web.cjs} +46 -12
  29. package/dist/llms/{bedrock.d.ts → bedrock/web.d.ts} +17 -4
  30. package/dist/llms/{bedrock.js → bedrock/web.js} +46 -12
  31. package/dist/load/import_constants.cjs +3 -0
  32. package/dist/load/import_constants.js +3 -0
  33. package/dist/load/import_map.cjs +4 -2
  34. package/dist/load/import_map.d.ts +2 -0
  35. package/dist/load/import_map.js +2 -0
  36. package/dist/output_parsers/json.cjs +77 -0
  37. package/dist/output_parsers/json.d.ts +1 -0
  38. package/dist/output_parsers/json.js +73 -0
  39. package/dist/output_parsers/openai_functions.cjs +37 -2
  40. package/dist/output_parsers/openai_functions.d.ts +10 -5
  41. package/dist/output_parsers/openai_functions.js +38 -3
  42. package/dist/schema/index.cjs +33 -1
  43. package/dist/schema/index.d.ts +3 -1
  44. package/dist/schema/index.js +31 -0
  45. package/dist/schema/output_parser.cjs +63 -3
  46. package/dist/schema/output_parser.d.ts +16 -1
  47. package/dist/schema/output_parser.js +59 -0
  48. package/dist/schema/prompt_template.cjs +33 -0
  49. package/dist/schema/prompt_template.d.ts +12 -0
  50. package/dist/schema/prompt_template.js +29 -0
  51. package/dist/storage/convex.d.ts +21 -0
  52. package/dist/stores/message/cassandra.cjs +135 -0
  53. package/dist/stores/message/cassandra.d.ts +44 -0
  54. package/dist/stores/message/cassandra.js +131 -0
  55. package/dist/stores/message/convex.d.ts +21 -0
  56. package/dist/util/bedrock.cjs +13 -1
  57. package/dist/util/bedrock.d.ts +5 -2
  58. package/dist/util/bedrock.js +13 -1
  59. package/dist/util/fast-json-patch/index.cjs +1 -0
  60. package/dist/util/fast-json-patch/index.d.ts +1 -0
  61. package/dist/util/fast-json-patch/index.js +1 -0
  62. package/dist/util/fast-json-patch/src/duplex.cjs +237 -0
  63. package/dist/util/fast-json-patch/src/duplex.d.ts +23 -0
  64. package/dist/util/fast-json-patch/src/duplex.js +230 -0
  65. package/dist/vectorstores/cassandra.cjs +197 -47
  66. package/dist/vectorstores/cassandra.d.ts +47 -4
  67. package/dist/vectorstores/cassandra.js +197 -47
  68. package/dist/vectorstores/convex.d.ts +21 -0
  69. package/llms/bedrock/web.cjs +1 -0
  70. package/llms/bedrock/web.d.ts +1 -0
  71. package/llms/bedrock/web.js +1 -0
  72. package/llms/bedrock.cjs +1 -1
  73. package/llms/bedrock.d.ts +1 -1
  74. package/llms/bedrock.js +1 -1
  75. package/package.json +41 -1
  76. package/schema/prompt_template.cjs +1 -0
  77. package/schema/prompt_template.d.ts +1 -0
  78. package/schema/prompt_template.js +1 -0
  79. package/stores/message/cassandra.cjs +1 -0
  80. package/stores/message/cassandra.d.ts +1 -0
  81. package/stores/message/cassandra.js +1 -0
  82. package/dist/schema/runnable/remote.cjs +0 -225
  83. package/dist/schema/runnable/remote.d.ts +0 -28
  84. package/dist/schema/runnable/remote.js +0 -221
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatDocument = void 0;
4
+ /**
5
+ * Formats a document using a given prompt template.
6
+ *
7
+ * @async
8
+ * @param {Document} document - The document to format.
9
+ * @param {BasePromptTemplate} prompt - The prompt template to use for formatting.
10
+ * @returns {Promise<string>} A Promise that resolves to the formatted document as a string.
11
+ * @throws {Error} If the document is missing required metadata variables specified in the prompt template.
12
+ */
13
+ const formatDocument = async (document, prompt) => {
14
+ const baseInfo = {
15
+ pageContent: document.pageContent,
16
+ ...document.metadata,
17
+ };
18
+ const variables = new Set(prompt.inputVariables);
19
+ const requiredMetadata = new Set(prompt.inputVariables
20
+ .map((v) => (v !== "pageContent" ? v : null))
21
+ .filter((v) => v !== null));
22
+ const missingMetadata = [];
23
+ for (const variable of variables) {
24
+ if (!(variable in baseInfo) && variable !== "pageContent") {
25
+ missingMetadata.push(variable);
26
+ }
27
+ }
28
+ if (missingMetadata.length) {
29
+ throw new Error(`Document prompt requires documents to have metadata variables: ${JSON.stringify(requiredMetadata)}. Received document with missing metadata: ${JSON.stringify(missingMetadata)}`);
30
+ }
31
+ return prompt.format(baseInfo);
32
+ };
33
+ exports.formatDocument = formatDocument;
@@ -0,0 +1,12 @@
1
+ import { Document } from "../document.js";
2
+ import { BasePromptTemplate } from "../prompts/base.js";
3
+ /**
4
+ * Formats a document using a given prompt template.
5
+ *
6
+ * @async
7
+ * @param {Document} document - The document to format.
8
+ * @param {BasePromptTemplate} prompt - The prompt template to use for formatting.
9
+ * @returns {Promise<string>} A Promise that resolves to the formatted document as a string.
10
+ * @throws {Error} If the document is missing required metadata variables specified in the prompt template.
11
+ */
12
+ export declare const formatDocument: (document: Document, prompt: BasePromptTemplate) => Promise<string>;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Formats a document using a given prompt template.
3
+ *
4
+ * @async
5
+ * @param {Document} document - The document to format.
6
+ * @param {BasePromptTemplate} prompt - The prompt template to use for formatting.
7
+ * @returns {Promise<string>} A Promise that resolves to the formatted document as a string.
8
+ * @throws {Error} If the document is missing required metadata variables specified in the prompt template.
9
+ */
10
+ export const formatDocument = async (document, prompt) => {
11
+ const baseInfo = {
12
+ pageContent: document.pageContent,
13
+ ...document.metadata,
14
+ };
15
+ const variables = new Set(prompt.inputVariables);
16
+ const requiredMetadata = new Set(prompt.inputVariables
17
+ .map((v) => (v !== "pageContent" ? v : null))
18
+ .filter((v) => v !== null));
19
+ const missingMetadata = [];
20
+ for (const variable of variables) {
21
+ if (!(variable in baseInfo) && variable !== "pageContent") {
22
+ missingMetadata.push(variable);
23
+ }
24
+ }
25
+ if (missingMetadata.length) {
26
+ throw new Error(`Document prompt requires documents to have metadata variables: ${JSON.stringify(requiredMetadata)}. Received document with missing metadata: ${JSON.stringify(missingMetadata)}`);
27
+ }
28
+ return prompt.format(baseInfo);
29
+ };
@@ -21,12 +21,33 @@ export type ConvexKVStoreConfig<DataModel extends GenericDataModel, TableName ex
21
21
  key: string;
22
22
  }>> = {
23
23
  readonly ctx: GenericActionCtx<DataModel>;
24
+ /**
25
+ * Defaults to "cache"
26
+ */
24
27
  readonly table?: TableName;
28
+ /**
29
+ * Defaults to "byKey"
30
+ */
25
31
  readonly index?: IndexName;
32
+ /**
33
+ * Defaults to "key"
34
+ */
26
35
  readonly keyField?: KeyFieldName;
36
+ /**
37
+ * Defaults to "value"
38
+ */
27
39
  readonly valueField?: ValueFieldName;
40
+ /**
41
+ * Defaults to `internal.langchain.db.upsert`
42
+ */
28
43
  readonly upsert?: UpsertMutation;
44
+ /**
45
+ * Defaults to `internal.langchain.db.lookup`
46
+ */
29
47
  readonly lookup?: LookupQuery;
48
+ /**
49
+ * Defaults to `internal.langchain.db.deleteMany`
50
+ */
30
51
  readonly deleteMany?: DeleteManyMutation;
31
52
  };
32
53
  /**
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CassandraChatMessageHistory = void 0;
4
+ const cassandra_driver_1 = require("cassandra-driver");
5
+ const index_js_1 = require("../../schema/index.cjs");
6
+ const utils_js_1 = require("./utils.cjs");
7
+ /**
8
+ * Class for storing chat message history within Cassandra. It extends the
9
+ * BaseListChatMessageHistory class and provides methods to get, add, and
10
+ * clear messages.
11
+ */
12
+ class CassandraChatMessageHistory extends index_js_1.BaseListChatMessageHistory {
13
+ constructor(options) {
14
+ super();
15
+ Object.defineProperty(this, "lc_namespace", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: ["langchain", "stores", "message", "cassandra"]
20
+ });
21
+ Object.defineProperty(this, "keyspace", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ Object.defineProperty(this, "table", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ Object.defineProperty(this, "client", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ Object.defineProperty(this, "sessionId", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ Object.defineProperty(this, "tableExists", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "options", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ Object.defineProperty(this, "queries", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: void 0
62
+ });
63
+ this.client = new cassandra_driver_1.Client(options);
64
+ this.keyspace = options.keyspace;
65
+ this.table = options.table;
66
+ this.sessionId = options.sessionId;
67
+ this.tableExists = false;
68
+ this.options = options;
69
+ }
70
+ /**
71
+ * Method to get all the messages stored in the Cassandra database.
72
+ * @returns Array of stored BaseMessage instances.
73
+ */
74
+ async getMessages() {
75
+ await this.ensureTable();
76
+ const resultSet = await this.client.execute(this.queries.select, [this.sessionId], { prepare: true });
77
+ const storedMessages = resultSet.rows.map((row) => ({
78
+ type: row.message_type,
79
+ data: JSON.parse(row.data),
80
+ }));
81
+ const baseMessages = (0, utils_js_1.mapStoredMessagesToChatMessages)(storedMessages);
82
+ return baseMessages;
83
+ }
84
+ /**
85
+ * Method to add a new message to the Cassandra database.
86
+ * @param message The BaseMessage instance to add.
87
+ * @returns A promise that resolves when the message has been added.
88
+ */
89
+ async addMessage(message) {
90
+ await this.ensureTable();
91
+ const messages = (0, utils_js_1.mapChatMessagesToStoredMessages)([message]);
92
+ const { type, data } = messages[0];
93
+ return this.client
94
+ .execute(this.queries.insert, [this.sessionId, type, JSON.stringify(data)], { prepare: true, ...this.options })
95
+ .then(() => { });
96
+ }
97
+ /**
98
+ * Method to clear all the messages from the Cassandra database.
99
+ * @returns A promise that resolves when all messages have been cleared.
100
+ */
101
+ async clear() {
102
+ await this.ensureTable();
103
+ return this.client
104
+ .execute(this.queries.delete, [this.sessionId], {
105
+ prepare: true,
106
+ ...this.options,
107
+ })
108
+ .then(() => { });
109
+ }
110
+ /**
111
+ * Method to initialize the Cassandra database.
112
+ * @returns Promise that resolves when the database has been initialized.
113
+ */
114
+ async ensureTable() {
115
+ if (this.tableExists) {
116
+ return;
117
+ }
118
+ await this.client.execute(`
119
+ CREATE TABLE IF NOT EXISTS ${this.keyspace}.${this.table} (
120
+ session_id text,
121
+ message_ts timestamp,
122
+ message_type text,
123
+ data text,
124
+ PRIMARY KEY ((session_id), message_ts)
125
+ );
126
+ `);
127
+ this.queries = {
128
+ insert: `INSERT INTO ${this.keyspace}.${this.table} (session_id, message_ts, message_type, data) VALUES (?, toTimestamp(now()), ?, ?);`,
129
+ select: `SELECT message_type, data FROM ${this.keyspace}.${this.table} WHERE session_id = ?;`,
130
+ delete: `DELETE FROM ${this.keyspace}.${this.table} WHERE session_id = ?;`,
131
+ };
132
+ this.tableExists = true;
133
+ }
134
+ }
135
+ exports.CassandraChatMessageHistory = CassandraChatMessageHistory;
@@ -0,0 +1,44 @@
1
+ import { DseClientOptions } from "cassandra-driver";
2
+ import { BaseMessage, BaseListChatMessageHistory } from "../../schema/index.js";
3
+ export interface CassandraChatMessageHistoryOptions extends DseClientOptions {
4
+ keyspace: string;
5
+ table: string;
6
+ sessionId: string;
7
+ }
8
+ /**
9
+ * Class for storing chat message history within Cassandra. It extends the
10
+ * BaseListChatMessageHistory class and provides methods to get, add, and
11
+ * clear messages.
12
+ */
13
+ export declare class CassandraChatMessageHistory extends BaseListChatMessageHistory {
14
+ lc_namespace: string[];
15
+ private keyspace;
16
+ private table;
17
+ private client;
18
+ private sessionId;
19
+ private tableExists;
20
+ private options;
21
+ private queries;
22
+ constructor(options: CassandraChatMessageHistoryOptions);
23
+ /**
24
+ * Method to get all the messages stored in the Cassandra database.
25
+ * @returns Array of stored BaseMessage instances.
26
+ */
27
+ getMessages(): Promise<BaseMessage[]>;
28
+ /**
29
+ * Method to add a new message to the Cassandra database.
30
+ * @param message The BaseMessage instance to add.
31
+ * @returns A promise that resolves when the message has been added.
32
+ */
33
+ addMessage(message: BaseMessage): Promise<void>;
34
+ /**
35
+ * Method to clear all the messages from the Cassandra database.
36
+ * @returns A promise that resolves when all messages have been cleared.
37
+ */
38
+ clear(): Promise<void>;
39
+ /**
40
+ * Method to initialize the Cassandra database.
41
+ * @returns Promise that resolves when the database has been initialized.
42
+ */
43
+ private ensureTable;
44
+ }
@@ -0,0 +1,131 @@
1
+ import { Client } from "cassandra-driver";
2
+ import { BaseListChatMessageHistory, } from "../../schema/index.js";
3
+ import { mapChatMessagesToStoredMessages, mapStoredMessagesToChatMessages, } from "./utils.js";
4
+ /**
5
+ * Class for storing chat message history within Cassandra. It extends the
6
+ * BaseListChatMessageHistory class and provides methods to get, add, and
7
+ * clear messages.
8
+ */
9
+ export class CassandraChatMessageHistory extends BaseListChatMessageHistory {
10
+ constructor(options) {
11
+ super();
12
+ Object.defineProperty(this, "lc_namespace", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: ["langchain", "stores", "message", "cassandra"]
17
+ });
18
+ Object.defineProperty(this, "keyspace", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "table", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ Object.defineProperty(this, "client", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ Object.defineProperty(this, "sessionId", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ Object.defineProperty(this, "tableExists", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: void 0
47
+ });
48
+ Object.defineProperty(this, "options", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: void 0
53
+ });
54
+ Object.defineProperty(this, "queries", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: void 0
59
+ });
60
+ this.client = new Client(options);
61
+ this.keyspace = options.keyspace;
62
+ this.table = options.table;
63
+ this.sessionId = options.sessionId;
64
+ this.tableExists = false;
65
+ this.options = options;
66
+ }
67
+ /**
68
+ * Method to get all the messages stored in the Cassandra database.
69
+ * @returns Array of stored BaseMessage instances.
70
+ */
71
+ async getMessages() {
72
+ await this.ensureTable();
73
+ const resultSet = await this.client.execute(this.queries.select, [this.sessionId], { prepare: true });
74
+ const storedMessages = resultSet.rows.map((row) => ({
75
+ type: row.message_type,
76
+ data: JSON.parse(row.data),
77
+ }));
78
+ const baseMessages = mapStoredMessagesToChatMessages(storedMessages);
79
+ return baseMessages;
80
+ }
81
+ /**
82
+ * Method to add a new message to the Cassandra database.
83
+ * @param message The BaseMessage instance to add.
84
+ * @returns A promise that resolves when the message has been added.
85
+ */
86
+ async addMessage(message) {
87
+ await this.ensureTable();
88
+ const messages = mapChatMessagesToStoredMessages([message]);
89
+ const { type, data } = messages[0];
90
+ return this.client
91
+ .execute(this.queries.insert, [this.sessionId, type, JSON.stringify(data)], { prepare: true, ...this.options })
92
+ .then(() => { });
93
+ }
94
+ /**
95
+ * Method to clear all the messages from the Cassandra database.
96
+ * @returns A promise that resolves when all messages have been cleared.
97
+ */
98
+ async clear() {
99
+ await this.ensureTable();
100
+ return this.client
101
+ .execute(this.queries.delete, [this.sessionId], {
102
+ prepare: true,
103
+ ...this.options,
104
+ })
105
+ .then(() => { });
106
+ }
107
+ /**
108
+ * Method to initialize the Cassandra database.
109
+ * @returns Promise that resolves when the database has been initialized.
110
+ */
111
+ async ensureTable() {
112
+ if (this.tableExists) {
113
+ return;
114
+ }
115
+ await this.client.execute(`
116
+ CREATE TABLE IF NOT EXISTS ${this.keyspace}.${this.table} (
117
+ session_id text,
118
+ message_ts timestamp,
119
+ message_type text,
120
+ data text,
121
+ PRIMARY KEY ((session_id), message_ts)
122
+ );
123
+ `);
124
+ this.queries = {
125
+ insert: `INSERT INTO ${this.keyspace}.${this.table} (session_id, message_ts, message_type, data) VALUES (?, toTimestamp(now()), ?, ?);`,
126
+ select: `SELECT message_type, data FROM ${this.keyspace}.${this.table} WHERE session_id = ?;`,
127
+ delete: `DELETE FROM ${this.keyspace}.${this.table} WHERE session_id = ?;`,
128
+ };
129
+ this.tableExists = true;
130
+ }
131
+ }
@@ -21,12 +21,33 @@ export type ConvexChatMessageHistoryInput<DataModel extends GenericDataModel, Ta
21
21
  }> = any> = {
22
22
  readonly ctx: GenericActionCtx<DataModel>;
23
23
  readonly sessionId: DocumentByName<DataModel, TableName>[SessionIdFieldName];
24
+ /**
25
+ * Defaults to "messages"
26
+ */
24
27
  readonly table?: TableName;
28
+ /**
29
+ * Defaults to "bySessionId"
30
+ */
25
31
  readonly index?: IndexName;
32
+ /**
33
+ * Defaults to "sessionId"
34
+ */
26
35
  readonly sessionIdField?: SessionIdFieldName;
36
+ /**
37
+ * Defaults to "message"
38
+ */
27
39
  readonly messageTextFieldName?: MessageTextFieldName;
40
+ /**
41
+ * Defaults to `internal.langchain.db.insert`
42
+ */
28
43
  readonly insert?: InsertMutation;
44
+ /**
45
+ * Defaults to `internal.langchain.db.lookup`
46
+ */
29
47
  readonly lookup?: LookupQuery;
48
+ /**
49
+ * Defaults to `internal.langchain.db.deleteMany`
50
+ */
30
51
  readonly deleteMany?: DeleteManyMutation;
31
52
  };
32
53
  export declare class ConvexChatMessageHistory<DataModel extends GenericDataModel, SessionIdFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>> = "sessionId", TableName extends TableNamesInDataModel<DataModel> = "messages", IndexName extends VectorIndexNames<NamedTableInfo<DataModel, TableName>> = "bySessionId", MessageTextFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>> = "message", InsertMutation extends FunctionReference<"mutation", "internal", {
@@ -11,7 +11,7 @@ class BedrockLLMInputOutputAdapter {
11
11
  /** Adapter class to prepare the inputs from Langchain to a format
12
12
  that LLM model expects. Also, provides a helper function to extract
13
13
  the generated text from the model response. */
14
- static prepareInput(provider, prompt, maxTokens = 50, temperature = 0, stopSequences = undefined, modelKwargs = {}) {
14
+ static prepareInput(provider, prompt, maxTokens = 50, temperature = 0, stopSequences = undefined, modelKwargs = {}, bedrockMethod = "invoke") {
15
15
  const inputBody = {};
16
16
  if (provider === "anthropic") {
17
17
  inputBody.prompt = prompt;
@@ -32,6 +32,15 @@ class BedrockLLMInputOutputAdapter {
32
32
  temperature,
33
33
  };
34
34
  }
35
+ else if (provider === "cohere") {
36
+ inputBody.prompt = prompt;
37
+ inputBody.max_tokens = maxTokens;
38
+ inputBody.temperature = temperature;
39
+ inputBody.stop_sequences = stopSequences;
40
+ if (bedrockMethod === "invoke-with-response-stream") {
41
+ inputBody.stream = true;
42
+ }
43
+ }
35
44
  return { ...inputBody, ...modelKwargs };
36
45
  }
37
46
  /**
@@ -48,6 +57,9 @@ class BedrockLLMInputOutputAdapter {
48
57
  else if (provider === "ai21") {
49
58
  return responseBody?.completions?.[0]?.data?.text ?? "";
50
59
  }
60
+ else if (provider === "cohere") {
61
+ return responseBody?.generations?.[0]?.text ?? responseBody?.text ?? "";
62
+ }
51
63
  // I haven't been able to get a response with more than one result in it.
52
64
  return responseBody.results?.[0]?.outputText;
53
65
  }
@@ -29,7 +29,10 @@ export interface BaseBedrockInput {
29
29
  endpointUrl?: string;
30
30
  /** Override the default endpoint hostname. */
31
31
  endpointHost?: string;
32
- /** Optional additional stop sequences to pass to the model. Currently only supported for Anthropic and AI21. */
32
+ /**
33
+ * Optional additional stop sequences to pass to the model. Currently only supported for Anthropic and AI21.
34
+ * @deprecated Use .bind({ "stop": [...] }) instead
35
+ * */
33
36
  stopSequences?: string[];
34
37
  /** Additional kwargs to pass to the model. */
35
38
  modelKwargs?: Record<string, unknown>;
@@ -49,7 +52,7 @@ export declare class BedrockLLMInputOutputAdapter {
49
52
  /** Adapter class to prepare the inputs from Langchain to a format
50
53
  that LLM model expects. Also, provides a helper function to extract
51
54
  the generated text from the model response. */
52
- static prepareInput(provider: string, prompt: string, maxTokens?: number, temperature?: number, stopSequences?: string[] | undefined, modelKwargs?: Record<string, unknown>): Dict;
55
+ static prepareInput(provider: string, prompt: string, maxTokens?: number, temperature?: number, stopSequences?: string[] | undefined, modelKwargs?: Record<string, unknown>, bedrockMethod?: "invoke" | "invoke-with-response-stream"): Dict;
53
56
  /**
54
57
  * Extracts the generated text from the service response.
55
58
  * @param provider The provider name.
@@ -8,7 +8,7 @@ export class BedrockLLMInputOutputAdapter {
8
8
  /** Adapter class to prepare the inputs from Langchain to a format
9
9
  that LLM model expects. Also, provides a helper function to extract
10
10
  the generated text from the model response. */
11
- static prepareInput(provider, prompt, maxTokens = 50, temperature = 0, stopSequences = undefined, modelKwargs = {}) {
11
+ static prepareInput(provider, prompt, maxTokens = 50, temperature = 0, stopSequences = undefined, modelKwargs = {}, bedrockMethod = "invoke") {
12
12
  const inputBody = {};
13
13
  if (provider === "anthropic") {
14
14
  inputBody.prompt = prompt;
@@ -29,6 +29,15 @@ export class BedrockLLMInputOutputAdapter {
29
29
  temperature,
30
30
  };
31
31
  }
32
+ else if (provider === "cohere") {
33
+ inputBody.prompt = prompt;
34
+ inputBody.max_tokens = maxTokens;
35
+ inputBody.temperature = temperature;
36
+ inputBody.stop_sequences = stopSequences;
37
+ if (bedrockMethod === "invoke-with-response-stream") {
38
+ inputBody.stream = true;
39
+ }
40
+ }
32
41
  return { ...inputBody, ...modelKwargs };
33
42
  }
34
43
  /**
@@ -45,6 +54,9 @@ export class BedrockLLMInputOutputAdapter {
45
54
  else if (provider === "ai21") {
46
55
  return responseBody?.completions?.[0]?.data?.text ?? "";
47
56
  }
57
+ else if (provider === "cohere") {
58
+ return responseBody?.generations?.[0]?.text ?? responseBody?.text ?? "";
59
+ }
48
60
  // I haven't been able to get a response with more than one result in it.
49
61
  return responseBody.results?.[0]?.outputText;
50
62
  }
@@ -28,6 +28,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.unescapePathComponent = exports.escapePathComponent = exports.deepClone = exports.JsonPatchError = void 0;
30
30
  __exportStar(require("./src/core.cjs"), exports);
31
+ __exportStar(require("./src/duplex.cjs"), exports);
31
32
  var helpers_js_1 = require("./src/helpers.cjs");
32
33
  Object.defineProperty(exports, "JsonPatchError", { enumerable: true, get: function () { return helpers_js_1.PatchError; } });
33
34
  Object.defineProperty(exports, "deepClone", { enumerable: true, get: function () { return helpers_js_1._deepClone; } });
@@ -1,4 +1,5 @@
1
1
  export * from "./src/core.js";
2
+ export * from "./src/duplex.js";
2
3
  export { PatchError as JsonPatchError, _deepClone as deepClone, escapePathComponent, unescapePathComponent, } from "./src/helpers.js";
3
4
  /**
4
5
  * Default export for backwards compat
@@ -1,4 +1,5 @@
1
1
  export * from "./src/core.js";
2
+ export * from "./src/duplex.js";
2
3
  export { PatchError as JsonPatchError, _deepClone as deepClone, escapePathComponent, unescapePathComponent, } from "./src/helpers.js";
3
4
  /**
4
5
  * Default export for backwards compat