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
- State updates: Updates to the conversation state
feedback_config
class-attribute
instance-attribute
#
history_persistence
class-attribute
instance-attribute
#
create_text_response
staticmethod
#
Helper method to create a text response.
create_reference
staticmethod
#
Helper method to create a reference response.
Source code in packages/ragbits-chat/src/ragbits/chat/interface/_interface.py
create_state_update
staticmethod
#
Helper method to create a state update response with signature.
Source code in packages/ragbits-chat/src/ragbits/chat/interface/_interface.py
verify_state
staticmethod
#
Verify that a state and signature match.
PARAMETER | DESCRIPTION |
---|---|
state |
The state dictionary to verify
TYPE:
|
signature |
The signature to check against
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
True if the signature is valid, False otherwise |
Source code in packages/ragbits-chat/src/ragbits/chat/interface/_interface.py
setup
async
#
Setup the chat interface.
This method is called after the chat interface is initialized and before the chat method is called. It is used to setup the chat interface, such as loading the model or initializing the vector store.
This method is optional and can be overridden by subclasses.
Source code in packages/ragbits-chat/src/ragbits/chat/interface/_interface.py
chat
abstractmethod
async
#
chat(message: str, history: ChatFormat | None = None, context: ChatContext | None = None) -> AsyncGenerator[ChatResponse, None]
Process a chat message and yield responses asynchronously.
PARAMETER | DESCRIPTION |
---|---|
message |
The current user message
TYPE:
|
history |
Optional list of previous messages in the conversation
TYPE:
|
context |
Optional context containing conversation metadata and state. Will be automatically populated with message_id and conversation_id.
TYPE:
|
YIELDS | DESCRIPTION |
---|---|
AsyncGenerator[ChatResponse, None]
|
ChatResponse objects containing different types of content: |
AsyncGenerator[ChatResponse, None]
|
|
AsyncGenerator[ChatResponse, None]
|
|
AsyncGenerator[ChatResponse, None]
|
|
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}")
elif state := response.as_state_update():
if verify_state(state.state, state.signature):
# Update client state
pass
Source code in packages/ragbits-chat/src/ragbits/chat/interface/_interface.py
save_feedback
async
#
Save feedback about a chat message.
PARAMETER | DESCRIPTION |
---|---|
message_id |
The ID of the message
TYPE:
|
feedback |
The type of feedback
TYPE:
|
payload |
The payload of the feedback
TYPE:
|