-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make full Request object available in chat #1213
Comments
@willydouhard Would love your ideas on this one, should be really easy to implement. Are there any security or other implications? |
Much needed feature imo. Only thing stopping me from deploying Chainlit for a number of clients at the enterprise level. |
I need this very urgend to send user links for special training bots |
@willydouhard would this PR #1260 help ? For HTTP hooks, I am using ContextVar like this in server.py # Create a context variable to store the request
request_context_var: ContextVar[Request] = ContextVar("request_context_var")
def get_current_request() -> Request:
return request_context_var.get()
class RequestContextMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
# Set the request in the context variable
request_context_var.set(request)
# Process the request and get the response
response = await call_next(request)
return response
# Add request contextvar middleware to the FastAPI application
app.add_middleware(RequestContextMiddleware) |
I like this approach and hadn't thought of it! @nethi I'd love to hear your thoughts about the pros/cons of using context variables here versus adding an optional My rationale for the optional parameter (only in Love to hear your feedback on this, it's a feature many users ask for and from where I stand either the functional approach or the context var approach are acceptable. Curious what @willydouhard's view is on this. |
+1 for passing the Request object in on_chat_start(). seems more flexible and explicit than the context approach for this specific goal (ie outside of the asgi server goals that #1260 addresses). That said, having reliable path/query access is a pretty important feature that a LOT of people seem to be waiting on so would be awesome to get Request/path/query param exposure added asap one way or another |
@dokterbob For my use case (of serving multiple no-code agents from single chainlit agent), I had two different issues to deal with: a) Get access to agent_id in Http related call backs (such as set_chat_profiles decorator) For (b), I would agree that request parameter in on_chat_start would be ideal, but it may mean contract change today. for (a), explicit request parameter works too. But again means change to existing contract. Btw, in my PR, ContextVar approach is used only for (a). For (b), it exposes two session variables: I am not very opinionated about either of these approaches at this point as I can understand backward compatibility angle - I can rework the PR based on comments/feedback accordingly. one question though - on_chat_start callback is from within websocket context, which is handled by socketio.ASGIApp. Will it have access to starlette.requests.Request object ? |
on_chat_start()
Having had initial discussion with Willy, my tentative view (anticipating more reflection) is: if we ensure there's a single hook (e.g. independent of auth) where we've access to the Alternatively, we might always make the entire (initial) request available on the session, but as the WebSocket (corresponding with Happy to hear your comments and feedback. I will probably sleep a bit on it and take good care to extensively read and (in the process) clean up the code to make sure we really do this well in the first go. |
@dokterbob I would like to see some closure to this requirement. Given socketio & awsgi doesn't deal with startlette requests, I am wondering if this is even possible to do today. How about making entire WSGI environ dict available in on_chat_start instead (as WSGI environ is sort of equivalent to request object)? |
I figured out how to make FastAPI request available in socketio connect() event handler. It seems to work fine (changes shown below).
Passing request object as on_chat_start() callback may mean change in contract ? Do we want to do it that way or store request object in session ? |
This PR is the only one I need to start shipping my chainlit products. |
Is your feature request related to a problem?
This would address, in one go:
#393
#144
#964
#1002
#1103
#1140
It might also work towards solving:
#1056
user_session
.languages
and other headers copied in verbatim.Describe the solution you'd like
Accept
request : [starlette.requests.Request](https://www.starlette.io/requests/)
as an optional parameter toon_chat_start()
.Describe alternatives you've considered
See issues/PR above. The proposed solution is more generic, requires less maintenance (e.g. no specific support for various use cases required).
Open question: are there any reasons (security or otherwise) not to do this?
The text was updated successfully, but these errors were encountered: