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,
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,
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: UUID | None = None,
- **kwargs: 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: UUID | None = None,
- **kwargs: 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: UUID | None = None,
- **kwargs: 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: UUID | None = None,
- **kwargs: 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: List[str] | None = None,
- parent_run_id: UUID | None = None,
- metadata: Dict[str, Any] | None = None,
- run_type: str | None = None,
- name: str | None = None,
- **kwargs: Any,
Do nothing when chain starts
- Parameters:
serialized (Dict[str, Any])
inputs (Dict[str, Any])
run_id (UUID)
tags (List[str] | None)
parent_run_id (UUID | None)
metadata (Dict[str, Any] | None)
run_type (str | None)
name (str | None)
kwargs (Any)
- Return type:
None
- on_chat_model_start(
- serialized: dict[str, Any],
- messages: list[list[BaseMessage]],
- *,
- run_id: UUID,
- parent_run_id: UUID | None = None,
- tags: list[str] | None = None,
- metadata: dict[str, Any] | None = None,
- **kwargs: 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: list[str] | None = None,
- metadata: dict[str, Any] | None = None,
- **kwargs: 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
Added in version 0.2.15.
- on_llm_end(
- response: LLMResult,
- *,
- run_id: UUID,
- parent_run_id: UUID | None = None,
- **kwargs: Any,
Log records to uptrain when an LLM ends.
- Parameters:
response (LLMResult)
run_id (UUID)
parent_run_id (UUID | None)
kwargs (Any)
- Return type:
None
- on_llm_error(
- error: BaseException,
- *,
- run_id: UUID,
- parent_run_id: UUID | None = None,
- **kwargs: 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: GenerationChunk | ChatGenerationChunk | None = None,
- run_id: UUID,
- parent_run_id: UUID | None = None,
- **kwargs: Any,
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:
Any
- on_llm_start(
- serialized: dict[str, Any],
- prompts: list[str],
- *,
- run_id: UUID,
- parent_run_id: UUID | None = None,
- tags: list[str] | None = None,
- metadata: dict[str, Any] | None = None,
- **kwargs: Any,
Run when LLM starts running.
- ATTENTION: This method is called for non-chat models (regular LLMs). If
youβre implementing a handler for a chat model, you should use on_chat_model_start instead.
- Parameters:
serialized (dict[str, Any]) β The serialized LLM.
prompts (list[str]) β The prompts.
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: UUID | None = None,
- **kwargs: Any,
Run when Retriever ends running.
- Parameters:
documents (Sequence[Document])
run_id (UUID)
parent_run_id (UUID | None)
kwargs (Any)
- Return type:
Any
- on_retriever_error(
- error: BaseException,
- *,
- run_id: UUID,
- parent_run_id: UUID | None = None,
- **kwargs: 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: UUID | None = None,
- tags: List[str] | None = None,
- metadata: Dict[str, Any] | None = None,
- **kwargs: Any,
Run when the Retriever starts running.
- Parameters:
serialized (dict[str, Any]) β The serialized Retriever.
query (str) β The query.
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: UUID | None = None,
- **kwargs: Any,
Run on a retry event.
- Parameters:
retry_state (RetryCallState) β The retry state.
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: UUID | None = None,
- **kwargs: Any,
Run on an arbitrary text.
- Parameters:
text (str) β The text.
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: UUID | None = None,
- **kwargs: Any,
Run when the tool ends running.
- Parameters:
output (Any) β The output of the tool.
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: UUID | None = None,
- **kwargs: 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: UUID | None = None,
- tags: list[str] | None = None,
- metadata: dict[str, Any] | None = None,
- inputs: dict[str, Any] | None = None,
- **kwargs: Any,
Run when the tool starts running.
- Parameters:
serialized (dict[str, Any]) β The serialized tool.
input_str (str) β The input string.
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]]) β The inputs.
kwargs (Any) β Additional keyword arguments.
- Return type:
Any
Examples using UpTrainCallbackHandler