langchain 0.0.87 → 0.0.89

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 (93) hide show
  1. package/dist/agents/chat_convo/prompt.cjs +2 -2
  2. package/dist/agents/chat_convo/prompt.d.ts +2 -2
  3. package/dist/agents/chat_convo/prompt.js +2 -2
  4. package/dist/agents/structured_chat/outputParser.cjs +2 -2
  5. package/dist/agents/structured_chat/outputParser.d.ts +2 -1
  6. package/dist/agents/structured_chat/outputParser.js +2 -2
  7. package/dist/agents/structured_chat/prompt.cjs +1 -1
  8. package/dist/agents/structured_chat/prompt.d.ts +1 -1
  9. package/dist/agents/structured_chat/prompt.js +1 -1
  10. package/dist/chains/query_constructor/index.cjs +3 -3
  11. package/dist/chains/query_constructor/index.d.ts +1 -1
  12. package/dist/chains/query_constructor/index.js +1 -1
  13. package/dist/chat_models/openai.cjs +111 -1
  14. package/dist/chat_models/openai.d.ts +11 -0
  15. package/dist/chat_models/openai.js +109 -0
  16. package/dist/document_loaders/fs/unstructured.cjs +6 -0
  17. package/dist/document_loaders/fs/unstructured.js +6 -0
  18. package/dist/experimental/generative_agents/generative_agent.cjs +276 -0
  19. package/dist/experimental/generative_agents/generative_agent.d.ts +43 -0
  20. package/dist/experimental/generative_agents/generative_agent.js +272 -0
  21. package/dist/experimental/generative_agents/generative_agent_memory.cjs +326 -0
  22. package/dist/experimental/generative_agents/generative_agent_memory.d.ts +51 -0
  23. package/dist/experimental/generative_agents/generative_agent_memory.js +322 -0
  24. package/dist/experimental/generative_agents/index.cjs +7 -0
  25. package/dist/experimental/generative_agents/index.d.ts +2 -0
  26. package/dist/experimental/generative_agents/index.js +2 -0
  27. package/dist/llms/openai-chat.cjs +29 -20
  28. package/dist/llms/openai-chat.d.ts +4 -0
  29. package/dist/llms/openai-chat.js +29 -20
  30. package/dist/llms/openai.cjs +31 -20
  31. package/dist/llms/openai.d.ts +3 -0
  32. package/dist/llms/openai.js +31 -20
  33. package/dist/output_parsers/expression.cjs +24 -6
  34. package/dist/output_parsers/expression.d.ts +9 -0
  35. package/dist/output_parsers/expression.js +24 -6
  36. package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.d.ts +2 -3
  37. package/dist/output_parsers/expression_type_handlers/base.cjs +11 -13
  38. package/dist/output_parsers/expression_type_handlers/base.d.ts +15 -17
  39. package/dist/output_parsers/expression_type_handlers/base.js +11 -13
  40. package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.d.ts +2 -3
  41. package/dist/output_parsers/expression_type_handlers/call_expression_handler.d.ts +2 -3
  42. package/dist/output_parsers/expression_type_handlers/factory.d.ts +2 -3
  43. package/dist/output_parsers/expression_type_handlers/grammar/parser_grammar.cjs +296 -0
  44. package/dist/output_parsers/expression_type_handlers/grammar/parser_grammar.d.ts +13 -0
  45. package/dist/output_parsers/expression_type_handlers/grammar/parser_grammar.js +293 -0
  46. package/dist/output_parsers/expression_type_handlers/identifier_handler.d.ts +2 -3
  47. package/dist/output_parsers/expression_type_handlers/member_expression_handler.cjs +2 -2
  48. package/dist/output_parsers/expression_type_handlers/member_expression_handler.d.ts +2 -3
  49. package/dist/output_parsers/expression_type_handlers/member_expression_handler.js +2 -2
  50. package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.d.ts +2 -3
  51. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.cjs +1 -1
  52. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.d.ts +2 -3
  53. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.js +1 -1
  54. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.cjs +1 -1
  55. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.d.ts +2 -3
  56. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.js +1 -1
  57. package/dist/output_parsers/expression_type_handlers/string_literal_handler.d.ts +2 -3
  58. package/dist/retrievers/self_query/index.cjs +1 -1
  59. package/dist/retrievers/self_query/index.js +2 -2
  60. package/dist/retrievers/time_weighted.cjs +6 -0
  61. package/dist/retrievers/time_weighted.d.ts +1 -0
  62. package/dist/retrievers/time_weighted.js +6 -0
  63. package/dist/retrievers/vespa.cjs +38 -0
  64. package/dist/retrievers/vespa.d.ts +19 -0
  65. package/dist/retrievers/vespa.js +34 -0
  66. package/dist/tools/brave_search.cjs +57 -0
  67. package/dist/tools/brave_search.d.ts +12 -0
  68. package/dist/tools/brave_search.js +53 -0
  69. package/dist/tools/index.cjs +3 -1
  70. package/dist/tools/index.d.ts +1 -0
  71. package/dist/tools/index.js +1 -0
  72. package/dist/util/prompt-layer.cjs +28 -0
  73. package/dist/util/prompt-layer.d.ts +3 -0
  74. package/dist/util/prompt-layer.js +24 -0
  75. package/dist/vectorstores/milvus.cjs +2 -0
  76. package/dist/vectorstores/milvus.d.ts +2 -0
  77. package/dist/vectorstores/milvus.js +2 -0
  78. package/dist/vectorstores/singlestore.cjs +109 -0
  79. package/dist/vectorstores/singlestore.d.ts +25 -0
  80. package/dist/vectorstores/singlestore.js +105 -0
  81. package/dist/vectorstores/supabase.cjs +15 -4
  82. package/dist/vectorstores/supabase.d.ts +6 -3
  83. package/dist/vectorstores/supabase.js +15 -4
  84. package/experimental/generative_agents.cjs +1 -0
  85. package/experimental/generative_agents.d.ts +1 -0
  86. package/experimental/generative_agents.js +1 -0
  87. package/package.json +39 -5
  88. package/retrievers/vespa.cjs +1 -0
  89. package/retrievers/vespa.d.ts +1 -0
  90. package/retrievers/vespa.js +1 -0
  91. package/vectorstores/singlestore.cjs +1 -0
  92. package/vectorstores/singlestore.d.ts +1 -0
  93. package/vectorstores/singlestore.js +1 -0
