From 45672fe96ea454ae3d8e7603bb306caab89a4c9f Mon Sep 17 00:00:00 2001 From: ArslanSaleem Date: Tue, 10 Oct 2023 12:49:08 +0500 Subject: [PATCH] feat(ApiLogger): logger to send data to remote api --- examples/custom_logger.py | 32 +++++++++++++++++++++++++++ pandasai/custom_loggers/api_logger.py | 28 +++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 examples/custom_logger.py create mode 100644 pandasai/custom_loggers/api_logger.py diff --git a/examples/custom_logger.py b/examples/custom_logger.py new file mode 100644 index 000000000..11cf478d3 --- /dev/null +++ b/examples/custom_logger.py @@ -0,0 +1,32 @@ +import pandas as pd +from pandasai import Agent +from pandasai.custom_loggers.api_logger import APILogger + +from pandasai.llm.openai import OpenAI + +employees_data = { + "EmployeeID": [1, 2, 3, 4, 5], + "Name": ["John", "Emma", "Liam", "Olivia", "William"], + "Department": ["HR", "Sales", "IT", "Marketing", "Finance"], +} + +salaries_data = { + "EmployeeID": [1, 2, 3, 4, 5], + "Salary": [5000, 6000, 4500, 7000, 5500], +} + +employees_df = pd.DataFrame(employees_data) +salaries_df = pd.DataFrame(salaries_data) + + +llm = OpenAI("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") +agent = Agent( + [employees_df, salaries_df], + config={"llm": llm, "enable_cache": True}, + memory_size=10, + logger=APILogger("SERVER-URL", "USER-ID", "API-KEY"), +) + +# Chat with the agent +response = agent.chat("Who gets paid the most?") +print(response) diff --git a/pandasai/custom_loggers/api_logger.py b/pandasai/custom_loggers/api_logger.py new file mode 100644 index 000000000..3570c1838 --- /dev/null +++ b/pandasai/custom_loggers/api_logger.py @@ -0,0 +1,28 @@ +import logging +import requests +from pandasai.helpers.logger import Logger + + +class APILogger(Logger): + _api_key: str = None + _server_url: str = None + _user_id: str = None + + def __init__(self, server_url: str, user_id: str, api_key: str): + self._api_key = api_key + self._server_url = server_url + self._user_id = user_id + + def log(self, message: str, level: int = logging.INFO): + try: + log_data = { + # TODO - Remove user id from the API + "user_id": self._user_id, + "api_key_id": self._api_key, + "json_log": message, + } + response = requests.post(f"{self._server_url}/api/log/add", json=log_data) + if response.status_code != 200: + raise Exception(response.text) + except Exception: + pass