Skip to content

Commit 1f5bd51

Browse files
authored
feat: setup method in ragbits-chat (#586)
1 parent e07c411 commit 1f5bd51

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

packages/ragbits-chat/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add setup method to ChatInterface (#586)
6+
57
## 0.19.1 (2025-05-27)
68

79
### Changed

packages/ragbits-chat/src/ragbits/chat/api.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import logging
44
from collections.abc import AsyncGenerator
5+
from contextlib import asynccontextmanager
56
from pathlib import Path
67
from typing import Any, Literal
78

@@ -59,11 +60,17 @@ def __init__(
5960
cors_origins: List of allowed CORS origins. If None, defaults to common development origins.
6061
ui_build_dir: Path to a custom UI build directory. If None, uses the default package UI.
6162
"""
62-
self.app = FastAPI()
6363
self.chat_interface: ChatInterface = self._load_chat_interface(chat_interface)
6464
self.dist_dir = Path(ui_build_dir) if ui_build_dir else Path(__file__).parent / "ui-build"
6565
self.cors_origins = cors_origins or []
6666

67+
@asynccontextmanager
68+
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
69+
await self.chat_interface.setup()
70+
yield
71+
72+
self.app = FastAPI(lifespan=lifespan)
73+
6774
self.configure_app()
6875
self.setup_routes()
6976
self.setup_exception_handlers()

packages/ragbits-chat/src/ragbits/chat/interface/_interface.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ def verify_state(state: dict[str, Any], signature: str) -> bool:
150150
expected_signature = ChatInterface._sign_state(state)
151151
return hmac.compare_digest(expected_signature, signature)
152152

153+
async def setup(self) -> None: # noqa: B027
154+
"""
155+
Setup the chat interface.
156+
157+
This method is called after the chat interface is initialized and before the chat method is called.
158+
It is used to setup the chat interface, such as loading the model or initializing the vector store.
159+
160+
This method is optional and can be overridden by subclasses.
161+
"""
162+
pass
163+
153164
@abstractmethod
154165
async def chat(
155166
self,

0 commit comments

Comments
 (0)