langchain 0.0.142 → 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 (68) 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.cjs +1 -0
  23. package/dist/chat_models/openai.d.ts +2 -3
  24. package/dist/chat_models/openai.js +1 -0
  25. package/dist/document_loaders/fs/openai_whisper_audio.cjs +32 -0
  26. package/dist/document_loaders/fs/openai_whisper_audio.d.ts +11 -0
  27. package/dist/document_loaders/fs/openai_whisper_audio.js +28 -0
  28. package/dist/document_loaders/web/github.cjs +210 -24
  29. package/dist/document_loaders/web/github.d.ts +44 -1
  30. package/dist/document_loaders/web/github.js +210 -24
  31. package/dist/document_loaders/web/recursive_url.cjs +13 -0
  32. package/dist/document_loaders/web/recursive_url.js +13 -0
  33. package/dist/embeddings/hf_transformers.cjs +71 -0
  34. package/dist/embeddings/hf_transformers.d.ts +29 -0
  35. package/dist/embeddings/hf_transformers.js +67 -0
  36. package/dist/embeddings/openai.cjs +2 -1
  37. package/dist/embeddings/openai.js +2 -1
  38. package/dist/experimental/chat_models/anthropic_functions.d.ts +2 -5
  39. package/dist/llms/openai-chat.cjs +1 -0
  40. package/dist/llms/openai-chat.js +1 -0
  41. package/dist/llms/openai.cjs +1 -0
  42. package/dist/llms/openai.js +1 -0
  43. package/dist/load/import_constants.cjs +3 -0
  44. package/dist/load/import_constants.js +3 -0
  45. package/dist/prompts/chat.cjs +27 -1
  46. package/dist/prompts/chat.d.ts +3 -2
  47. package/dist/prompts/chat.js +28 -2
  48. package/dist/schema/index.cjs +44 -1
  49. package/dist/schema/index.d.ts +10 -0
  50. package/dist/schema/index.js +41 -0
  51. package/dist/tools/serpapi.cjs +108 -13
  52. package/dist/tools/serpapi.js +108 -13
  53. package/dist/vectorstores/redis.cjs +12 -4
  54. package/dist/vectorstores/redis.d.ts +8 -0
  55. package/dist/vectorstores/redis.js +12 -4
  56. package/dist/vectorstores/tigris.cjs +2 -0
  57. package/dist/vectorstores/tigris.d.ts +2 -3
  58. package/dist/vectorstores/tigris.js +2 -0
  59. package/dist/vectorstores/vectara.cjs +30 -12
  60. package/dist/vectorstores/vectara.d.ts +1 -1
  61. package/dist/vectorstores/vectara.js +30 -12
  62. package/document_loaders/fs/openai_whisper_audio.cjs +1 -0
  63. package/document_loaders/fs/openai_whisper_audio.d.ts +1 -0
  64. package/document_loaders/fs/openai_whisper_audio.js +1 -0
  65. package/embeddings/hf_transformers.cjs +1 -0
  66. package/embeddings/hf_transformers.d.ts +1 -0
  67. package/embeddings/hf_transformers.js +1 -0
  68. package/package.json +36 -6
@@ -1,13 +1,10 @@
1
- import type { OpenAI as OpenAIClient } from "openai";
2
1
  import { BaseChatModelParams } from "../../chat_models/base.js";
3
2
  import { CallbackManagerForLLMRun } from "../../callbacks/manager.js";
4
3
  import { BaseMessage, ChatResult } from "../../schema/index.js";
5
4
  import { ChatAnthropic, type AnthropicInput } from "../../chat_models/anthropic.js";
6
- import { BaseLanguageModelCallOptions } from "../../base_language/index.js";
5
+ import { BaseFunctionCallOptions } from "../../base_language/index.js";
7
6
  import { StructuredTool } from "../../tools/base.js";
8
- export interface ChatAnthropicFunctionsCallOptions extends BaseLanguageModelCallOptions {
9
- function_call?: OpenAIClient.Chat.ChatCompletionCreateParams.FunctionCallOption;
10
- functions?: OpenAIClient.Chat.ChatCompletionCreateParams.Function[];
7
+ export interface ChatAnthropicFunctionsCallOptions extends BaseFunctionCallOptions {
11
8
  tools?: StructuredTool[];
12
9
  }
13
10
  export declare class AnthropicFunctions extends ChatAnthropic<ChatAnthropicFunctionsCallOptions> {
@@ -258,6 +258,7 @@ class OpenAIChat extends base_js_1.LLM {
258
258
  if (!this.azureOpenAIApiVersion) {
259
259
  throw new Error("Azure OpenAI API version not found");
260
260
  }
261
+ this.openAIApiKey = this.openAIApiKey ?? "";
261
262
  }
262
263
  this.clientConfig = {
263
264
  apiKey: this.openAIApiKey,
@@ -255,6 +255,7 @@ export class OpenAIChat extends LLM {
255
255
  if (!this.azureOpenAIApiVersion) {
256
256
  throw new Error("Azure OpenAI API version not found");
257
257
  }
258
+ this.openAIApiKey = this.openAIApiKey ?? "";
258
259
  }
259
260
  this.clientConfig = {
260
261
  apiKey: this.openAIApiKey,
@@ -266,6 +266,7 @@ class OpenAI extends base_js_1.BaseLLM {
266
266
  if (!this.azureOpenAIApiVersion) {
267
267
  throw new Error("Azure OpenAI API version not found");
268
268
  }
269
+ this.openAIApiKey = this.openAIApiKey ?? "";
269
270
  }
270
271
  this.clientConfig = {
271
272
  apiKey: this.openAIApiKey,
@@ -263,6 +263,7 @@ export class OpenAI extends BaseLLM {
263
263
  if (!this.azureOpenAIApiVersion) {
264
264
  throw new Error("Azure OpenAI API version not found");
265
265
  }
266
+ this.openAIApiKey = this.openAIApiKey ?? "";
266
267
  }
267
268
  this.clientConfig = {
268
269
  apiKey: this.openAIApiKey,
@@ -18,6 +18,7 @@ exports.optionalImportEntrypoints = [
18
18
  "langchain/embeddings/cohere",
19
19
  "langchain/embeddings/tensorflow",
20
20
  "langchain/embeddings/hf",
21
+ "langchain/embeddings/hf_transformers",
21
22
  "langchain/embeddings/googlevertexai",
22
23
  "langchain/embeddings/googlepalm",
23
24
  "langchain/llms/load",
@@ -87,11 +88,13 @@ exports.optionalImportEntrypoints = [
87
88
  "langchain/document_loaders/fs/csv",
88
89
  "langchain/document_loaders/fs/notion",
89
90
  "langchain/document_loaders/fs/unstructured",
91
+ "langchain/document_loaders/fs/openai_whisper_audio",
90
92
  "langchain/document_transformers/html_to_text",
91
93
  "langchain/document_transformers/mozilla_readability",
92
94
  "langchain/chat_models/googlevertexai",
93
95
  "langchain/chat_models/googlepalm",
94
96
  "langchain/sql_db",
97
+ "langchain/callbacks/handlers/llmonitor",
95
98
  "langchain/output_parsers/expression",
96
99
  "langchain/retrievers/amazon_kendra",
97
100
  "langchain/retrievers/supabase",
@@ -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.