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/retrievers/zep.js
CHANGED
|
@@ -44,6 +44,12 @@ export class ZepRetriever extends BaseRetriever {
|
|
|
44
44
|
writable: true,
|
|
45
45
|
value: void 0
|
|
46
46
|
});
|
|
47
|
+
Object.defineProperty(this, "searchScope", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
configurable: true,
|
|
50
|
+
writable: true,
|
|
51
|
+
value: void 0
|
|
52
|
+
});
|
|
47
53
|
Object.defineProperty(this, "searchType", {
|
|
48
54
|
enumerable: true,
|
|
49
55
|
configurable: true,
|
|
@@ -64,17 +70,18 @@ export class ZepRetriever extends BaseRetriever {
|
|
|
64
70
|
});
|
|
65
71
|
this.sessionId = config.sessionId;
|
|
66
72
|
this.topK = config.topK;
|
|
73
|
+
this.searchScope = config.searchScope;
|
|
67
74
|
this.searchType = config.searchType;
|
|
68
75
|
this.mmrLambda = config.mmrLambda;
|
|
69
76
|
this.filter = config.filter;
|
|
70
77
|
this.zepClientPromise = ZepClient.init(config.url, config.apiKey);
|
|
71
78
|
}
|
|
72
79
|
/**
|
|
73
|
-
* Converts an array of search results to an array of Document objects.
|
|
80
|
+
* Converts an array of message search results to an array of Document objects.
|
|
74
81
|
* @param {MemorySearchResult[]} results - The array of search results.
|
|
75
82
|
* @returns {Document[]} An array of Document objects representing the search results.
|
|
76
83
|
*/
|
|
77
|
-
|
|
84
|
+
searchMessageResultToDoc(results) {
|
|
78
85
|
return results
|
|
79
86
|
.filter((r) => r.message)
|
|
80
87
|
.map(({ message: { content, metadata: messageMetadata } = {}, dist, ...rest }) => new Document({
|
|
@@ -82,6 +89,19 @@ export class ZepRetriever extends BaseRetriever {
|
|
|
82
89
|
metadata: { score: dist, ...messageMetadata, ...rest },
|
|
83
90
|
}));
|
|
84
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Converts an array of summary search results to an array of Document objects.
|
|
94
|
+
* @param {MemorySearchResult[]} results - The array of search results.
|
|
95
|
+
* @returns {Document[]} An array of Document objects representing the search results.
|
|
96
|
+
*/
|
|
97
|
+
searchSummaryResultToDoc(results) {
|
|
98
|
+
return results
|
|
99
|
+
.filter((r) => r.summary)
|
|
100
|
+
.map(({ summary: { content, metadata: summaryMetadata } = {}, dist, ...rest }) => new Document({
|
|
101
|
+
pageContent: content ?? "",
|
|
102
|
+
metadata: { score: dist, ...summaryMetadata, ...rest },
|
|
103
|
+
}));
|
|
104
|
+
}
|
|
85
105
|
/**
|
|
86
106
|
* Retrieves the relevant documents based on the given query.
|
|
87
107
|
* @param {string} query - The query string.
|
|
@@ -91,6 +111,7 @@ export class ZepRetriever extends BaseRetriever {
|
|
|
91
111
|
const payload = {
|
|
92
112
|
text: query,
|
|
93
113
|
metadata: this.filter,
|
|
114
|
+
search_scope: this.searchScope,
|
|
94
115
|
search_type: this.searchType,
|
|
95
116
|
mmr_lambda: this.mmrLambda,
|
|
96
117
|
};
|
|
@@ -101,7 +122,9 @@ export class ZepRetriever extends BaseRetriever {
|
|
|
101
122
|
}
|
|
102
123
|
try {
|
|
103
124
|
const results = await zepClient.memory.searchMemory(this.sessionId, payload, this.topK);
|
|
104
|
-
return this.
|
|
125
|
+
return this.searchScope === "summary"
|
|
126
|
+
? this.searchSummaryResultToDoc(results)
|
|
127
|
+
: this.searchMessageResultToDoc(results);
|
|
105
128
|
}
|
|
106
129
|
catch (error) {
|
|
107
130
|
// eslint-disable-next-line no-instanceof/no-instanceof
|
package/dist/util/bedrock.d.ts
CHANGED
|
@@ -33,6 +33,8 @@ export interface BaseBedrockInput {
|
|
|
33
33
|
stopSequences?: string[];
|
|
34
34
|
/** Additional kwargs to pass to the model. */
|
|
35
35
|
modelKwargs?: Record<string, unknown>;
|
|
36
|
+
/** Whether or not to stream responses */
|
|
37
|
+
streaming: boolean;
|
|
36
38
|
}
|
|
37
39
|
type Dict = {
|
|
38
40
|
[key: string]: unknown;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMessages = exports.getLines = exports.getBytes = exports.EventStreamContentType = void 0;
|
|
3
|
+
exports.convertEventStreamToIterableReadableDataStream = exports.getMessages = exports.getLines = exports.getBytes = exports.EventStreamContentType = void 0;
|
|
4
|
+
const stream_js_1 = require("./stream.cjs");
|
|
4
5
|
exports.EventStreamContentType = "text/event-stream";
|
|
5
6
|
function isNodeJSReadable(x) {
|
|
6
7
|
return x != null && typeof x === "object" && "on" in x;
|
|
@@ -199,6 +200,24 @@ function newMessage() {
|
|
|
199
200
|
retry: undefined,
|
|
200
201
|
};
|
|
201
202
|
}
|
|
203
|
+
function convertEventStreamToIterableReadableDataStream(stream) {
|
|
204
|
+
const dataStream = new ReadableStream({
|
|
205
|
+
async start(controller) {
|
|
206
|
+
const enqueueLine = getMessages((msg) => {
|
|
207
|
+
if (msg.data)
|
|
208
|
+
controller.enqueue(msg.data);
|
|
209
|
+
});
|
|
210
|
+
const onLine = (line, fieldLength, flush) => {
|
|
211
|
+
enqueueLine(line, fieldLength, flush);
|
|
212
|
+
if (flush)
|
|
213
|
+
controller.close();
|
|
214
|
+
};
|
|
215
|
+
await getBytes(stream, getLines(onLine));
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
return stream_js_1.IterableReadableStream.fromReadableStream(dataStream);
|
|
219
|
+
}
|
|
220
|
+
exports.convertEventStreamToIterableReadableDataStream = convertEventStreamToIterableReadableDataStream;
|
|
202
221
|
function isEmpty(message) {
|
|
203
222
|
return (message.data === "" &&
|
|
204
223
|
message.event === "" &&
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IterableReadableStream } from "./stream.js";
|
|
1
2
|
export declare const EventStreamContentType = "text/event-stream";
|
|
2
3
|
/**
|
|
3
4
|
* Represents a message sent in an event stream
|
|
@@ -35,3 +36,4 @@ export declare function getLines(onLine: (line: Uint8Array, fieldLength: number,
|
|
|
35
36
|
* @returns A function that should be called for each incoming line buffer.
|
|
36
37
|
*/
|
|
37
38
|
export declare function getMessages(onMessage?: (msg: EventSourceMessage) => void, onId?: (id: string) => void, onRetry?: (retry: number) => void): (line: Uint8Array, fieldLength: number, flush?: boolean) => void;
|
|
39
|
+
export declare function convertEventStreamToIterableReadableDataStream(stream: ReadableStream): IterableReadableStream<any>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IterableReadableStream } from "./stream.js";
|
|
1
2
|
export const EventStreamContentType = "text/event-stream";
|
|
2
3
|
function isNodeJSReadable(x) {
|
|
3
4
|
return x != null && typeof x === "object" && "on" in x;
|
|
@@ -193,6 +194,23 @@ function newMessage() {
|
|
|
193
194
|
retry: undefined,
|
|
194
195
|
};
|
|
195
196
|
}
|
|
197
|
+
export function convertEventStreamToIterableReadableDataStream(stream) {
|
|
198
|
+
const dataStream = new ReadableStream({
|
|
199
|
+
async start(controller) {
|
|
200
|
+
const enqueueLine = getMessages((msg) => {
|
|
201
|
+
if (msg.data)
|
|
202
|
+
controller.enqueue(msg.data);
|
|
203
|
+
});
|
|
204
|
+
const onLine = (line, fieldLength, flush) => {
|
|
205
|
+
enqueueLine(line, fieldLength, flush);
|
|
206
|
+
if (flush)
|
|
207
|
+
controller.close();
|
|
208
|
+
};
|
|
209
|
+
await getBytes(stream, getLines(onLine));
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
return IterableReadableStream.fromReadableStream(dataStream);
|
|
213
|
+
}
|
|
196
214
|
function isEmpty(message) {
|
|
197
215
|
return (message.data === "" &&
|
|
198
216
|
message.event === "" &&
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseWebSocketStream = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) with [Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API)
|
|
6
|
+
*
|
|
7
|
+
* @see https://web.dev/websocketstream/
|
|
8
|
+
*/
|
|
9
|
+
class BaseWebSocketStream {
|
|
10
|
+
constructor(url, options = {}) {
|
|
11
|
+
Object.defineProperty(this, "url", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: void 0
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(this, "connection", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: void 0
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(this, "closed", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: void 0
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(this, "close", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: void 0
|
|
34
|
+
});
|
|
35
|
+
if (options.signal?.aborted) {
|
|
36
|
+
throw new DOMException("This operation was aborted", "AbortError");
|
|
37
|
+
}
|
|
38
|
+
this.url = url;
|
|
39
|
+
const ws = this.openWebSocket(url, options);
|
|
40
|
+
const closeWithInfo = ({ code, reason } = {}) => ws.close(code, reason);
|
|
41
|
+
this.connection = new Promise((resolve, reject) => {
|
|
42
|
+
ws.onopen = () => {
|
|
43
|
+
resolve({
|
|
44
|
+
readable: new ReadableStream({
|
|
45
|
+
start(controller) {
|
|
46
|
+
ws.onmessage = ({ data }) => controller.enqueue(data);
|
|
47
|
+
ws.onerror = (e) => controller.error(e);
|
|
48
|
+
},
|
|
49
|
+
cancel: closeWithInfo,
|
|
50
|
+
}),
|
|
51
|
+
writable: new WritableStream({
|
|
52
|
+
write(chunk) {
|
|
53
|
+
ws.send(chunk);
|
|
54
|
+
},
|
|
55
|
+
abort() {
|
|
56
|
+
ws.close();
|
|
57
|
+
},
|
|
58
|
+
close: closeWithInfo,
|
|
59
|
+
}),
|
|
60
|
+
protocol: ws.protocol,
|
|
61
|
+
extensions: ws.extensions,
|
|
62
|
+
});
|
|
63
|
+
ws.removeEventListener("error", reject);
|
|
64
|
+
};
|
|
65
|
+
ws.addEventListener("error", reject);
|
|
66
|
+
});
|
|
67
|
+
this.closed = new Promise((resolve, reject) => {
|
|
68
|
+
ws.onclose = ({ code, reason }) => {
|
|
69
|
+
resolve({ code, reason });
|
|
70
|
+
ws.removeEventListener("error", reject);
|
|
71
|
+
};
|
|
72
|
+
ws.addEventListener("error", reject);
|
|
73
|
+
});
|
|
74
|
+
if (options.signal) {
|
|
75
|
+
// eslint-disable-next-line no-param-reassign
|
|
76
|
+
options.signal.onabort = () => ws.close();
|
|
77
|
+
}
|
|
78
|
+
this.close = closeWithInfo;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.BaseWebSocketStream = BaseWebSocketStream;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface WebSocketConnection<T extends Uint8Array | string = Uint8Array | string> {
|
|
2
|
+
readable: ReadableStream<T>;
|
|
3
|
+
writable: WritableStream<T>;
|
|
4
|
+
protocol: string;
|
|
5
|
+
extensions: string;
|
|
6
|
+
}
|
|
7
|
+
export interface WebSocketCloseInfo {
|
|
8
|
+
code?: number;
|
|
9
|
+
reason?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface WebSocketStreamOptions {
|
|
12
|
+
protocols?: string[];
|
|
13
|
+
signal?: AbortSignal;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) with [Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API)
|
|
17
|
+
*
|
|
18
|
+
* @see https://web.dev/websocketstream/
|
|
19
|
+
*/
|
|
20
|
+
export declare abstract class BaseWebSocketStream<T extends Uint8Array | string = Uint8Array | string> {
|
|
21
|
+
readonly url: string;
|
|
22
|
+
readonly connection: Promise<WebSocketConnection<T>>;
|
|
23
|
+
readonly closed: Promise<WebSocketCloseInfo>;
|
|
24
|
+
readonly close: (closeInfo?: WebSocketCloseInfo) => void;
|
|
25
|
+
constructor(url: string, options?: WebSocketStreamOptions);
|
|
26
|
+
abstract openWebSocket(url: string, options: WebSocketStreamOptions): WebSocket;
|
|
27
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) with [Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API)
|
|
3
|
+
*
|
|
4
|
+
* @see https://web.dev/websocketstream/
|
|
5
|
+
*/
|
|
6
|
+
export class BaseWebSocketStream {
|
|
7
|
+
constructor(url, options = {}) {
|
|
8
|
+
Object.defineProperty(this, "url", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true,
|
|
12
|
+
value: void 0
|
|
13
|
+
});
|
|
14
|
+
Object.defineProperty(this, "connection", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: void 0
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(this, "closed", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
writable: true,
|
|
24
|
+
value: void 0
|
|
25
|
+
});
|
|
26
|
+
Object.defineProperty(this, "close", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true,
|
|
30
|
+
value: void 0
|
|
31
|
+
});
|
|
32
|
+
if (options.signal?.aborted) {
|
|
33
|
+
throw new DOMException("This operation was aborted", "AbortError");
|
|
34
|
+
}
|
|
35
|
+
this.url = url;
|
|
36
|
+
const ws = this.openWebSocket(url, options);
|
|
37
|
+
const closeWithInfo = ({ code, reason } = {}) => ws.close(code, reason);
|
|
38
|
+
this.connection = new Promise((resolve, reject) => {
|
|
39
|
+
ws.onopen = () => {
|
|
40
|
+
resolve({
|
|
41
|
+
readable: new ReadableStream({
|
|
42
|
+
start(controller) {
|
|
43
|
+
ws.onmessage = ({ data }) => controller.enqueue(data);
|
|
44
|
+
ws.onerror = (e) => controller.error(e);
|
|
45
|
+
},
|
|
46
|
+
cancel: closeWithInfo,
|
|
47
|
+
}),
|
|
48
|
+
writable: new WritableStream({
|
|
49
|
+
write(chunk) {
|
|
50
|
+
ws.send(chunk);
|
|
51
|
+
},
|
|
52
|
+
abort() {
|
|
53
|
+
ws.close();
|
|
54
|
+
},
|
|
55
|
+
close: closeWithInfo,
|
|
56
|
+
}),
|
|
57
|
+
protocol: ws.protocol,
|
|
58
|
+
extensions: ws.extensions,
|
|
59
|
+
});
|
|
60
|
+
ws.removeEventListener("error", reject);
|
|
61
|
+
};
|
|
62
|
+
ws.addEventListener("error", reject);
|
|
63
|
+
});
|
|
64
|
+
this.closed = new Promise((resolve, reject) => {
|
|
65
|
+
ws.onclose = ({ code, reason }) => {
|
|
66
|
+
resolve({ code, reason });
|
|
67
|
+
ws.removeEventListener("error", reject);
|
|
68
|
+
};
|
|
69
|
+
ws.addEventListener("error", reject);
|
|
70
|
+
});
|
|
71
|
+
if (options.signal) {
|
|
72
|
+
// eslint-disable-next-line no-param-reassign
|
|
73
|
+
options.signal.onabort = () => ws.close();
|
|
74
|
+
}
|
|
75
|
+
this.close = closeWithInfo;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createLlamaSession = exports.createLlamaContext = exports.createLlamaModel = void 0;
|
|
4
|
+
const node_llama_cpp_1 = require("node-llama-cpp");
|
|
5
|
+
function createLlamaModel(inputs) {
|
|
6
|
+
const options = {
|
|
7
|
+
gpuLayers: inputs?.gpuLayers,
|
|
8
|
+
modelPath: inputs.modelPath,
|
|
9
|
+
useMlock: inputs?.useMlock,
|
|
10
|
+
useMmap: inputs?.useMmap,
|
|
11
|
+
vocabOnly: inputs?.vocabOnly,
|
|
12
|
+
};
|
|
13
|
+
return new node_llama_cpp_1.LlamaModel(options);
|
|
14
|
+
}
|
|
15
|
+
exports.createLlamaModel = createLlamaModel;
|
|
16
|
+
function createLlamaContext(model, inputs) {
|
|
17
|
+
const options = {
|
|
18
|
+
batchSize: inputs?.batchSize,
|
|
19
|
+
contextSize: inputs?.contextSize,
|
|
20
|
+
embedding: inputs?.embedding,
|
|
21
|
+
f16Kv: inputs?.f16Kv,
|
|
22
|
+
logitsAll: inputs?.logitsAll,
|
|
23
|
+
model,
|
|
24
|
+
prependBos: inputs?.prependBos,
|
|
25
|
+
seed: inputs?.seed,
|
|
26
|
+
threads: inputs?.threads,
|
|
27
|
+
};
|
|
28
|
+
return new node_llama_cpp_1.LlamaContext(options);
|
|
29
|
+
}
|
|
30
|
+
exports.createLlamaContext = createLlamaContext;
|
|
31
|
+
function createLlamaSession(context) {
|
|
32
|
+
return new node_llama_cpp_1.LlamaChatSession({ context });
|
|
33
|
+
}
|
|
34
|
+
exports.createLlamaSession = createLlamaSession;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { LlamaModel, LlamaContext, LlamaChatSession } from "node-llama-cpp";
|
|
2
|
+
/**
|
|
3
|
+
* Note that the modelPath is the only required parameter. For testing you
|
|
4
|
+
* can set this in the environment variable `LLAMA_PATH`.
|
|
5
|
+
*/
|
|
6
|
+
export interface LlamaBaseCppInputs {
|
|
7
|
+
/** Prompt processing batch size. */
|
|
8
|
+
batchSize?: number;
|
|
9
|
+
/** Text context size. */
|
|
10
|
+
contextSize?: number;
|
|
11
|
+
/** Embedding mode only. */
|
|
12
|
+
embedding?: boolean;
|
|
13
|
+
/** Use fp16 for KV cache. */
|
|
14
|
+
f16Kv?: boolean;
|
|
15
|
+
/** Number of layers to store in VRAM. */
|
|
16
|
+
gpuLayers?: number;
|
|
17
|
+
/** The llama_eval() call computes all logits, not just the last one. */
|
|
18
|
+
logitsAll?: boolean;
|
|
19
|
+
/** */
|
|
20
|
+
maxTokens?: number;
|
|
21
|
+
/** Path to the model on the filesystem. */
|
|
22
|
+
modelPath: string;
|
|
23
|
+
/** Add the begining of sentence token. */
|
|
24
|
+
prependBos?: boolean;
|
|
25
|
+
/** If null, a random seed will be used. */
|
|
26
|
+
seed?: null | number;
|
|
27
|
+
/** The randomness of the responses, e.g. 0.1 deterministic, 1.5 creative, 0.8 balanced, 0 disables. */
|
|
28
|
+
temperature?: number;
|
|
29
|
+
/** Number of threads to use to evaluate tokens. */
|
|
30
|
+
threads?: number;
|
|
31
|
+
/** Trim whitespace from the end of the generated text Disabled by default. */
|
|
32
|
+
trimWhitespaceSuffix?: boolean;
|
|
33
|
+
/** Consider the n most likely tokens, where n is 1 to vocabulary size, 0 disables (uses full vocabulary). Note: only applies when `temperature` > 0. */
|
|
34
|
+
topK?: number;
|
|
35
|
+
/** Selects the smallest token set whose probability exceeds P, where P is between 0 - 1, 1 disables. Note: only applies when `temperature` > 0. */
|
|
36
|
+
topP?: number;
|
|
37
|
+
/** Force system to keep model in RAM. */
|
|
38
|
+
useMlock?: boolean;
|
|
39
|
+
/** Use mmap if possible. */
|
|
40
|
+
useMmap?: boolean;
|
|
41
|
+
/** Only load the vocabulary, no weights. */
|
|
42
|
+
vocabOnly?: boolean;
|
|
43
|
+
}
|
|
44
|
+
export declare function createLlamaModel(inputs: LlamaBaseCppInputs): LlamaModel;
|
|
45
|
+
export declare function createLlamaContext(model: LlamaModel, inputs: LlamaBaseCppInputs): LlamaContext;
|
|
46
|
+
export declare function createLlamaSession(context: LlamaContext): LlamaChatSession;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { LlamaModel, LlamaContext, LlamaChatSession } from "node-llama-cpp";
|
|
2
|
+
export function createLlamaModel(inputs) {
|
|
3
|
+
const options = {
|
|
4
|
+
gpuLayers: inputs?.gpuLayers,
|
|
5
|
+
modelPath: inputs.modelPath,
|
|
6
|
+
useMlock: inputs?.useMlock,
|
|
7
|
+
useMmap: inputs?.useMmap,
|
|
8
|
+
vocabOnly: inputs?.vocabOnly,
|
|
9
|
+
};
|
|
10
|
+
return new LlamaModel(options);
|
|
11
|
+
}
|
|
12
|
+
export function createLlamaContext(model, inputs) {
|
|
13
|
+
const options = {
|
|
14
|
+
batchSize: inputs?.batchSize,
|
|
15
|
+
contextSize: inputs?.contextSize,
|
|
16
|
+
embedding: inputs?.embedding,
|
|
17
|
+
f16Kv: inputs?.f16Kv,
|
|
18
|
+
logitsAll: inputs?.logitsAll,
|
|
19
|
+
model,
|
|
20
|
+
prependBos: inputs?.prependBos,
|
|
21
|
+
seed: inputs?.seed,
|
|
22
|
+
threads: inputs?.threads,
|
|
23
|
+
};
|
|
24
|
+
return new LlamaContext(options);
|
|
25
|
+
}
|
|
26
|
+
export function createLlamaSession(context) {
|
|
27
|
+
return new LlamaChatSession({ context });
|
|
28
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatFunctionDefinitions = void 0;
|
|
4
|
+
function isAnyOfProp(prop) {
|
|
5
|
+
return (prop.anyOf !== undefined &&
|
|
6
|
+
Array.isArray(prop.anyOf));
|
|
7
|
+
}
|
|
8
|
+
// When OpenAI use functions in the prompt, they format them as TypeScript definitions rather than OpenAPI JSON schemas.
|
|
9
|
+
// This function converts the JSON schemas into TypeScript definitions.
|
|
10
|
+
function formatFunctionDefinitions(functions) {
|
|
11
|
+
const lines = ["namespace functions {", ""];
|
|
12
|
+
for (const f of functions) {
|
|
13
|
+
if (f.description) {
|
|
14
|
+
lines.push(`// ${f.description}`);
|
|
15
|
+
}
|
|
16
|
+
if (Object.keys(f.parameters.properties ?? {}).length > 0) {
|
|
17
|
+
lines.push(`type ${f.name} = (_: {`);
|
|
18
|
+
lines.push(formatObjectProperties(f.parameters, 0));
|
|
19
|
+
lines.push("}) => any;");
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
lines.push(`type ${f.name} = () => any;`);
|
|
23
|
+
}
|
|
24
|
+
lines.push("");
|
|
25
|
+
}
|
|
26
|
+
lines.push("} // namespace functions");
|
|
27
|
+
return lines.join("\n");
|
|
28
|
+
}
|
|
29
|
+
exports.formatFunctionDefinitions = formatFunctionDefinitions;
|
|
30
|
+
// Format just the properties of an object (not including the surrounding braces)
|
|
31
|
+
function formatObjectProperties(obj, indent) {
|
|
32
|
+
const lines = [];
|
|
33
|
+
for (const [name, param] of Object.entries(obj.properties ?? {})) {
|
|
34
|
+
if (param.description && indent < 2) {
|
|
35
|
+
lines.push(`// ${param.description}`);
|
|
36
|
+
}
|
|
37
|
+
if (obj.required?.includes(name)) {
|
|
38
|
+
lines.push(`${name}: ${formatType(param, indent)},`);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
lines.push(`${name}?: ${formatType(param, indent)},`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return lines.map((line) => " ".repeat(indent) + line).join("\n");
|
|
45
|
+
}
|
|
46
|
+
// Format a single property type
|
|
47
|
+
function formatType(param, indent) {
|
|
48
|
+
if (isAnyOfProp(param)) {
|
|
49
|
+
return param.anyOf.map((v) => formatType(v, indent)).join(" | ");
|
|
50
|
+
}
|
|
51
|
+
switch (param.type) {
|
|
52
|
+
case "string":
|
|
53
|
+
if (param.enum) {
|
|
54
|
+
return param.enum.map((v) => `"${v}"`).join(" | ");
|
|
55
|
+
}
|
|
56
|
+
return "string";
|
|
57
|
+
case "number":
|
|
58
|
+
if (param.enum) {
|
|
59
|
+
return param.enum.map((v) => `${v}`).join(" | ");
|
|
60
|
+
}
|
|
61
|
+
return "number";
|
|
62
|
+
case "integer":
|
|
63
|
+
if (param.enum) {
|
|
64
|
+
return param.enum.map((v) => `${v}`).join(" | ");
|
|
65
|
+
}
|
|
66
|
+
return "number";
|
|
67
|
+
case "boolean":
|
|
68
|
+
return "boolean";
|
|
69
|
+
case "null":
|
|
70
|
+
return "null";
|
|
71
|
+
case "object":
|
|
72
|
+
return ["{", formatObjectProperties(param, indent + 2), "}"].join("\n");
|
|
73
|
+
case "array":
|
|
74
|
+
if (param.items) {
|
|
75
|
+
return `${formatType(param.items, indent)}[]`;
|
|
76
|
+
}
|
|
77
|
+
return "any[]";
|
|
78
|
+
default:
|
|
79
|
+
return "";
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formatting function definitions for calculating openai function defination token usage.
|
|
3
|
+
*
|
|
4
|
+
* https://github.com/hmarr/openai-chat-tokens/blob/main/src/functions.ts
|
|
5
|
+
* (c) 2023 Harry Marr
|
|
6
|
+
* MIT license
|
|
7
|
+
*/
|
|
8
|
+
import OpenAI from "openai";
|
|
9
|
+
type OpenAIFunction = OpenAI.Chat.ChatCompletionCreateParams.Function;
|
|
10
|
+
export interface FunctionDef extends Omit<OpenAIFunction, "parameters"> {
|
|
11
|
+
name: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
parameters: ObjectProp;
|
|
14
|
+
}
|
|
15
|
+
interface ObjectProp {
|
|
16
|
+
type: "object";
|
|
17
|
+
properties?: {
|
|
18
|
+
[key: string]: Prop;
|
|
19
|
+
};
|
|
20
|
+
required?: string[];
|
|
21
|
+
}
|
|
22
|
+
interface AnyOfProp {
|
|
23
|
+
anyOf: Prop[];
|
|
24
|
+
}
|
|
25
|
+
type Prop = {
|
|
26
|
+
description?: string;
|
|
27
|
+
} & (AnyOfProp | ObjectProp | {
|
|
28
|
+
type: "string";
|
|
29
|
+
enum?: string[];
|
|
30
|
+
} | {
|
|
31
|
+
type: "number" | "integer";
|
|
32
|
+
minimum?: number;
|
|
33
|
+
maximum?: number;
|
|
34
|
+
enum?: number[];
|
|
35
|
+
} | {
|
|
36
|
+
type: "boolean";
|
|
37
|
+
} | {
|
|
38
|
+
type: "null";
|
|
39
|
+
} | {
|
|
40
|
+
type: "array";
|
|
41
|
+
items?: Prop;
|
|
42
|
+
});
|
|
43
|
+
export declare function formatFunctionDefinitions(functions: FunctionDef[]): string;
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
function isAnyOfProp(prop) {
|
|
2
|
+
return (prop.anyOf !== undefined &&
|
|
3
|
+
Array.isArray(prop.anyOf));
|
|
4
|
+
}
|
|
5
|
+
// When OpenAI use functions in the prompt, they format them as TypeScript definitions rather than OpenAPI JSON schemas.
|
|
6
|
+
// This function converts the JSON schemas into TypeScript definitions.
|
|
7
|
+
export function formatFunctionDefinitions(functions) {
|
|
8
|
+
const lines = ["namespace functions {", ""];
|
|
9
|
+
for (const f of functions) {
|
|
10
|
+
if (f.description) {
|
|
11
|
+
lines.push(`// ${f.description}`);
|
|
12
|
+
}
|
|
13
|
+
if (Object.keys(f.parameters.properties ?? {}).length > 0) {
|
|
14
|
+
lines.push(`type ${f.name} = (_: {`);
|
|
15
|
+
lines.push(formatObjectProperties(f.parameters, 0));
|
|
16
|
+
lines.push("}) => any;");
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
lines.push(`type ${f.name} = () => any;`);
|
|
20
|
+
}
|
|
21
|
+
lines.push("");
|
|
22
|
+
}
|
|
23
|
+
lines.push("} // namespace functions");
|
|
24
|
+
return lines.join("\n");
|
|
25
|
+
}
|
|
26
|
+
// Format just the properties of an object (not including the surrounding braces)
|
|
27
|
+
function formatObjectProperties(obj, indent) {
|
|
28
|
+
const lines = [];
|
|
29
|
+
for (const [name, param] of Object.entries(obj.properties ?? {})) {
|
|
30
|
+
if (param.description && indent < 2) {
|
|
31
|
+
lines.push(`// ${param.description}`);
|
|
32
|
+
}
|
|
33
|
+
if (obj.required?.includes(name)) {
|
|
34
|
+
lines.push(`${name}: ${formatType(param, indent)},`);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
lines.push(`${name}?: ${formatType(param, indent)},`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return lines.map((line) => " ".repeat(indent) + line).join("\n");
|
|
41
|
+
}
|
|
42
|
+
// Format a single property type
|
|
43
|
+
function formatType(param, indent) {
|
|
44
|
+
if (isAnyOfProp(param)) {
|
|
45
|
+
return param.anyOf.map((v) => formatType(v, indent)).join(" | ");
|
|
46
|
+
}
|
|
47
|
+
switch (param.type) {
|
|
48
|
+
case "string":
|
|
49
|
+
if (param.enum) {
|
|
50
|
+
return param.enum.map((v) => `"${v}"`).join(" | ");
|
|
51
|
+
}
|
|
52
|
+
return "string";
|
|
53
|
+
case "number":
|
|
54
|
+
if (param.enum) {
|
|
55
|
+
return param.enum.map((v) => `${v}`).join(" | ");
|
|
56
|
+
}
|
|
57
|
+
return "number";
|
|
58
|
+
case "integer":
|
|
59
|
+
if (param.enum) {
|
|
60
|
+
return param.enum.map((v) => `${v}`).join(" | ");
|
|
61
|
+
}
|
|
62
|
+
return "number";
|
|
63
|
+
case "boolean":
|
|
64
|
+
return "boolean";
|
|
65
|
+
case "null":
|
|
66
|
+
return "null";
|
|
67
|
+
case "object":
|
|
68
|
+
return ["{", formatObjectProperties(param, indent + 2), "}"].join("\n");
|
|
69
|
+
case "array":
|
|
70
|
+
if (param.items) {
|
|
71
|
+
return `${formatType(param.items, indent)}[]`;
|
|
72
|
+
}
|
|
73
|
+
return "any[]";
|
|
74
|
+
default:
|
|
75
|
+
return "";
|
|
76
|
+
}
|
|
77
|
+
}
|
package/dist/util/openapi.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export declare class OpenAPISpec {
|
|
|
7
7
|
getParametersStrict(): Record<string, OpenAPIV3.ParameterObject | OpenAPIV3_1.ReferenceObject>;
|
|
8
8
|
getSchemasStrict(): Record<string, OpenAPIV3_1.SchemaObject>;
|
|
9
9
|
getRequestBodiesStrict(): Record<string, OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.RequestBodyObject>;
|
|
10
|
-
getPathStrict(path: string): Omit<OpenAPIV3.PathItemObject<{}>, "
|
|
10
|
+
getPathStrict(path: string): Omit<OpenAPIV3.PathItemObject<{}>, "parameters" | "servers"> & {
|
|
11
11
|
servers?: OpenAPIV3_1.ServerObject[] | undefined;
|
|
12
12
|
parameters?: (OpenAPIV3.ParameterObject | OpenAPIV3_1.ReferenceObject)[] | undefined;
|
|
13
13
|
} & {
|
|
@@ -59,7 +59,7 @@ export declare class OpenAPISpec {
|
|
|
59
59
|
deprecated?: boolean | undefined;
|
|
60
60
|
security?: OpenAPIV3.SecurityRequirementObject[] | undefined;
|
|
61
61
|
servers?: OpenAPIV3.ServerObject[] | undefined;
|
|
62
|
-
}, "callbacks" | "
|
|
62
|
+
}, "callbacks" | "parameters" | "servers" | "responses" | "requestBody"> & {
|
|
63
63
|
parameters?: (OpenAPIV3.ParameterObject | OpenAPIV3_1.ReferenceObject)[] | undefined;
|
|
64
64
|
requestBody?: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.RequestBodyObject | undefined;
|
|
65
65
|
responses?: OpenAPIV3_1.ResponsesObject | undefined;
|