langchain 0.0.143 → 0.0.144

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 (60) hide show
  1. package/callbacks/handlers/llmonitor.cjs +1 -0
  2. package/callbacks/handlers/llmonitor.d.ts +1 -0
  3. package/callbacks/handlers/llmonitor.js +1 -0
  4. package/dist/agents/mrkl/outputParser.cjs +1 -1
  5. package/dist/agents/mrkl/outputParser.js +1 -1
  6. package/dist/base_language/index.cjs +2 -1
  7. package/dist/base_language/index.d.ts +7 -2
  8. package/dist/base_language/index.js +2 -1
  9. package/dist/callbacks/handlers/llmonitor.cjs +223 -0
  10. package/dist/callbacks/handlers/llmonitor.d.ts +35 -0
  11. package/dist/callbacks/handlers/llmonitor.js +215 -0
  12. package/dist/chains/openai_functions/extraction.d.ts +4 -4
  13. package/dist/chains/openai_functions/openapi.d.ts +3 -3
  14. package/dist/chains/openai_functions/structured_output.d.ts +5 -4
  15. package/dist/chains/openai_functions/tagging.d.ts +4 -4
  16. package/dist/chains/openai_moderation.cjs +1 -0
  17. package/dist/chains/openai_moderation.js +1 -0
  18. package/dist/chat_models/base.cjs +4 -3
  19. package/dist/chat_models/base.d.ts +3 -3
  20. package/dist/chat_models/base.js +5 -4
  21. package/dist/chat_models/minimax.d.ts +6 -28
  22. package/dist/chat_models/openai.d.ts +2 -3
  23. package/dist/document_loaders/fs/openai_whisper_audio.cjs +32 -0
  24. package/dist/document_loaders/fs/openai_whisper_audio.d.ts +11 -0
  25. package/dist/document_loaders/fs/openai_whisper_audio.js +28 -0
  26. package/dist/document_loaders/web/github.cjs +210 -24
  27. package/dist/document_loaders/web/github.d.ts +44 -1
  28. package/dist/document_loaders/web/github.js +210 -24
  29. package/dist/document_loaders/web/recursive_url.cjs +13 -0
  30. package/dist/document_loaders/web/recursive_url.js +13 -0
  31. package/dist/embeddings/hf_transformers.cjs +71 -0
  32. package/dist/embeddings/hf_transformers.d.ts +29 -0
  33. package/dist/embeddings/hf_transformers.js +67 -0
  34. package/dist/experimental/chat_models/anthropic_functions.d.ts +2 -5
  35. package/dist/load/import_constants.cjs +3 -0
  36. package/dist/load/import_constants.js +3 -0
  37. package/dist/prompts/chat.cjs +27 -1
  38. package/dist/prompts/chat.d.ts +3 -2
  39. package/dist/prompts/chat.js +28 -2
  40. package/dist/schema/index.cjs +44 -1
  41. package/dist/schema/index.d.ts +10 -0
  42. package/dist/schema/index.js +41 -0
  43. package/dist/tools/serpapi.cjs +108 -13
  44. package/dist/tools/serpapi.js +108 -13
  45. package/dist/vectorstores/redis.cjs +12 -4
  46. package/dist/vectorstores/redis.d.ts +8 -0
  47. package/dist/vectorstores/redis.js +12 -4
  48. package/dist/vectorstores/tigris.cjs +2 -0
  49. package/dist/vectorstores/tigris.d.ts +2 -3
  50. package/dist/vectorstores/tigris.js +2 -0
  51. package/dist/vectorstores/vectara.cjs +30 -12
  52. package/dist/vectorstores/vectara.d.ts +1 -1
  53. package/dist/vectorstores/vectara.js +30 -12
  54. package/document_loaders/fs/openai_whisper_audio.cjs +1 -0
  55. package/document_loaders/fs/openai_whisper_audio.d.ts +1 -0
  56. package/document_loaders/fs/openai_whisper_audio.js +1 -0
  57. package/embeddings/hf_transformers.cjs +1 -0
  58. package/embeddings/hf_transformers.d.ts +1 -0
  59. package/embeddings/hf_transformers.js +1 -0
  60. package/package.json +36 -6
