-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor dialogue manager initialization & usage
- Loading branch information
1 parent
4996d2a
commit a2534b6
Showing
5 changed files
with
52 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
from fastapi import APIRouter, HTTPException, Body, Request | ||
from fastapi import APIRouter, HTTPException, Body, Request, Depends | ||
from app.bot.dialogue_manager.models import ChatModel | ||
from app.dependencies import get_dialogue_manager | ||
|
||
router = APIRouter(prefix="/v1", tags=["bots"]) | ||
|
||
@router.post("/chat") | ||
async def chat(request: Request, body: dict): | ||
async def chat(request: Request, body: dict, dialogue_manager = Depends(get_dialogue_manager)): | ||
""" | ||
Endpoint to converse with the chatbot. | ||
Delegates the request processing to DialogueManager. | ||
:return: JSON response with the chatbot's reply and context. | ||
""" | ||
try: | ||
# Access the dialogue manager from the fast api application state. | ||
chat_request = ChatModel.from_json(body) | ||
chat_response = await request.app.state.dialogue_manager.process(chat_request) | ||
return chat_response.to_json() | ||
except Exception as e: | ||
raise HTTPException(status_code=500, detail=f"Error processing request: {e}") | ||
|
||
# Access the dialogue manager from the fast api application state. | ||
chat_request = ChatModel.from_json(body) | ||
chat_response = await dialogue_manager.process(chat_request) | ||
return chat_response.to_json() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import Optional | ||
from app.bot.dialogue_manager.dialogue_manager import DialogueManager | ||
from app.config import app_config | ||
|
||
_dialogue_manager: Optional[DialogueManager] = None | ||
|
||
async def get_dialogue_manager(): | ||
global _dialogue_manager | ||
return _dialogue_manager | ||
|
||
async def set_dialogue_manager(dialogue_manager: DialogueManager): | ||
global _dialogue_manager | ||
_dialogue_manager = dialogue_manager | ||
|
||
async def init_dialogue_manager(): | ||
global _dialogue_manager | ||
print("initializing dialogue manager") | ||
_dialogue_manager = await DialogueManager.from_config() | ||
_dialogue_manager.update_model(app_config.MODELS_DIR) | ||
print("dialogue manager initialized") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters