base.d.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { Serializable, SerializedConstructor } from "../load/serializable.js";
  2. import { StringWithAutocomplete } from "../utils/types/index.js";
  3. export interface StoredMessageData {
  4. content: string;
  5. role: string | undefined;
  6. name: string | undefined;
  7. tool_call_id: string | undefined;
  8. additional_kwargs?: Record<string, any>;
  9. /** Response metadata. For example: response headers, logprobs, token counts. */
  10. response_metadata?: Record<string, any>;
  11. id?: string;
  12. }
  13. export interface StoredMessage {
  14. type: string;
  15. data: StoredMessageData;
  16. }
  17. export interface StoredGeneration {
  18. text: string;
  19. message?: StoredMessage;
  20. }
  21. export interface StoredMessageV1 {
  22. type: string;
  23. role: string | undefined;
  24. text: string;
  25. }
  26. export type MessageType = "human" | "ai" | "generic" | "developer" | "system" | "function" | "tool" | "remove";
  27. export type ImageDetail = "auto" | "low" | "high";
  28. export type MessageContentText = {
  29. type: "text";
  30. text: string;
  31. };
  32. export type MessageContentImageUrl = {
  33. type: "image_url";
  34. image_url: string | {
  35. url: string;
  36. detail?: ImageDetail;
  37. };
  38. };
  39. export type MessageContentComplex = MessageContentText | MessageContentImageUrl | (Record<string, any> & {
  40. type?: "text" | "image_url" | string;
  41. }) | (Record<string, any> & {
  42. type?: never;
  43. });
  44. export type MessageContent = string | MessageContentComplex[];
  45. export interface FunctionCall {
  46. /**
  47. * The arguments to call the function with, as generated by the model in JSON
  48. * format. Note that the model does not always generate valid JSON, and may
  49. * hallucinate parameters not defined by your function schema. Validate the
  50. * arguments in your code before calling your function.
  51. */
  52. arguments: string;
  53. /**
  54. * The name of the function to call.
  55. */
  56. name: string;
  57. }
  58. export type BaseMessageFields = {
  59. content: MessageContent;
  60. name?: string;
  61. additional_kwargs?: {
  62. /**
  63. * @deprecated Use "tool_calls" field on AIMessages instead
  64. */
  65. function_call?: FunctionCall;
  66. /**
  67. * @deprecated Use "tool_calls" field on AIMessages instead
  68. */
  69. tool_calls?: OpenAIToolCall[];
  70. [key: string]: unknown;
  71. };
  72. /** Response metadata. For example: response headers, logprobs, token counts. */
  73. response_metadata?: Record<string, any>;
  74. /**
  75. * An optional unique identifier for the message. This should ideally be
  76. * provided by the provider/model which created the message.
  77. */
  78. id?: string;
  79. };
  80. export declare function mergeContent(firstContent: MessageContent, secondContent: MessageContent): MessageContent;
  81. /**
  82. * 'Merge' two statuses. If either value passed is 'error', it will return 'error'. Else
  83. * it will return 'success'.
  84. *
  85. * @param {"success" | "error" | undefined} left The existing value to 'merge' with the new value.
  86. * @param {"success" | "error" | undefined} right The new value to 'merge' with the existing value
  87. * @returns {"success" | "error"} The 'merged' value.
  88. */
  89. export declare function _mergeStatus(left?: "success" | "error", right?: "success" | "error"): "success" | "error" | undefined;
  90. /**
  91. * Base class for all types of messages in a conversation. It includes
  92. * properties like `content`, `name`, and `additional_kwargs`. It also
  93. * includes methods like `toDict()` and `_getType()`.
  94. */
  95. export declare abstract class BaseMessage extends Serializable implements BaseMessageFields {
  96. lc_namespace: string[];
  97. lc_serializable: boolean;
  98. get lc_aliases(): Record<string, string>;
  99. /**
  100. * Get text content of the message.
  101. */
  102. get text(): string;
  103. /** The content of the message. */
  104. content: MessageContent;
  105. /** The name of the message sender in a multi-user chat. */
  106. name?: string;
  107. /** Additional keyword arguments */
  108. additional_kwargs: NonNullable<BaseMessageFields["additional_kwargs"]>;
  109. /** Response metadata. For example: response headers, logprobs, token counts. */
  110. response_metadata: NonNullable<BaseMessageFields["response_metadata"]>;
  111. /**
  112. * An optional unique identifier for the message. This should ideally be
  113. * provided by the provider/model which created the message.
  114. */
  115. id?: string;
  116. /**
  117. * @deprecated Use .getType() instead or import the proper typeguard.
  118. * For example:
  119. *
  120. * ```ts
  121. * import { isAIMessage } from "@langchain/core/messages";
  122. *
  123. * const message = new AIMessage("Hello!");
  124. * isAIMessage(message); // true
  125. * ```
  126. */
  127. abstract _getType(): MessageType;
  128. /** The type of the message. */
  129. getType(): MessageType;
  130. constructor(fields: string | BaseMessageFields,
  131. /** @deprecated */
  132. kwargs?: Record<string, unknown>);
  133. toDict(): StoredMessage;
  134. static lc_name(): string;
  135. get _printableFields(): Record<string, unknown>;
  136. _updateId(value: string | undefined): void;
  137. get [Symbol.toStringTag](): any;
  138. }
  139. /**
  140. * @deprecated Use "tool_calls" field on AIMessages instead
  141. */
  142. export type OpenAIToolCall = {
  143. /**
  144. * The ID of the tool call.
  145. */
  146. id: string;
  147. /**
  148. * The function that the model called.
  149. */
  150. function: FunctionCall;
  151. /**
  152. * The type of the tool. Currently, only `function` is supported.
  153. */
  154. type: "function";
  155. index?: number;
  156. };
  157. export declare function isOpenAIToolCallArray(value?: unknown): value is OpenAIToolCall[];
  158. export declare function _mergeDicts(left: Record<string, any>, right: Record<string, any>): Record<string, any>;
  159. export declare function _mergeLists(left?: any[], right?: any[]): any[] | undefined;
  160. export declare function _mergeObj<T = any>(left: T | undefined, right: T | undefined): T;
  161. /**
  162. * Represents a chunk of a message, which can be concatenated with other
  163. * message chunks. It includes a method `_merge_kwargs_dict()` for merging
  164. * additional keyword arguments from another `BaseMessageChunk` into this
  165. * one. It also overrides the `__add__()` method to support concatenation
  166. * of `BaseMessageChunk` instances.
  167. */
  168. export declare abstract class BaseMessageChunk extends BaseMessage {
  169. abstract concat(chunk: BaseMessageChunk): BaseMessageChunk;
  170. }
  171. export type MessageFieldWithRole = {
  172. role: StringWithAutocomplete<"user" | "assistant" | MessageType>;
  173. content: MessageContent;
  174. name?: string;
  175. } & Record<string, unknown>;
  176. export declare function _isMessageFieldWithRole(x: BaseMessageLike): x is MessageFieldWithRole;
  177. export type BaseMessageLike = BaseMessage | MessageFieldWithRole | [
  178. StringWithAutocomplete<MessageType | "user" | "assistant" | "placeholder">,
  179. MessageContent
  180. ] | string
  181. /**
  182. * @deprecated Specifying "type" is deprecated and will be removed in 0.4.0.
  183. */
  184. | ({
  185. type: MessageType | "user" | "assistant" | "placeholder";
  186. } & BaseMessageFields & Record<string, unknown>) | SerializedConstructor;
  187. export declare function isBaseMessage(messageLike?: unknown): messageLike is BaseMessage;
  188. export declare function isBaseMessageChunk(messageLike?: unknown): messageLike is BaseMessageChunk;