@@ -15,6 +15,7 @@ export const optionalImportEntrypoints = [
15
15
  "langchain/embeddings/cohere",
16
16
  "langchain/embeddings/tensorflow",
17
17
  "langchain/embeddings/hf",
18
+ "langchain/embeddings/hf_transformers",
18
19
  "langchain/embeddings/googlevertexai",
19
20
  "langchain/embeddings/googlepalm",
20
21
  "langchain/llms/load",
@@ -84,11 +85,13 @@ export const optionalImportEntrypoints = [
84
85
  "langchain/document_loaders/fs/csv",
85
86
  "langchain/document_loaders/fs/notion",
86
87
  "langchain/document_loaders/fs/unstructured",
88
+ "langchain/document_loaders/fs/openai_whisper_audio",
87
89
  "langchain/document_transformers/html_to_text",
88
90
  "langchain/document_transformers/mozilla_readability",
89
91
  "langchain/chat_models/googlevertexai",
90
92
  "langchain/chat_models/googlepalm",
91
93
  "langchain/sql_db",
94
+ "langchain/callbacks/handlers/llmonitor",
92
95
  "langchain/output_parsers/expression",
93
96
  "langchain/retrievers/amazon_kendra",
94
97
  "langchain/retrievers/supabase",
@@ -233,6 +233,32 @@ class SystemMessagePromptTemplate extends BaseMessageStringPromptTemplate {
233
233
  }
234
234
  }
235
235
  exports.SystemMessagePromptTemplate = SystemMessagePromptTemplate;
236
+ function _isBaseMessagePromptTemplate(baseMessagePromptTemplateLike) {
237
+ return (typeof baseMessagePromptTemplateLike
238
+ .formatMessages === "function");
239
+ }
240
+ function _coerceMessagePromptTemplateLike(messagePromptTemplateLike) {
241
+ if (_isBaseMessagePromptTemplate(messagePromptTemplateLike) ||
242
+ (0, index_js_1.isBaseMessage)(messagePromptTemplateLike)) {
243
+ return messagePromptTemplateLike;
244
+ }
245
+ const message = (0, index_js_1.coerceMessageLikeToMessage)(messagePromptTemplateLike);
246
+ if (message._getType() === "human") {
247
+ return HumanMessagePromptTemplate.fromTemplate(message.content);
248
+ }
249
+ else if (message._getType() === "ai") {
250
+ return AIMessagePromptTemplate.fromTemplate(message.content);
251
+ }
252
+ else if (message._getType() === "system") {
253
+ return SystemMessagePromptTemplate.fromTemplate(message.content);
254
+ }
255
+ else if (index_js_1.ChatMessage.isInstance(message)) {
256
+ return ChatMessagePromptTemplate.fromTemplate(message.content, message.role);
257
+ }
258
+ else {
259
+ throw new Error(`Could not coerce message prompt template from input. Received message type: "${message._getType()}".`);
260
+ }
261
+ }
236
262
  /**
237
263
  * Class that represents a chat prompt. It extends the
238
264
  * BaseChatPromptTemplate and uses an array of BaseMessagePromptTemplate
@@ -336,7 +362,7 @@ class ChatPromptTemplate extends BaseChatPromptTemplate {
336
362
  // eslint-disable-next-line no-instanceof/no-instanceof
337
363
  promptMessage instanceof ChatPromptTemplate
338
364
  ? promptMessage.promptMessages
339
- : [promptMessage]), []);
365
+ : [_coerceMessagePromptTemplateLike(promptMessage)]), []);
340
366
  const flattenedPartialVariables = promptMessages.reduce((acc, promptMessage) =>
341
367
  // eslint-disable-next-line no-instanceof/no-instanceof
342
368
  promptMessage instanceof ChatPromptTemplate
@@ -1,5 +1,5 @@
1
1
  import { BaseCallbackConfig } from "../callbacks/manager.js";
2
- import { BaseMessage, BasePromptValue, InputValues, PartialValues } from "../schema/index.js";
2
+ import { BaseMessage, BaseMessageLike, BasePromptValue, InputValues, PartialValues } from "../schema/index.js";
3
3
  import { Runnable } from "../schema/runnable.js";
4
4
  import { BasePromptTemplate, BasePromptTemplateInput, BaseStringPromptTemplate, TypedPromptInputValues } from "./base.js";
5
5
  /**
@@ -152,6 +152,7 @@ export interface ChatPromptTemplateInput<RunInput extends InputValues = any, Par
152
152
  */
153
153
  validateTemplate?: boolean;
154
154
  }