@@ -20,7 +20,7 @@ Markdown code snippet formatted in the following schema:
20
20
 
21
21
  \`\`\`json
22
22
  {{{{
23
- "action": string // The action to take. Must be one of [{tool_names}]
23
+ "action": string, // The action to take. Must be one of [{tool_names}]
24
24
  "action_input": string // The input to the action. May be a stringified object.
25
25
  }}}}
26
26
  \`\`\`
@@ -57,4 +57,4 @@ exports.TEMPLATE_TOOL_RESPONSE = `TOOL RESPONSE:
57
57
  USER'S INPUT
58
58
  --------------------
59
59
 
60
- Okay, so what is the response to my original question? If using information from tools, you must say it explicitly - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else.`;
60
+ Okay, so what is the response to my last comment? If using information obtained from the tools you must mention it explicitly without mentioning the tool names - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else.`;
@@ -1,5 +1,5 @@
1
1
  export declare const DEFAULT_PREFIX = "Assistant is a large language model trained by OpenAI.\n\nAssistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n\nAssistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.\n\nOverall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.";
2
2
  export declare const PREFIX_END = " However, above all else, all responses must adhere to the format of RESPONSE FORMAT INSTRUCTIONS.";
3
- export declare const FORMAT_INSTRUCTIONS = "RESPONSE FORMAT INSTRUCTIONS\n----------------------------\n\nOutput a JSON markdown code snippet containing a valid JSON object in one of two formats:\n\n**Option 1:**\nUse this if you want the human to use a tool.\nMarkdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": string // The action to take. Must be one of [{tool_names}]\n \"action_input\": string // The input to the action. May be a stringified object.\n}}}}\n```\n\n**Option #2:**\nUse this if you want to respond directly and conversationally to the human. Markdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": \"Final Answer\",\n \"action_input\": string // You should put what you want to return to use here and make sure to use valid json newline characters.\n}}}}\n```\n\nFor both options, remember to always include the surrounding markdown code snippet delimiters (begin with \"```json\" and end with \"```\")!\n";
3
+ export declare const FORMAT_INSTRUCTIONS = "RESPONSE FORMAT INSTRUCTIONS\n----------------------------\n\nOutput a JSON markdown code snippet containing a valid JSON object in one of two formats:\n\n**Option 1:**\nUse this if you want the human to use a tool.\nMarkdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": string, // The action to take. Must be one of [{tool_names}]\n \"action_input\": string // The input to the action. May be a stringified object.\n}}}}\n```\n\n**Option #2:**\nUse this if you want to respond directly and conversationally to the human. Markdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": \"Final Answer\",\n \"action_input\": string // You should put what you want to return to use here and make sure to use valid json newline characters.\n}}}}\n```\n\nFor both options, remember to always include the surrounding markdown code snippet delimiters (begin with \"```json\" and end with \"```\")!\n";
4
4
  export declare const DEFAULT_SUFFIX = "TOOLS\n------\nAssistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:\n\n{tools}\n\n{format_instructions}\n\nUSER'S INPUT\n--------------------\nHere is the user's input (remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else):\n\n{{input}}";
