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.
pythonimport 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.
pythoninference_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.
python - 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
To store every input and output that hits the API in S3, you can modify the
log_inferencemethod of the custom logger class to write the request and response data to a file in S3. Here's how you can do it:- Import the necessary libraries and modules.
pythonimport logging import boto3- Define the
InferenceLoggerclass with thelog_inferencemethod that writes the request and response data to a file in S3.
pythonclass InferenceLogger(logging.Logger): def log_inference(self, request, response): s3 = boto3.client("s3") s3.put_object( Bucket="your-bucket-name", Key="inference_logs.txt", Body=f"Request: {request}, Response: {response}", )- Create an instance of the class and set its level to the desired logging level.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.