langchain_community.callbacks.bedrock_anthropic_callback.BedrockAnthropicTokenUsageCallbackHandler

class langchain_community.callbacks.bedrock_anthropic_callback.BedrockAnthropicTokenUsageCallbackHandler[source]

回调处理程序,用于跟踪 Bedrock Anthropic 信息。

属性

always_verbose

即使 verbose 为 False,是否调用 verbose 回调。

completion_tokens

ignore_agent

是否忽略 agent 回调。

ignore_chain

是否忽略 chain 回调。

ignore_chat_model

是否忽略 chat model 回调。

ignore_custom_event

忽略自定义事件。

ignore_llm

是否忽略 LLM 回调。

ignore_retriever

是否忽略 retriever 回调。

ignore_retry

是否忽略 retry 回调。

prompt_tokens

raise_error

如果发生异常是否引发错误。

run_inline

是否内联运行回调。

successful_requests

total_cost

total_tokens

方法

__init__()

on_agent_action(action, *, run_id[, ...])

在 agent action 上运行。

on_agent_finish(finish, *, run_id[, ...])

在 agent 结束时运行。

on_chain_end(outputs, *, run_id[, parent_run_id])

当 chain 结束运行时运行。

on_chain_error(error, *, run_id[, parent_run_id])

当 chain 出错时运行。

on_chain_start(serialized, inputs, *, run_id)

当 chain 开始运行时运行。

on_chat_model_start(serialized, messages, *, ...)

当 chat 模型开始运行时运行。

on_custom_event(name, data, *, run_id[, ...])

重写以定义自定义事件的处理程序。

on_llm_end(response, **kwargs)

收集 token 使用量。

on_llm_error(error, *, run_id[, parent_run_id])

当 LLM 出错时运行。

on_llm_new_token(token, **kwargs)

打印 token。

on_llm_start(serialized, prompts, **kwargs)

打印 prompts。

on_retriever_end(documents, *, run_id[, ...])

当 Retriever 结束运行时运行。

on_retriever_error(error, *, run_id[, ...])

当 Retriever 出错时运行。

on_retriever_start(serialized, query, *, run_id)

当 Retriever 开始运行时运行。

on_retry(retry_state, *, run_id[, parent_run_id])

在 retry 事件时运行。

on_text(text, *, run_id[, parent_run_id])

在任意文本上运行。

on_tool_end(output, *, run_id[, parent_run_id])

当 tool 结束运行时运行。

on_tool_error(error, *, run_id[, parent_run_id])

当 tool 出错时运行。

on_tool_start(serialized, input_str, *, run_id)

当 tool 开始运行时运行。

__init__() None[source]
返回类型

None

on_agent_action(action: AgentAction, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) Any

在 agent action 上运行。

参数
  • action (AgentAction) – Agent action。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_agent_finish(finish: AgentFinish, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) Any

在 agent 结束时运行。

参数
  • finish (AgentFinish) – Agent 结束。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_chain_end(outputs: Dict[str, Any], *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) Any

当 chain 结束运行时运行。

参数
  • outputs (Dict[str, Any]) – chain 的输出。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_chain_error(error: BaseException, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) Any

当 chain 出错时运行。

参数
  • error (BaseException) – 发生的错误。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_chain_start(serialized: Dict[str, Any], inputs: Dict[str, Any], *, run_id: UUID, parent_run_id: Optional[UUID] = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) Any

当 chain 开始运行时运行。

参数
  • serialized (Dict[str, Any]) – 序列化的 chain。

  • inputs (Dict[str, Any]) – 输入。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • tags (Optional[List[str]]) – 标签。

  • metadata (Optional[Dict[str, Any]]) – 元数据。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_chat_model_start(serialized: Dict[str, Any], messages: List[List[BaseMessage]], *, run_id: UUID, parent_run_id: Optional[UUID] = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) Any

当 chat 模型开始运行时运行。

注意:此方法为 chat 模型调用。 如果您正在实现

非 chat 模型的处理程序,则应使用 on_llm_start 代替。

参数
  • serialized (Dict[str, Any]) – 序列化的 chat 模型。

  • messages (List[List[BaseMessage]]) – 消息。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • tags (Optional[List[str]]) – 标签。

  • metadata (Optional[Dict[str, Any]]) – 元数据。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_custom_event(name: str, data: Any, *, run_id: UUID, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) Any

重写以定义自定义事件的处理程序。

参数
  • name (str) – 自定义事件的名称。

  • data (Any) – 自定义事件的数据。格式将与用户指定的格式匹配。

  • run_id (UUID) – 运行的 ID。

  • tags (Optional[List[str]]) – 与自定义事件关联的标签(包括继承的标签)。

  • metadata (Optional[Dict[str, Any]]) – 与自定义事件关联的元数据(包括继承的元数据)。

  • kwargs (Any) –

返回类型

Any

0.2.15 版本新增。

on_llm_end(response: LLMResult, **kwargs: Any) None[source]

收集 token 使用量。

参数
返回类型

None

on_llm_error(error: BaseException, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) Any

当 LLM 出错时运行。

参数
  • error (BaseException) – 发生的错误。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_llm_new_token(token: str, **kwargs: Any) None[source]

打印 token。

参数
  • token (str) –

  • kwargs (Any) –

返回类型

None

on_llm_start(serialized: Dict[str, Any], prompts: List[str], **kwargs: Any) None[source]

打印 prompts。

参数
  • serialized (Dict[str, Any]) –

  • prompts (List[str]) –

  • kwargs (Any) –

返回类型

None

on_retriever_end(documents: Sequence[Document], *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) Any

当 Retriever 结束运行时运行。

参数
  • documents (Sequence[Document]) – 检索到的文档。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_retriever_error(error: BaseException, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) Any

当 Retriever 出错时运行。

参数
  • error (BaseException) – 发生的错误。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_retriever_start(serialized: Dict[str, Any], query: str, *, run_id: UUID, parent_run_id: Optional[UUID] = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) Any

当 Retriever 开始运行时运行。

参数
  • serialized (Dict[str, Any]) – 序列化的检索器。

  • query (str) – 查询。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • tags (Optional[List[str]]) – 标签。

  • metadata (Optional[Dict[str, Any]]) – 元数据。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_retry(retry_state: RetryCallState, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) Any

在 retry 事件时运行。

参数
  • retry_state (RetryCallState) – 重试状态。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_text(text: str, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) Any

在任意文本上运行。

参数
  • text (str) – 文本。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_tool_end(output: Any, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) Any

当 tool 结束运行时运行。

参数
  • output (Any) – 工具的输出。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_tool_error(error: BaseException, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) Any

当 tool 出错时运行。

参数
  • error (BaseException) – 发生的错误。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any

on_tool_start(serialized: Dict[str, Any], input_str: str, *, run_id: UUID, parent_run_id: Optional[UUID] = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, inputs: Optional[Dict[str, Any]] = None, **kwargs: Any) Any

当 tool 开始运行时运行。

参数
  • serialized (Dict[str, Any]) – 序列化的工具。

  • input_str (str) – 输入字符串。

  • run_id (UUID) – 运行 ID。这是当前运行的 ID。

  • parent_run_id (UUID) – 父运行 ID。这是父运行的 ID。

  • tags (Optional[List[str]]) – 标签。

  • metadata (Optional[Dict[str, Any]]) – 元数据。

  • inputs (Optional[Dict[str, Any]]) – 输入。

  • kwargs (Any) – 额外的关键词参数。

返回类型

Any