base.d.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import type { ChainValues } from "../utils/types/index.js";
  2. import type { BaseMessage } from "../messages/base.js";
  3. import type { AgentAction, AgentFinish } from "../agents.js";
  4. import type { ChatGenerationChunk, GenerationChunk, LLMResult } from "../outputs.js";
  5. import { Serializable, Serialized, SerializedNotImplemented } from "../load/serializable.js";
  6. import type { SerializedFields } from "../load/map_keys.js";
  7. import type { DocumentInterface } from "../documents/document.js";
  8. type Error = any;
  9. /**
  10. * Interface for the input parameters of the BaseCallbackHandler class. It
  11. * allows to specify which types of events should be ignored by the
  12. * callback handler.
  13. */
  14. export interface BaseCallbackHandlerInput {
  15. ignoreLLM?: boolean;
  16. ignoreChain?: boolean;
  17. ignoreAgent?: boolean;
  18. ignoreRetriever?: boolean;
  19. ignoreCustomEvent?: boolean;
  20. _awaitHandler?: boolean;
  21. raiseError?: boolean;
  22. }
  23. /**
  24. * Interface for the indices of a new token produced by an LLM or Chat
  25. * Model in streaming mode.
  26. */
  27. export interface NewTokenIndices {
  28. prompt: number;
  29. completion: number;
  30. }
  31. export type HandleLLMNewTokenCallbackFields = {
  32. chunk?: GenerationChunk | ChatGenerationChunk;
  33. };
  34. /**
  35. * Abstract class that provides a set of optional methods that can be
  36. * overridden in derived classes to handle various events during the
  37. * execution of a LangChain application.
  38. */
  39. declare abstract class BaseCallbackHandlerMethodsClass {
  40. /**
  41. * Called at the start of an LLM or Chat Model run, with the prompt(s)
  42. * and the run ID.
  43. */
  44. handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>, runName?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  45. Promise<any> | any;
  46. /**
  47. * Called when an LLM/ChatModel in `streaming` mode produces a new token
  48. */
  49. handleLLMNewToken?(token: string,
  50. /**
  51. * idx.prompt is the index of the prompt that produced the token
  52. * (if there are multiple prompts)
  53. * idx.completion is the index of the completion that produced the token
  54. * (if multiple completions per prompt are requested)
  55. */
  56. idx: NewTokenIndices, runId: string, parentRunId?: string, tags?: string[], fields?: HandleLLMNewTokenCallbackFields): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  57. Promise<any> | any;
  58. /**
  59. * Called if an LLM/ChatModel run encounters an error
  60. */
  61. handleLLMError?(err: Error, runId: string, parentRunId?: string, tags?: string[], extraParams?: Record<string, unknown>): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  62. Promise<any> | any;
  63. /**
  64. * Called at the end of an LLM/ChatModel run, with the output and the run ID.
  65. */
  66. handleLLMEnd?(output: LLMResult, runId: string, parentRunId?: string, tags?: string[], extraParams?: Record<string, unknown>): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  67. Promise<any> | any;
  68. /**
  69. * Called at the start of a Chat Model run, with the prompt(s)
  70. * and the run ID.
  71. */
  72. handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>, runName?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  73. Promise<any> | any;
  74. /**
  75. * Called at the start of a Chain run, with the chain name and inputs
  76. * and the run ID.
  77. */
  78. handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runType?: string, runName?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  79. Promise<any> | any;
  80. /**
  81. * Called if a Chain run encounters an error
  82. */
  83. handleChainError?(err: Error, runId: string, parentRunId?: string, tags?: string[], kwargs?: {
  84. inputs?: Record<string, unknown>;
  85. }): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  86. Promise<any> | any;
  87. /**
  88. * Called at the end of a Chain run, with the outputs and the run ID.
  89. */
  90. handleChainEnd?(outputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], kwargs?: {
  91. inputs?: Record<string, unknown>;
  92. }): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  93. Promise<any> | any;
  94. /**
  95. * Called at the start of a Tool run, with the tool name and input
  96. * and the run ID.
  97. */
  98. handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runName?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  99. Promise<any> | any;
  100. /**
  101. * Called if a Tool run encounters an error
  102. */
  103. handleToolError?(err: Error, runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  104. Promise<any> | any;
  105. /**
  106. * Called at the end of a Tool run, with the tool output and the run ID.
  107. */
  108. handleToolEnd?(output: any, runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  109. Promise<any> | any;
  110. handleText?(text: string, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
  111. /**
  112. * Called when an agent is about to execute an action,
  113. * with the action and the run ID.
  114. */
  115. handleAgentAction?(action: AgentAction, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
  116. /**
  117. * Called when an agent finishes execution, before it exits.
  118. * with the final output and the run ID.
  119. */
  120. handleAgentEnd?(action: AgentFinish, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
  121. handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, name?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  122. Promise<any> | any;
  123. handleRetrieverEnd?(documents: DocumentInterface[], runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  124. Promise<any> | any;
  125. handleRetrieverError?(err: Error, runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  126. Promise<any> | any;
  127. handleCustomEvent?(eventName: string, data: any, runId: string, tags?: string[], metadata?: Record<string, any>): // eslint-disable-next-line @typescript-eslint/no-explicit-any
  128. Promise<any> | any;
  129. }
  130. /**
  131. * Base interface for callbacks. All methods are optional. If a method is not
  132. * implemented, it will be ignored. If a method is implemented, it will be
  133. * called at the appropriate time. All methods are called with the run ID of
  134. * the LLM/ChatModel/Chain that is running, which is generated by the
  135. * CallbackManager.
  136. *
  137. * @interface
  138. */
  139. export type CallbackHandlerMethods = BaseCallbackHandlerMethodsClass;
  140. /**
  141. * Interface for handlers that can indicate a preference for streaming responses.
  142. * When implemented, this allows the handler to signal whether it prefers to receive
  143. * streaming responses from language models rather than complete responses.
  144. */
  145. export interface CallbackHandlerPrefersStreaming {
  146. readonly lc_prefer_streaming: boolean;
  147. }
  148. export declare function callbackHandlerPrefersStreaming(x: BaseCallbackHandler): unknown;
  149. /**
  150. * Abstract base class for creating callback handlers in the LangChain
  151. * framework. It provides a set of optional methods that can be overridden
  152. * in derived classes to handle various events during the execution of a
  153. * LangChain application.
  154. */
  155. export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMethodsClass implements BaseCallbackHandlerInput, Serializable {
  156. lc_serializable: boolean;
  157. get lc_namespace(): ["langchain_core", "callbacks", string];
  158. get lc_secrets(): {
  159. [key: string]: string;
  160. } | undefined;
  161. get lc_attributes(): {
  162. [key: string]: string;
  163. } | undefined;
  164. get lc_aliases(): {
  165. [key: string]: string;
  166. } | undefined;
  167. get lc_serializable_keys(): string[] | undefined;
  168. /**
  169. * The name of the serializable. Override to provide an alias or
  170. * to preserve the serialized module name in minified environments.
  171. *
  172. * Implemented as a static method to support loading logic.
  173. */
  174. static lc_name(): string;
  175. /**
  176. * The final serialized identifier for the module.
  177. */
  178. get lc_id(): string[];
  179. lc_kwargs: SerializedFields;
  180. abstract name: string;
  181. ignoreLLM: boolean;
  182. ignoreChain: boolean;
  183. ignoreAgent: boolean;
  184. ignoreRetriever: boolean;
  185. ignoreCustomEvent: boolean;
  186. raiseError: boolean;
  187. awaitHandlers: boolean;
  188. constructor(input?: BaseCallbackHandlerInput);
  189. copy(): BaseCallbackHandler;
  190. toJSON(): Serialized;
  191. toJSONNotImplemented(): SerializedNotImplemented;
  192. static fromMethods(methods: CallbackHandlerMethods): {
  193. name: string;
  194. lc_serializable: boolean;
  195. readonly lc_namespace: ["langchain_core", "callbacks", string];
  196. readonly lc_secrets: {
  197. [key: string]: string;
  198. } | undefined;
  199. readonly lc_attributes: {
  200. [key: string]: string;
  201. } | undefined;
  202. readonly lc_aliases: {
  203. [key: string]: string;
  204. } | undefined;
  205. readonly lc_serializable_keys: string[] | undefined;
  206. /**
  207. * The final serialized identifier for the module.
  208. */
  209. readonly lc_id: string[];
  210. lc_kwargs: SerializedFields;
  211. ignoreLLM: boolean;
  212. ignoreChain: boolean;
  213. ignoreAgent: boolean;
  214. ignoreRetriever: boolean;
  215. ignoreCustomEvent: boolean;
  216. raiseError: boolean;
  217. awaitHandlers: boolean;
  218. copy(): BaseCallbackHandler;
  219. toJSON(): Serialized;
  220. toJSONNotImplemented(): SerializedNotImplemented;
  221. /**
  222. * Called at the start of an LLM or Chat Model run, with the prompt(s)
  223. * and the run ID.
  224. */
  225. handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runName?: string | undefined): any;
  226. /**
  227. * Called when an LLM/ChatModel in `streaming` mode produces a new token
  228. */
  229. handleLLMNewToken?(token: string, idx: NewTokenIndices, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, fields?: HandleLLMNewTokenCallbackFields | undefined): any;
  230. /**
  231. * Called if an LLM/ChatModel run encounters an error
  232. */
  233. handleLLMError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, extraParams?: Record<string, unknown> | undefined): any;
  234. /**
  235. * Called at the end of an LLM/ChatModel run, with the output and the run ID.
  236. */
  237. handleLLMEnd?(output: LLMResult, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, extraParams?: Record<string, unknown> | undefined): any;
  238. /**
  239. * Called at the start of a Chat Model run, with the prompt(s)
  240. * and the run ID.
  241. */
  242. handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runName?: string | undefined): any;
  243. /**
  244. * Called at the start of a Chain run, with the chain name and inputs
  245. * and the run ID.
  246. */
  247. handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runType?: string | undefined, runName?: string | undefined): any;
  248. /**
  249. * Called if a Chain run encounters an error
  250. */
  251. handleChainError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, kwargs?: {
  252. inputs?: Record<string, unknown> | undefined;
  253. } | undefined): any;
  254. /**
  255. * Called at the end of a Chain run, with the outputs and the run ID.
  256. */
  257. handleChainEnd?(outputs: ChainValues, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, kwargs?: {
  258. inputs?: Record<string, unknown> | undefined;
  259. } | undefined): any;
  260. /**
  261. * Called at the start of a Tool run, with the tool name and input
  262. * and the run ID.
  263. */
  264. handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runName?: string | undefined): any;
  265. /**
  266. * Called if a Tool run encounters an error
  267. */
  268. handleToolError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
  269. /**
  270. * Called at the end of a Tool run, with the tool output and the run ID.
  271. */
  272. handleToolEnd?(output: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
  273. handleText?(text: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
  274. /**
  275. * Called when an agent is about to execute an action,
  276. * with the action and the run ID.
  277. */
  278. handleAgentAction?(action: AgentAction, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
  279. /**
  280. * Called when an agent finishes execution, before it exits.
  281. * with the final output and the run ID.
  282. */
  283. handleAgentEnd?(action: AgentFinish, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
  284. handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, name?: string | undefined): any;
  285. handleRetrieverEnd?(documents: DocumentInterface<Record<string, any>>[], runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
  286. handleRetrieverError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
  287. handleCustomEvent?(eventName: string, data: any, runId: string, tags?: string[] | undefined, metadata?: Record<string, any> | undefined): any;
  288. };
  289. }
  290. export declare const isBaseCallbackHandler: (x: unknown) => boolean;
  291. export {};