langchain 0.0.150 → 0.0.152

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/cache/cloudflare_kv.cjs +1 -0
  2. package/cache/cloudflare_kv.d.ts +1 -0
  3. package/cache/cloudflare_kv.js +1 -0
  4. package/dist/agents/chat/index.cjs +1 -1
  5. package/dist/agents/chat/index.js +1 -1
  6. package/dist/agents/chat_convo/index.cjs +1 -1
  7. package/dist/agents/chat_convo/index.js +1 -1
  8. package/dist/agents/openai/index.cjs +1 -1
  9. package/dist/agents/openai/index.js +1 -1
  10. package/dist/agents/structured_chat/index.cjs +1 -1
  11. package/dist/agents/structured_chat/index.js +1 -1
  12. package/dist/agents/xml/index.cjs +1 -1
  13. package/dist/agents/xml/index.js +1 -1
  14. package/dist/base_language/count_tokens.cjs +1 -0
  15. package/dist/base_language/count_tokens.js +1 -0
  16. package/dist/base_language/index.cjs +5 -3
  17. package/dist/base_language/index.d.ts +1 -1
  18. package/dist/base_language/index.js +4 -3
  19. package/dist/cache/cloudflare_kv.cjs +61 -0
  20. package/dist/cache/cloudflare_kv.d.ts +29 -0
  21. package/dist/cache/cloudflare_kv.js +57 -0
  22. package/dist/chains/openai_functions/openapi.cjs +1 -1
  23. package/dist/chains/openai_functions/openapi.js +1 -1
  24. package/dist/chains/question_answering/map_reduce_prompts.cjs +2 -3
  25. package/dist/chains/question_answering/map_reduce_prompts.js +2 -3
  26. package/dist/chains/question_answering/refine_prompts.cjs +2 -2
  27. package/dist/chains/question_answering/refine_prompts.js +2 -2
  28. package/dist/chains/question_answering/stuff_prompts.cjs +1 -2
  29. package/dist/chains/question_answering/stuff_prompts.js +1 -2
  30. package/dist/chat_models/ollama.cjs +3 -7
  31. package/dist/chat_models/ollama.d.ts +1 -1
  32. package/dist/chat_models/ollama.js +3 -7
  33. package/dist/document_loaders/web/pdf.cjs +87 -0
  34. package/dist/document_loaders/web/pdf.d.ts +17 -0
  35. package/dist/document_loaders/web/pdf.js +83 -0
  36. package/dist/evaluation/agents/prompt.cjs +2 -3
  37. package/dist/evaluation/agents/prompt.js +2 -3
  38. package/dist/experimental/chat_models/bittensor.cjs +141 -0
  39. package/dist/experimental/chat_models/bittensor.d.ts +36 -0
  40. package/dist/experimental/chat_models/bittensor.js +137 -0
  41. package/dist/experimental/plan_and_execute/prompt.cjs +1 -1
  42. package/dist/experimental/plan_and_execute/prompt.js +1 -1
  43. package/dist/llms/llama_cpp.cjs +10 -4
  44. package/dist/llms/llama_cpp.d.ts +2 -1
  45. package/dist/llms/llama_cpp.js +10 -4
  46. package/dist/llms/ollama.cjs +5 -6
  47. package/dist/llms/ollama.d.ts +2 -2
  48. package/dist/llms/ollama.js +5 -6
  49. package/dist/llms/openai.cjs +3 -3
  50. package/dist/llms/openai.js +3 -3
  51. package/dist/load/import_constants.cjs +4 -0
  52. package/dist/load/import_constants.js +4 -0
  53. package/dist/load/import_map.cjs +2 -1
  54. package/dist/load/import_map.d.ts +1 -0
  55. package/dist/load/import_map.js +1 -0
  56. package/dist/prompts/chat.cjs +12 -1
  57. package/dist/prompts/chat.d.ts +8 -0
  58. package/dist/prompts/chat.js +12 -1
  59. package/dist/schema/runnable/base.cjs +10 -2
  60. package/dist/schema/runnable/base.d.ts +2 -0
  61. package/dist/schema/runnable/base.js +9 -2
  62. package/dist/schema/runnable/branch.cjs +106 -0
  63. package/dist/schema/runnable/branch.d.ts +66 -0
  64. package/dist/schema/runnable/branch.js +102 -0
  65. package/dist/schema/runnable/index.cjs +12 -16
  66. package/dist/schema/runnable/index.d.ts +2 -1
  67. package/dist/schema/runnable/index.js +2 -1
  68. package/dist/stores/message/cloudflare_d1.cjs +134 -0
  69. package/dist/stores/message/cloudflare_d1.d.ts +49 -0
  70. package/dist/stores/message/cloudflare_d1.js +130 -0
  71. package/dist/types/openai-types.d.ts +2 -0
  72. package/dist/vectorstores/pgvector.cjs +277 -0
  73. package/dist/vectorstores/pgvector.d.ts +132 -0
  74. package/dist/vectorstores/pgvector.js +270 -0
  75. package/document_loaders/web/pdf.cjs +1 -0
  76. package/document_loaders/web/pdf.d.ts +1 -0
  77. package/document_loaders/web/pdf.js +1 -0
  78. package/experimental/chat_models/bittensor.cjs +1 -0
  79. package/experimental/chat_models/bittensor.d.ts +1 -0
  80. package/experimental/chat_models/bittensor.js +1 -0
  81. package/package.json +46 -1
  82. package/stores/message/cloudflare_d1.cjs +1 -0
  83. package/stores/message/cloudflare_d1.d.ts +1 -0
  84. package/stores/message/cloudflare_d1.js +1 -0
  85. package/vectorstores/pgvector.cjs +1 -0
  86. package/vectorstores/pgvector.d.ts +1 -0
  87. package/vectorstores/pgvector.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'