155
+ export type BaseMessagePromptTemplateLike = BaseMessagePromptTemplate | BaseMessageLike;
155
156
  /**
156
157
  * Class that represents a chat prompt. It extends the
157
158
  * BaseChatPromptTemplate and uses an array of BaseMessagePromptTemplate
@@ -168,5 +169,5 @@ export declare class ChatPromptTemplate<RunInput extends InputValues = any, Part
168
169
  _getPromptType(): "chat";
169
170
  formatMessages(values: TypedPromptInputValues<RunInput>): Promise<BaseMessage[]>;
170
171
  partial<NewPartialVariableName extends string>(values: PartialValues<NewPartialVariableName>): Promise<ChatPromptTemplate<InputValues<Exclude<Extract<keyof RunInput, string>, NewPartialVariableName>>, any>>;
171
- static fromPromptMessages<RunInput extends InputValues = any>(promptMessages: (BaseMessagePromptTemplate<InputValues> | ChatPromptTemplate<InputValues, string> | BaseMessage)[]): ChatPromptTemplate<RunInput>;
172
+ static fromPromptMessages<RunInput extends InputValues = any>(promptMessages: (ChatPromptTemplate<InputValues, string> | BaseMessagePromptTemplateLike)[]): ChatPromptTemplate<RunInput>;
172
173
  }
@@ -1,6 +1,6 @@
1
1
  // Default generic "any" values are for backwards compatibility.
2
2
  // Replace with "string" when we are comfortable with a breaking change.
3
- import { AIMessage, BaseMessage, BasePromptValue, ChatMessage, HumanMessage, SystemMessage, } from "../schema/index.js";
3
+ import { AIMessage, BaseMessage, BasePromptValue, ChatMessage, HumanMessage, SystemMessage, coerceMessageLikeToMessage, isBaseMessage, } from "../schema/index.js";
4
4
  import { Runnable } from "../schema/runnable.js";
5
5
  import { BasePromptTemplate, } from "./base.js";
6
6
  import { PromptTemplate } from "./prompt.js";
@@ -221,6 +221,32 @@ export class SystemMessagePromptTemplate extends BaseMessageStringPromptTemplate
221
221
  return new this(PromptTemplate.fromTemplate(template));
222
222
  }
223
223
  }
224
+ function _isBaseMessagePromptTemplate(baseMessagePromptTemplateLike) {
225
+ return (typeof baseMessagePromptTemplateLike
226
+ .formatMessages === "function");
227
+ }
228
+ function _coerceMessagePromptTemplateLike(messagePromptTemplateLike) {
229
+ if (_isBaseMessagePromptTemplate(messagePromptTemplateLike) ||
230
+ isBaseMessage(messagePromptTemplateLike)) {
231
+ return messagePromptTemplateLike;
232
+ }
233
+ const message = coerceMessageLikeToMessage(messagePromptTemplateLike);
234
+ if (message._getType() === "human") {
235
+ return HumanMessagePromptTemplate.fromTemplate(message.content);
236
+ }
237
+ else if (message._getType() === "ai") {
238
+ return AIMessagePromptTemplate.fromTemplate(message.content);
239
+ }
240
+ else if (message._getType() === "system") {
241
+ return SystemMessagePromptTemplate.fromTemplate(message.content);
242
+ }
243
+ else if (ChatMessage.isInstance(message)) {
244
+ return ChatMessagePromptTemplate.fromTemplate(message.content, message.role);
245
+ }
246
+ else {
247
+ throw new Error(`Could not coerce message prompt template from input. Received message type: "${message._getType()}".`);
248
+ }
249
+ }
224
250
  /**
225
251
  * Class that represents a chat prompt. It extends the
226
252
  * BaseChatPromptTemplate and uses an array of BaseMessagePromptTemplate
@@ -324,7 +350,7 @@ export class ChatPromptTemplate extends BaseChatPromptTemplate {
324
350
  // eslint-disable-next-line no-instanceof/no-instanceof
325
351
  promptMessage instanceof ChatPromptTemplate
326
352
  ? promptMessage.promptMessages
327
- : [promptMessage]), []);
353
+ : [_coerceMessagePromptTemplateLike(promptMessage)]), []);
328
354
  const flattenedPartialVariables = promptMessages.reduce((acc, promptMessage) =>
329
355
  // eslint-disable-next-line no-instanceof/no-instanceof
330
356
  promptMessage instanceof ChatPromptTemplate
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Docstore = exports.BaseEntityStore = exports.BaseFileStore = exports.BaseCache = exports.BaseListChatMessageHistory = exports.BaseChatMessageHistory = exports.BasePromptValue = exports.ChatGenerationChunk = exports.ChatMessageChunk = exports.ChatMessage = exports.FunctionMessageChunk = exports.FunctionMessage = exports.SystemChatMessage = exports.AIChatMessage = exports.HumanChatMessage = exports.BaseChatMessage = exports.SystemMessageChunk = exports.SystemMessage = exports.AIMessageChunk = exports.AIMessage = exports.HumanMessageChunk = exports.HumanMessage = exports.BaseMessageChunk = exports.BaseMessage = exports.GenerationChunk = exports.RUN_KEY = void 0;
3
+ exports.Docstore = exports.BaseEntityStore = exports.BaseFileStore = exports.BaseCache = exports.BaseListChatMessageHistory = exports.BaseChatMessageHistory = exports.BasePromptValue = exports.ChatGenerationChunk = exports.ChatMessageChunk = exports.coerceMessageLikeToMessage = exports.isBaseMessage = exports.ChatMessage = exports.FunctionMessageChunk = exports.FunctionMessage = exports.SystemChatMessage = exports.AIChatMessage = exports.HumanChatMessage = exports.BaseChatMessage = exports.SystemMessageChunk = exports.SystemMessage = exports.AIMessageChunk = exports.AIMessage = exports.HumanMessageChunk = exports.HumanMessage = exports.BaseMessageChunk = exports.BaseMessage = exports.GenerationChunk = exports.RUN_KEY = void 0;
4
4
  const serializable_js_1 = require("../load/serializable.cjs");
5
5
  exports.RUN_KEY = "__run";
6
6
  /**
@@ -322,6 +322,49 @@ class ChatMessage extends BaseMessage {
322
322
  }
323
323
  }
324
324
  exports.ChatMessage = ChatMessage;
325
+ function isBaseMessage(messageLike) {
326
+ return typeof messageLike._getType === "function";
327
+ }
328
+ exports.isBaseMessage = isBaseMessage;
329
+ function coerceMessageLikeToMessage(messageLike) {
330
+ if (typeof messageLike === "string") {
331
+ return new HumanMessage(messageLike);
332
+ }
333
+ else if (isBaseMessage(messageLike)) {
334
+ return messageLike;
335
+ }
336
+ let role;
337
+ let content;
338
+ let name;
339
+ if (Array.isArray(messageLike)) {
340
+ [role, content] = messageLike;
341
+ name = "";
342
+ }
343
+ else {
344
+ role = messageLike.role;
345
+ content = messageLike.content;
346
+ name = messageLike.name;
347
+ }
348
+ if (role === "human" || role === "user") {
349
+ return new HumanMessage({ content });
350
+ }
351
+ else if (role === "ai" || role === "assistant") {
352
+ return new AIMessage({ content });
353
+ }
354
+ else if (role === "system") {
355
+ return new SystemMessage({ content });
356
+ }
357
+ else if (role === "function") {
358
+ if (!name) {
359
+ throw new Error(`Unable to coerce function message from object: no "name" field provided.`);
360
+ }
361
+ return new FunctionMessage({ content, name });
362
+ }
363
+ else {
364
+ return new ChatMessage({ content, role });
365
+ }
366
+ }
367
+ exports.coerceMessageLikeToMessage = coerceMessageLikeToMessage;
325
368
  /**
326
369
  * Represents a chunk of a chat message, which can be concatenated with
327
370
  * other chat message chunks.
@@ -210,6 +210,16 @@ export declare class ChatMessage extends BaseMessage implements ChatMessageField
210
210
  _getType(): MessageType;
211
211
  static isInstance(message: BaseMessage): message is ChatMessage;
212
212
  }
213
+ export type BaseMessageLike = BaseMessage | {
214
+ role: MessageType | "user" | "assistant" | (string & Record<never, never>);
215
+ content: string;
216
+ name?: string;
217
+ } | [
218
+ MessageType | "user" | "assistant" | (string & Record<never, never>),
219
+ string
220
+ ] | string;
221
+ export declare function isBaseMessage(messageLike: BaseMessageLike): messageLike is BaseMessage;
222
+ export declare function coerceMessageLikeToMessage(messageLike: BaseMessageLike): BaseMessage;
213
223
  /**
214
224
  * Represents a chunk of a chat message, which can be concatenated with
215
225
  * other chat message chunks.
@@ -307,6 +307,47 @@ export class ChatMessage extends BaseMessage {
307
307
  return message._getType() === "generic";
308
308
  }
309
309
  }
310
+ export function isBaseMessage(messageLike) {
311
+ return typeof messageLike._getType === "function";
312
+ }
313
+ export function coerceMessageLikeToMessage(messageLike) {
314
+ if (typeof messageLike === "string") {
315
+ return new HumanMessage(messageLike);
316
+ }
317
+ else if (isBaseMessage(messageLike)) {
318
+ return messageLike;
319
+ }
320
+ let role;
321
+ let content;
322
+ let name;
323
+ if (Array.isArray(messageLike)) {
324
+ [role, content] = messageLike;
325
+ name = "";
326
+ }
327
+ else {
328
+ role = messageLike.role;
329
+ content = messageLike.content;
330
+ name = messageLike.name;
331
+ }
332
+ if (role === "human" || role === "user") {
333
+ return new HumanMessage({ content });
334
+ }
335
+ else if (role === "ai" || role === "assistant") {
336
+ return new AIMessage({ content });
337
+ }
338
+ else if (role === "system") {
339
+ return new SystemMessage({ content });
340
+ }
341
+ else if (role === "function") {
342
+ if (!name) {
343
+ throw new Error(`Unable to coerce function message from object: no "name" field provided.`);
344
+ }
345
+ return new FunctionMessage({ content, name });
346
+ }
347
+ else {
348
+ return new ChatMessage({ content, role });
349
+ }
350
+ }
310
351
  /**
311
352
  * Represents a chunk of a chat message, which can be concatenated with
312
353
  * other chat message chunks.
@@ -82,25 +82,120 @@ class SerpAPI extends base_js_1.Tool {
82
82
  if (res.error) {
83
83
  throw new Error(`Got error from serpAPI: ${res.error}`);
84
84
  }
85
- if (res.answer_box?.answer) {
86
- return res.answer_box.answer;
85
+ const answer_box = res.answer_box_list
86
+ ? res.answer_box_list[0]
87
+ : res.answer_box;
88
+ if (answer_box) {
89
+ if (answer_box.result) {
90
+ return answer_box.result;
91
+ }
92
+ else if (answer_box.answer) {
93
+ return answer_box.answer;
94
+ }
95
+ else if (answer_box.snippet) {
96
+ return answer_box.snippet;
97
+ }
98
+ else if (answer_box.snippet_highlighted_words) {
99
+ return answer_box.snippet_highlighted_words.toString();
100
+ }
101
+ else {
102
+ const answer = {};
103
+ Object.keys(answer_box)
104
+ .filter((k) => !Array.isArray(answer_box[k]) &&
105
+ typeof answer_box[k] !== "object" &&
106
+ !(typeof answer_box[k] === "string" &&
107
+ answer_box[k].startsWith("http")))
108
+ .forEach((k) => {
109
+ answer[k] = answer_box[k];
110
+ });
111
+ return JSON.stringify(answer);
112
+ }
87
113
  }
88
- if (res.answer_box?.snippet) {
89
- return res.answer_box.snippet;
114
+ if (res.events_results) {
115
+ return JSON.stringify(res.events_results);
90
116
  }
91
- if (res.answer_box?.snippet_highlighted_words) {
92
- return res.answer_box.snippet_highlighted_words[0];
117
+ if (res.sports_results) {
118
+ return JSON.stringify(res.sports_results);
93
119
  }
94
- if (res.sports_results?.game_spotlight) {
95
- return res.sports_results.game_spotlight;
120
+ if (res.top_stories) {
121
+ return JSON.stringify(res.top_stories);
96
122
  }
97
- if (res.knowledge_graph?.description) {
98
- return res.knowledge_graph.description;
123
+ if (res.news_results) {
124
+ return JSON.stringify(res.news_results);
99
125
  }
100
- if (res.organic_results?.[0]?.snippet) {
101
- return res.organic_results[0].snippet;
126
+ if (res.jobs_results?.jobs) {
127
+ return JSON.stringify(res.jobs_results.jobs);
128
+ }
129
+ if (res.questions_and_answers) {
130
+ return JSON.stringify(res.questions_and_answers);
131
+ }
132
+ if (res.popular_destinations?.destinations) {
133
+ return JSON.stringify(res.popular_destinations.destinations);
134
+ }
135
+ if (res.top_sights?.sights) {
136
+ const sights = res.top_sights.sights
137
+ .map((s) => ({
138
+ title: s.title,
139
+ description: s.description,
140
+ price: s.price,
141
+ }))
142
+ .slice(0, 8);
143
+ return JSON.stringify(sights);
144
+ }
145
+ if (res.shopping_results && res.shopping_results[0]?.title) {
146
+ return JSON.stringify(res.shopping_results.slice(0, 3));
147
+ }
148
+ if (res.images_results && res.images_results[0]?.thumbnail) {
149
+ return res.images_results
150
+ .map((ir) => ir.thumbnail)
151
+ .slice(0, 10)
152
+ .toString();
153
+ }
154
+ const snippets = [];
155
+ if (res.knowledge_graph) {
156
+ if (res.knowledge_graph.description) {
157
+ snippets.push(res.knowledge_graph.description);
158
+ }
159
+ const title = res.knowledge_graph.title || "";
160
+ Object.keys(res.knowledge_graph)
161
+ .filter((k) => typeof res.knowledge_graph[k] === "string" &&
162
+ k !== "title" &&
163
+ k !== "description" &&
164
+ !k.endsWith("_stick") &&
165
+ !k.endsWith("_link") &&
166
+ !k.startsWith("http"))
167
+ .forEach((k) => snippets.push(`${title} ${k}: ${res.knowledge_graph[k]}`));
168
+ }
169
+ const first_organic_result = res.organic_results?.[0];
170
+ if (first_organic_result) {
171
+ if (first_organic_result.snippet) {
172
+ snippets.push(first_organic_result.snippet);
173
+ }
174
+ else if (first_organic_result.snippet_highlighted_words) {
175
+ snippets.push(first_organic_result.snippet_highlighted_words);
176
+ }
177
+ else if (first_organic_result.rich_snippet) {
178
+ snippets.push(first_organic_result.rich_snippet);
179
+ }
180
+ else if (first_organic_result.rich_snippet_table) {
181
+ snippets.push(first_organic_result.rich_snippet_table);
182
+ }
183
+ else if (first_organic_result.link) {
184
+ snippets.push(first_organic_result.link);
185
+ }
186
+ }
187
+ if (res.buying_guide) {
188
+ snippets.push(res.buying_guide);
189
+ }
190
+ if (res.local_results?.places) {
191
+ snippets.push(res.local_results.places);
192
+ }
193
+ if (snippets.length > 0) {
194
+ return JSON.stringify(snippets);
195
+ }
196
+ else {
197
+ return "No good search result found";
102
198
  }
103
- return "No good search result found";
104
199
  }
105
200
  }
106
201
  exports.SerpAPI = SerpAPI;
@@ -79,24 +79,119 @@ export class SerpAPI extends Tool {
79
79
  if (res.error) {
80
80
  throw new Error(`Got error from serpAPI: ${res.error}`);
81
81
  }
82
- if (res.answer_box?.answer) {
83
- return res.answer_box.answer;
82
+ const answer_box = res.answer_box_list
83
+ ? res.answer_box_list[0]
84
+ : res.answer_box;
85
+ if (answer_box) {
86
+ if (answer_box.result) {
87
+ return answer_box.result;
88
+ }
89
+ else if (answer_box.answer) {
90
+ return answer_box.answer;
91
+ }
92
+ else if (answer_box.snippet) {
93
+ return answer_box.snippet;
94
+ }
95
+ else if (answer_box.snippet_highlighted_words) {
96
+ return answer_box.snippet_highlighted_words.toString();
97
+ }
98
+ else {
99
+ const answer = {};
100
+ Object.keys(answer_box)
101
+ .filter((k) => !Array.isArray(answer_box[k]) &&
102
+ typeof answer_box[k] !== "object" &&
103
+ !(typeof answer_box[k] === "string" &&
104
+ answer_box[k].startsWith("http")))
105
+ .forEach((k) => {
106
+ answer[k] = answer_box[k];
107
+ });
108
+ return JSON.stringify(answer);
109
+ }
84
110
  }
85
- if (res.answer_box?.snippet) {
86
- return res.answer_box.snippet;
111
+ if (res.events_results) {
112
+ return JSON.stringify(res.events_results);
87
113
  }
88
- if (res.answer_box?.snippet_highlighted_words) {
89
- return res.answer_box.snippet_highlighted_words[0];
114
+ if (res.sports_results) {
115
+ return JSON.stringify(res.sports_results);
90
116
  }
91
- if (res.sports_results?.game_spotlight) {
92
- return res.sports_results.game_spotlight;
117
+ if (res.top_stories) {
118
+ return JSON.stringify(res.top_stories);
93
119
  }
94
- if (res.knowledge_graph?.description) {
95
- return res.knowledge_graph.description;
120
+ if (res.news_results) {
121
+ return JSON.stringify(res.news_results);
96
122
  }
97
- if (res.organic_results?.[0]?.snippet) {
98
- return res.organic_results[0].snippet;
123
+ if (res.jobs_results?.jobs) {
124
+ return JSON.stringify(res.jobs_results.jobs);
125
+ }
126
+ if (res.questions_and_answers) {
127
+ return JSON.stringify(res.questions_and_answers);
128
+ }
129
+ if (res.popular_destinations?.destinations) {
130
+ return JSON.stringify(res.popular_destinations.destinations);
131
+ }
132
+ if (res.top_sights?.sights) {
133
+ const sights = res.top_sights.sights
134
+ .map((s) => ({
135
+ title: s.title,
136
+ description: s.description,
137
+ price: s.price,
138
+ }))
139
+ .slice(0, 8);
140
+ return JSON.stringify(sights);
141
+ }
142
+ if (res.shopping_results && res.shopping_results[0]?.title) {
143
+ return JSON.stringify(res.shopping_results.slice(0, 3));
144
+ }
145
+ if (res.images_results && res.images_results[0]?.thumbnail) {
146
+ return res.images_results
147
+ .map((ir) => ir.thumbnail)
148
+ .slice(0, 10)
149
+ .toString();
150
+ }
151
+ const snippets = [];
152
+ if (res.knowledge_graph) {
153
+ if (res.knowledge_graph.description) {
154
+ snippets.push(res.knowledge_graph.description);
155
+ }
156
+ const title = res.knowledge_graph.title || "";
157
+ Object.keys(res.knowledge_graph)
158
+ .filter((k) => typeof res.knowledge_graph[k] === "string" &&
159
+ k !== "title" &&
160
+ k !== "description" &&
161
+ !k.endsWith("_stick") &&
162
+ !k.endsWith("_link") &&
163
+ !k.startsWith("http"))
164
+ .forEach((k) => snippets.push(`${title} ${k}: ${res.knowledge_graph[k]}`));
165
+ }
166
+ const first_organic_result = res.organic_results?.[0];
167
+ if (first_organic_result) {
168
+ if (first_organic_result.snippet) {
169
+ snippets.push(first_organic_result.snippet);
170
+ }
171
+ else if (first_organic_result.snippet_highlighted_words) {
172
+ snippets.push(first_organic_result.snippet_highlighted_words);
173
+ }
174
+ else if (first_organic_result.rich_snippet) {
175
+ snippets.push(first_organic_result.rich_snippet);
176
+ }
177
+ else if (first_organic_result.rich_snippet_table) {
178
+ snippets.push(first_organic_result.rich_snippet_table);
179
+ }
180
+ else if (first_organic_result.link) {
181
+ snippets.push(first_organic_result.link);
182
+ }
183
+ }
184
+ if (res.buying_guide) {
185
+ snippets.push(res.buying_guide);
186
+ }
187
+ if (res.local_results?.places) {
188
+ snippets.push(res.local_results.places);
189
+ }
190
+ if (snippets.length > 0) {
191
+ return JSON.stringify(snippets);
192
+ }
193
+ else {
194
+ return "No good search result found";
99
195
  }
100
- return "No good search result found";
101
196
  }
102
197
  }
@@ -33,6 +33,12 @@ class RedisVectorStore extends base_js_1.VectorStore {
33
33
  writable: true,
34
34
  value: void 0
35
35
  });
36
+ Object.defineProperty(this, "createIndexOptions", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
36
42
  Object.defineProperty(this, "keyPrefix", {
37
43
  enumerable: true,
38
44
  configurable: true,
@@ -74,6 +80,11 @@ class RedisVectorStore extends base_js_1.VectorStore {
74
80
  this.metadataKey = _dbConfig.metadataKey ?? "metadata";
75
81
  this.vectorKey = _dbConfig.vectorKey ?? "content_vector";
76
82
  this.filter = _dbConfig.filter;
83
+ this.createIndexOptions = {
84
+ ON: "HASH",
85
+ PREFIX: this.keyPrefix,
86
+ ..._dbConfig.createIndexOptions,
87
+ };
77
88
  }
78
89
  /**
79
90
  * Method for adding documents to the RedisVectorStore. It first converts
@@ -228,10 +239,7 @@ class RedisVectorStore extends base_js_1.VectorStore {
228
239
  [this.contentKey]: redis_1.SchemaFieldTypes.TEXT,
229
240
  [this.metadataKey]: redis_1.SchemaFieldTypes.TEXT,
230
241
  };
231
- await this.redisClient.ft.create(this.indexName, schema, {
232
- ON: "HASH",
233
- PREFIX: this.keyPrefix,
234
- });
242
+ await this.redisClient.ft.create(this.indexName, schema, this.createIndexOptions);
235
243
  }
236
244
  /**
237
245
  * Method for dropping an index from the RedisVectorStore.
@@ -29,6 +29,11 @@ export type CreateSchemaHNSWVectorField = CreateSchemaVectorField<VectorAlgorith
29
29
  EF_CONSTRUCTION?: number;
30
30
  EF_RUNTIME?: number;
31
31
  }>;
32
+ type CreateIndexOptions = NonNullable<Parameters<ReturnType<typeof createClient>["ft"]["create"]>[3]>;
33
+ export type RedisSearchLanguages = `${NonNullable<CreateIndexOptions["LANGUAGE"]>}`;
34
+ export type RedisVectorStoreIndexOptions = Omit<CreateIndexOptions, "LANGUAGE"> & {
35
+ LANGUAGE?: RedisSearchLanguages;
36
+ };
32
37
  /**
33
38
  * Interface for the configuration of the RedisVectorStore. It includes
34
39
  * the Redis client, index name, index options, key prefix, content key,
@@ -38,6 +43,7 @@ export interface RedisVectorStoreConfig {
38
43
  redisClient: ReturnType<typeof createClient> | ReturnType<typeof createCluster>;
39
44
  indexName: string;
40
45
  indexOptions?: CreateSchemaFlatVectorField | CreateSchemaHNSWVectorField;
46
+ createIndexOptions?: Omit<RedisVectorStoreIndexOptions, "PREFIX">;
41
47
  keyPrefix?: string;
42
48
  contentKey?: string;
43
49
  metadataKey?: string;
@@ -67,6 +73,7 @@ export declare class RedisVectorStore extends VectorStore {
67
73
  private redisClient;
68
74
  indexName: string;
69
75
  indexOptions: CreateSchemaFlatVectorField | CreateSchemaHNSWVectorField;
76
+ createIndexOptions: CreateIndexOptions;
70
77
  keyPrefix: string;
71
78
  contentKey: string;
72
79
  metadataKey: string;
@@ -176,3 +183,4 @@ export declare class RedisVectorStore extends VectorStore {
176
183
  */