5
- export declare const TEMPLATE_TOOL_RESPONSE = "TOOL RESPONSE:\n---------------------\n{observation}\n\nUSER'S INPUT\n--------------------\n\nOkay, so what is the response to my original question? If using information from tools, you must say it explicitly - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else.";
5
+ export declare const TEMPLATE_TOOL_RESPONSE = "TOOL RESPONSE:\n---------------------\n{observation}\n\nUSER'S INPUT\n--------------------\n\nOkay, so what is the response to my last comment? If using information obtained from the tools you must mention it explicitly without mentioning the tool names - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else.";
@@ -17,7 +17,7 @@ Markdown code snippet formatted in the following schema:
17
17
 
18
18
  \`\`\`json
19
19
  {{{{
20
- "action": string // The action to take. Must be one of [{tool_names}]
20
+ "action": string, // The action to take. Must be one of [{tool_names}]
21
21
  "action_input": string // The input to the action. May be a stringified object.
22
22
  }}}}
23
23
  \`\`\`
@@ -54,4 +54,4 @@ export const TEMPLATE_TOOL_RESPONSE = `TOOL RESPONSE:
54
54
  USER'S INPUT
55
55
  --------------------
56
56
 
57
- Okay, so what is the response to my original question? If using information from tools, you must say it explicitly - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else.`;
57
+ Okay, so what is the response to my last comment? If using information obtained from the tools you must mention it explicitly without mentioning the tool names - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else.`;
@@ -67,9 +67,9 @@ class StructuredChatOutputParserWithRetries extends types_js_1.AgentActionOutput
67
67
  fields?.baseParser ?? new StructuredChatOutputParser(this.toolNames);
68
68
  this.outputFixingParser = fields?.outputFixingParser;
69
69
  }
70
- async parse(text) {
70
+ async parse(text, callbacks) {
71
71
  if (this.outputFixingParser !== undefined) {
72
- return this.outputFixingParser.parse(text);
72
+ return this.outputFixingParser.parse(text, callbacks);
73
73
  }
74
74
  return this.baseParser.parse(text);
75
75
  }
@@ -2,6 +2,7 @@ import { AgentActionOutputParser } from "../types.js";
2
2
  import { OutputFixingParser } from "../../output_parsers/fix.js";
3
3
  import { BaseLanguageModel } from "../../base_language/index.js";
4
4
  import { AgentAction, AgentFinish } from "../../schema/index.js";
5
+ import { Callbacks } from "../../callbacks/manager.js";
5
6
  export declare class StructuredChatOutputParser extends AgentActionOutputParser {
6
7
  private toolNames;
7
8
  constructor(toolNames: string[]);
@@ -18,7 +19,7 @@ export declare class StructuredChatOutputParserWithRetries extends AgentActionOu
18
19
  private outputFixingParser?;
19
20
  private toolNames;
20
21
  constructor(fields: StructuredChatOutputParserArgs);
21
- parse(text: string): Promise<AgentAction | AgentFinish>;
22
+ parse(text: string, callbacks?: Callbacks): Promise<AgentAction | AgentFinish>;
22
23
  getFormatInstructions(): string;
23
24
  static fromLLM(llm: BaseLanguageModel, options: Omit<StructuredChatOutputParserArgs, "outputFixingParser">): StructuredChatOutputParserWithRetries;
24
25
  }
@@ -63,9 +63,9 @@ export class StructuredChatOutputParserWithRetries extends AgentActionOutputPars
63
63
  fields?.baseParser ?? new StructuredChatOutputParser(this.toolNames);
64
64
  this.outputFixingParser = fields?.outputFixingParser;
65
65
  }
66
- async parse(text) {
66
+ async parse(text, callbacks) {
67
67
  if (this.outputFixingParser !== undefined) {
68
- return this.outputFixingParser.parse(text);
68
+ return this.outputFixingParser.parse(text, callbacks);
69
69
  }
70
70
  return this.baseParser.parse(text);
71
71
  }
@@ -11,7 +11,7 @@ The $JSON_BLOB must be valid, parseable JSON and only contain a SINGLE action. H
11
11
 
12
12
  \`\`\`json
13
13
  {{
14
- "action": $TOOL_NAME
14
+ "action": $TOOL_NAME,
15
15
  "action_input": $INPUT
16
16
  }}
17
17
  \`\`\`
@@ -1,4 +1,4 @@
1
1
  export declare const PREFIX = "Answer the following questions truthfully and as best you can.";
2
- export declare const AGENT_ACTION_FORMAT_INSTRUCTIONS = "Output a JSON markdown code snippet containing a valid JSON blob (denoted below by $JSON_BLOB).\nThis $JSON_BLOB must have a \"action\" key (with the name of the tool to use) and an \"action_input\" key (tool input).\n\nValid \"action\" values: \"Final Answer\" (which you must use when giving your final response to the user), or one of [{tool_names}].\n\nThe $JSON_BLOB must be valid, parseable JSON and only contain a SINGLE action. Here is an example of an acceptable output:\n\n```json\n{{\n \"action\": $TOOL_NAME\n \"action_input\": $INPUT\n}}\n```\n\nRemember to include the surrounding markdown code snippet delimiters (begin with \"```\" json and close with \"```\")!\n";
2
+ export declare const AGENT_ACTION_FORMAT_INSTRUCTIONS = "Output a JSON markdown code snippet containing a valid JSON blob (denoted below by $JSON_BLOB).\nThis $JSON_BLOB must have a \"action\" key (with the name of the tool to use) and an \"action_input\" key (tool input).\n\nValid \"action\" values: \"Final Answer\" (which you must use when giving your final response to the user), or one of [{tool_names}].\n\nThe $JSON_BLOB must be valid, parseable JSON and only contain a SINGLE action. Here is an example of an acceptable output:\n\n```json\n{{\n \"action\": $TOOL_NAME,\n \"action_input\": $INPUT\n}}\n```\n\nRemember to include the surrounding markdown code snippet delimiters (begin with \"```\" json and close with \"```\")!\n";
3
3
  export declare const FORMAT_INSTRUCTIONS: string;
4
4
  export declare const SUFFIX = "Begin! Reminder to ALWAYS use the above format, and to use tools if appropriate.";
@@ -8,7 +8,7 @@ The $JSON_BLOB must be valid, parseable JSON and only contain a SINGLE action. H
8
8
 
9
9
  \`\`\`json
10
10
  {{
11
- "action": $TOOL_NAME
11
+ "action": $TOOL_NAME,
12
12
  "action_input": $INPUT
13
13
  }}
14
14
  \`\`\`
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.loadQueryContstructorChain = exports.formatAttributeInfo = exports.StructuredQueryOutputParser = exports.EXAMPLE_PROMPT = exports.DEFAULT_SUFFIX = exports.DEFAULT_SCHEMA = exports.DEFAULT_PREFIX = exports.DEFAULT_EXAMPLES = exports.QueryTransformer = void 0;
3
+ exports.loadQueryConstructorChain = exports.formatAttributeInfo = exports.StructuredQueryOutputParser = exports.EXAMPLE_PROMPT = exports.DEFAULT_SUFFIX = exports.DEFAULT_SCHEMA = exports.DEFAULT_PREFIX = exports.DEFAULT_EXAMPLES = exports.QueryTransformer = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const parser_js_1 = require("./parser.cjs");
6
6
  Object.defineProperty(exports, "QueryTransformer", { enumerable: true, get: function () { return parser_js_1.QueryTransformer; } });
@@ -95,11 +95,11 @@ function _getPrompt(documentContents, attributeInfo, allowedComparators, allowed
95
95
  outputParser,
96
96
  });
97
97
  }
98
- function loadQueryContstructorChain(opts) {
98
+ function loadQueryConstructorChain(opts) {
99
99
  const prompt = _getPrompt(opts.documentContents, opts.attributeInfo, opts.allowedComparators, opts.allowedOperators, opts.examples);
100
100
  return new llm_chain_js_1.LLMChain({
101
101
  llm: opts.llm,
102
102
  prompt,
103
103
  });
104
104
  }
105
- exports.loadQueryContstructorChain = loadQueryContstructorChain;
105
+ exports.loadQueryConstructorChain = loadQueryConstructorChain;
@@ -34,4 +34,4 @@ export type QueryConstructorChainOptions = {
34
34
  allowedComparators?: Comparator[];
35
35
  allowedOperators?: Operator[];
36
36
  };
37
- export declare function loadQueryContstructorChain(opts: QueryConstructorChainOptions): LLMChain<string>;
37
+ export declare function loadQueryConstructorChain(opts: QueryConstructorChainOptions): LLMChain<string>;
@@ -86,7 +86,7 @@ function _getPrompt(documentContents, attributeInfo, allowedComparators, allowed
86
86
  outputParser,
87
87
  });
88
88
  }
89
- export function loadQueryContstructorChain(opts) {
89
+ export function loadQueryConstructorChain(opts) {
90
90
  const prompt = _getPrompt(opts.documentContents, opts.attributeInfo, opts.allowedComparators, opts.allowedOperators, opts.examples);
91
91
  return new LLMChain({
92
92
  llm: opts.llm,
@@ -3,13 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ChatOpenAI = void 0;
6
+ exports.PromptLayerChatOpenAI = exports.ChatOpenAI = void 0;
7
7
  const openai_1 = require("openai");
8
8
  const env_js_1 = require("../util/env.cjs");
9
9
  const axios_fetch_adapter_js_1 = __importDefault(require("../util/axios-fetch-adapter.cjs"));
10
10
  const base_js_1 = require("./base.cjs");
11
11
  const index_js_1 = require("../schema/index.cjs");
12
12
  const count_tokens_js_1 = require("../base_language/count_tokens.cjs");
13
+ const prompt_layer_js_1 = require("../util/prompt-layer.cjs");
13
14
  function messageTypeToOpenAIRole(type) {
14
15
  switch (type) {
15
16
  case "system":
@@ -447,3 +448,112 @@ class ChatOpenAI extends base_js_1.BaseChatModel {
447
448
  }
448
449
  }
449
450
  exports.ChatOpenAI = ChatOpenAI;
451
+ class PromptLayerChatOpenAI extends ChatOpenAI {
452
+ constructor(fields) {
453
+ super(fields);
454
+ Object.defineProperty(this, "promptLayerApiKey", {
455
+ enumerable: true,
456
+ configurable: true,
457
+ writable: true,
458
+ value: void 0
459
+ });
460
+ Object.defineProperty(this, "plTags", {
461
+ enumerable: true,
462
+ configurable: true,
463
+ writable: true,
464
+ value: void 0
465
+ });
466
+ Object.defineProperty(this, "returnPromptLayerId", {
467
+ enumerable: true,
468
+ configurable: true,
469
+ writable: true,
470
+ value: void 0
471
+ });
472
+ this.promptLayerApiKey =
473
+ fields?.promptLayerApiKey ??
474
+ (typeof process !== "undefined"
475
+ ? // eslint-disable-next-line no-process-env
476
+ process.env?.PROMPTLAYER_API_KEY
477
+ : undefined);
478
+ this.plTags = fields?.plTags ?? [];
479
+ this.returnPromptLayerId = fields?.returnPromptLayerId ?? false;
480
+ }
481
+ async _generate(messages, options, runManager) {
482
+ const requestStartTime = Date.now();
483
+ let parsedOptions;
484
+ if (Array.isArray(options)) {
485
+ parsedOptions = { stop: options };
486
+ }
487
+ else if (options?.timeout && !options.signal) {
488
+ parsedOptions = {
489
+ ...options,
490
+ signal: AbortSignal.timeout(options.timeout),
491
+ };
492
+ }
493
+ else {
494
+ parsedOptions = options ?? {};
495
+ }
496
+ const generatedResponses = await super._generate(messages, parsedOptions, runManager);
497
+ const requestEndTime = Date.now();
498
+ const _convertMessageToDict = (message) => {
499
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
500
+ let messageDict;
501
+ if (message._getType() === "human") {
502
+ messageDict = { role: "user", content: message.text };
503
+ }
504
+ else if (message._getType() === "ai") {
505
+ messageDict = { role: "assistant", content: message.text };
506
+ }
507
+ else if (message._getType() === "system") {
508
+ messageDict = { role: "system", content: message.text };
509
+ }
510
+ else if (message._getType() === "generic") {
511
+ messageDict = {
512
+ role: message.role,
513
+ content: message.text,
514
+ };
515
+ }
516
+ else {
517
+ throw new Error(`Got unknown type ${message}`);
518
+ }
519
+ return messageDict;
520
+ };
521
+ const _createMessageDicts = (messages, callOptions) => {
522
+ const params = {
523
+ ...this.invocationParams(),
524
+ model: this.modelName,
525
+ };
526
+ if (callOptions?.stop) {
527
+ if (Object.keys(params).includes("stop")) {
528
+ throw new Error("`stop` found in both the input and default params.");
529
+ }
530
+ }
531
+ const messageDicts = messages.map((message) => _convertMessageToDict(message));
532
+ return messageDicts;
533
+ };
534
+ for (let i = 0; i < generatedResponses.generations.length; i += 1) {
535
+ const generation = generatedResponses.generations[i];
536
+ const messageDicts = _createMessageDicts(messages, parsedOptions);
537
+ let promptLayerRequestId;
538
+ const parsedResp = [
539
+ {
540
+ content: generation.text,
541
+ role: messageTypeToOpenAIRole(generation.message._getType()),
542
+ },
543
+ ];
544
+ const promptLayerRespBody = await (0, prompt_layer_js_1.promptLayerTrackRequest)(this.caller, "langchain.PromptLayerChatOpenAI", messageDicts, this._identifyingParams(), this.plTags, parsedResp, requestStartTime, requestEndTime, this.promptLayerApiKey);
545
+ if (this.returnPromptLayerId === true) {
546
+ if (promptLayerRespBody.success === true) {
547
+ promptLayerRequestId = promptLayerRespBody.request_id;
548
+ }
549
+ if (!generation.generationInfo ||
550
+ typeof generation.generationInfo !== "object") {
551
+ generation.generationInfo = {};
552
+ }
553
+ generation.generationInfo.promptLayerRequestId = promptLayerRequestId;
554
+ }
555
+ }
556
+ return generatedResponses;
557
+ }
558
+ }
559
+ exports.PromptLayerChatOpenAI = PromptLayerChatOpenAI;
@@ -121,3 +121,14 @@ export declare class ChatOpenAI extends BaseChatModel implements OpenAIChatInput
121
121
  /** @ignore */
122
122
  _combineLLMOutput(...llmOutputs: OpenAILLMOutput[]): OpenAILLMOutput;
123
123
  }
124
+ export declare class PromptLayerChatOpenAI extends ChatOpenAI {
125
+ promptLayerApiKey?: string;
126
+ plTags?: string[];
127
+ returnPromptLayerId?: boolean;
128
+ constructor(fields?: ConstructorParameters<typeof ChatOpenAI>[0] & {
129
+ promptLayerApiKey?: string;
130
+ plTags?: string[];
131
+ returnPromptLayerId?: boolean;
132
+ });
133
+ _generate(messages: BaseChatMessage[], options?: string[] | this["CallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
134
+ }
@@ -4,6 +4,7 @@ import fetchAdapter from "../util/axios-fetch-adapter.js";
4
4
  import { BaseChatModel } from "./base.js";
5
5
  import { AIChatMessage, ChatMessage, HumanChatMessage, SystemChatMessage, } from "../schema/index.js";
6
6
  import { getModelNameForTiktoken } from "../base_language/count_tokens.js";
7
+ import { promptLayerTrackRequest } from "../util/prompt-layer.js";
7
8
  function messageTypeToOpenAIRole(type) {
8
9
  switch (type) {
9
10
  case "system":
@@ -440,3 +441,111 @@ export class ChatOpenAI extends BaseChatModel {
440
441
  });
441
442
  }
442
443
  }
444
+ export class PromptLayerChatOpenAI extends ChatOpenAI {
445
+ constructor(fields) {
446
+ super(fields);
447
+ Object.defineProperty(this, "promptLayerApiKey", {
448
+ enumerable: true,
449
+ configurable: true,
450
+ writable: true,
451
+ value: void 0
452
+ });
453
+ Object.defineProperty(this, "plTags", {
454
+ enumerable: true,
455
+ configurable: true,
456
+ writable: true,
457
+ value: void 0
458
+ });
459
+ Object.defineProperty(this, "returnPromptLayerId", {
460
+ enumerable: true,
461
+ configurable: true,
462
+ writable: true,
463
+ value: void 0
464
+ });
465
+ this.promptLayerApiKey =
466
+ fields?.promptLayerApiKey ??
467
+ (typeof process !== "undefined"
468
+ ? // eslint-disable-next-line no-process-env
469
+ process.env?.PROMPTLAYER_API_KEY
470
+ : undefined);
471
+ this.plTags = fields?.plTags ?? [];
472
+ this.returnPromptLayerId = fields?.returnPromptLayerId ?? false;
473
+ }
474
+ async _generate(messages, options, runManager) {
475
+ const requestStartTime = Date.now();
476
+ let parsedOptions;
477
+ if (Array.isArray(options)) {
478
+ parsedOptions = { stop: options };
479
+ }
480
+ else if (options?.timeout && !options.signal) {
481
+ parsedOptions = {
482
+ ...options,
483
+ signal: AbortSignal.timeout(options.timeout),
484
+ };
485
+ }
486
+ else {
487
+ parsedOptions = options ?? {};
488
+ }
489
+ const generatedResponses = await super._generate(messages, parsedOptions, runManager);
490
+ const requestEndTime = Date.now();
491
+ const _convertMessageToDict = (message) => {
492
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
493
+ let messageDict;
494
+ if (message._getType() === "human") {
495
+ messageDict = { role: "user", content: message.text };
496
+ }
497
+ else if (message._getType() === "ai") {
498
+ messageDict = { role: "assistant", content: message.text };
499
+ }
500
+ else if (message._getType() === "system") {
501
+ messageDict = { role: "system", content: message.text };
502
+ }
503
+ else if (message._getType() === "generic") {
504
+ messageDict = {
505
+ role: message.role,
506
+ content: message.text,
507
+ };
508
+ }
509
+ else {
510
+ throw new Error(`Got unknown type ${message}`);
511
+ }
512
+ return messageDict;
513
+ };
514
+ const _createMessageDicts = (messages, callOptions) => {
515
+ const params = {
516
+ ...this.invocationParams(),
517
+ model: this.modelName,
518
+ };
519
+ if (callOptions?.stop) {
520
+ if (Object.keys(params).includes("stop")) {
521
+ throw new Error("`stop` found in both the input and default params.");
522
+ }
523
+ }
524
+ const messageDicts = messages.map((message) => _convertMessageToDict(message));
525
+ return messageDicts;
526
+ };
527
+ for (let i = 0; i < generatedResponses.generations.length; i += 1) {
528
+ const generation = generatedResponses.generations[i];
529
+ const messageDicts = _createMessageDicts(messages, parsedOptions);
530
+ let promptLayerRequestId;
531
+ const parsedResp = [
532
+ {
533
+ content: generation.text,
534
+ role: messageTypeToOpenAIRole(generation.message._getType()),
535
+ },
536
+ ];
537
+ const promptLayerRespBody = await promptLayerTrackRequest(this.caller, "langchain.PromptLayerChatOpenAI", messageDicts, this._identifyingParams(), this.plTags, parsedResp, requestStartTime, requestEndTime, this.promptLayerApiKey);
538
+ if (this.returnPromptLayerId === true) {
539
+ if (promptLayerRespBody.success === true) {
540
+ promptLayerRequestId = promptLayerRespBody.request_id;
541
+ }
542
+ if (!generation.generationInfo ||
543
+ typeof generation.generationInfo !== "object") {
544
+ generation.generationInfo = {};
545
+ }
546
+ generation.generationInfo.promptLayerRequestId = promptLayerRequestId;
547
+ }
548
+ }
549
+ return generatedResponses;
550
+ }
551
+ }
@@ -16,10 +16,16 @@ const UNSTRUCTURED_API_FILETYPES = [
16
16
  ".jpeg",
17
17
  ".eml",
18
18
  ".html",
19
+ ".htm",
19
20
  ".md",
20
21
  ".pptx",
21
22
  ".ppt",
22
23
  ".msg",
24
+ ".rtf",
25
+ ".xlsx",
26
+ ".xls",
27
+ ".odt",
28
+ ".epub",
23
29
  ];
24
30
  class UnstructuredLoader extends base_js_1.BaseDocumentLoader {
25
31
  constructor(filePathOrLegacyApiUrl, optionsOrLegacyFilePath = {}) {
@@ -12,10 +12,16 @@ const UNSTRUCTURED_API_FILETYPES = [
12
12
  ".jpeg",
13
13
  ".eml",
14
14
  ".html",
15
+ ".htm",
15
16
  ".md",
16
17
  ".pptx",
17
18
  ".ppt",
18
19
  ".msg",
20
+ ".rtf",
21
+ ".xlsx",
22
+ ".xls",
23
+ ".odt",
24
+ ".epub",
19
25
  ];
20
26
  export class UnstructuredLoader extends BaseDocumentLoader {
21
27
  constructor(filePathOrLegacyApiUrl, optionsOrLegacyFilePath = {}) {