langchain 0.0.187 → 0.0.188

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.
@@ -158,7 +158,7 @@ class BedrockChat extends base_js_1.SimpleChatModel {
158
158
  value: true
159
159
  });
160
160
  this.model = fields?.model ?? this.model;
161
- const allowedModels = ["ai21", "anthropic", "amazon", "cohere"];
161
+ const allowedModels = ["ai21", "anthropic", "amazon", "cohere", "meta"];
162
162
  if (!allowedModels.includes(this.model.split(".")[0])) {
163
163
  throw new Error(`Unknown model: '${this.model}', only these are supported: ${allowedModels}`);
164
164
  }
@@ -260,7 +260,7 @@ class BedrockChat extends base_js_1.SimpleChatModel {
260
260
  const provider = this.model.split(".")[0];
261
261
  const service = "bedrock-runtime";
262
262
  const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
263
- const bedrockMethod = provider === "anthropic" || provider === "cohere"
263
+ const bedrockMethod = provider === "anthropic" || provider === "cohere" || provider === "meta"
264
264
  ? "invoke-with-response-stream"
265
265
  : "invoke";
266
266
  const response = await this._signedFetch(messages, options, {
@@ -271,7 +271,9 @@ class BedrockChat extends base_js_1.SimpleChatModel {
271
271
  if (response.status < 200 || response.status >= 300) {
272
272
  throw Error(`Failed to access underlying url '${endpointHost}': got ${response.status} ${response.statusText}: ${await response.text()}`);
273
273
  }
274
- if (provider === "anthropic" || provider === "cohere") {
274
+ if (provider === "anthropic" ||
275
+ provider === "cohere" ||
276
+ provider === "meta") {
275
277
  const reader = response.body?.getReader();
276
278
  const decoder = new TextDecoder();
277
279
  for await (const chunk of this._readChunks(reader)) {
@@ -153,7 +153,7 @@ export class BedrockChat extends SimpleChatModel {
153
153
  value: true
154
154
  });
155
155
  this.model = fields?.model ?? this.model;
156
- const allowedModels = ["ai21", "anthropic", "amazon", "cohere"];
156
+ const allowedModels = ["ai21", "anthropic", "amazon", "cohere", "meta"];
157
157
  if (!allowedModels.includes(this.model.split(".")[0])) {
158
158
  throw new Error(`Unknown model: '${this.model}', only these are supported: ${allowedModels}`);
159
159
  }
@@ -255,7 +255,7 @@ export class BedrockChat extends SimpleChatModel {
255
255
  const provider = this.model.split(".")[0];
256
256
  const service = "bedrock-runtime";
257
257
  const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
258
- const bedrockMethod = provider === "anthropic" || provider === "cohere"
258
+ const bedrockMethod = provider === "anthropic" || provider === "cohere" || provider === "meta"
259
259
  ? "invoke-with-response-stream"
260
260
  : "invoke";
261
261
  const response = await this._signedFetch(messages, options, {
@@ -266,7 +266,9 @@ export class BedrockChat extends SimpleChatModel {
266
266
  if (response.status < 200 || response.status >= 300) {
267
267
  throw Error(`Failed to access underlying url '${endpointHost}': got ${response.status} ${response.statusText}: ${await response.text()}`);
268
268
  }
269
- if (provider === "anthropic" || provider === "cohere") {
269
+ if (provider === "anthropic" ||
270
+ provider === "cohere" ||
271
+ provider === "meta") {
270
272
  const reader = response.body?.getReader();
271
273
  const decoder = new TextDecoder();
272
274
  for await (const chunk of this._readChunks(reader)) {
@@ -27,6 +27,12 @@ class CohereEmbeddings extends base_js_1.Embeddings {
27
27
  writable: true,
28
28
  value: 48
29
29
  });
30
+ Object.defineProperty(this, "inputType", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
30
36
  Object.defineProperty(this, "apiKey", {
31
37
  enumerable: true,
32
38
  configurable: true,
@@ -45,6 +51,7 @@ class CohereEmbeddings extends base_js_1.Embeddings {
45
51
  }
46
52
  this.modelName = fieldsWithDefaults?.modelName ?? this.modelName;
47
53
  this.batchSize = fieldsWithDefaults?.batchSize ?? this.batchSize;
54
+ this.inputType = fieldsWithDefaults?.inputType;
48
55
  this.apiKey = apiKey;
49
56
  }
50
57
  /**
@@ -58,14 +65,15 @@ class CohereEmbeddings extends base_js_1.Embeddings {
58
65
  const batchRequests = batches.map((batch) => this.embeddingWithRetry({
59
66
  model: this.modelName,
60
67
  texts: batch,
68
+ inputType: this.inputType,
61
69
  }));
62
70
  const batchResponses = await Promise.all(batchRequests);
63
71
  const embeddings = [];
64
72
  for (let i = 0; i < batchResponses.length; i += 1) {
65
73
  const batch = batches[i];
66
- const { body: batchResponse } = batchResponses[i];
74
+ const { embeddings: batchResponse } = batchResponses[i];
67
75
  for (let j = 0; j < batch.length; j += 1) {
68
- embeddings.push(batchResponse.embeddings[j]);
76
+ embeddings.push(batchResponse[j]);
69
77
  }
70
78
  }
71
79
  return embeddings;
@@ -77,11 +85,11 @@ class CohereEmbeddings extends base_js_1.Embeddings {
77
85
  */
78
86
  async embedQuery(text) {
79
87
  await this.maybeInitClient();
80
- const { body } = await this.embeddingWithRetry({
88
+ const { embeddings } = await this.embeddingWithRetry({
81
89
  model: this.modelName,
82
90
  texts: [text],
83
91
  });
84
- return body.embeddings[0];
92
+ return embeddings[0];
85
93
  }
86
94
  /**
87
95
  * Generates embeddings with retry capabilities.
@@ -97,16 +105,17 @@ class CohereEmbeddings extends base_js_1.Embeddings {
97
105
  */
98
106
  async maybeInitClient() {
99
107
  if (!this.client) {
100
- const { cohere } = await CohereEmbeddings.imports();
101
- this.client = cohere;
102
- this.client.init(this.apiKey);
108
+ const { CohereClient } = await CohereEmbeddings.imports();
109
+ this.client = new CohereClient({
110
+ token: this.apiKey,
111
+ });
103
112
  }
104
113
  }
105
114
  /** @ignore */
106
115
  static async imports() {
107
116
  try {
108
- const { default: cohere } = await import("cohere-ai");
109
- return { cohere };
117
+ const { CohereClient } = await import("cohere-ai");
118
+ return { CohereClient };
110
119
  }
111
120
  catch (e) {
112
121
  throw new Error("Please install cohere-ai as a dependency with, e.g. `yarn add cohere-ai`");
@@ -10,6 +10,17 @@ export interface CohereEmbeddingsParams extends EmbeddingsParams {
10
10
  * limited by the Cohere API to a maximum of 96.
11
11
  */
12
12
  batchSize?: number;
13
+ /**
14
+ * Specifies the type of input you're giving to the model.
15
+ * Not required for older versions of the embedding models (i.e. anything lower than v3),
16
+ * but is required for more recent versions (i.e. anything bigger than v2).
17
+ *
18
+ * * `search_document` - Use this when you encode documents for embeddings that you store in a vector database for search use-cases.
19
+ * * `search_query` - Use this when you query your vector DB to find relevant documents.
20
+ * * `classification` - Use this when you use the embeddings as an input to a text classifier.
21
+ * * `clustering` - Use this when you want to cluster the embeddings.
22
+ */
23
+ inputType?: string;
13
24
  }
14
25
  /**
15
26
  * A class for generating embeddings using the Cohere API.
@@ -17,6 +28,7 @@ export interface CohereEmbeddingsParams extends EmbeddingsParams {
17
28
  export declare class CohereEmbeddings extends Embeddings implements CohereEmbeddingsParams {
18
29
  modelName: string;
19
30
  batchSize: number;
31
+ inputType: string | undefined;
20
32
  private apiKey;
21
33
  private client;
22
34
  /**
@@ -51,6 +63,6 @@ export declare class CohereEmbeddings extends Embeddings implements CohereEmbedd
51
63
  private maybeInitClient;
52
64
  /** @ignore */
53
65
  static imports(): Promise<{
54
- cohere: typeof import("cohere-ai");
66
+ CohereClient: typeof import("cohere-ai").CohereClient;
55
67
  }>;
56
68
  }
@@ -24,6 +24,12 @@ export class CohereEmbeddings extends Embeddings {
24
24
  writable: true,
25
25
  value: 48
26
26
  });
27
+ Object.defineProperty(this, "inputType", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
27
33
  Object.defineProperty(this, "apiKey", {
28
34
  enumerable: true,
29
35
  configurable: true,
@@ -42,6 +48,7 @@ export class CohereEmbeddings extends Embeddings {
42
48
  }
43
49
  this.modelName = fieldsWithDefaults?.modelName ?? this.modelName;
44
50
  this.batchSize = fieldsWithDefaults?.batchSize ?? this.batchSize;
51
+ this.inputType = fieldsWithDefaults?.inputType;
45
52
  this.apiKey = apiKey;
46
53
  }
47
54
  /**
@@ -55,14 +62,15 @@ export class CohereEmbeddings extends Embeddings {
55
62
  const batchRequests = batches.map((batch) => this.embeddingWithRetry({
56
63
  model: this.modelName,
57
64
  texts: batch,
65
+ inputType: this.inputType,
58
66
  }));
59
67
  const batchResponses = await Promise.all(batchRequests);
60
68
  const embeddings = [];
61
69
  for (let i = 0; i < batchResponses.length; i += 1) {
62
70
  const batch = batches[i];
63
- const { body: batchResponse } = batchResponses[i];
71
+ const { embeddings: batchResponse } = batchResponses[i];
64
72
  for (let j = 0; j < batch.length; j += 1) {
65
- embeddings.push(batchResponse.embeddings[j]);
73
+ embeddings.push(batchResponse[j]);
66
74
  }
67
75
  }
68
76
  return embeddings;
@@ -74,11 +82,11 @@ export class CohereEmbeddings extends Embeddings {
74
82
  */
75
83
  async embedQuery(text) {
76
84
  await this.maybeInitClient();
77
- const { body } = await this.embeddingWithRetry({
85
+ const { embeddings } = await this.embeddingWithRetry({
78
86
  model: this.modelName,
79
87
  texts: [text],
80
88
  });
81
- return body.embeddings[0];
89
+ return embeddings[0];
82
90
  }
83
91
  /**
84
92
  * Generates embeddings with retry capabilities.
@@ -94,16 +102,17 @@ export class CohereEmbeddings extends Embeddings {
94
102
  */
95
103
  async maybeInitClient() {
96
104
  if (!this.client) {
97
- const { cohere } = await CohereEmbeddings.imports();
98
- this.client = cohere;
99
- this.client.init(this.apiKey);
105
+ const { CohereClient } = await CohereEmbeddings.imports();
106
+ this.client = new CohereClient({
107
+ token: this.apiKey,
108
+ });
100
109
  }
101
110
  }
102
111
  /** @ignore */
103
112
  static async imports() {
104
113
  try {
105
- const { default: cohere } = await import("cohere-ai");
106
- return { cohere };
114
+ const { CohereClient } = await import("cohere-ai");
115
+ return { CohereClient };
107
116
  }
108
117
  catch (e) {
109
118
  throw new Error("Please install cohere-ai as a dependency with, e.g. `yarn add cohere-ai`");
@@ -80,7 +80,7 @@ class OllamaFunctions extends base_js_1.BaseChatModel {
80
80
  else if (functions.length === 0) {
81
81
  functions.push(this.defaultResponseFunction);
82
82
  }
83
- const defaultContent = await TOOL_SYSTEM_PROMPT.format({
83
+ const defaultContent = await this.toolSystemPrompt.format({
84
84
  tools: JSON.stringify(functions, null, 2),
85
85
  });
86
86
  const systemMessage = new index_js_1.SystemMessage({ content: defaultContent });
@@ -61,7 +61,7 @@ export declare class OllamaFunctions extends BaseChatModel<ChatOllamaFunctionsCa
61
61
  tfs_z: number | undefined;
62
62
  top_k: number | undefined;
63
63
  top_p: number | undefined;
64
- typical_p: number | undefined;
64
+ typical_p: number | undefined; /** @ignore */
65
65
  use_mlock: boolean | undefined;
66
66
  use_mmap: boolean | undefined;
67
67
  vocab_only: boolean | undefined;
@@ -77,7 +77,7 @@ export class OllamaFunctions extends BaseChatModel {
77
77
  else if (functions.length === 0) {
78
78
  functions.push(this.defaultResponseFunction);
79
79
  }
80
- const defaultContent = await TOOL_SYSTEM_PROMPT.format({
80
+ const defaultContent = await this.toolSystemPrompt.format({
81
81
  tools: JSON.stringify(functions, null, 2),
82
82
  });
83
83
  const systemMessage = new SystemMessage({ content: defaultContent });
@@ -117,7 +117,7 @@ class Bedrock extends base_js_1.LLM {
117
117
  value: true
118
118
  });
119
119
  this.model = fields?.model ?? this.model;
120
- const allowedModels = ["ai21", "anthropic", "amazon", "cohere"];
120
+ const allowedModels = ["ai21", "anthropic", "amazon", "cohere", "meta"];
121
121
  if (!allowedModels.includes(this.model.split(".")[0])) {
122
122
  throw new Error(`Unknown model: '${this.model}', only these are supported: ${allowedModels}`);
123
123
  }
@@ -223,7 +223,7 @@ class Bedrock extends base_js_1.LLM {
223
223
  }
224
224
  async *_streamResponseChunks(prompt, options, runManager) {
225
225
  const provider = this.model.split(".")[0];
226
- const bedrockMethod = provider === "anthropic" || provider === "cohere"
226
+ const bedrockMethod = provider === "anthropic" || provider === "cohere" || provider === "meta"
227
227
  ? "invoke-with-response-stream"
228
228
  : "invoke";
229
229
  const service = "bedrock-runtime";
@@ -237,7 +237,9 @@ class Bedrock extends base_js_1.LLM {
237
237
  if (response.status < 200 || response.status >= 300) {
238
238
  throw Error(`Failed to access underlying url '${endpointHost}': got ${response.status} ${response.statusText}: ${await response.text()}`);
239
239
  }
240
- if (provider === "anthropic" || provider === "cohere") {
240
+ if (provider === "anthropic" ||
241
+ provider === "cohere" ||
242
+ provider === "meta") {
241
243
  const reader = response.body?.getReader();
242
244
  const decoder = new TextDecoder();
243
245
  for await (const chunk of this._readChunks(reader)) {
@@ -114,7 +114,7 @@ export class Bedrock extends LLM {
114
114
  value: true
115
115
  });
116
116
  this.model = fields?.model ?? this.model;
117
- const allowedModels = ["ai21", "anthropic", "amazon", "cohere"];
117
+ const allowedModels = ["ai21", "anthropic", "amazon", "cohere", "meta"];
118
118
  if (!allowedModels.includes(this.model.split(".")[0])) {
119
119
  throw new Error(`Unknown model: '${this.model}', only these are supported: ${allowedModels}`);
120
120
  }
@@ -220,7 +220,7 @@ export class Bedrock extends LLM {
220
220
  }
221
221
  async *_streamResponseChunks(prompt, options, runManager) {
222
222
  const provider = this.model.split(".")[0];
223
- const bedrockMethod = provider === "anthropic" || provider === "cohere"
223
+ const bedrockMethod = provider === "anthropic" || provider === "cohere" || provider === "meta"
224
224
  ? "invoke-with-response-stream"
225
225
  : "invoke";
226
226
  const service = "bedrock-runtime";
@@ -234,7 +234,9 @@ export class Bedrock extends LLM {
234
234
  if (response.status < 200 || response.status >= 300) {
235
235
  throw Error(`Failed to access underlying url '${endpointHost}': got ${response.status} ${response.statusText}: ${await response.text()}`);
236
236
  }
237
- if (provider === "anthropic" || provider === "cohere") {
237
+ if (provider === "anthropic" ||
238
+ provider === "cohere" ||
239
+ provider === "meta") {
238
240
  const reader = response.body?.getReader();
239
241
  const decoder = new TextDecoder();
240
242
  for await (const chunk of this._readChunks(reader)) {
@@ -67,18 +67,20 @@ class Cohere extends base_js_1.LLM {
67
67
  }
68
68
  /** @ignore */
69
69
  async _call(prompt, options) {
70
- const { cohere } = await Cohere.imports();
71
- cohere.init(this.apiKey);
70
+ const { CohereClient } = await Cohere.imports();
71
+ const cohere = new CohereClient({
72
+ token: this.apiKey,
73
+ });
72
74
  // Hit the `generate` endpoint on the `large` model
73
75
  const generateResponse = await this.caller.callWithOptions({ signal: options.signal }, cohere.generate.bind(cohere), {
74
76
  prompt,
75
77
  model: this.model,
76
- max_tokens: this.maxTokens,
78
+ maxTokens: this.maxTokens,
77
79
  temperature: this.temperature,
78
- end_sequences: options.stop,
80
+ endSequences: options.stop,
79
81
  });
80
82
  try {
81
- return generateResponse.body.generations[0].text;
83
+ return generateResponse.generations[0].text;
82
84
  }
83
85
  catch {
84
86
  console.log(generateResponse);
@@ -88,8 +90,8 @@ class Cohere extends base_js_1.LLM {
88
90
  /** @ignore */
89
91
  static async imports() {
90
92
  try {
91
- const { default: cohere } = await import("cohere-ai");
92
- return { cohere };
93
+ const { CohereClient } = await import("cohere-ai");
94
+ return { CohereClient };
93
95
  }
94
96
  catch (e) {
95
97
  throw new Error("Please install cohere-ai as a dependency with, e.g. `yarn add cohere-ai`");
@@ -36,6 +36,6 @@ export declare class Cohere extends LLM implements CohereInput {
36
36
  _call(prompt: string, options: this["ParsedCallOptions"]): Promise<string>;
37
37
  /** @ignore */
38
38
  static imports(): Promise<{
39
- cohere: typeof import("cohere-ai");
39
+ CohereClient: typeof import("cohere-ai").CohereClient;
40
40
  }>;
41
41
  }
@@ -64,18 +64,20 @@ export class Cohere extends LLM {
64
64
  }
65
65
  /** @ignore */
66
66
  async _call(prompt, options) {
67
- const { cohere } = await Cohere.imports();
68
- cohere.init(this.apiKey);
67
+ const { CohereClient } = await Cohere.imports();
68
+ const cohere = new CohereClient({
69
+ token: this.apiKey,
70
+ });
69
71
  // Hit the `generate` endpoint on the `large` model
70
72
  const generateResponse = await this.caller.callWithOptions({ signal: options.signal }, cohere.generate.bind(cohere), {
71
73
  prompt,
72
74
  model: this.model,
73
- max_tokens: this.maxTokens,
75
+ maxTokens: this.maxTokens,
74
76
  temperature: this.temperature,
75
- end_sequences: options.stop,
77
+ endSequences: options.stop,
76
78
  });
77
79
  try {
78
- return generateResponse.body.generations[0].text;
80
+ return generateResponse.generations[0].text;
79
81
  }
80
82
  catch {
81
83
  console.log(generateResponse);
@@ -85,8 +87,8 @@ export class Cohere extends LLM {
85
87
  /** @ignore */
86
88
  static async imports() {
87
89
  try {
88
- const { default: cohere } = await import("cohere-ai");
89
- return { cohere };
90
+ const { CohereClient } = await import("cohere-ai");
91
+ return { CohereClient };
90
92
  }
91
93
  catch (e) {
92
94
  throw new Error("Please install cohere-ai as a dependency with, e.g. `yarn add cohere-ai`");
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpResponseOutputParser = void 0;
4
+ const output_parser_js_1 = require("../schema/output_parser.cjs");
5
+ /**
6
+ * OutputParser that formats chunks emitted from an LLM for different HTTP content types.
7
+ */
8
+ class HttpResponseOutputParser extends output_parser_js_1.BaseTransformOutputParser {
9
+ static lc_name() {
10
+ return "HttpResponseOutputParser";
11
+ }
12
+ constructor(fields) {
13
+ super(fields);
14
+ Object.defineProperty(this, "lc_namespace", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: ["langchain", "output_parser"]
19
+ });
20
+ Object.defineProperty(this, "lc_serializable", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: true
25
+ });
26
+ Object.defineProperty(this, "outputParser", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: new output_parser_js_1.StringOutputParser()
31
+ });
32
+ Object.defineProperty(this, "contentType", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: "text/plain"
37
+ });
38
+ this.outputParser = fields?.outputParser ?? this.outputParser;
39
+ this.contentType = fields?.contentType ?? this.contentType;
40
+ }
41
+ async *_transform(inputGenerator) {
42
+ for await (const chunk of this.outputParser._transform(inputGenerator)) {
43
+ if (typeof chunk === "string") {
44
+ yield this.parse(chunk);
45
+ }
46
+ else {
47
+ yield this.parse(JSON.stringify(chunk));
48
+ }
49
+ }
50
+ if (this.contentType === "text/event-stream") {
51
+ const encoder = new TextEncoder();
52
+ yield encoder.encode(`event: end\n\n`);
53
+ }
54
+ }
55
+ /**
56
+ * Parses a string output from an LLM call. This method is meant to be
57
+ * implemented by subclasses to define how a string output from an LLM
58
+ * should be parsed.
59
+ * @param text The string output from an LLM call.
60
+ * @param callbacks Optional callbacks.
61
+ * @returns A promise of the parsed output.
62
+ */
63
+ async parse(text) {
64
+ const chunk = await this.outputParser.parse(text);
65
+ let parsedChunk;
66
+ if (typeof chunk === "string") {
67
+ parsedChunk = chunk;
68
+ }
69
+ else {
70
+ parsedChunk = JSON.stringify(chunk);
71
+ }
72
+ const encoder = new TextEncoder();
73
+ if (this.contentType === "text/event-stream") {
74
+ return encoder.encode(`event: data\ndata: ${parsedChunk}\n\n`);
75
+ }
76
+ return encoder.encode(parsedChunk);
77
+ }
78
+ getFormatInstructions() {
79
+ return "";
80
+ }
81
+ }
82
+ exports.HttpResponseOutputParser = HttpResponseOutputParser;
@@ -0,0 +1,28 @@
1
+ import { BaseMessage } from "../schema/index.js";
2
+ import { BaseTransformOutputParser } from "../schema/output_parser.js";
3
+ export type HttpResponseOutputParserInput = {
4
+ outputParser?: BaseTransformOutputParser;
5
+ contentType?: "text/plain" | "text/event-stream";
6
+ };
7
+ /**
8
+ * OutputParser that formats chunks emitted from an LLM for different HTTP content types.
9
+ */
10
+ export declare class HttpResponseOutputParser extends BaseTransformOutputParser<Uint8Array> {
11
+ static lc_name(): string;
12
+ lc_namespace: string[];
13
+ lc_serializable: boolean;
14
+ outputParser: BaseTransformOutputParser;
15
+ contentType: "text/plain" | "text/event-stream";
16
+ constructor(fields?: HttpResponseOutputParserInput);
17
+ _transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<Uint8Array>;
18
+ /**
19
+ * Parses a string output from an LLM call. This method is meant to be
20
+ * implemented by subclasses to define how a string output from an LLM
21
+ * should be parsed.
22
+ * @param text The string output from an LLM call.
23
+ * @param callbacks Optional callbacks.
24
+ * @returns A promise of the parsed output.
25
+ */
26
+ parse(text: string): Promise<Uint8Array>;
27
+ getFormatInstructions(): string;
28
+ }
@@ -0,0 +1,78 @@
1
+ import { BaseTransformOutputParser, StringOutputParser, } from "../schema/output_parser.js";
2
+ /**
3
+ * OutputParser that formats chunks emitted from an LLM for different HTTP content types.
4
+ */
5
+ export class HttpResponseOutputParser extends BaseTransformOutputParser {
6
+ static lc_name() {
7
+ return "HttpResponseOutputParser";
8
+ }
9
+ constructor(fields) {
10
+ super(fields);
11
+ Object.defineProperty(this, "lc_namespace", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: ["langchain", "output_parser"]
16
+ });
17
+ Object.defineProperty(this, "lc_serializable", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: true
22
+ });
23
+ Object.defineProperty(this, "outputParser", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: new StringOutputParser()
28
+ });
29
+ Object.defineProperty(this, "contentType", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: "text/plain"
34
+ });
35
+ this.outputParser = fields?.outputParser ?? this.outputParser;
36
+ this.contentType = fields?.contentType ?? this.contentType;
37
+ }
38
+ async *_transform(inputGenerator) {
39
+ for await (const chunk of this.outputParser._transform(inputGenerator)) {
40
+ if (typeof chunk === "string") {
41
+ yield this.parse(chunk);
42
+ }
43
+ else {
44
+ yield this.parse(JSON.stringify(chunk));
45
+ }
46
+ }
47
+ if (this.contentType === "text/event-stream") {
48
+ const encoder = new TextEncoder();
49
+ yield encoder.encode(`event: end\n\n`);
50
+ }
51
+ }
52
+ /**
53
+ * Parses a string output from an LLM call. This method is meant to be
54
+ * implemented by subclasses to define how a string output from an LLM
55
+ * should be parsed.
56
+ * @param text The string output from an LLM call.
57
+ * @param callbacks Optional callbacks.
58
+ * @returns A promise of the parsed output.
59
+ */
60
+ async parse(text) {
61
+ const chunk = await this.outputParser.parse(text);
62
+ let parsedChunk;
63
+ if (typeof chunk === "string") {
64
+ parsedChunk = chunk;
65
+ }
66
+ else {
67
+ parsedChunk = JSON.stringify(chunk);
68
+ }
69
+ const encoder = new TextEncoder();
70
+ if (this.contentType === "text/event-stream") {
71
+ return encoder.encode(`event: data\ndata: ${parsedChunk}\n\n`);
72
+ }
73
+ return encoder.encode(parsedChunk);
74
+ }
75
+ getFormatInstructions() {
76
+ return "";
77
+ }
78
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JsonOutputToolsParser = exports.JsonKeyOutputFunctionsParser = exports.JsonOutputFunctionsParser = exports.OutputFunctionsParser = exports.CustomListOutputParser = exports.RouterOutputParser = exports.CombiningOutputParser = exports.OutputFixingParser = exports.JsonMarkdownStructuredOutputParser = exports.AsymmetricStructuredOutputParser = exports.StructuredOutputParser = exports.RegexParser = exports.CommaSeparatedListOutputParser = exports.ListOutputParser = void 0;
3
+ exports.HttpResponseOutputParser = exports.JsonOutputToolsParser = exports.JsonKeyOutputFunctionsParser = exports.JsonOutputFunctionsParser = exports.OutputFunctionsParser = exports.CustomListOutputParser = exports.RouterOutputParser = exports.CombiningOutputParser = exports.OutputFixingParser = exports.JsonMarkdownStructuredOutputParser = exports.AsymmetricStructuredOutputParser = exports.StructuredOutputParser = exports.RegexParser = exports.CommaSeparatedListOutputParser = exports.ListOutputParser = void 0;
4
4
  var list_js_1 = require("./list.cjs");
5
5
  Object.defineProperty(exports, "ListOutputParser", { enumerable: true, get: function () { return list_js_1.ListOutputParser; } });
6
6
  Object.defineProperty(exports, "CommaSeparatedListOutputParser", { enumerable: true, get: function () { return list_js_1.CommaSeparatedListOutputParser; } });
@@ -24,3 +24,5 @@ Object.defineProperty(exports, "JsonOutputFunctionsParser", { enumerable: true,
24
24
  Object.defineProperty(exports, "JsonKeyOutputFunctionsParser", { enumerable: true, get: function () { return openai_functions_js_1.JsonKeyOutputFunctionsParser; } });
25
25
  var openai_tools_js_1 = require("../output_parsers/openai_tools.cjs");
26
26
  Object.defineProperty(exports, "JsonOutputToolsParser", { enumerable: true, get: function () { return openai_tools_js_1.JsonOutputToolsParser; } });
27
+ var http_response_js_1 = require("./http_response.cjs");
28
+ Object.defineProperty(exports, "HttpResponseOutputParser", { enumerable: true, get: function () { return http_response_js_1.HttpResponseOutputParser; } });
@@ -7,3 +7,4 @@ export { RouterOutputParser, type RouterOutputParserInput } from "./router.js";
7
7
  export { CustomListOutputParser } from "./list.js";
8
8
  export { type FunctionParameters, OutputFunctionsParser, JsonOutputFunctionsParser, JsonKeyOutputFunctionsParser, } from "../output_parsers/openai_functions.js";
9
9
  export { type ParsedToolCall, JsonOutputToolsParser, } from "../output_parsers/openai_tools.js";
10
+ export { HttpResponseOutputParser, type HttpResponseOutputParserInput, } from "./http_response.js";
@@ -7,3 +7,4 @@ export { RouterOutputParser } from "./router.js";
7
7
  export { CustomListOutputParser } from "./list.js";
8
8
  export { OutputFunctionsParser, JsonOutputFunctionsParser, JsonKeyOutputFunctionsParser, } from "../output_parsers/openai_functions.js";
9
9
  export { JsonOutputToolsParser, } from "../output_parsers/openai_tools.js";
10
+ export { HttpResponseOutputParser, } from "./http_response.js";
@@ -134,18 +134,16 @@ class JsonOutputFunctionsParser extends output_parser_js_1.BaseCumulativeTransfo
134
134
  if (!result) {
135
135
  throw new Error(`No result from "OutputFunctionsParser" ${JSON.stringify(generations)}`);
136
136
  }
137
- const parsedResult = JSON.parse(result);
137
+ return this.parse(result);
138
+ }
139
+ async parse(text) {
140
+ const parsedResult = JSON.parse(text);
138
141
  if (this.argsOnly) {
139
142
  return parsedResult;
140
143
  }
141
144
  parsedResult.arguments = JSON.parse(parsedResult.arguments);
142
145
  return parsedResult;
143
146
  }
144
- // This method would be called by the default implementation of `parse_result`
145
- // but we're overriding that method so it's not needed.
146
- async parse(_text) {
147
- throw new Error("Not implemented.");
148
- }
149
147
  getFormatInstructions() {
150
148
  return "";
151
149
  }
@@ -49,7 +49,7 @@ export declare class JsonOutputFunctionsParser extends BaseCumulativeTransformOu
49
49
  * @returns A JSON object representation of the function call or its arguments.
50
50
  */
51
51
  parseResult(generations: Generation[] | ChatGeneration[]): Promise<object>;
52
- parse(_text: string): Promise<object>;
52
+ parse(text: string): Promise<object>;
53
53
  getFormatInstructions(): string;
54
54
  }
55
55
  /**
@@ -130,18 +130,16 @@ export class JsonOutputFunctionsParser extends BaseCumulativeTransformOutputPars
130
130
  if (!result) {
131
131
  throw new Error(`No result from "OutputFunctionsParser" ${JSON.stringify(generations)}`);
132
132
  }
133
- const parsedResult = JSON.parse(result);
133
+ return this.parse(result);
134
+ }
135
+ async parse(text) {
136
+ const parsedResult = JSON.parse(text);
134
137
  if (this.argsOnly) {
135
138
  return parsedResult;
136
139
  }
137
140
  parsedResult.arguments = JSON.parse(parsedResult.arguments);
138
141
  return parsedResult;
139
142
  }
140
- // This method would be called by the default implementation of `parse_result`
141
- // but we're overriding that method so it's not needed.
142
- async parse(_text) {
143
- throw new Error("Not implemented.");
144
- }
145
143
  getFormatInstructions() {
146
144
  return "";
147
145
  }
@@ -77,7 +77,7 @@ export declare abstract class BaseOutputParser<T = unknown> extends BaseLLMOutpu
77
77
  * Class to parse the output of an LLM call that also allows streaming inputs.
78
78
  */
79
79
  export declare abstract class BaseTransformOutputParser<T = unknown> extends BaseOutputParser<T> {
80
- protected _transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<T>;
80
+ _transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<T>;
81
81
  /**
82
82
  * Transforms an asynchronous generator of input into an asynchronous
83
83
  * generator of parsed output.
@@ -100,7 +100,7 @@ export declare abstract class BaseCumulativeTransformOutputParser<T = unknown> e
100
100
  constructor(fields?: BaseCumulativeTransformOutputParserInput);
101
101
  protected abstract _diff(prev: any | undefined, next: any): any;
102
102
  abstract parsePartialResult(generations: Generation[] | ChatGeneration[]): Promise<T | undefined>;
103
- protected _transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<T>;
103
+ _transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<T>;
104
104
  }
105
105
  /**
106
106
  * OutputParser that parses LLMResult into the top likely string.
@@ -25,6 +25,11 @@ class BedrockLLMInputOutputAdapter {
25
25
  inputBody.temperature = temperature;
26
26
  inputBody.stopSequences = stopSequences;
27
27
  }
28
+ else if (provider === "meta") {
29
+ inputBody.prompt = prompt;
30
+ inputBody.max_gen_len = maxTokens;
31
+ inputBody.temperature = temperature;
32
+ }
28
33
  else if (provider === "amazon") {
29
34
  inputBody.inputText = prompt;
30
35
  inputBody.textGenerationConfig = {
@@ -60,6 +65,9 @@ class BedrockLLMInputOutputAdapter {
60
65
  else if (provider === "cohere") {
61
66
  return responseBody?.generations?.[0]?.text ?? responseBody?.text ?? "";
62
67
  }
68
+ else if (provider === "meta") {
69
+ return responseBody.generation;
70
+ }
63
71
  // I haven't been able to get a response with more than one result in it.
64
72
  return responseBody.results?.[0]?.outputText;
65
73
  }
@@ -22,6 +22,11 @@ export class BedrockLLMInputOutputAdapter {
22
22
  inputBody.temperature = temperature;
23
23
  inputBody.stopSequences = stopSequences;
24
24
  }
25
+ else if (provider === "meta") {
26
+ inputBody.prompt = prompt;
27
+ inputBody.max_gen_len = maxTokens;
28
+ inputBody.temperature = temperature;
29
+ }
25
30
  else if (provider === "amazon") {
26
31
  inputBody.inputText = prompt;
27
32
  inputBody.textGenerationConfig = {
@@ -57,6 +62,9 @@ export class BedrockLLMInputOutputAdapter {
57
62
  else if (provider === "cohere") {
58
63
  return responseBody?.generations?.[0]?.text ?? responseBody?.text ?? "";
59
64
  }
65
+ else if (provider === "meta") {
66
+ return responseBody.generation;
67
+ }
60
68
  // I haven't been able to get a response with more than one result in it.
61
69
  return responseBody.results?.[0]?.outputText;
62
70
  }
@@ -177,10 +177,13 @@ class OpenAPISpec {
177
177
  static getCleanedOperationId(operation, path, method) {
178
178
  let { operationId } = operation;
179
179
  if (operationId === undefined) {
180
- const updatedPath = path.replace(/[^a-zA-Z0-9]/, "_");
180
+ const updatedPath = path.replaceAll(/[^a-zA-Z0-9]/, "_");
181
181
  operationId = `${updatedPath.startsWith("/") ? updatedPath.slice(1) : updatedPath}_${method}`;
182
182
  }
183
- return operationId.replace("-", "_").replace(".", "_").replace("/", "_");
183
+ return operationId
184
+ .replaceAll("-", "_")
185
+ .replaceAll(".", "_")
186
+ .replaceAll("/", "_");
184
187
  }
185
188
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
186
189
  static alertUnsupportedSpec(document) {
@@ -151,10 +151,13 @@ export class OpenAPISpec {
151
151
  static getCleanedOperationId(operation, path, method) {
152
152
  let { operationId } = operation;
153
153
  if (operationId === undefined) {
154
- const updatedPath = path.replace(/[^a-zA-Z0-9]/, "_");
154
+ const updatedPath = path.replaceAll(/[^a-zA-Z0-9]/, "_");
155
155
  operationId = `${updatedPath.startsWith("/") ? updatedPath.slice(1) : updatedPath}_${method}`;
156
156
  }
157
- return operationId.replace("-", "_").replace(".", "_").replace("/", "_");
157
+ return operationId
158
+ .replaceAll("-", "_")
159
+ .replaceAll(".", "_")
160
+ .replaceAll("/", "_");
158
161
  }
159
162
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
160
163
  static alertUnsupportedSpec(document) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.0.187",
3
+ "version": "0.0.188",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -902,7 +902,7 @@
902
902
  "closevector-common": "0.1.0-alpha.1",
903
903
  "closevector-node": "0.1.0-alpha.10",
904
904
  "closevector-web": "0.1.0-alpha.15",
905
- "cohere-ai": ">=6.0.0",
905
+ "cohere-ai": "^7.2.0",
906
906
  "convex": "^1.3.1",
907
907
  "d3-dsv": "^2.0.0",
908
908
  "dotenv": "^16.0.3",
@@ -954,6 +954,7 @@
954
954
  "sqlite3": "^5.1.4",
955
955
  "srt-parser-2": "^1.2.2",
956
956
  "ts-jest": "^29.1.0",
957
+ "ts-morph": "^20.0.0",
957
958
  "typeorm": "^0.3.12",
958
959
  "typescript": "^5.0.0",
959
960
  "typesense": "^1.5.3",
@@ -1019,7 +1020,7 @@
1019
1020
  "closevector-common": "0.1.0-alpha.1",
1020
1021
  "closevector-node": "0.1.0-alpha.10",
1021
1022
  "closevector-web": "0.1.0-alpha.16",
1022
- "cohere-ai": ">=6.0.0",
1023
+ "cohere-ai": "^7.2.0",
1023
1024
  "convex": "^1.3.1",
1024
1025
  "d3-dsv": "^2.0.0",
1025
1026
  "epub2": "^3.0.1",