177
184
  private getFloat32Buffer;
178
185
  }
186
+ export {};
@@ -30,6 +30,12 @@ export class RedisVectorStore extends VectorStore {
30
30
  writable: true,
31
31
  value: void 0
32
32
  });
33
+ Object.defineProperty(this, "createIndexOptions", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
33
39
  Object.defineProperty(this, "keyPrefix", {
34
40
  enumerable: true,
35
41
  configurable: true,
@@ -71,6 +77,11 @@ export class RedisVectorStore extends VectorStore {
71
77
  this.metadataKey = _dbConfig.metadataKey ?? "metadata";
72
78
  this.vectorKey = _dbConfig.vectorKey ?? "content_vector";
73
79
  this.filter = _dbConfig.filter;
80
+ this.createIndexOptions = {
81
+ ON: "HASH",
82
+ PREFIX: this.keyPrefix,
83
+ ..._dbConfig.createIndexOptions,
84
+ };
74
85
  }
75
86
  /**
76
87
  * Method for adding documents to the RedisVectorStore. It first converts
@@ -225,10 +236,7 @@ export class RedisVectorStore extends VectorStore {
225
236
  [this.contentKey]: SchemaFieldTypes.TEXT,
226
237
  [this.metadataKey]: SchemaFieldTypes.TEXT,
227
238
  };
228
- await this.redisClient.ft.create(this.indexName, schema, {
229
- ON: "HASH",
230
- PREFIX: this.keyPrefix,
231
- });
239
+ await this.redisClient.ft.create(this.indexName, schema, this.createIndexOptions);
232
240
  }
233
241
  /**
234
242
  * Method for dropping an index from the RedisVectorStore.
@@ -37,6 +37,7 @@ class TigrisVectorStore extends base_js_1.VectorStore {
37
37
  }
38
38
  constructor(embeddings, args) {
39
39
  super(embeddings, args);
40
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
41
  Object.defineProperty(this, "index", {
41
42
  enumerable: true,
42
43
  configurable: true,
@@ -98,6 +99,7 @@ class TigrisVectorStore extends base_js_1.VectorStore {
98
99
  if (!result) {
99
100
  return [];
100
101
  }
102
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
101
103
  return result.map(([document, score]) => [
102
104
  new document_js_1.Document({
103
105
  pageContent: document.content,