Source code for langchain.callbacks.context_callback
"""Callback handler for Context AI"""
import os
from typing import Any, Dict, List
from uuid import UUID
from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema import (
    BaseMessage,
    LLMResult,
)
[docs]def import_context() -> Any:
    """Import the `getcontext` package."""
    try:
        import getcontext  # noqa: F401
        from getcontext.generated.models import (
            Conversation,
            Message,
            MessageRole,
            Rating,
        )
        from getcontext.token import Credential  # noqa: F401
    except ImportError:
        raise ImportError(
            "To use the context callback manager you need to have the "
            "`getcontext` python package installed (version >=0.3.0). "
            "Please install it with `pip install --upgrade python-context`"
        )
    return getcontext, Credential, Conversation, Message, MessageRole, Rating
[docs]class ContextCallbackHandler(BaseCallbackHandler):
    """Callback Handler that records transcripts to the Context service.
     (https://context.ai).
    Keyword Args:
        token (optional): The token with which to authenticate requests to Context.
            Visit https://with.context.ai/settings to generate a token.
            If not provided, the value of the `CONTEXT_TOKEN` environment
            variable will be used.
    Raises:
        ImportError: if the `context-python` package is not installed.
    Chat Example:
        >>> from langchain.llms import ChatOpenAI
        >>> from langchain.callbacks import ContextCallbackHandler
        >>> context_callback = ContextCallbackHandler(
        ...     token="<CONTEXT_TOKEN_HERE>",
        ... )
        >>> chat = ChatOpenAI(
        ...     temperature=0,