@@ -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.fromPromptMessages(messages);
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.fromPromptMessages(messages);
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.fromPromptMessages(messages);
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.fromPromptMessages(messages);
110
+ return ChatPromptTemplate.fromMessages(messages);
111
111
  }
112
112
  /**
113
113
  * Creates an instance of the ChatConversationalAgent class from a
@@ -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.fromPromptMessages([
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.fromPromptMessages([
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.fromPromptMessages(messages);
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.fromPromptMessages(messages);
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.fromPromptMessages([
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
  ]);
@@ -61,7 +61,7 @@ export class XMLAgent extends BaseSingleActionAgent {
61
61
  return ["input"];
62
62
  }
63
63
  static createPrompt() {
64
- return ChatPromptTemplate.fromPromptMessages([
64
+ return ChatPromptTemplate.fromMessages([
65
65
  HumanMessagePromptTemplate.fromTemplate(AGENT_INSTRUCTIONS),
66
66
  AIMessagePromptTemplate.fromTemplate("{intermediate_steps}"),
67
67
  ]);
@@ -63,6 +63,7 @@ const calculateMaxTokens = async ({ prompt, modelName, }) => {
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);
@@ -57,6 +57,7 @@ export const calculateMaxTokens = async ({ prompt, modelName, }) => {
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
- * Calculate max tokens for given model and prompt.
159
- * That is the model size - number of tokens in prompt.
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
- * Calculate max tokens for given model and prompt.
154
- * That is the model size - number of tokens in prompt.
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.fromPromptMessages([
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.fromPromptMessages([
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, {
@@ -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.fromPromptMessages(combine_messages);
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.fromPromptMessages(combine_messages);
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.fromPromptMessages(messages);
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.fromPromptMessages(chat_messages);
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.fromPromptMessages(messages);
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.fromPromptMessages(chat_messages);
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]]);
@@ -336,14 +336,10 @@ class ChatOllama extends base_js_1.SimpleChatModel {
336
336
  return formattedMessages;
337
337
  }
338
338
  /** @ignore */
