How to change the value of request body? #560
Replies: 2 comments
-
What do you need to accomplish? I'm sure there's a way to mutate the Request body, but it's not super common practice. If you're already mutating the request body, why not avoid using langserve and just use the underlying runnable with fast API directly? Chat gpt suggests doing something like this: body = await request.json()
# Modify the body
body["extra_field"] = "extra_value"
# Create a new request with the modified body
request.scope["body"] = json.dumps(body).encode("utf-8") |
Beta Was this translation helpful? Give feedback.
0 replies
-
@eyurtsev I achieved it this way. @app.post(f"/{APP_USER_NAME}/invoke", include_in_schema=True)
async def invoke(request: Request) -> Response:
request_json = await request.json()
input = request_json["input"]["input"]
rag_message = retriever.rag(input=input, k=2, score_threshold=0.9, verbose=verbose)
if verbose is True:
print("\033[0;33m" + f"my prompt is: {rag_message}" + "\033[0m")
async def receive_func() -> Message:
m: dict[str, Any] = {"input": {"input": rag_message}}
s = json.dumps(m, ensure_ascii=False)
result: Message = {
"type": "http.request",
"body": s.encode("utf-8")
}
return result
r = Request(scope=request.scope, receive=receive_func)
return await api_handler.invoke(request=r) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to change the request body value of
simple_invoke
,would you give me a demo? I can't change request body value.Based on my experience with Java and C #, I believe this can be solved, but I am not very familiar with FastAPI . Can you help me?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions