ai.d.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { BaseMessage, BaseMessageChunk, type MessageType, BaseMessageFields } from "./base.js";
  2. import { InvalidToolCall, ToolCall, ToolCallChunk } from "./tool.js";
  3. export type AIMessageFields = BaseMessageFields & {
  4. tool_calls?: ToolCall[];
  5. invalid_tool_calls?: InvalidToolCall[];
  6. usage_metadata?: UsageMetadata;
  7. };
  8. export type ModalitiesTokenDetails = {
  9. /**
  10. * Text tokens.
  11. * Does not need to be reported, but some models will do so.
  12. */
  13. text?: number;
  14. /**
  15. * Image (non-video) tokens.
  16. */
  17. image?: number;
  18. /**
  19. * Audio tokens.
  20. */
  21. audio?: number;
  22. /**
  23. * Video tokens.
  24. */
  25. video?: number;
  26. /**
  27. * Document tokens.
  28. * e.g. PDF
  29. */
  30. document?: number;
  31. };
  32. /**
  33. * Breakdown of input token counts.
  34. *
  35. * Does not *need* to sum to full input token count. Does *not* need to have all keys.
  36. */
  37. export type InputTokenDetails = ModalitiesTokenDetails & {
  38. /**
  39. * Input tokens that were cached and there was a cache hit.
  40. *
  41. * Since there was a cache hit, the tokens were read from the cache.
  42. * More precisely, the model state given these tokens was read from the cache.
  43. */
  44. cache_read?: number;
  45. /**
  46. * Input tokens that were cached and there was a cache miss.
  47. *
  48. * Since there was a cache miss, the cache was created from these tokens.
  49. */
  50. cache_creation?: number;
  51. };
  52. /**
  53. * Breakdown of output token counts.
  54. *
  55. * Does *not* need to sum to full output token count. Does *not* need to have all keys.
  56. */
  57. export type OutputTokenDetails = ModalitiesTokenDetails & {
  58. /**
  59. * Reasoning output tokens.
  60. *
  61. * Tokens generated by the model in a chain of thought process (i.e. by
  62. * OpenAI's o1 models) that are not returned as part of model output.
  63. */
  64. reasoning?: number;
  65. };
  66. /**
  67. * Usage metadata for a message, such as token counts.
  68. */
  69. export type UsageMetadata = {
  70. /**
  71. * Count of input (or prompt) tokens. Sum of all input token types.
  72. */
  73. input_tokens: number;
  74. /**
  75. * Count of output (or completion) tokens. Sum of all output token types.
  76. */
  77. output_tokens: number;
  78. /**
  79. * Total token count. Sum of input_tokens + output_tokens.
  80. */
  81. total_tokens: number;
  82. /**
  83. * Breakdown of input token counts.
  84. *
  85. * Does *not* need to sum to full input token count. Does *not* need to have all keys.
  86. */
  87. input_token_details?: InputTokenDetails;
  88. /**
  89. * Breakdown of output token counts.
  90. *
  91. * Does *not* need to sum to full output token count. Does *not* need to have all keys.
  92. */
  93. output_token_details?: OutputTokenDetails;
  94. };
  95. /**
  96. * Represents an AI message in a conversation.
  97. */
  98. export declare class AIMessage extends BaseMessage {
  99. tool_calls?: ToolCall[];
  100. invalid_tool_calls?: InvalidToolCall[];
  101. /**
  102. * If provided, token usage information associated with the message.
  103. */
  104. usage_metadata?: UsageMetadata;
  105. get lc_aliases(): Record<string, string>;
  106. constructor(fields: string | AIMessageFields,
  107. /** @deprecated */
  108. kwargs?: Record<string, unknown>);
  109. static lc_name(): string;
  110. _getType(): MessageType;
  111. get _printableFields(): Record<string, unknown>;
  112. }
  113. export declare function isAIMessage(x: BaseMessage): x is AIMessage;
  114. export declare function isAIMessageChunk(x: BaseMessageChunk): x is AIMessageChunk;
  115. export type AIMessageChunkFields = AIMessageFields & {
  116. tool_call_chunks?: ToolCallChunk[];
  117. };
  118. /**
  119. * Represents a chunk of an AI message, which can be concatenated with
  120. * other AI message chunks.
  121. */
  122. export declare class AIMessageChunk extends BaseMessageChunk {
  123. tool_calls?: ToolCall[];
  124. invalid_tool_calls?: InvalidToolCall[];
  125. tool_call_chunks?: ToolCallChunk[];
  126. /**
  127. * If provided, token usage information associated with the message.
  128. */
  129. usage_metadata?: UsageMetadata;
  130. constructor(fields: string | AIMessageChunkFields);
  131. get lc_aliases(): Record<string, string>;
  132. static lc_name(): string;
  133. _getType(): MessageType;
  134. get _printableFields(): Record<string, unknown>;
  135. concat(chunk: AIMessageChunk): AIMessageChunk;
  136. }