langchain 0.0.176 → 0.0.178
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/chat_models/iflytek_xinghuo/web.cjs +1 -0
- package/chat_models/iflytek_xinghuo/web.d.ts +1 -0
- package/chat_models/iflytek_xinghuo/web.js +1 -0
- package/chat_models/iflytek_xinghuo.cjs +1 -0
- package/chat_models/iflytek_xinghuo.d.ts +1 -0
- package/chat_models/iflytek_xinghuo.js +1 -0
- package/dist/chat_models/bedrock.cjs +25 -4
- package/dist/chat_models/bedrock.d.ts +2 -1
- package/dist/chat_models/bedrock.js +25 -4
- package/dist/chat_models/cloudflare_workersai.cjs +70 -24
- package/dist/chat_models/cloudflare_workersai.d.ts +6 -2
- package/dist/chat_models/cloudflare_workersai.js +71 -25
- package/dist/chat_models/iflytek_xinghuo/common.cjs +335 -0
- package/dist/chat_models/iflytek_xinghuo/common.d.ts +165 -0
- package/dist/chat_models/iflytek_xinghuo/common.js +331 -0
- package/dist/chat_models/iflytek_xinghuo/index.cjs +35 -0
- package/dist/chat_models/iflytek_xinghuo/index.d.ts +5 -0
- package/dist/chat_models/iflytek_xinghuo/index.js +28 -0
- package/dist/chat_models/iflytek_xinghuo/web.cjs +30 -0
- package/dist/chat_models/iflytek_xinghuo/web.d.ts +5 -0
- package/dist/chat_models/iflytek_xinghuo/web.js +26 -0
- package/dist/chat_models/llama_cpp.cjs +31 -79
- package/dist/chat_models/llama_cpp.d.ts +15 -58
- package/dist/chat_models/llama_cpp.js +32 -80
- package/dist/chat_models/openai.cjs +91 -6
- package/dist/chat_models/openai.d.ts +10 -0
- package/dist/chat_models/openai.js +91 -6
- package/dist/embeddings/hf.cjs +10 -1
- package/dist/embeddings/hf.d.ts +4 -2
- package/dist/embeddings/hf.js +10 -1
- package/dist/embeddings/llama_cpp.cjs +67 -0
- package/dist/embeddings/llama_cpp.d.ts +26 -0
- package/dist/embeddings/llama_cpp.js +63 -0
- package/dist/embeddings/ollama.cjs +7 -1
- package/dist/embeddings/ollama.js +7 -1
- package/dist/graphs/neo4j_graph.cjs +36 -5
- package/dist/graphs/neo4j_graph.js +14 -3
- package/dist/llms/bedrock.cjs +25 -3
- package/dist/llms/bedrock.d.ts +2 -1
- package/dist/llms/bedrock.js +25 -3
- package/dist/llms/cloudflare_workersai.cjs +59 -13
- package/dist/llms/cloudflare_workersai.d.ts +9 -3
- package/dist/llms/cloudflare_workersai.js +59 -13
- package/dist/llms/hf.cjs +10 -1
- package/dist/llms/hf.d.ts +3 -0
- package/dist/llms/hf.js +10 -1
- package/dist/llms/llama_cpp.cjs +25 -65
- package/dist/llms/llama_cpp.d.ts +7 -43
- package/dist/llms/llama_cpp.js +25 -65
- package/dist/load/import_constants.cjs +3 -0
- package/dist/load/import_constants.js +3 -0
- package/dist/prompts/chat.cjs +8 -0
- package/dist/prompts/chat.d.ts +5 -0
- package/dist/prompts/chat.js +8 -0
- package/dist/prompts/few_shot.cjs +162 -1
- package/dist/prompts/few_shot.d.ts +90 -2
- package/dist/prompts/few_shot.js +160 -0
- package/dist/prompts/index.cjs +2 -1
- package/dist/prompts/index.d.ts +1 -1
- package/dist/prompts/index.js +1 -1
- package/dist/retrievers/zep.cjs +26 -3
- package/dist/retrievers/zep.d.ts +11 -2
- package/dist/retrievers/zep.js +26 -3
- package/dist/util/bedrock.d.ts +2 -0
- package/dist/util/event-source-parse.cjs +20 -1
- package/dist/util/event-source-parse.d.ts +2 -0
- package/dist/util/event-source-parse.js +18 -0
- package/dist/util/iflytek_websocket_stream.cjs +81 -0
- package/dist/util/iflytek_websocket_stream.d.ts +27 -0
- package/dist/util/iflytek_websocket_stream.js +77 -0
- package/dist/util/llama_cpp.cjs +34 -0
- package/dist/util/llama_cpp.d.ts +46 -0
- package/dist/util/llama_cpp.js +28 -0
- package/dist/util/openai-format-fndef.cjs +81 -0
- package/dist/util/openai-format-fndef.d.ts +44 -0
- package/dist/util/openai-format-fndef.js +77 -0
- package/dist/util/openapi.d.ts +2 -2
- package/dist/vectorstores/pinecone.cjs +5 -5
- package/dist/vectorstores/pinecone.d.ts +2 -2
- package/dist/vectorstores/pinecone.js +5 -5
- package/embeddings/llama_cpp.cjs +1 -0
- package/embeddings/llama_cpp.d.ts +1 -0
- package/embeddings/llama_cpp.js +1 -0
- package/package.json +34 -5
package/dist/embeddings/hf.js
CHANGED
|
@@ -21,6 +21,12 @@ export class HuggingFaceInferenceEmbeddings extends Embeddings {
|
|
|
21
21
|
writable: true,
|
|
22
22
|
value: void 0
|
|
23
23
|
});
|
|
24
|
+
Object.defineProperty(this, "endpointUrl", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: void 0
|
|
29
|
+
});
|
|
24
30
|
Object.defineProperty(this, "client", {
|
|
25
31
|
enumerable: true,
|
|
26
32
|
configurable: true,
|
|
@@ -31,7 +37,10 @@ export class HuggingFaceInferenceEmbeddings extends Embeddings {
|
|
|
31
37
|
fields?.model ?? "sentence-transformers/distilbert-base-nli-mean-tokens";
|
|
32
38
|
this.apiKey =
|
|
33
39
|
fields?.apiKey ?? getEnvironmentVariable("HUGGINGFACEHUB_API_KEY");
|
|
34
|
-
this.
|
|
40
|
+
this.endpointUrl = fields?.endpointUrl;
|
|
41
|
+
this.client = this.endpointUrl
|
|
42
|
+
? new HfInference(this.apiKey).endpoint(this.endpointUrl)
|
|
43
|
+
: new HfInference(this.apiKey);
|
|
35
44
|
}
|
|
36
45
|
async _embed(texts) {
|
|
37
46
|
// replace newlines, which can negatively affect performance.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LlamaCppEmbeddings = void 0;
|
|
4
|
+
const llama_cpp_js_1 = require("../util/llama_cpp.cjs");
|
|
5
|
+
const base_js_1 = require("./base.cjs");
|
|
6
|
+
class LlamaCppEmbeddings extends base_js_1.Embeddings {
|
|
7
|
+
constructor(inputs) {
|
|
8
|
+
super(inputs);
|
|
9
|
+
Object.defineProperty(this, "_model", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: void 0
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(this, "_context", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: void 0
|
|
20
|
+
});
|
|
21
|
+
const _inputs = inputs;
|
|
22
|
+
_inputs.embedding = true;
|
|
23
|
+
this._model = (0, llama_cpp_js_1.createLlamaModel)(_inputs);
|
|
24
|
+
this._context = (0, llama_cpp_js_1.createLlamaContext)(this._model, _inputs);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Generates embeddings for an array of texts.
|
|
28
|
+
* @param texts - An array of strings to generate embeddings for.
|
|
29
|
+
* @returns A Promise that resolves to an array of embeddings.
|
|
30
|
+
*/
|
|
31
|
+
async embedDocuments(texts) {
|
|
32
|
+
const tokensArray = [];
|
|
33
|
+
for (const text of texts) {
|
|
34
|
+
const encodings = await this.caller.call(() => new Promise((resolve) => {
|
|
35
|
+
resolve(this._context.encode(text));
|
|
36
|
+
}));
|
|
37
|
+
tokensArray.push(encodings);
|
|
38
|
+
}
|
|
39
|
+
const embeddings = [];
|
|
40
|
+
for (const tokens of tokensArray) {
|
|
41
|
+
const embedArray = [];
|
|
42
|
+
for (let i = 0; i < tokens.length; i += 1) {
|
|
43
|
+
const nToken = +tokens[i];
|
|
44
|
+
embedArray.push(nToken);
|
|
45
|
+
}
|
|
46
|
+
embeddings.push(embedArray);
|
|
47
|
+
}
|
|
48
|
+
return embeddings;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Generates an embedding for a single text.
|
|
52
|
+
* @param text - A string to generate an embedding for.
|
|
53
|
+
* @returns A Promise that resolves to an array of numbers representing the embedding.
|
|
54
|
+
*/
|
|
55
|
+
async embedQuery(text) {
|
|
56
|
+
const tokens = [];
|
|
57
|
+
const encodings = await this.caller.call(() => new Promise((resolve) => {
|
|
58
|
+
resolve(this._context.encode(text));
|
|
59
|
+
}));
|
|
60
|
+
for (let i = 0; i < encodings.length; i += 1) {
|
|
61
|
+
const token = +encodings[i];
|
|
62
|
+
tokens.push(token);
|
|
63
|
+
}
|
|
64
|
+
return tokens;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.LlamaCppEmbeddings = LlamaCppEmbeddings;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { LlamaModel, LlamaContext } from "node-llama-cpp";
|
|
2
|
+
import { LlamaBaseCppInputs } from "../util/llama_cpp.js";
|
|
3
|
+
import { Embeddings, EmbeddingsParams } from "./base.js";
|
|
4
|
+
/**
|
|
5
|
+
* Note that the modelPath is the only required parameter. For testing you
|
|
6
|
+
* can set this in the environment variable `LLAMA_PATH`.
|
|
7
|
+
*/
|
|
8
|
+
export interface LlamaCppEmbeddingsParams extends LlamaBaseCppInputs, EmbeddingsParams {
|
|
9
|
+
}
|
|
10
|
+
export declare class LlamaCppEmbeddings extends Embeddings {
|
|
11
|
+
_model: LlamaModel;
|
|
12
|
+
_context: LlamaContext;
|
|
13
|
+
constructor(inputs: LlamaCppEmbeddingsParams);
|
|
14
|
+
/**
|
|
15
|
+
* Generates embeddings for an array of texts.
|
|
16
|
+
* @param texts - An array of strings to generate embeddings for.
|
|
17
|
+
* @returns A Promise that resolves to an array of embeddings.
|
|
18
|
+
*/
|
|
19
|
+
embedDocuments(texts: string[]): Promise<number[][]>;
|
|
20
|
+
/**
|
|
21
|
+
* Generates an embedding for a single text.
|
|
22
|
+
* @param text - A string to generate an embedding for.
|
|
23
|
+
* @returns A Promise that resolves to an array of numbers representing the embedding.
|
|
24
|
+
*/
|
|
25
|
+
embedQuery(text: string): Promise<number[]>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { createLlamaModel, createLlamaContext, } from "../util/llama_cpp.js";
|
|
2
|
+
import { Embeddings } from "./base.js";
|
|
3
|
+
export class LlamaCppEmbeddings extends Embeddings {
|
|
4
|
+
constructor(inputs) {
|
|
5
|
+
super(inputs);
|
|
6
|
+
Object.defineProperty(this, "_model", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
configurable: true,
|
|
9
|
+
writable: true,
|
|
10
|
+
value: void 0
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(this, "_context", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value: void 0
|
|
17
|
+
});
|
|
18
|
+
const _inputs = inputs;
|
|
19
|
+
_inputs.embedding = true;
|
|
20
|
+
this._model = createLlamaModel(_inputs);
|
|
21
|
+
this._context = createLlamaContext(this._model, _inputs);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Generates embeddings for an array of texts.
|
|
25
|
+
* @param texts - An array of strings to generate embeddings for.
|
|
26
|
+
* @returns A Promise that resolves to an array of embeddings.
|
|
27
|
+
*/
|
|
28
|
+
async embedDocuments(texts) {
|
|
29
|
+
const tokensArray = [];
|
|
30
|
+
for (const text of texts) {
|
|
31
|
+
const encodings = await this.caller.call(() => new Promise((resolve) => {
|
|
32
|
+
resolve(this._context.encode(text));
|
|
33
|
+
}));
|
|
34
|
+
tokensArray.push(encodings);
|
|
35
|
+
}
|
|
36
|
+
const embeddings = [];
|
|
37
|
+
for (const tokens of tokensArray) {
|
|
38
|
+
const embedArray = [];
|
|
39
|
+
for (let i = 0; i < tokens.length; i += 1) {
|
|
40
|
+
const nToken = +tokens[i];
|
|
41
|
+
embedArray.push(nToken);
|
|
42
|
+
}
|
|
43
|
+
embeddings.push(embedArray);
|
|
44
|
+
}
|
|
45
|
+
return embeddings;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Generates an embedding for a single text.
|
|
49
|
+
* @param text - A string to generate an embedding for.
|
|
50
|
+
* @returns A Promise that resolves to an array of numbers representing the embedding.
|
|
51
|
+
*/
|
|
52
|
+
async embedQuery(text) {
|
|
53
|
+
const tokens = [];
|
|
54
|
+
const encodings = await this.caller.call(() => new Promise((resolve) => {
|
|
55
|
+
resolve(this._context.encode(text));
|
|
56
|
+
}));
|
|
57
|
+
for (let i = 0; i < encodings.length; i += 1) {
|
|
58
|
+
const token = +encodings[i];
|
|
59
|
+
tokens.push(token);
|
|
60
|
+
}
|
|
61
|
+
return tokens;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -81,7 +81,13 @@ class OllamaEmbeddings extends base_js_1.Embeddings {
|
|
|
81
81
|
}
|
|
82
82
|
async _request(prompt) {
|
|
83
83
|
const { model, baseUrl, requestOptions } = this;
|
|
84
|
-
|
|
84
|
+
let formattedBaseUrl = baseUrl;
|
|
85
|
+
if (formattedBaseUrl.startsWith("http://localhost:")) {
|
|
86
|
+
// Node 18 has issues with resolving "localhost"
|
|
87
|
+
// See https://github.com/node-fetch/node-fetch/issues/1624
|
|
88
|
+
formattedBaseUrl = formattedBaseUrl.replace("http://localhost:", "http://127.0.0.1:");
|
|
89
|
+
}
|
|
90
|
+
const response = await fetch(`${formattedBaseUrl}/api/embeddings`, {
|
|
85
91
|
method: "POST",
|
|
86
92
|
headers: { "Content-Type": "application/json" },
|
|
87
93
|
body: JSON.stringify({
|
|
@@ -78,7 +78,13 @@ export class OllamaEmbeddings extends Embeddings {
|
|
|
78
78
|
}
|
|
79
79
|
async _request(prompt) {
|
|
80
80
|
const { model, baseUrl, requestOptions } = this;
|
|
81
|
-
|
|
81
|
+
let formattedBaseUrl = baseUrl;
|
|
82
|
+
if (formattedBaseUrl.startsWith("http://localhost:")) {
|
|
83
|
+
// Node 18 has issues with resolving "localhost"
|
|
84
|
+
// See https://github.com/node-fetch/node-fetch/issues/1624
|
|
85
|
+
formattedBaseUrl = formattedBaseUrl.replace("http://localhost:", "http://127.0.0.1:");
|
|
86
|
+
}
|
|
87
|
+
const response = await fetch(`${formattedBaseUrl}/api/embeddings`, {
|
|
82
88
|
method: "POST",
|
|
83
89
|
headers: { "Content-Type": "application/json" },
|
|
84
90
|
body: JSON.stringify({
|
|
@@ -1,10 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
4
24
|
};
|
|
5
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
26
|
exports.Neo4jGraph = void 0;
|
|
7
|
-
const neo4j_driver_1 =
|
|
27
|
+
const neo4j_driver_1 = __importStar(require("neo4j-driver"));
|
|
8
28
|
/**
|
|
9
29
|
* @security *Security note*: Make sure that the database connection uses credentials
|
|
10
30
|
* that are narrowly-scoped to only include necessary permissions.
|
|
@@ -61,7 +81,12 @@ class Neo4jGraph {
|
|
|
61
81
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
62
82
|
}
|
|
63
83
|
catch (error) {
|
|
64
|
-
|
|
84
|
+
const message = [
|
|
85
|
+
"Could not use APOC procedures.",
|
|
86
|
+
"Please ensure the APOC plugin is installed in Neo4j and that",
|
|
87
|
+
"'apoc.meta.data()' is allowed in Neo4j configuration",
|
|
88
|
+
].join("\n");
|
|
89
|
+
throw new Error(message);
|
|
65
90
|
}
|
|
66
91
|
finally {
|
|
67
92
|
console.log("Schema refreshed successfully.");
|
|
@@ -78,9 +103,15 @@ class Neo4jGraph {
|
|
|
78
103
|
database: this.database,
|
|
79
104
|
});
|
|
80
105
|
return toObjects(result.records);
|
|
106
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
81
107
|
}
|
|
82
108
|
catch (error) {
|
|
83
|
-
|
|
109
|
+
if (
|
|
110
|
+
// eslint-disable-next-line
|
|
111
|
+
error instanceof neo4j_driver_1.Neo4jError &&
|
|
112
|
+
error.code === "Neo.ClientError.Procedure.ProcedureNotFound") {
|
|
113
|
+
throw new Error("Procedure not found in Neo4j.");
|
|
114
|
+
}
|
|
84
115
|
}
|
|
85
116
|
return undefined;
|
|
86
117
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import neo4j from "neo4j-driver";
|
|
1
|
+
import neo4j, { Neo4jError } from "neo4j-driver";
|
|
2
2
|
/**
|
|
3
3
|
* @security *Security note*: Make sure that the database connection uses credentials
|
|
4
4
|
* that are narrowly-scoped to only include necessary permissions.
|
|
@@ -55,7 +55,12 @@ export class Neo4jGraph {
|
|
|
55
55
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
56
56
|
}
|
|
57
57
|
catch (error) {
|
|
58
|
-
|
|
58
|
+
const message = [
|
|
59
|
+
"Could not use APOC procedures.",
|
|
60
|
+
"Please ensure the APOC plugin is installed in Neo4j and that",
|
|
61
|
+
"'apoc.meta.data()' is allowed in Neo4j configuration",
|
|
62
|
+
].join("\n");
|
|
63
|
+
throw new Error(message);
|
|
59
64
|
}
|
|
60
65
|
finally {
|
|
61
66
|
console.log("Schema refreshed successfully.");
|
|
@@ -72,9 +77,15 @@ export class Neo4jGraph {
|
|
|
72
77
|
database: this.database,
|
|
73
78
|
});
|
|
74
79
|
return toObjects(result.records);
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
75
81
|
}
|
|
76
82
|
catch (error) {
|
|
77
|
-
|
|
83
|
+
if (
|
|
84
|
+
// eslint-disable-next-line
|
|
85
|
+
error instanceof Neo4jError &&
|
|
86
|
+
error.code === "Neo.ClientError.Procedure.ProcedureNotFound") {
|
|
87
|
+
throw new Error("Procedure not found in Neo4j.");
|
|
88
|
+
}
|
|
78
89
|
}
|
|
79
90
|
return undefined;
|
|
80
91
|
}
|
package/dist/llms/bedrock.cjs
CHANGED
|
@@ -92,6 +92,12 @@ class Bedrock extends base_js_1.LLM {
|
|
|
92
92
|
writable: true,
|
|
93
93
|
value: new eventstream_codec_1.EventStreamCodec(util_utf8_1.toUtf8, util_utf8_1.fromUtf8)
|
|
94
94
|
});
|
|
95
|
+
Object.defineProperty(this, "streaming", {
|
|
96
|
+
enumerable: true,
|
|
97
|
+
configurable: true,
|
|
98
|
+
writable: true,
|
|
99
|
+
value: false
|
|
100
|
+
});
|
|
95
101
|
this.model = fields?.model ?? this.model;
|
|
96
102
|
const allowedModels = ["ai21", "anthropic", "amazon"];
|
|
97
103
|
if (!allowedModels.includes(this.model.split(".")[0])) {
|
|
@@ -109,6 +115,7 @@ class Bedrock extends base_js_1.LLM {
|
|
|
109
115
|
this.endpointHost = fields?.endpointHost ?? fields?.endpointUrl;
|
|
110
116
|
this.stopSequences = fields?.stopSequences;
|
|
111
117
|
this.modelKwargs = fields?.modelKwargs;
|
|
118
|
+
this.streaming = fields?.streaming ?? this.streaming;
|
|
112
119
|
}
|
|
113
120
|
/** Call out to Bedrock service model.
|
|
114
121
|
Arguments:
|
|
@@ -120,10 +127,23 @@ class Bedrock extends base_js_1.LLM {
|
|
|
120
127
|
Example:
|
|
121
128
|
response = model.call("Tell me a joke.")
|
|
122
129
|
*/
|
|
123
|
-
async _call(prompt, options) {
|
|
130
|
+
async _call(prompt, options, runManager) {
|
|
124
131
|
const service = "bedrock-runtime";
|
|
125
132
|
const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
|
|
126
133
|
const provider = this.model.split(".")[0];
|
|
134
|
+
if (this.streaming) {
|
|
135
|
+
const stream = this._streamResponseChunks(prompt, options, runManager);
|
|
136
|
+
let finalResult;
|
|
137
|
+
for await (const chunk of stream) {
|
|
138
|
+
if (finalResult === undefined) {
|
|
139
|
+
finalResult = chunk;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
finalResult = finalResult.concat(chunk);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return finalResult?.text ?? "";
|
|
146
|
+
}
|
|
127
147
|
const response = await this._signedFetch(prompt, options, {
|
|
128
148
|
bedrockMethod: "invoke",
|
|
129
149
|
endpointHost,
|
|
@@ -204,7 +224,8 @@ class Bedrock extends base_js_1.LLM {
|
|
|
204
224
|
text,
|
|
205
225
|
generationInfo: {},
|
|
206
226
|
});
|
|
207
|
-
|
|
227
|
+
// eslint-disable-next-line no-void
|
|
228
|
+
void runManager?.handleLLMNewToken(text);
|
|
208
229
|
}
|
|
209
230
|
}
|
|
210
231
|
}
|
|
@@ -215,7 +236,8 @@ class Bedrock extends base_js_1.LLM {
|
|
|
215
236
|
text,
|
|
216
237
|
generationInfo: {},
|
|
217
238
|
});
|
|
218
|
-
|
|
239
|
+
// eslint-disable-next-line no-void
|
|
240
|
+
void runManager?.handleLLMNewToken(text);
|
|
219
241
|
}
|
|
220
242
|
}
|
|
221
243
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
package/dist/llms/bedrock.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export declare class Bedrock extends LLM implements BaseBedrockInput {
|
|
|
23
23
|
stopSequences?: string[];
|
|
24
24
|
modelKwargs?: Record<string, unknown>;
|
|
25
25
|
codec: EventStreamCodec;
|
|
26
|
+
streaming: boolean;
|
|
26
27
|
get lc_secrets(): {
|
|
27
28
|
[key: string]: string;
|
|
28
29
|
} | undefined;
|
|
@@ -39,7 +40,7 @@ export declare class Bedrock extends LLM implements BaseBedrockInput {
|
|
|
39
40
|
Example:
|
|
40
41
|
response = model.call("Tell me a joke.")
|
|
41
42
|
*/
|
|
42
|
-
_call(prompt: string, options: this["ParsedCallOptions"]): Promise<string>;
|
|
43
|
+
_call(prompt: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
|
|
43
44
|
_signedFetch(prompt: string, options: this["ParsedCallOptions"], fields: {
|
|
44
45
|
bedrockMethod: "invoke" | "invoke-with-response-stream";
|
|
45
46
|
endpointHost: string;
|
package/dist/llms/bedrock.js
CHANGED
|
@@ -89,6 +89,12 @@ export class Bedrock extends LLM {
|
|
|
89
89
|
writable: true,
|
|
90
90
|
value: new EventStreamCodec(toUtf8, fromUtf8)
|
|
91
91
|
});
|
|
92
|
+
Object.defineProperty(this, "streaming", {
|
|
93
|
+
enumerable: true,
|
|
94
|
+
configurable: true,
|
|
95
|
+
writable: true,
|
|
96
|
+
value: false
|
|
97
|
+
});
|
|
92
98
|
this.model = fields?.model ?? this.model;
|
|
93
99
|
const allowedModels = ["ai21", "anthropic", "amazon"];
|
|
94
100
|
if (!allowedModels.includes(this.model.split(".")[0])) {
|
|
@@ -106,6 +112,7 @@ export class Bedrock extends LLM {
|
|
|
106
112
|
this.endpointHost = fields?.endpointHost ?? fields?.endpointUrl;
|
|
107
113
|
this.stopSequences = fields?.stopSequences;
|
|
108
114
|
this.modelKwargs = fields?.modelKwargs;
|
|
115
|
+
this.streaming = fields?.streaming ?? this.streaming;
|
|
109
116
|
}
|
|
110
117
|
/** Call out to Bedrock service model.
|
|
111
118
|
Arguments:
|
|
@@ -117,10 +124,23 @@ export class Bedrock extends LLM {
|
|
|
117
124
|
Example:
|
|
118
125
|
response = model.call("Tell me a joke.")
|
|
119
126
|
*/
|
|
120
|
-
async _call(prompt, options) {
|
|
127
|
+
async _call(prompt, options, runManager) {
|
|
121
128
|
const service = "bedrock-runtime";
|
|
122
129
|
const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
|
|
123
130
|
const provider = this.model.split(".")[0];
|
|
131
|
+
if (this.streaming) {
|
|
132
|
+
const stream = this._streamResponseChunks(prompt, options, runManager);
|
|
133
|
+
let finalResult;
|
|
134
|
+
for await (const chunk of stream) {
|
|
135
|
+
if (finalResult === undefined) {
|
|
136
|
+
finalResult = chunk;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
finalResult = finalResult.concat(chunk);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return finalResult?.text ?? "";
|
|
143
|
+
}
|
|
124
144
|
const response = await this._signedFetch(prompt, options, {
|
|
125
145
|
bedrockMethod: "invoke",
|
|
126
146
|
endpointHost,
|
|
@@ -201,7 +221,8 @@ export class Bedrock extends LLM {
|
|
|
201
221
|
text,
|
|
202
222
|
generationInfo: {},
|
|
203
223
|
});
|
|
204
|
-
|
|
224
|
+
// eslint-disable-next-line no-void
|
|
225
|
+
void runManager?.handleLLMNewToken(text);
|
|
205
226
|
}
|
|
206
227
|
}
|
|
207
228
|
}
|
|
@@ -212,7 +233,8 @@ export class Bedrock extends LLM {
|
|
|
212
233
|
text,
|
|
213
234
|
generationInfo: {},
|
|
214
235
|
});
|
|
215
|
-
|
|
236
|
+
// eslint-disable-next-line no-void
|
|
237
|
+
void runManager?.handleLLMNewToken(text);
|
|
216
238
|
}
|
|
217
239
|
}
|
|
218
240
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.CloudflareWorkersAI = void 0;
|
|
4
4
|
const base_js_1 = require("./base.cjs");
|
|
5
5
|
const env_js_1 = require("../util/env.cjs");
|
|
6
|
+
const index_js_1 = require("../schema/index.cjs");
|
|
7
|
+
const event_source_parse_js_1 = require("../util/event-source-parse.cjs");
|
|
6
8
|
/**
|
|
7
9
|
* Class representing the CloudflareWorkersAI language model. It extends the LLM (Large
|
|
8
10
|
* Language Model) class, providing a standard interface for interacting
|
|
@@ -38,6 +40,12 @@ class CloudflareWorkersAI extends base_js_1.LLM {
|
|
|
38
40
|
writable: true,
|
|
39
41
|
value: void 0
|
|
40
42
|
});
|
|
43
|
+
Object.defineProperty(this, "streaming", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
configurable: true,
|
|
46
|
+
writable: true,
|
|
47
|
+
value: false
|
|
48
|
+
});
|
|
41
49
|
Object.defineProperty(this, "lc_serializable", {
|
|
42
50
|
enumerable: true,
|
|
43
51
|
configurable: true,
|
|
@@ -45,6 +53,7 @@ class CloudflareWorkersAI extends base_js_1.LLM {
|
|
|
45
53
|
value: true
|
|
46
54
|
});
|
|
47
55
|
this.model = fields?.model ?? this.model;
|
|
56
|
+
this.streaming = fields?.streaming ?? this.streaming;
|
|
48
57
|
this.cloudflareAccountId =
|
|
49
58
|
fields?.cloudflareAccountId ??
|
|
50
59
|
(0, env_js_1.getEnvironmentVariable)("CLOUDFLARE_ACCOUNT_ID");
|
|
@@ -87,23 +96,15 @@ class CloudflareWorkersAI extends base_js_1.LLM {
|
|
|
87
96
|
_llmType() {
|
|
88
97
|
return "cloudflare";
|
|
89
98
|
}
|
|
90
|
-
|
|
91
|
-
Args:
|
|
92
|
-
prompt: The prompt to pass into the model.
|
|
93
|
-
Returns:
|
|
94
|
-
The string generated by the model.
|
|
95
|
-
Example:
|
|
96
|
-
let response = CloudflareWorkersAI.call("Tell me a joke.");
|
|
97
|
-
*/
|
|
98
|
-
async _call(prompt, options) {
|
|
99
|
+
async _request(prompt, options, stream) {
|
|
99
100
|
this.validateEnvironment();
|
|
100
101
|
const url = `${this.baseUrl}/${this.model}`;
|
|
101
102
|
const headers = {
|
|
102
103
|
Authorization: `Bearer ${this.cloudflareApiToken}`,
|
|
103
104
|
"Content-Type": "application/json",
|
|
104
105
|
};
|
|
105
|
-
const data = { prompt };
|
|
106
|
-
|
|
106
|
+
const data = { prompt, stream };
|
|
107
|
+
return this.caller.call(async () => {
|
|
107
108
|
const response = await fetch(url, {
|
|
108
109
|
method: "POST",
|
|
109
110
|
headers,
|
|
@@ -116,9 +117,54 @@ class CloudflareWorkersAI extends base_js_1.LLM {
|
|
|
116
117
|
error.response = response;
|
|
117
118
|
throw error;
|
|
118
119
|
}
|
|
119
|
-
return response
|
|
120
|
+
return response;
|
|
120
121
|
});
|
|
121
|
-
|
|
122
|
+
}
|
|
123
|
+
async *_streamResponseChunks(prompt, options, runManager) {
|
|
124
|
+
const response = await this._request(prompt, options, true);
|
|
125
|
+
if (!response.body) {
|
|
126
|
+
throw new Error("Empty response from Cloudflare. Please try again.");
|
|
127
|
+
}
|
|
128
|
+
const stream = (0, event_source_parse_js_1.convertEventStreamToIterableReadableDataStream)(response.body);
|
|
129
|
+
for await (const chunk of stream) {
|
|
130
|
+
if (chunk !== "[DONE]") {
|
|
131
|
+
const parsedChunk = JSON.parse(chunk);
|
|
132
|
+
const generationChunk = new index_js_1.GenerationChunk({
|
|
133
|
+
text: parsedChunk.response,
|
|
134
|
+
});
|
|
135
|
+
yield generationChunk;
|
|
136
|
+
// eslint-disable-next-line no-void
|
|
137
|
+
void runManager?.handleLLMNewToken(generationChunk.text ?? "");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/** Call out to CloudflareWorkersAI's complete endpoint.
|
|
142
|
+
Args:
|
|
143
|
+
prompt: The prompt to pass into the model.
|
|
144
|
+
Returns:
|
|
145
|
+
The string generated by the model.
|
|
146
|
+
Example:
|
|
147
|
+
let response = CloudflareWorkersAI.call("Tell me a joke.");
|
|
148
|
+
*/
|
|
149
|
+
async _call(prompt, options, runManager) {
|
|
150
|
+
if (!this.streaming) {
|
|
151
|
+
const response = await this._request(prompt, options);
|
|
152
|
+
const responseData = await response.json();
|
|
153
|
+
return responseData.result.response;
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
const stream = this._streamResponseChunks(prompt, options, runManager);
|
|
157
|
+
let finalResult;
|
|
158
|
+
for await (const chunk of stream) {
|
|
159
|
+
if (finalResult === undefined) {
|
|
160
|
+
finalResult = chunk;
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
finalResult = finalResult.concat(chunk);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return finalResult?.text ?? "";
|
|
167
|
+
}
|
|
122
168
|
}
|
|
123
169
|
}
|
|
124
170
|
exports.CloudflareWorkersAI = CloudflareWorkersAI;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { LLM, BaseLLMParams } from "./base.js";
|
|
2
|
+
import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
|
|
3
|
+
import { GenerationChunk } from "../schema/index.js";
|
|
2
4
|
/**
|
|
3
5
|
* Interface for CloudflareWorkersAI input parameters.
|
|
4
6
|
*/
|
|
@@ -7,6 +9,7 @@ export interface CloudflareWorkersAIInput {
|
|
|
7
9
|
cloudflareApiToken?: string;
|
|
8
10
|
model?: string;
|
|
9
11
|
baseUrl?: string;
|
|
12
|
+
streaming?: boolean;
|
|
10
13
|
}
|
|
11
14
|
/**
|
|
12
15
|
* Class representing the CloudflareWorkersAI language model. It extends the LLM (Large
|
|
@@ -18,6 +21,7 @@ export declare class CloudflareWorkersAI extends LLM implements CloudflareWorker
|
|
|
18
21
|
cloudflareAccountId?: string;
|
|
19
22
|
cloudflareApiToken?: string;
|
|
20
23
|
baseUrl: string;
|
|
24
|
+
streaming: boolean;
|
|
21
25
|
static lc_name(): string;
|
|
22
26
|
lc_serializable: boolean;
|
|
23
27
|
constructor(fields?: CloudflareWorkersAIInput & BaseLLMParams);
|
|
@@ -37,13 +41,15 @@ export declare class CloudflareWorkersAI extends LLM implements CloudflareWorker
|
|
|
37
41
|
};
|
|
38
42
|
/** Get the type of LLM. */
|
|
39
43
|
_llmType(): string;
|
|
44
|
+
_request(prompt: string, options: this["ParsedCallOptions"], stream?: boolean): Promise<Response>;
|
|
45
|
+
_streamResponseChunks(prompt: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<GenerationChunk>;
|
|
40
46
|
/** Call out to CloudflareWorkersAI's complete endpoint.
|
|
41
47
|
Args:
|
|
42
48
|
prompt: The prompt to pass into the model.
|
|
43
|
-
|
|
44
|
-
|
|
49
|
+
Returns:
|
|
50
|
+
The string generated by the model.
|
|
45
51
|
Example:
|
|
46
52
|
let response = CloudflareWorkersAI.call("Tell me a joke.");
|
|
47
53
|
*/
|
|
48
|
-
_call(prompt: string, options: this["ParsedCallOptions"]): Promise<string>;
|
|
54
|
+
_call(prompt: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
|
|
49
55
|
}
|