How to assign a FastAPI Uvicorn thread to a session id?

I am integrating an AI model into a web app (this model needs to have some context in order to maintain a fluid conversation with the user) on a local deployment. The problem is the intrinsic structure of the thread.

I know how a pool thread works. And the problem is that, when doing multiple POST request (for chatting with the bot), there is possibily that another thread that has not been used responses to that request. Then, the context will be saved in the memory of that thread, not in the one that we have been previously using.

Firstly, I want to mention that none solution should not be either implemeting cookies or creating a file for saving the context. The idea is to assign a thread per session_token.

I have tried the following:

from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from dotenv import load_dotenv
from openai import AzureOpenAI
import os
from typing import Dict, List
from uuid import UUID, uuid4

load_dotenv()
router = APIRouter()

class Message(BaseModel):
    """Class representing the message of a conversation"""
    role: str  # either 'user' or 'assistant'
    content: str

class Prompt(BaseModel):
    """Class for sending or receiving messages"""
    session_id: UUID
    prompt: str

class NewSessionResponse(BaseModel):
    session_id: UUID
    
class ResetRequest(BaseModel):
    session_id: UUID

# Dictionary to store conversations
conversations: Dict[UUID, List[Message]] = {}

@router.post("/ai/chat")
async def chat(prompt: Prompt):
    """In charge of executing and obtaining the connection with the model"""
    api_key = os.getenv("key")
    api_url = os.getenv("endpoint url")
    
    if not api_key or not api_url:
        raise HTTPException(status_code=500, detail='API key or API endpoint not found! Try again')
    
    client = AzureOpenAI("here goes some parameters")
    
    # Retrieve the conversation history for the session
    session_id = prompt.session_id
    if session_id not in conversations:
        conversations[session_id] = []
    
    # Add the user's prompt to the conversation history
    conversations[session_id].append(Message(role="user", content=prompt.prompt))
    
    # Create the context for the API request
    context = [{'role': msg.role, 'content': msg.content} for msg in conversations[session_id]]
    
    # Request completion from the model
    response = client.chat.completions.create(
        model="gpt-35-turbo-4k-0613",
        messages=context
    )
    
    # Extract the model's response
    model_response = response.choices[0].message.content
    
    # Add the assistant's response to the conversation history
    conversations[session_id].append(Message(role='assistant', content=model_response))
    print(conversations)
    return {"response": model_response}

@router.post("/ai/new_session", response_model=NewSessionResponse)
async def newSession():
    """Creates a new conversation session"""
    session_id = uuid4()
    conversations[session_id] = []
    return NewSessionResponse(session_id=session_id)

I also try to implement threading when managing requests, but it did not work. I suppose that this happens because the Uvicorn threads and the threads created here are not the same.

New contributor

Javier Martín Pizarro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật