langchain_nvidia_ai_endpoints.callbacks.UsageCallbackHandlerยถ

class langchain_nvidia_ai_endpoints.callbacks.UsageCallbackHandler[source]ยถ

Callback Handler that tracks OpenAI info.

Attributes

always_verbose

Whether to call verbose callbacks even if verbose is False.

completion_tokens

Completion tokens used.

ignore_agent

Whether to ignore agent callbacks.

ignore_chain

Whether to ignore chain callbacks.

ignore_chat_model

Whether to ignore chat model callbacks.

ignore_custom_event

Ignore custom event.

ignore_llm

Whether to ignore LLM callbacks.

ignore_retriever

Whether to ignore retriever callbacks.

ignore_retry

Whether to ignore retry callbacks.

llm_output

model_usage

Whether to call verbose callbacks even if verbose is False.

price_map

prompt_tokens

Prompt tokens used.

raise_error

Whether to raise an error if an exception occurs.

run_inline

Whether to run the callback inline.

successful_requests

Total successful requests.

total_cost

Total cost in USD.

total_tokens

Total tokens used.

Methods

__init__()

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

Run on agent action.

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

Run on the agent end.

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

Run when chain ends running.

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

Run when chain errors.

on_chain_start(serialized, inputs, *, run_id)

Run when a chain starts running.

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

Run when a chat model starts running.

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

Override to define a handler for a custom event.

on_llm_end(response, **kwargs)

Run when LLM ends running.

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

Run when LLM errors.

on_llm_new_token(token, **kwargs)

Run on new LLM token.

on_llm_start(serialized, prompts, **kwargs)

Run when LLM starts running.

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

Run when Retriever ends running.

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

Run when Retriever errors.

on_retriever_start(serialized, query, *, run_id)

Run when the Retriever starts running.

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

Run on a retry event.

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

Run on an arbitrary text.

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

Run when the tool ends running.

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

Run when tool errors.

on_tool_start(serialized, input_str, *, run_id)

Run when the tool starts running.

reset()

Reset the model usage.

__init__() โ†’ None[source]ยถ
Return type

None

on_agent_action(action: AgentAction, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) โ†’ Anyยถ

Run on agent action.

Parameters
  • action (AgentAction) โ€“ The agent action.

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

Any

on_agent_finish(finish: AgentFinish, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) โ†’ Anyยถ

Run on the agent end.

Parameters
  • finish (AgentFinish) โ€“ The agent finish.

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

Any

on_chain_end(outputs: Dict[str, Any], *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) โ†’ Anyยถ

Run when chain ends running.

Parameters
  • outputs (Dict[str, Any]) โ€“ The outputs of the chain.

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

Any

on_chain_error(error: BaseException, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) โ†’ Anyยถ

Run when chain errors.

Parameters
  • error (BaseException) โ€“ The error that occurred.

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

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ยถ

Run when a chain starts running.

Parameters
  • serialized (Dict[str, Any]) โ€“ The serialized chain.

  • inputs (Dict[str, Any]) โ€“ The inputs.

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • tags (Optional[List[str]]) โ€“ The tags.

  • metadata (Optional[Dict[str, Any]]) โ€“ The metadata.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

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ยถ

Run when a chat model starts running.

ATTENTION: This method is called for chat models. If youโ€™re implementing

a handler for a non-chat model, you should use on_llm_start instead.

Parameters
  • serialized (Dict[str, Any]) โ€“ The serialized chat model.

  • messages (List[List[BaseMessage]]) โ€“ The messages.

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • tags (Optional[List[str]]) โ€“ The tags.

  • metadata (Optional[Dict[str, Any]]) โ€“ The metadata.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

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ยถ

Override to define a handler for a custom event.

Parameters
  • name (str) โ€“ The name of the custom event.

  • data (Any) โ€“ The data for the custom event. Format will match the format specified by the user.

  • run_id (UUID) โ€“ The ID of the run.

  • tags (Optional[List[str]]) โ€“ The tags associated with the custom event (includes inherited tags).

  • metadata (Optional[Dict[str, Any]]) โ€“ The metadata associated with the custom event (includes inherited metadata).

  • kwargs (Any) โ€“

Return type

Any

New in version 0.2.15.

on_llm_end(response: LLMResult, **kwargs: Any) โ†’ None[source]ยถ

Run when LLM ends running.

Parameters
  • response (LLMResult) โ€“ The response which was generated.

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

None

on_llm_error(error: BaseException, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) โ†’ Anyยถ

Run when LLM errors.

Parameters
  • error (BaseException) โ€“ The error that occurred.

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

Any

on_llm_new_token(token: str, **kwargs: Any) โ†’ None[source]ยถ

Run on new LLM token. Only available when streaming is enabled.

Parameters
  • token (str) โ€“ The new token.

  • chunk (GenerationChunk | ChatGenerationChunk) โ€“ The new generated chunk, containing content and other information.

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

None

on_llm_start(serialized: Dict[str, Any], prompts: List[str], **kwargs: Any) โ†’ None[source]ยถ

Run when LLM starts running.

ๆณจๆ„๏ผšๆญคๆ–นๆณ•้€‚็”จไบŽ้ž่Šๅคฉๆจกๅž‹๏ผˆๅธธ่ง„ LLM๏ผ‰ใ€‚ๅฆ‚ๆžœ

ๆ‚จๆญฃๅœจไธบ่Šๅคฉๆจกๅž‹ๅฎž็Žฐๅค„็†็จ‹ๅบ๏ผŒๅˆ™ๅบ”ไฝฟ็”จ on_chat_model_start ไปฃๆ›ฟใ€‚

Parameters
  • serialized (Dict[str, Any]) โ€“ ๅบๅˆ—ๅŒ–็š„ LLMใ€‚

  • prompts (List[str]) โ€“ ๆ็คบใ€‚

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • tags (Optional[List[str]]) โ€“ The tags.

  • metadata (Optional[Dict[str, Any]]) โ€“ The metadata.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

None

on_retriever_end(documents: Sequence[Document], *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) โ†’ Anyยถ

Run when Retriever ends running.

Parameters
  • documents (Sequence[Document]) โ€“ ๆฃ€็ดขๅˆฐ็š„ๆ–‡ๆกฃใ€‚

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

Any

on_retriever_error(error: BaseException, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) โ†’ Anyยถ

Run when Retriever errors.

Parameters
  • error (BaseException) โ€“ The error that occurred.

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

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ยถ

Run when the Retriever starts running.

Parameters
  • serialized (Dict[str, Any]) โ€“ ๅบๅˆ—ๅŒ–็š„ๆฃ€็ดขๅ™จใ€‚

  • query (str) โ€“ ๆŸฅ่ฏขใ€‚

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • tags (Optional[List[str]]) โ€“ The tags.

  • metadata (Optional[Dict[str, Any]]) โ€“ The metadata.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

Any

on_retry(retry_state: RetryCallState, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) โ†’ Anyยถ

Run on a retry event.

Parameters
  • retry_state (RetryCallState) โ€“ ้‡่ฏ•็Šถๆ€ใ€‚

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

Any

on_text(text: str, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) โ†’ Anyยถ

Run on an arbitrary text.

Parameters
  • text (str) โ€“ ๆ–‡ๆœฌใ€‚

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

Any

on_tool_end(output: Any, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) โ†’ Anyยถ

Run when the tool ends running.

Parameters
  • output (Any) โ€“ ๅทฅๅ…ท็š„่พ“ๅ‡บใ€‚

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

Any

on_tool_error(error: BaseException, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) โ†’ Anyยถ

Run when tool errors.

Parameters
  • error (BaseException) โ€“ The error that occurred.

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

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ยถ

Run when the tool starts running.

Parameters
  • serialized (Dict[str, Any]) โ€“ ๅบๅˆ—ๅŒ–็š„ๅทฅๅ…ทใ€‚

  • input_str (str) โ€“ ่พ“ๅ…ฅๅญ—็ฌฆไธฒใ€‚

  • run_id (UUID) โ€“ The run ID. This is the ID of the current run.

  • parent_run_id (UUID) โ€“ The parent run ID. This is the ID of the parent run.

  • tags (Optional[List[str]]) โ€“ The tags.

  • metadata (Optional[Dict[str, Any]]) โ€“ The metadata.

  • inputs (Optional[Dict[str, Any]]) โ€“ ่พ“ๅ…ฅใ€‚

  • kwargs (Any) โ€“ Additional keyword arguments.

Return type

Any

reset() โ†’ None[source]ยถ

Reset the model usage.

Return type

None