langchain_community.callbacks.uptrain_callback.UpTrainCallbackHandler

class langchain_community.callbacks.uptrain_callback.UpTrainCallbackHandler(*, project_name: str = 'langchain', key_type: str = 'openai', api_key: str = 'sk-****************', model: str = 'gpt-3.5-turbo', log_results: bool = True)[source]

Callback Handler that logs evaluation results to uptrain and the console.

Parameters
  • project_name (str) – The project name to be shown in UpTrain dashboard.

  • key_type (str) – Type of key to use. Must be ‘uptrain’ or ‘openai’.

  • api_key (str) – API key for the UpTrain or OpenAI API.

  • GPT.) ((This key is required to perform evaluations using) –

  • model (str) –

  • log_results (bool) –

Raises
  • ValueError – If the key type is invalid.

  • ImportError – If the uptrain package is not installed.

Initializes the UpTrainCallbackHandler.

Attributes

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.

raise_error

Whether to raise an error if an exception occurs.

run_inline

Whether to run the callback inline.

Methods

__init__(*[, project_name, key_type, ...])

Initializes the UpTrainCallbackHandler.

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)

Do nothing when chain starts

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, *, run_id[, parent_run_id])

Log records to uptrain when an LLM ends.

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

Run when LLM errors.

on_llm_new_token(token, *[, chunk, ...])

Run on new LLM token.

on_llm_start(serialized, prompts, *, run_id)

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.

uptrain_evaluate(evaluation_name, data, checks)

Run an evaluation on the UpTrain server using UpTrain client.

__init__(*, project_name: str = 'langchain', key_type: str = 'openai', api_key: str = 'sk-****************', model: str = 'gpt-3.5-turbo', log_results: bool = True) None[source]

Initializes the UpTrainCallbackHandler.

Parameters
  • project_name (str) –

  • key_type (str) –

  • api_key (str) –

  • model (str) –

  • log_results (bool) –

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, tags: Optional[List[str]] = None, parent_run_id: Optional[UUID] = None, metadata: Optional[Dict[str, Any]] = None, run_type: Optional[str] = None, name: Optional[str] = None, **kwargs: Any) None[source]

Do nothing when chain starts

Parameters
  • serialized (Dict[str, Any]) –

  • inputs (Dict[str, Any]) –

  • run_id (UUID) –

  • tags (Optional[List[str]]) –

  • parent_run_id (Optional[UUID]) –

  • metadata (Optional[Dict[str, Any]]) –

  • run_type (Optional[str]) –

  • name (Optional[str]) –

  • kwargs (Any) –

Return type

None

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, *, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) None[source]

Log records to uptrain when an LLM ends.

Parameters
  • response (LLMResult) –

  • run_id (UUID) –

  • parent_run_id (Optional[UUID]) –

  • kwargs (Any) –

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, *, chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]] = None, run_id: UUID, parent_run_id: Optional[UUID] = None, **kwargs: Any) Any

在新的 LLM token 上运行。 仅在启用流式传输时可用。

Parameters
  • token (str) – 新的 token。

  • chunk (GenerationChunk | ChatGenerationChunk) – 新生成的 chunk,包含内容和其他信息。

  • 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_start(serialized: Dict[str, Any], prompts: List[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 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

Any

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

Run when Retriever ends running.

Parameters
  • documents (Sequence[Document]) –

  • run_id (UUID) –

  • parent_run_id (Optional[UUID]) –

  • kwargs (Any) –

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) None[source]

Run when the Retriever starts running.

Parameters
  • serialized (Dict[str, Any]) – 序列化的 Retriever。

  • 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

None

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

uptrain_evaluate(evaluation_name: str, data: List[Dict[str, Any]], checks: List[str]) None[source]

Run an evaluation on the UpTrain server using UpTrain client.

Parameters
  • evaluation_name (str) –

  • data (List[Dict[str, Any]]) –

  • checks (List[str]) –

Return type

None

UpTrainCallbackHandler 的使用示例