langchain 0.0.151 → 0.0.153
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/cache/cloudflare_kv.cjs +1 -0
- package/cache/cloudflare_kv.d.ts +1 -0
- package/cache/cloudflare_kv.js +1 -0
- package/chat_models/fireworks.cjs +1 -0
- package/chat_models/fireworks.d.ts +1 -0
- package/chat_models/fireworks.js +1 -0
- package/dist/agents/chat/index.cjs +1 -1
- package/dist/agents/chat/index.js +1 -1
- package/dist/agents/chat_convo/index.cjs +1 -1
- package/dist/agents/chat_convo/index.js +1 -1
- package/dist/agents/executor.cjs +9 -2
- package/dist/agents/executor.js +9 -2
- package/dist/agents/openai/index.cjs +1 -1
- package/dist/agents/openai/index.js +1 -1
- package/dist/agents/structured_chat/index.cjs +1 -1
- package/dist/agents/structured_chat/index.js +1 -1
- package/dist/agents/xml/index.cjs +1 -1
- package/dist/agents/xml/index.js +1 -1
- package/dist/base_language/count_tokens.cjs +2 -1
- package/dist/base_language/count_tokens.js +2 -1
- package/dist/base_language/index.cjs +5 -3
- package/dist/base_language/index.d.ts +1 -1
- package/dist/base_language/index.js +4 -3
- package/dist/cache/cloudflare_kv.cjs +61 -0
- package/dist/cache/cloudflare_kv.d.ts +29 -0
- package/dist/cache/cloudflare_kv.js +57 -0
- package/dist/chains/openai_functions/openapi.cjs +1 -1
- package/dist/chains/openai_functions/openapi.js +1 -1
- package/dist/chains/openai_functions/structured_output.d.ts +2 -2
- package/dist/chains/question_answering/map_reduce_prompts.cjs +2 -3
- package/dist/chains/question_answering/map_reduce_prompts.js +2 -3
- package/dist/chains/question_answering/refine_prompts.cjs +2 -2
- package/dist/chains/question_answering/refine_prompts.js +2 -2
- package/dist/chains/question_answering/stuff_prompts.cjs +1 -2
- package/dist/chains/question_answering/stuff_prompts.js +1 -2
- package/dist/chat_models/base.d.ts +1 -1
- package/dist/chat_models/fireworks.cjs +81 -0
- package/dist/chat_models/fireworks.d.ts +33 -0
- package/dist/chat_models/fireworks.js +77 -0
- package/dist/chat_models/ollama.cjs +25 -12
- package/dist/chat_models/ollama.d.ts +2 -3
- package/dist/chat_models/ollama.js +25 -12
- package/dist/chat_models/openai.d.ts +2 -2
- package/dist/document_loaders/web/pdf.cjs +87 -0
- package/dist/document_loaders/web/pdf.d.ts +17 -0
- package/dist/document_loaders/web/pdf.js +83 -0
- package/dist/evaluation/agents/prompt.cjs +2 -3
- package/dist/evaluation/agents/prompt.js +2 -3
- package/dist/experimental/plan_and_execute/prompt.cjs +1 -1
- package/dist/experimental/plan_and_execute/prompt.js +1 -1
- package/dist/llms/fireworks.cjs +92 -0
- package/dist/llms/fireworks.d.ts +33 -0
- package/dist/llms/fireworks.js +88 -0
- package/dist/llms/llama_cpp.cjs +10 -4
- package/dist/llms/llama_cpp.d.ts +2 -1
- package/dist/llms/llama_cpp.js +10 -4
- package/dist/llms/ollama.cjs +29 -14
- package/dist/llms/ollama.d.ts +3 -4
- package/dist/llms/ollama.js +29 -14
- package/dist/llms/openai-chat.cjs +1 -5
- package/dist/llms/openai-chat.d.ts +1 -1
- package/dist/llms/openai-chat.js +1 -5
- package/dist/llms/openai.cjs +3 -4
- package/dist/llms/openai.d.ts +2 -2
- package/dist/llms/openai.js +3 -4
- package/dist/load/import_constants.cjs +3 -0
- package/dist/load/import_constants.js +3 -0
- package/dist/load/import_map.cjs +4 -2
- package/dist/load/import_map.d.ts +2 -0
- package/dist/load/import_map.js +2 -0
- package/dist/prompts/chat.cjs +12 -1
- package/dist/prompts/chat.d.ts +8 -0
- package/dist/prompts/chat.js +12 -1
- package/dist/schema/output_parser.cjs +38 -6
- package/dist/schema/output_parser.d.ts +20 -5
- package/dist/schema/output_parser.js +38 -6
- package/dist/schema/runnable/base.cjs +65 -10
- package/dist/schema/runnable/base.d.ts +17 -3
- package/dist/schema/runnable/base.js +65 -10
- package/dist/stores/message/cloudflare_d1.cjs +134 -0
- package/dist/stores/message/cloudflare_d1.d.ts +49 -0
- package/dist/stores/message/cloudflare_d1.js +130 -0
- package/dist/types/openai-types.d.ts +2 -0
- package/dist/util/ollama.cjs +2 -2
- package/dist/util/ollama.d.ts +6 -0
- package/dist/util/ollama.js +2 -2
- package/document_loaders/web/pdf.cjs +1 -0
- package/document_loaders/web/pdf.d.ts +1 -0
- package/document_loaders/web/pdf.js +1 -0
- package/llms/fireworks.cjs +1 -0
- package/llms/fireworks.d.ts +1 -0
- package/llms/fireworks.js +1 -0
- package/package.json +46 -1
- package/stores/message/cloudflare_d1.cjs +1 -0
- package/stores/message/cloudflare_d1.d.ts +1 -0
- package/stores/message/cloudflare_d1.js +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../dist/cache/cloudflare_kv.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/cache/cloudflare_kv.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/cache/cloudflare_kv.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../dist/chat_models/fireworks.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/chat_models/fireworks.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/chat_models/fireworks.js'
|
|
@@ -91,7 +91,7 @@ class ChatAgent extends agent_js_1.Agent {
|
|
|
91
91
|
chat_js_1.SystemMessagePromptTemplate.fromTemplate(template),
|
|
92
92
|
chat_js_1.HumanMessagePromptTemplate.fromTemplate(humanMessageTemplate),
|
|
93
93
|
];
|
|
94
|
-
return chat_js_1.ChatPromptTemplate.
|
|
94
|
+
return chat_js_1.ChatPromptTemplate.fromMessages(messages);
|
|
95
95
|
}
|
|
96
96
|
/**
|
|
97
97
|
* Creates a ChatAgent instance using a language model, tools, and
|
|
@@ -88,7 +88,7 @@ export class ChatAgent extends Agent {
|
|
|
88
88
|
SystemMessagePromptTemplate.fromTemplate(template),
|
|
89
89
|
HumanMessagePromptTemplate.fromTemplate(humanMessageTemplate),
|
|
90
90
|
];
|
|
91
|
-
return ChatPromptTemplate.
|
|
91
|
+
return ChatPromptTemplate.fromMessages(messages);
|
|
92
92
|
}
|
|
93
93
|
/**
|
|
94
94
|
* Creates a ChatAgent instance using a language model, tools, and
|
|
@@ -110,7 +110,7 @@ class ChatConversationalAgent extends agent_js_1.Agent {
|
|
|
110
110
|
chat_js_1.HumanMessagePromptTemplate.fromTemplate(renderedHumanMessage),
|
|
111
111
|
new chat_js_1.MessagesPlaceholder("agent_scratchpad"),
|
|
112
112
|
];
|
|
113
|
-
return chat_js_1.ChatPromptTemplate.
|
|
113
|
+
return chat_js_1.ChatPromptTemplate.fromMessages(messages);
|
|
114
114
|
}
|
|
115
115
|
/**
|
|
116
116
|
* Creates an instance of the ChatConversationalAgent class from a
|
|
@@ -107,7 +107,7 @@ export class ChatConversationalAgent extends Agent {
|
|
|
107
107
|
HumanMessagePromptTemplate.fromTemplate(renderedHumanMessage),
|
|
108
108
|
new MessagesPlaceholder("agent_scratchpad"),
|
|
109
109
|
];
|
|
110
|
-
return ChatPromptTemplate.
|
|
110
|
+
return ChatPromptTemplate.fromMessages(messages);
|
|
111
111
|
}
|
|
112
112
|
/**
|
|
113
113
|
* Creates an instance of the ChatConversationalAgent class from a
|
package/dist/agents/executor.cjs
CHANGED
|
@@ -147,8 +147,15 @@ class AgentExecutor extends base_js_1.BaseChain {
|
|
|
147
147
|
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
148
148
|
if (e instanceof output_parser_js_1.OutputParserException) {
|
|
149
149
|
let observation;
|
|
150
|
+
let text = e.message;
|
|
150
151
|
if (this.handleParsingErrors === true) {
|
|
151
|
-
|
|
152
|
+
if (e.sendToLLM) {
|
|
153
|
+
observation = e.observation;
|
|
154
|
+
text = e.llmOutput ?? "";
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
observation = "Invalid or incomplete response";
|
|
158
|
+
}
|
|
152
159
|
}
|
|
153
160
|
else if (typeof this.handleParsingErrors === "string") {
|
|
154
161
|
observation = this.handleParsingErrors;
|
|
@@ -162,7 +169,7 @@ class AgentExecutor extends base_js_1.BaseChain {
|
|
|
162
169
|
output = {
|
|
163
170
|
tool: "_Exception",
|
|
164
171
|
toolInput: observation,
|
|
165
|
-
log:
|
|
172
|
+
log: text,
|
|
166
173
|
};
|
|
167
174
|
}
|
|
168
175
|
else {
|
package/dist/agents/executor.js
CHANGED
|
@@ -143,8 +143,15 @@ export class AgentExecutor extends BaseChain {
|
|
|
143
143
|
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
144
144
|
if (e instanceof OutputParserException) {
|
|
145
145
|
let observation;
|
|
146
|
+
let text = e.message;
|
|
146
147
|
if (this.handleParsingErrors === true) {
|
|
147
|
-
|
|
148
|
+
if (e.sendToLLM) {
|
|
149
|
+
observation = e.observation;
|
|
150
|
+
text = e.llmOutput ?? "";
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
observation = "Invalid or incomplete response";
|
|
154
|
+
}
|
|
148
155
|
}
|
|
149
156
|
else if (typeof this.handleParsingErrors === "string") {
|
|
150
157
|
observation = this.handleParsingErrors;
|
|
@@ -158,7 +165,7 @@ export class AgentExecutor extends BaseChain {
|
|
|
158
165
|
output = {
|
|
159
166
|
tool: "_Exception",
|
|
160
167
|
toolInput: observation,
|
|
161
|
-
log:
|
|
168
|
+
log: text,
|
|
162
169
|
};
|
|
163
170
|
}
|
|
164
171
|
else {
|
|
@@ -102,7 +102,7 @@ class OpenAIAgent extends agent_js_1.Agent {
|
|
|
102
102
|
*/
|
|
103
103
|
static createPrompt(_tools, fields) {
|
|
104
104
|
const { prefix = prompt_js_1.PREFIX } = fields || {};
|
|
105
|
-
return chat_js_1.ChatPromptTemplate.
|
|
105
|
+
return chat_js_1.ChatPromptTemplate.fromMessages([
|
|
106
106
|
chat_js_1.SystemMessagePromptTemplate.fromTemplate(prefix),
|
|
107
107
|
new chat_js_1.MessagesPlaceholder("chat_history"),
|
|
108
108
|
chat_js_1.HumanMessagePromptTemplate.fromTemplate("{input}"),
|
|
@@ -98,7 +98,7 @@ export class OpenAIAgent extends Agent {
|
|
|
98
98
|
*/
|
|
99
99
|
static createPrompt(_tools, fields) {
|
|
100
100
|
const { prefix = PREFIX } = fields || {};
|
|
101
|
-
return ChatPromptTemplate.
|
|
101
|
+
return ChatPromptTemplate.fromMessages([
|
|
102
102
|
SystemMessagePromptTemplate.fromTemplate(prefix),
|
|
103
103
|
new MessagesPlaceholder("chat_history"),
|
|
104
104
|
HumanMessagePromptTemplate.fromTemplate("{input}"),
|
|
@@ -120,7 +120,7 @@ class StructuredChatAgent extends agent_js_1.Agent {
|
|
|
120
120
|
inputVariables,
|
|
121
121
|
})),
|
|
122
122
|
];
|
|
123
|
-
return chat_js_1.ChatPromptTemplate.
|
|
123
|
+
return chat_js_1.ChatPromptTemplate.fromMessages(messages);
|
|
124
124
|
}
|
|
125
125
|
/**
|
|
126
126
|
* Creates a StructuredChatAgent from an LLM and a list of tools.
|
|
@@ -117,7 +117,7 @@ export class StructuredChatAgent extends Agent {
|
|
|
117
117
|
inputVariables,
|
|
118
118
|
})),
|
|
119
119
|
];
|
|
120
|
-
return ChatPromptTemplate.
|
|
120
|
+
return ChatPromptTemplate.fromMessages(messages);
|
|
121
121
|
}
|
|
122
122
|
/**
|
|
123
123
|
* Creates a StructuredChatAgent from an LLM and a list of tools.
|
|
@@ -65,7 +65,7 @@ class XMLAgent extends agent_js_1.BaseSingleActionAgent {
|
|
|
65
65
|
return ["input"];
|
|
66
66
|
}
|
|
67
67
|
static createPrompt() {
|
|
68
|
-
return chat_js_1.ChatPromptTemplate.
|
|
68
|
+
return chat_js_1.ChatPromptTemplate.fromMessages([
|
|
69
69
|
chat_js_1.HumanMessagePromptTemplate.fromTemplate(prompt_js_1.AGENT_INSTRUCTIONS),
|
|
70
70
|
chat_js_1.AIMessagePromptTemplate.fromTemplate("{intermediate_steps}"),
|
|
71
71
|
]);
|
package/dist/agents/xml/index.js
CHANGED
|
@@ -61,7 +61,7 @@ export class XMLAgent extends BaseSingleActionAgent {
|
|
|
61
61
|
return ["input"];
|
|
62
62
|
}
|
|
63
63
|
static createPrompt() {
|
|
64
|
-
return ChatPromptTemplate.
|
|
64
|
+
return ChatPromptTemplate.fromMessages([
|
|
65
65
|
HumanMessagePromptTemplate.fromTemplate(AGENT_INSTRUCTIONS),
|
|
66
66
|
AIMessagePromptTemplate.fromTemplate("{intermediate_steps}"),
|
|
67
67
|
]);
|
|
@@ -58,11 +58,12 @@ exports.getModelContextSize = getModelContextSize;
|
|
|
58
58
|
const calculateMaxTokens = async ({ prompt, modelName, }) => {
|
|
59
59
|
let numTokens;
|
|
60
60
|
try {
|
|
61
|
-
numTokens = (await (0, tiktoken_js_1.encodingForModel)(modelName)).encode(prompt).length;
|
|
61
|
+
numTokens = (await (0, tiktoken_js_1.encodingForModel)((0, exports.getModelNameForTiktoken)(modelName))).encode(prompt).length;
|
|
62
62
|
}
|
|
63
63
|
catch (error) {
|
|
64
64
|
console.warn("Failed to calculate number of tokens, falling back to approximate count");
|
|
65
65
|
// fallback to approximate calculation if tiktoken is not available
|
|
66
|
+
// each token is ~4 characters: https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them#
|
|
66
67
|
numTokens = Math.ceil(prompt.length / 4);
|
|
67
68
|
}
|
|
68
69
|
const maxTokens = (0, exports.getModelContextSize)(modelName);
|
|
@@ -52,11 +52,12 @@ export const getModelContextSize = (modelName) => {
|
|
|
52
52
|
export const calculateMaxTokens = async ({ prompt, modelName, }) => {
|
|
53
53
|
let numTokens;
|
|
54
54
|
try {
|
|
55
|
-
numTokens = (await encodingForModel(modelName)).encode(prompt).length;
|
|
55
|
+
numTokens = (await encodingForModel(getModelNameForTiktoken(modelName))).encode(prompt).length;
|
|
56
56
|
}
|
|
57
57
|
catch (error) {
|
|
58
58
|
console.warn("Failed to calculate number of tokens, falling back to approximate count");
|
|
59
59
|
// fallback to approximate calculation if tiktoken is not available
|
|
60
|
+
// each token is ~4 characters: https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them#
|
|
60
61
|
numTokens = Math.ceil(prompt.length / 4);
|
|
61
62
|
}
|
|
62
63
|
const maxTokens = getModelContextSize(modelName);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateMaxTokens = exports.BaseLanguageModel = exports.BaseLangChain = void 0;
|
|
3
|
+
exports.getModelContextSize = exports.calculateMaxTokens = exports.BaseLanguageModel = exports.BaseLangChain = void 0;
|
|
4
4
|
const index_js_1 = require("../schema/index.cjs");
|
|
5
5
|
const async_caller_js_1 = require("../util/async_caller.cjs");
|
|
6
6
|
const count_tokens_js_1 = require("./count_tokens.cjs");
|
|
@@ -155,8 +155,10 @@ class BaseLanguageModel extends BaseLangChain {
|
|
|
155
155
|
}
|
|
156
156
|
exports.BaseLanguageModel = BaseLanguageModel;
|
|
157
157
|
/*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
158
|
+
* Export utility functions for token calculations:
|
|
159
|
+
* - calculateMaxTokens: Calculate max tokens for a given model and prompt (the model context size - tokens in prompt).
|
|
160
|
+
* - getModelContextSize: Get the context size for a specific model.
|
|
160
161
|
*/
|
|
161
162
|
var count_tokens_js_2 = require("./count_tokens.cjs");
|
|
162
163
|
Object.defineProperty(exports, "calculateMaxTokens", { enumerable: true, get: function () { return count_tokens_js_2.calculateMaxTokens; } });
|
|
164
|
+
Object.defineProperty(exports, "getModelContextSize", { enumerable: true, get: function () { return count_tokens_js_2.getModelContextSize; } });
|
|
@@ -101,4 +101,4 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
|
|
|
101
101
|
*/
|
|
102
102
|
static deserialize(data: SerializedLLM): Promise<BaseLanguageModel>;
|
|
103
103
|
}
|
|
104
|
-
export { calculateMaxTokens } from "./count_tokens.js";
|
|
104
|
+
export { calculateMaxTokens, getModelContextSize } from "./count_tokens.js";
|
|
@@ -150,7 +150,8 @@ export class BaseLanguageModel extends BaseLangChain {
|
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
152
|
/*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
153
|
+
* Export utility functions for token calculations:
|
|
154
|
+
* - calculateMaxTokens: Calculate max tokens for a given model and prompt (the model context size - tokens in prompt).
|
|
155
|
+
* - getModelContextSize: Get the context size for a specific model.
|
|
155
156
|
*/
|
|
156
|
-
export { calculateMaxTokens } from "./count_tokens.js";
|
|
157
|
+
export { calculateMaxTokens, getModelContextSize } from "./count_tokens.js";
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CloudflareKVCache = void 0;
|
|
4
|
+
const index_js_1 = require("../schema/index.cjs");
|
|
5
|
+
const base_js_1 = require("./base.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Represents a specific implementation of a caching mechanism using Cloudflare KV
|
|
8
|
+
* as the underlying storage system. It extends the `BaseCache` class and
|
|
9
|
+
* overrides its methods to provide the Cloudflare KV-specific logic.
|
|
10
|
+
*/
|
|
11
|
+
class CloudflareKVCache extends index_js_1.BaseCache {
|
|
12
|
+
constructor(binding) {
|
|
13
|
+
super();
|
|
14
|
+
Object.defineProperty(this, "binding", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: void 0
|
|
19
|
+
});
|
|
20
|
+
this.binding = binding;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves data from the cache. It constructs a cache key from the given
|
|
24
|
+
* `prompt` and `llmKey`, and retrieves the corresponding value from the
|
|
25
|
+
* Cloudflare KV namespace.
|
|
26
|
+
* @param prompt The prompt used to construct the cache key.
|
|
27
|
+
* @param llmKey The LLM key used to construct the cache key.
|
|
28
|
+
* @returns An array of Generations if found, null otherwise.
|
|
29
|
+
*/
|
|
30
|
+
async lookup(prompt, llmKey) {
|
|
31
|
+
let idx = 0;
|
|
32
|
+
let key = (0, base_js_1.getCacheKey)(prompt, llmKey, String(idx));
|
|
33
|
+
let value = await this.binding.get(key);
|
|
34
|
+
const generations = [];
|
|
35
|
+
while (value) {
|
|
36
|
+
if (!value) {
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
generations.push({ text: value });
|
|
40
|
+
idx += 1;
|
|
41
|
+
key = (0, base_js_1.getCacheKey)(prompt, llmKey, String(idx));
|
|
42
|
+
value = await this.binding.get(key);
|
|
43
|
+
}
|
|
44
|
+
return generations.length > 0 ? generations : null;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Updates the cache with new data. It constructs a cache key from the
|
|
48
|
+
* given `prompt` and `llmKey`, and stores the `value` in the Cloudflare KV
|
|
49
|
+
* namespace.
|
|
50
|
+
* @param prompt The prompt used to construct the cache key.
|
|
51
|
+
* @param llmKey The LLM key used to construct the cache key.
|
|
52
|
+
* @param value The value to be stored in the cache.
|
|
53
|
+
*/
|
|
54
|
+
async update(prompt, llmKey, value) {
|
|
55
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
56
|
+
const key = (0, base_js_1.getCacheKey)(prompt, llmKey, String(i));
|
|
57
|
+
await this.binding.put(key, value[i].text);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.CloudflareKVCache = CloudflareKVCache;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { KVNamespace } from "@cloudflare/workers-types";
|
|
2
|
+
import { BaseCache, Generation } from "../schema/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Represents a specific implementation of a caching mechanism using Cloudflare KV
|
|
5
|
+
* as the underlying storage system. It extends the `BaseCache` class and
|
|
6
|
+
* overrides its methods to provide the Cloudflare KV-specific logic.
|
|
7
|
+
*/
|
|
8
|
+
export declare class CloudflareKVCache extends BaseCache {
|
|
9
|
+
private binding;
|
|
10
|
+
constructor(binding: KVNamespace);
|
|
11
|
+
/**
|
|
12
|
+
* Retrieves data from the cache. It constructs a cache key from the given
|
|
13
|
+
* `prompt` and `llmKey`, and retrieves the corresponding value from the
|
|
14
|
+
* Cloudflare KV namespace.
|
|
15
|
+
* @param prompt The prompt used to construct the cache key.
|
|
16
|
+
* @param llmKey The LLM key used to construct the cache key.
|
|
17
|
+
* @returns An array of Generations if found, null otherwise.
|
|
18
|
+
*/
|
|
19
|
+
lookup(prompt: string, llmKey: string): Promise<Generation[] | null>;
|
|
20
|
+
/**
|
|
21
|
+
* Updates the cache with new data. It constructs a cache key from the
|
|
22
|
+
* given `prompt` and `llmKey`, and stores the `value` in the Cloudflare KV
|
|
23
|
+
* namespace.
|
|
24
|
+
* @param prompt The prompt used to construct the cache key.
|
|
25
|
+
* @param llmKey The LLM key used to construct the cache key.
|
|
26
|
+
* @param value The value to be stored in the cache.
|
|
27
|
+
*/
|
|
28
|
+
update(prompt: string, llmKey: string, value: Generation[]): Promise<void>;
|
|
29
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { BaseCache } from "../schema/index.js";
|
|
2
|
+
import { getCacheKey } from "./base.js";
|
|
3
|
+
/**
|
|
4
|
+
* Represents a specific implementation of a caching mechanism using Cloudflare KV
|
|
5
|
+
* as the underlying storage system. It extends the `BaseCache` class and
|
|
6
|
+
* overrides its methods to provide the Cloudflare KV-specific logic.
|
|
7
|
+
*/
|
|
8
|
+
export class CloudflareKVCache extends BaseCache {
|
|
9
|
+
constructor(binding) {
|
|
10
|
+
super();
|
|
11
|
+
Object.defineProperty(this, "binding", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: void 0
|
|
16
|
+
});
|
|
17
|
+
this.binding = binding;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves data from the cache. It constructs a cache key from the given
|
|
21
|
+
* `prompt` and `llmKey`, and retrieves the corresponding value from the
|
|
22
|
+
* Cloudflare KV namespace.
|
|
23
|
+
* @param prompt The prompt used to construct the cache key.
|
|
24
|
+
* @param llmKey The LLM key used to construct the cache key.
|
|
25
|
+
* @returns An array of Generations if found, null otherwise.
|
|
26
|
+
*/
|
|
27
|
+
async lookup(prompt, llmKey) {
|
|
28
|
+
let idx = 0;
|
|
29
|
+
let key = getCacheKey(prompt, llmKey, String(idx));
|
|
30
|
+
let value = await this.binding.get(key);
|
|
31
|
+
const generations = [];
|
|
32
|
+
while (value) {
|
|
33
|
+
if (!value) {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
generations.push({ text: value });
|
|
37
|
+
idx += 1;
|
|
38
|
+
key = getCacheKey(prompt, llmKey, String(idx));
|
|
39
|
+
value = await this.binding.get(key);
|
|
40
|
+
}
|
|
41
|
+
return generations.length > 0 ? generations : null;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Updates the cache with new data. It constructs a cache key from the
|
|
45
|
+
* given `prompt` and `llmKey`, and stores the `value` in the Cloudflare KV
|
|
46
|
+
* namespace.
|
|
47
|
+
* @param prompt The prompt used to construct the cache key.
|
|
48
|
+
* @param llmKey The LLM key used to construct the cache key.
|
|
49
|
+
* @param value The value to be stored in the cache.
|
|
50
|
+
*/
|
|
51
|
+
async update(prompt, llmKey, value) {
|
|
52
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
53
|
+
const key = getCacheKey(prompt, llmKey, String(i));
|
|
54
|
+
await this.binding.put(key, value[i].text);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -375,7 +375,7 @@ async function createOpenAPIChain(spec, options = {}) {
|
|
|
375
375
|
if (defaultExecutionMethod === undefined) {
|
|
376
376
|
throw new Error(`Could not parse any valid operations from the provided spec.`);
|
|
377
377
|
}
|
|
378
|
-
const { llm = new openai_js_1.ChatOpenAI({ modelName: "gpt-3.5-turbo-0613" }), prompt = chat_js_1.ChatPromptTemplate.
|
|
378
|
+
const { llm = new openai_js_1.ChatOpenAI({ modelName: "gpt-3.5-turbo-0613" }), prompt = chat_js_1.ChatPromptTemplate.fromMessages([
|
|
379
379
|
chat_js_1.HumanMessagePromptTemplate.fromTemplate("Use the provided API's to respond to this user query:\n\n{query}"),
|
|
380
380
|
]), requestChain = new SimpleRequestChain({
|
|
381
381
|
requestMethod: async (name, args) => defaultExecutionMethod(name, args, {
|
|
@@ -371,7 +371,7 @@ export async function createOpenAPIChain(spec, options = {}) {
|
|
|
371
371
|
if (defaultExecutionMethod === undefined) {
|
|
372
372
|
throw new Error(`Could not parse any valid operations from the provided spec.`);
|
|
373
373
|
}
|
|
374
|
-
const { llm = new ChatOpenAI({ modelName: "gpt-3.5-turbo-0613" }), prompt = ChatPromptTemplate.
|
|
374
|
+
const { llm = new ChatOpenAI({ modelName: "gpt-3.5-turbo-0613" }), prompt = ChatPromptTemplate.fromMessages([
|
|
375
375
|
HumanMessagePromptTemplate.fromTemplate("Use the provided API's to respond to this user query:\n\n{query}"),
|
|
376
376
|
]), requestChain = new SimpleRequestChain({
|
|
377
377
|
requestMethod: async (name, args) => defaultExecutionMethod(name, args, {
|
|
@@ -46,5 +46,5 @@ export declare class FunctionCallStructuredOutputParser<T extends z.AnyZodObject
|
|
|
46
46
|
* as well as an additional required "outputSchema" JSON Schema object.
|
|
47
47
|
* @returns OpenAPIChain
|
|
48
48
|
*/
|
|
49
|
-
export declare function createStructuredOutputChain<T extends z.AnyZodObject = z.AnyZodObject>(input: StructuredOutputChainInput): LLMChain<any,
|
|
50
|
-
export declare function createStructuredOutputChainFromZod<T extends z.AnyZodObject>(zodSchema: T, input: Omit<StructuredOutputChainInput, "outputSchema">): LLMChain<any,
|
|
49
|
+
export declare function createStructuredOutputChain<T extends z.AnyZodObject = z.AnyZodObject>(input: StructuredOutputChainInput): LLMChain<any, BaseChatModel<BaseFunctionCallOptions> | ChatOpenAI<BaseFunctionCallOptions>>;
|
|
50
|
+
export declare function createStructuredOutputChainFromZod<T extends z.AnyZodObject>(zodSchema: T, input: Omit<StructuredOutputChainInput, "outputSchema">): LLMChain<any, BaseChatModel<BaseFunctionCallOptions> | ChatOpenAI<BaseFunctionCallOptions>>;
|
|
@@ -21,8 +21,7 @@ const messages = [
|
|
|
21
21
|
/*#__PURE__*/ chat_js_1.SystemMessagePromptTemplate.fromTemplate(system_template),
|
|
22
22
|
/*#__PURE__*/ chat_js_1.HumanMessagePromptTemplate.fromTemplate("{question}"),
|
|
23
23
|
];
|
|
24
|
-
const CHAT_QA_PROMPT =
|
|
25
|
-
/*#__PURE__*/ chat_js_1.ChatPromptTemplate.fromPromptMessages(messages);
|
|
24
|
+
const CHAT_QA_PROMPT = /*#__PURE__*/ chat_js_1.ChatPromptTemplate.fromMessages(messages);
|
|
26
25
|
exports.COMBINE_QA_PROMPT_SELECTOR =
|
|
27
26
|
/*#__PURE__*/ new conditional_js_1.ConditionalPromptSelector(exports.DEFAULT_COMBINE_QA_PROMPT, [
|
|
28
27
|
[conditional_js_1.isChatModel, CHAT_QA_PROMPT],
|
|
@@ -68,7 +67,7 @@ const combine_messages = [
|
|
|
68
67
|
/*#__PURE__*/ chat_js_1.HumanMessagePromptTemplate.fromTemplate("{question}"),
|
|
69
68
|
];
|
|
70
69
|
const CHAT_COMBINE_PROMPT =
|
|
71
|
-
/*#__PURE__*/ chat_js_1.ChatPromptTemplate.
|
|
70
|
+
/*#__PURE__*/ chat_js_1.ChatPromptTemplate.fromMessages(combine_messages);
|
|
72
71
|
exports.COMBINE_PROMPT_SELECTOR =
|
|
73
72
|
/*#__PURE__*/ new conditional_js_1.ConditionalPromptSelector(exports.COMBINE_PROMPT, [
|
|
74
73
|
[conditional_js_1.isChatModel, CHAT_COMBINE_PROMPT],
|
|
@@ -18,8 +18,7 @@ const messages = [
|
|
|
18
18
|
/*#__PURE__*/ SystemMessagePromptTemplate.fromTemplate(system_template),
|
|
19
19
|
/*#__PURE__*/ HumanMessagePromptTemplate.fromTemplate("{question}"),
|
|
20
20
|
];
|
|
21
|
-
const CHAT_QA_PROMPT =
|
|
22
|
-
/*#__PURE__*/ ChatPromptTemplate.fromPromptMessages(messages);
|
|
21
|
+
const CHAT_QA_PROMPT = /*#__PURE__*/ ChatPromptTemplate.fromMessages(messages);
|
|
23
22
|
export const COMBINE_QA_PROMPT_SELECTOR =
|
|
24
23
|
/*#__PURE__*/ new ConditionalPromptSelector(DEFAULT_COMBINE_QA_PROMPT, [
|
|
25
24
|
[isChatModel, CHAT_QA_PROMPT],
|
|
@@ -65,7 +64,7 @@ const combine_messages = [
|
|
|
65
64
|
/*#__PURE__*/ HumanMessagePromptTemplate.fromTemplate("{question}"),
|
|
66
65
|
];
|
|
67
66
|
const CHAT_COMBINE_PROMPT =
|
|
68
|
-
/*#__PURE__*/ ChatPromptTemplate.
|
|
67
|
+
/*#__PURE__*/ ChatPromptTemplate.fromMessages(combine_messages);
|
|
69
68
|
export const COMBINE_PROMPT_SELECTOR =
|
|
70
69
|
/*#__PURE__*/ new ConditionalPromptSelector(COMBINE_PROMPT, [
|
|
71
70
|
[isChatModel, CHAT_COMBINE_PROMPT],
|
|
@@ -32,7 +32,7 @@ const messages = [
|
|
|
32
32
|
/*#__PURE__*/ index_js_1.HumanMessagePromptTemplate.fromTemplate(refineTemplate),
|
|
33
33
|
];
|
|
34
34
|
exports.CHAT_REFINE_PROMPT =
|
|
35
|
-
/*#__PURE__*/ index_js_1.ChatPromptTemplate.
|
|
35
|
+
/*#__PURE__*/ index_js_1.ChatPromptTemplate.fromMessages(messages);
|
|
36
36
|
exports.REFINE_PROMPT_SELECTOR =
|
|
37
37
|
/*#__PURE__*/ new conditional_js_1.ConditionalPromptSelector(exports.DEFAULT_REFINE_PROMPT, [
|
|
38
38
|
[conditional_js_1.isChatModel, exports.CHAT_REFINE_PROMPT],
|
|
@@ -56,7 +56,7 @@ const chat_messages = [
|
|
|
56
56
|
/*#__PURE__*/ index_js_1.HumanMessagePromptTemplate.fromTemplate("{question}"),
|
|
57
57
|
];
|
|
58
58
|
exports.CHAT_QUESTION_PROMPT =
|
|
59
|
-
/*#__PURE__*/ index_js_1.ChatPromptTemplate.
|
|
59
|
+
/*#__PURE__*/ index_js_1.ChatPromptTemplate.fromMessages(chat_messages);
|
|
60
60
|
exports.QUESTION_PROMPT_SELECTOR =
|
|
61
61
|
/*#__PURE__*/ new conditional_js_1.ConditionalPromptSelector(exports.DEFAULT_TEXT_QA_PROMPT, [
|
|
62
62
|
[conditional_js_1.isChatModel, exports.CHAT_QUESTION_PROMPT],
|
|
@@ -29,7 +29,7 @@ const messages = [
|
|
|
29
29
|
/*#__PURE__*/ HumanMessagePromptTemplate.fromTemplate(refineTemplate),
|
|
30
30
|
];
|
|
31
31
|
export const CHAT_REFINE_PROMPT =
|
|
32
|
-
/*#__PURE__*/ ChatPromptTemplate.
|
|
32
|
+
/*#__PURE__*/ ChatPromptTemplate.fromMessages(messages);
|
|
33
33
|
export const REFINE_PROMPT_SELECTOR =
|
|
34
34
|
/*#__PURE__*/ new ConditionalPromptSelector(DEFAULT_REFINE_PROMPT, [
|
|
35
35
|
[isChatModel, CHAT_REFINE_PROMPT],
|
|
@@ -53,7 +53,7 @@ const chat_messages = [
|
|
|
53
53
|
/*#__PURE__*/ HumanMessagePromptTemplate.fromTemplate("{question}"),
|
|
54
54
|
];
|
|
55
55
|
export const CHAT_QUESTION_PROMPT =
|
|
56
|
-
/*#__PURE__*/ ChatPromptTemplate.
|
|
56
|
+
/*#__PURE__*/ ChatPromptTemplate.fromMessages(chat_messages);
|
|
57
57
|
export const QUESTION_PROMPT_SELECTOR =
|
|
58
58
|
/*#__PURE__*/ new ConditionalPromptSelector(DEFAULT_TEXT_QA_PROMPT, [
|
|
59
59
|
[isChatModel, CHAT_QUESTION_PROMPT],
|
|
@@ -17,6 +17,5 @@ const messages = [
|
|
|
17
17
|
/*#__PURE__*/ chat_js_1.SystemMessagePromptTemplate.fromTemplate(system_template),
|
|
18
18
|
/*#__PURE__*/ chat_js_1.HumanMessagePromptTemplate.fromTemplate("{question}"),
|
|
19
19
|
];
|
|
20
|
-
const CHAT_PROMPT =
|
|
21
|
-
/*#__PURE__*/ chat_js_1.ChatPromptTemplate.fromPromptMessages(messages);
|
|
20
|
+
const CHAT_PROMPT = /*#__PURE__*/ chat_js_1.ChatPromptTemplate.fromMessages(messages);
|
|
22
21
|
exports.QA_PROMPT_SELECTOR = new conditional_js_1.ConditionalPromptSelector(exports.DEFAULT_QA_PROMPT, [[conditional_js_1.isChatModel, CHAT_PROMPT]]);
|
|
@@ -14,6 +14,5 @@ const messages = [
|
|
|
14
14
|
/*#__PURE__*/ SystemMessagePromptTemplate.fromTemplate(system_template),
|
|
15
15
|
/*#__PURE__*/ HumanMessagePromptTemplate.fromTemplate("{question}"),
|
|
16
16
|
];
|
|
17
|
-
const CHAT_PROMPT =
|
|
18
|
-
/*#__PURE__*/ ChatPromptTemplate.fromPromptMessages(messages);
|
|
17
|
+
const CHAT_PROMPT = /*#__PURE__*/ ChatPromptTemplate.fromMessages(messages);
|
|
19
18
|
export const QA_PROMPT_SELECTOR = /*#__PURE__*/ new ConditionalPromptSelector(DEFAULT_QA_PROMPT, [[isChatModel, CHAT_PROMPT]]);
|
|
@@ -109,7 +109,7 @@ export declare abstract class BaseChatModel<CallOptions extends BaseChatModelCal
|
|
|
109
109
|
* An abstract class that extends BaseChatModel and provides a simple
|
|
110
110
|
* implementation of _generate.
|
|
111
111
|
*/
|
|
112
|
-
export declare abstract class SimpleChatModel extends BaseChatModel {
|
|
112
|
+
export declare abstract class SimpleChatModel<CallOptions extends BaseChatModelCallOptions = BaseChatModelCallOptions> extends BaseChatModel<CallOptions> {
|
|
113
113
|
abstract _call(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
|
|
114
114
|
_generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
115
115
|
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ChatFireworks = void 0;
|
|
4
|
+
const openai_js_1 = require("./openai.cjs");
|
|
5
|
+
const env_js_1 = require("../util/env.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Wrapper around Fireworks API for large language models fine-tuned for chat
|
|
8
|
+
*
|
|
9
|
+
* Fireworks API is compatible to the OpenAI API with some limitations described in
|
|
10
|
+
* https://readme.fireworks.ai/docs/openai-compatibility.
|
|
11
|
+
*
|
|
12
|
+
* To use, you should have the `openai` package installed and
|
|
13
|
+
* the `FIREWORKS_API_KEY` environment variable set.
|
|
14
|
+
*/
|
|
15
|
+
class ChatFireworks extends openai_js_1.ChatOpenAI {
|
|
16
|
+
static lc_name() {
|
|
17
|
+
return "ChatFireworks";
|
|
18
|
+
}
|
|
19
|
+
_llmType() {
|
|
20
|
+
return "fireworks";
|
|
21
|
+
}
|
|
22
|
+
get lc_secrets() {
|
|
23
|
+
return {
|
|
24
|
+
fireworksApiKey: "FIREWORKS_API_KEY",
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
constructor(fields) {
|
|
28
|
+
const fireworksApiKey = fields?.fireworksApiKey || (0, env_js_1.getEnvironmentVariable)("FIREWORKS_API_KEY");
|
|
29
|
+
if (!fireworksApiKey) {
|
|
30
|
+
throw new Error(`Fireworks API key not found. Please set the FIREWORKS_API_KEY environment variable or provide the key into "fireworksApiKey"`);
|
|
31
|
+
}
|
|
32
|
+
super({
|
|
33
|
+
...fields,
|
|
34
|
+
modelName: fields?.modelName || "accounts/fireworks/models/llama-v2-13b-chat",
|
|
35
|
+
openAIApiKey: fireworksApiKey,
|
|
36
|
+
configuration: {
|
|
37
|
+
baseURL: "https://api.fireworks.ai/inference/v1",
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: true,
|
|
44
|
+
value: true
|
|
45
|
+
});
|
|
46
|
+
Object.defineProperty(this, "fireworksApiKey", {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
configurable: true,
|
|
49
|
+
writable: true,
|
|
50
|
+
value: void 0
|
|
51
|
+
});
|
|
52
|
+
this.fireworksApiKey = fireworksApiKey;
|
|
53
|
+
}
|
|
54
|
+
toJSON() {
|
|
55
|
+
const result = super.toJSON();
|
|
56
|
+
if ("kwargs" in result &&
|
|
57
|
+
typeof result.kwargs === "object" &&
|
|
58
|
+
result.kwargs != null) {
|
|
59
|
+
delete result.kwargs.openai_api_key;
|
|
60
|
+
delete result.kwargs.configuration;
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Calls the Fireworks API with retry logic in case of failures.
|
|
66
|
+
* @param request The request to send to the Fireworks API.
|
|
67
|
+
* @param options Optional configuration for the API call.
|
|
68
|
+
* @returns The response from the Fireworks API.
|
|
69
|
+
*/
|
|
70
|
+
async completionWithRetry(request, options) {
|
|
71
|
+
delete request.frequency_penalty;
|
|
72
|
+
delete request.presence_penalty;
|
|
73
|
+
delete request.logit_bias;
|
|
74
|
+
delete request.functions;
|
|
75
|
+
if (request.stream === true) {
|
|
76
|
+
return super.completionWithRetry(request, options);
|
|
77
|
+
}
|
|
78
|
+
return super.completionWithRetry(request, options);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.ChatFireworks = ChatFireworks;
|