backend.api.fast_api

FastAPI Router: Authentication, User Management, Conversations, and Chat API

This module defines the HTTP API endpoints exposed by the backend. It handles: - User login, registration, verification, and logout - Conversation creation, update, retrieval - Messaging (new messages, fetch messages) - Feedback on messages - Integration with the LLM pipeline for legal Q&A

Each endpoint validates input via Pydantic models and returns structured responses.

Attributes

router

Creates the FastAPI router in which we define its routes

Functions

login(data, response)

Authenticate a user and set JWT as cookie.

register(data)

Register a new user account.

verify(data)

Verify a user's email using a code.

resend_code(data)

Resend a verification code to a user's email.

new_conversation(data)

Create a new conversation.

update_conversation(data)

Update the name of an existing conversation.

new_message(data)

Create a new message in a conversation.

get_user_conversations([token, username])

Fetch all conversations for a given user.

get_messages([token, conversation_id])

Fetch messages for a given conversation.

user_feedback(data)

Submit feedback for a user message.

get_user([token])

Retrieve user details from JWT token.

chat_endpoint(request_data, request)

Main chat endpoint integrating with the LLM pipeline.

logout(response)

Logout user by clearing JWT cookie.

Module Contents

router[source]

Creates the FastAPI router in which we define its routes

async login(data: backend.api.models.UserCredentials, response: fastapi.Response)[source]

Authenticate a user and set JWT as cookie.

Request Body

UserCredentials {username: str, password: str}

returns:

{'user_details': {...}} if successful.

rtype:

dict

raises HTTPException 401:

If authentication fails.

async register(data: backend.api.models.UserData)[source]

Register a new user account.

Request Body

UserData {username: str, password: str, email: str}

returns:

True if registration successful.

rtype:

bool

raises HTTPException 401:

If username or email already exists, or invalid password.

async verify(data: backend.api.models.VerifCode)[source]

Verify a user's email using a code.

Request Body

VerifCode {username: str, code: str}

returns:

True if verification successful.

rtype:

bool

raises HTTPException 401:

If code expired or mismatched.

async resend_code(data: backend.api.models.UserOpenData)[source]

Resend a verification code to a user's email.

Request Body

UserOpenData {username: str, email: str}

returns:

True if code resent.

rtype:

bool

async new_conversation(data: backend.api.models.ConversationCreationDetails)[source]

Create a new conversation.

Request Body

ConversationCreationDetails {username: str, conversation_name: str}

returns:

{'conversation_name': str, 'conversation_id': UUID}

rtype:

dict

async update_conversation(data: backend.api.models.UpdateConversationDetails)[source]

Update the name of an existing conversation.

Request Body

UpdateConversationDetails {conversation_id: str, conversation_name: str}

returns:

True if update successful.

rtype:

bool

async new_message(data: backend.api.models.NewMessage)[source]

Create a new message in a conversation.

Request Body

NewMessage {id: str, conversation_id: str, text: str, role: str, feedback: bool|None}

returns:

{'id': str, 'message': str, 'timestamp': str, 'role': str}

rtype:

dict

async get_user_conversations(token: str = Cookie(None), username: str = '')[source]

Fetch all conversations for a given user.

Request Body

usernamestr

The username whose conversations to fetch.

returns:

[{'conversation_name': str, 'conversation_id': UUID}, ...]

rtype:

list[dict]

async get_messages(token: str = Cookie(None), conversation_id: str = '')[source]

Fetch messages for a given conversation.

Request Body

conversation_idstr

ID of the conversation.

returns:

List of messages in ascending timestamp order.

rtype:

list[dict]

user_feedback(data: backend.api.models.UserFeedback)[source]

Submit feedback for a user message.

Request Body

UserFeedback {message_id: str, conversation_id: str, feedback: bool|None}

get_user(token: str = Cookie(None))[source]

Retrieve user details from JWT token.

Returns:

{'username': str, 'email': str, 'verified': bool|None}

Return type:

dict

async chat_endpoint(request_data: backend.api.models.Message, request: fastapi.Request)[source]

Main chat endpoint integrating with the LLM pipeline.

Request Body

Message {message: str, conversation_history: list[dict]}

returns:
  • StreamingResponse -- Streamed LLM-generated response (Server-Sent Events).

  • JSONResponse -- If the query is rejected as unsafe or non-legal.

async logout(response: fastapi.Response)[source]

Logout user by clearing JWT cookie.

Returns:

True if logout successful.

Return type:

bool