Skip to content

Chat Interface#

The ChatInterface is the main interface for the chat service. It defines the core functionality required for a chat service that can return various types of responses such as:

  • Text: Regular text responses streamed chunk by chunk
  • References: Source documents used to generate the answer

ragbits.chat.interface.ChatInterface #

Bases: ABC

Base interface for chat implementations.

This interface defines the core functionality required for a chat service that can return various types of responses such as:

  • Text: Regular text responses streamed chunk by chunk
  • References: Source documents used to generate the answer

feedback_config class-attribute instance-attribute #

feedback_config: FeedbackConfig = FeedbackConfig()

create_text_response staticmethod #

create_text_response(text: str) -> ChatResponse

Helper method to create a text response.

Source code in packages/ragbits-chat/src/ragbits/chat/interface/_interface.py
@staticmethod
def create_text_response(text: str) -> ChatResponse:
    """Helper method to create a text response."""
    return ChatResponse(type=ChatResponseType.TEXT, content=text)

create_reference staticmethod #

create_reference(title: str, content: str, url: str | None = None) -> ChatResponse

Helper method to create a reference response.

Source code in packages/ragbits-chat/src/ragbits/chat/interface/_interface.py
@staticmethod
def create_reference(
    title: str,
    content: str,
    url: str | None = None,
) -> ChatResponse:
    """Helper method to create a reference response."""
    return ChatResponse(
        type=ChatResponseType.REFERENCE,
        content=Reference(title=title, content=content, url=url),
    )

chat abstractmethod async #

chat(message: str, history: ChatFormat | None = None, context: dict | None = None) -> AsyncGenerator[ChatResponse, None]

Process a chat message and yield responses asynchronously.

PARAMETER DESCRIPTION
message

The current user message

TYPE: str

history

Optional list of previous messages in the conversation

TYPE: ChatFormat | None DEFAULT: None

context

Optional context

TYPE: dict | None DEFAULT: None

YIELDS DESCRIPTION
AsyncGenerator[ChatResponse, None]

ChatResponse objects containing different types of content:

AsyncGenerator[ChatResponse, None]
  • Text chunks for the actual response
AsyncGenerator[ChatResponse, None]
  • Reference documents used to generate the response
Example
chat = MyChatImplementation()
async for response in chat.chat("What is Python?"):
    if text := response.as_text():
        print(f"Text: {text}")
    elif ref := response.as_reference():
        print(f"Reference: {ref.title}")
Source code in packages/ragbits-chat/src/ragbits/chat/interface/_interface.py
@abstractmethod
async def chat(
    self,
    message: str,
    history: ChatFormat | None = None,
    context: dict | None = None,
) -> AsyncGenerator[ChatResponse, None]:
    """
    Process a chat message and yield responses asynchronously.

    Args:
        message: The current user message
        history: Optional list of previous messages in the conversation
        context: Optional context

    Yields:
        ChatResponse objects containing different types of content:
        - Text chunks for the actual response
        - Reference documents used to generate the response

    Example:
        ```python
        chat = MyChatImplementation()
        async for response in chat.chat("What is Python?"):
            if text := response.as_text():
                print(f"Text: {text}")
            elif ref := response.as_reference():
                print(f"Reference: {ref.title}")
        ```
    """
    yield ChatResponse(type=ChatResponseType.TEXT, content="Ragbits cannot respond - please implement chat method!")
    raise NotImplementedError("Chat implementations must implement chat method")

save_feedback async #

save_feedback(message_id: str, feedback: Literal['like', 'dislike'], payload: dict) -> None

Save feedback about a chat message.

PARAMETER DESCRIPTION
message_id

The ID of the message

TYPE: str

feedback

The type of feedback

TYPE: Literal['like', 'dislike']

payload

The payload of the feedback

TYPE: dict

Source code in packages/ragbits-chat/src/ragbits/chat/interface/_interface.py
async def save_feedback(
    self,
    message_id: str,
    feedback: Literal["like", "dislike"],
    payload: dict,
) -> None:
    """
    Save feedback about a chat message.

    Args:
        message_id: The ID of the message
        feedback: The type of feedback
        payload: The payload of the feedback
    """
    logger.info(f"[{self.__class__.__name__}] Saving {feedback} for message {message_id} with payload {payload}")