How can I do inference logging for a FastAPI application that's deployed via k8s
- User
How can I do inference logging for a FastAPI application that's deployed via k8s
- Assistant
To do inference logging for a FastAPI application deployed via k8s, you can follow these steps:
- Define a custom logger class that extends the built-in
logging.Loggerclass. This custom logger class should have a method calledlog_inferencethat takes in the request and response data as parameters.
import logging class InferenceLogger(logging.Logger): def log_inference(self, request, response): self.info(f"Request: {request}, Response: {response}")- Create an instance of the custom logger class and set its level to the desired logging level.
inference_logger = InferenceLogger("inference_logger") inference_logger.setLevel(logging.INFO)- In your FastAPI application, import the
inference_loggerinstance and use it to log the inference requests and responses.
from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): inference_logger.log_inference(request, response) return {"Hello": "World"}By following these steps, you can easily do inference logging for your FastAPI application deployed via k8s.
- Define a custom logger class that extends the built-in
- User
By "inference logging" I meant store every input and output that hits the API in S3
- Assistant
I see, in that case, you can modify the previous approach to store the input and output data in an Amazon S3 bucket instead of logging them to the console. Here's how you can do it:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.