base.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import * as uuid from "uuid";
  2. import { Serializable, get_lc_unique_name, } from "../load/serializable.js";
  3. import { getEnvironmentVariable } from "../utils/env.js";
  4. /**
  5. * Abstract class that provides a set of optional methods that can be
  6. * overridden in derived classes to handle various events during the
  7. * execution of a LangChain application.
  8. */
  9. class BaseCallbackHandlerMethodsClass {
  10. }
  11. export function callbackHandlerPrefersStreaming(x) {
  12. return "lc_prefer_streaming" in x && x.lc_prefer_streaming;
  13. }
  14. /**
  15. * Abstract base class for creating callback handlers in the LangChain
  16. * framework. It provides a set of optional methods that can be overridden
  17. * in derived classes to handle various events during the execution of a
  18. * LangChain application.
  19. */
  20. export class BaseCallbackHandler extends BaseCallbackHandlerMethodsClass {
  21. get lc_namespace() {
  22. return ["langchain_core", "callbacks", this.name];
  23. }
  24. get lc_secrets() {
  25. return undefined;
  26. }
  27. get lc_attributes() {
  28. return undefined;
  29. }
  30. get lc_aliases() {
  31. return undefined;
  32. }
  33. get lc_serializable_keys() {
  34. return undefined;
  35. }
  36. /**
  37. * The name of the serializable. Override to provide an alias or
  38. * to preserve the serialized module name in minified environments.
  39. *
  40. * Implemented as a static method to support loading logic.
  41. */
  42. static lc_name() {
  43. return this.name;
  44. }
  45. /**
  46. * The final serialized identifier for the module.
  47. */
  48. get lc_id() {
  49. return [
  50. ...this.lc_namespace,
  51. get_lc_unique_name(this.constructor),
  52. ];
  53. }
  54. constructor(input) {
  55. super();
  56. Object.defineProperty(this, "lc_serializable", {
  57. enumerable: true,
  58. configurable: true,
  59. writable: true,
  60. value: false
  61. });
  62. Object.defineProperty(this, "lc_kwargs", {
  63. enumerable: true,
  64. configurable: true,
  65. writable: true,
  66. value: void 0
  67. });
  68. Object.defineProperty(this, "ignoreLLM", {
  69. enumerable: true,
  70. configurable: true,
  71. writable: true,
  72. value: false
  73. });
  74. Object.defineProperty(this, "ignoreChain", {
  75. enumerable: true,
  76. configurable: true,
  77. writable: true,
  78. value: false
  79. });
  80. Object.defineProperty(this, "ignoreAgent", {
  81. enumerable: true,
  82. configurable: true,
  83. writable: true,
  84. value: false
  85. });
  86. Object.defineProperty(this, "ignoreRetriever", {
  87. enumerable: true,
  88. configurable: true,
  89. writable: true,
  90. value: false
  91. });
  92. Object.defineProperty(this, "ignoreCustomEvent", {
  93. enumerable: true,
  94. configurable: true,
  95. writable: true,
  96. value: false
  97. });
  98. Object.defineProperty(this, "raiseError", {
  99. enumerable: true,
  100. configurable: true,
  101. writable: true,
  102. value: false
  103. });
  104. Object.defineProperty(this, "awaitHandlers", {
  105. enumerable: true,
  106. configurable: true,
  107. writable: true,
  108. value: getEnvironmentVariable("LANGCHAIN_CALLBACKS_BACKGROUND") === "false"
  109. });
  110. this.lc_kwargs = input || {};
  111. if (input) {
  112. this.ignoreLLM = input.ignoreLLM ?? this.ignoreLLM;
  113. this.ignoreChain = input.ignoreChain ?? this.ignoreChain;
  114. this.ignoreAgent = input.ignoreAgent ?? this.ignoreAgent;
  115. this.ignoreRetriever = input.ignoreRetriever ?? this.ignoreRetriever;
  116. this.ignoreCustomEvent =
  117. input.ignoreCustomEvent ?? this.ignoreCustomEvent;
  118. this.raiseError = input.raiseError ?? this.raiseError;
  119. this.awaitHandlers =
  120. this.raiseError || (input._awaitHandler ?? this.awaitHandlers);
  121. }
  122. }
  123. copy() {
  124. return new this.constructor(this);
  125. }
  126. toJSON() {
  127. return Serializable.prototype.toJSON.call(this);
  128. }
  129. toJSONNotImplemented() {
  130. return Serializable.prototype.toJSONNotImplemented.call(this);
  131. }
  132. static fromMethods(methods) {
  133. class Handler extends BaseCallbackHandler {
  134. constructor() {
  135. super();
  136. Object.defineProperty(this, "name", {
  137. enumerable: true,
  138. configurable: true,
  139. writable: true,
  140. value: uuid.v4()
  141. });
  142. Object.assign(this, methods);
  143. }
  144. }
  145. return new Handler();
  146. }
  147. }
  148. export const isBaseCallbackHandler = (x) => {
  149. const callbackHandler = x;
  150. return (callbackHandler !== undefined &&
  151. typeof callbackHandler.copy === "function" &&
  152. typeof callbackHandler.name === "string" &&
  153. typeof callbackHandler.awaitHandlers === "boolean");
  154. };