339
- async _call(messages, options) {
340
- const stream = await this.caller.call(async () => (0, ollama_js_1.createOllamaStream)(this.baseUrl, {
341
- ...this.invocationParams(options),
342
- prompt: this._formatMessagesAsPrompt(messages),
343
- }, options));
339
+ async _call(messages, options, runManager) {
344
340
  const chunks = [];
345
- for await (const chunk of stream) {
346
- chunks.push(chunk.response);
341
+ for await (const chunk of this._streamResponseChunks(messages, options, runManager)) {
342
+ chunks.push(chunk.message.content);
347
343
  }
348
344
  return chunks.join("");
349
345
  }
@@ -97,5 +97,5 @@ export declare class ChatOllama extends SimpleChatModel implements OllamaInput {
97
97
  _streamResponseChunks(input: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
98
98
  protected _formatMessagesAsPrompt(messages: BaseMessage[]): string;
99
99
  /** @ignore */
100
- _call(messages: BaseMessage[], options: this["ParsedCallOptions"]): Promise<string>;
100
+ _call(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
101
101
  }
@@ -333,14 +333,10 @@ export class ChatOllama extends SimpleChatModel {
333
333
  return formattedMessages;
334
334
  }
335
335
  /** @ignore */
336
- async _call(messages, options) {
337
- const stream = await this.caller.call(async () => createOllamaStream(this.baseUrl, {
338
- ...this.invocationParams(options),
339
- prompt: this._formatMessagesAsPrompt(messages),
340
- }, options));
336
+ async _call(messages, options, runManager) {
341
337
  const chunks = [];
342
- for await (const chunk of stream) {
343
- chunks.push(chunk.response);
338
+ for await (const chunk of this._streamResponseChunks(messages, options, runManager)) {
339
+ chunks.push(chunk.message.content);
344
340
  }
345
341
  return chunks.join("");
346
342
  }
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebPDFLoader = void 0;
4
+ const pdf_js_1 = require("pdf-parse/lib/pdf.js/v1.10.100/build/pdf.js");
5
+ const document_js_1 = require("../../document.cjs");
6
+ const base_js_1 = require("../base.cjs");
7
+ /**
8
+ * A document loader for loading data from PDFs.
9
+ */
10
+ class WebPDFLoader extends base_js_1.BaseDocumentLoader {
11
+ constructor(blob, { splitPages = true } = {}) {
12
+ super();
13
+ Object.defineProperty(this, "blob", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "splitPages", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: true
24
+ });
25
+ this.blob = blob;
26
+ this.splitPages = splitPages ?? this.splitPages;
27
+ }
28
+ /**
29
+ * Loads the contents of the PDF as documents.
30
+ * @returns An array of Documents representing the retrieved data.
31
+ */
32
+ async load() {
33
+ const parsedPdf = await (0, pdf_js_1.getDocument)({
34
+ data: new Uint8Array(await this.blob.arrayBuffer()),
35
+ useWorkerFetch: false,
36
+ isEvalSupported: false,
37
+ useSystemFonts: true,
38
+ }).promise;
39
+ const meta = await parsedPdf.getMetadata().catch(() => null);
40
+ const documents = [];
41
+ for (let i = 1; i <= parsedPdf.numPages; i += 1) {
42
+ const page = await parsedPdf.getPage(i);
43
+ const content = await page.getTextContent();
44
+ if (content.items.length === 0) {
45
+ continue;
46
+ }
47
+ const text = content.items
48
+ .map((item) => item.str)
49
+ .join("\n");
50
+ documents.push(new document_js_1.Document({
51
+ pageContent: text,
52
+ metadata: {
53
+ pdf: {
54
+ version: pdf_js_1.version,
55
+ info: meta?.info,
56
+ metadata: meta?.metadata,
57
+ totalPages: parsedPdf.numPages,
58
+ },
59
+ loc: {
60
+ pageNumber: i,
61
+ },
62
+ },
63
+ }));
64
+ }
65
+ if (this.splitPages) {
66
+ return documents;
67
+ }
68
+ if (documents.length === 0) {
69
+ return [];
70
+ }
71
+ return [
72
+ new document_js_1.Document({
73
+ pageContent: documents.map((doc) => doc.pageContent).join("\n\n"),
74
+ metadata: {
75
+ pdf: {
76
+ version: pdf_js_1.version,
77
+ info: meta?.info,
78
+ metadata: meta?.metadata,
79
+ totalPages: parsedPdf.numPages,
80
+ },
81
+ },
82
+ }),
83
+ ];
84
+ return documents;
85
+ }
86
+ }
87
+ exports.WebPDFLoader = WebPDFLoader;
@@ -0,0 +1,17 @@
1
+ import { Document } from "../../document.js";
2
+ import { BaseDocumentLoader } from "../base.js";
3
+ /**
4
+ * A document loader for loading data from PDFs.
5
+ */
6
+ export declare class WebPDFLoader extends BaseDocumentLoader {
7
+ protected blob: Blob;
8
+ protected splitPages: boolean;
9
+ constructor(blob: Blob, { splitPages }?: {
10
+ splitPages?: boolean | undefined;
11
+ });
12
+ /**
13
+ * Loads the contents of the PDF as documents.
14
+ * @returns An array of Documents representing the retrieved data.
15
+ */
16
+ load(): Promise<Document[]>;
17
+ }