Devika is the first open-source implementation of an Agentic Software Engineer. Initially started as an open-source alternative to Devin.
Python
Log prompts, enable/disable API/prompts logging.
Rafal Lewczukefea047bc1a2
diff --git a/config.toml b/config.tomlindex 8e45dbc..a898fd1 100644--- a/config.toml+++ b/config.toml@@ -14,3 +14,7 @@ OPENAI = "<YOUR_OPENAI_API_KEY>"[API_ENDPOINTS]BING = "https://api.bing.microsoft.com/v7.0/search"++[LOGGING]+LOG_REST_API = "true"+LOG_PROMPTS = "false"diff --git a/src/config.py b/src/config.pyindex 4f0d06b..41be3ff 100644--- a/src/config.py+++ b/src/config.py@@ -39,7 +39,13 @@ class Config:def get_repos_dir(self):return self.config["STORAGE"]["REPOS_DIR"]-++ def get_logging_rest_api(self):+ return self.config["LOGGING"]["LOG_REST_API"] == "true"++ def get_logging_prompts(self):+ return self.config["LOGGING"]["LOG_PROMPTS"] == "true"+def set_bing_api_key(self, key):self.config["API_KEYS"]["BING"] = keyself.save_config()@@ -84,6 +90,14 @@ class Config:self.config["STORAGE"]["REPOS_DIR"] = dirself.save_config()+ def set_logging_rest_api(self, value):+ self.config["LOGGING"]["LOG_REST_API"] = "true" if value else "false"+ self.save_config()++ def set_logging_prompts(self, value):+ self.config["LOGGING"]["LOG_PROMPTS"] = "true" if value else "false"+ self.save_config()+def save_config(self):with open("config.toml", "w") as f:toml.dump(self.config, f)diff --git a/src/llm/llm.py b/src/llm/llm.pyindex 4080b5e..be292bd 100644--- a/src/llm/llm.py+++ b/src/llm/llm.py@@ -6,6 +6,9 @@ from .openai_client import OpenAIimport tiktoken+from ..config import Config+from ..logger import Logger+TOKEN_USAGE = 0TIKTOKEN_ENC = tiktoken.get_encoding("cl100k_base")@@ -23,10 +26,14 @@ class Model(Enum):for model in Ollama.list_models()]++logger = Logger(filename="devika_prompts.log")+class LLM:def __init__(self, model_id: str = None):self.model_id = model_id-+ self.log_prompts = Config().get_logging_prompts()+def list_models(self) -> list[tuple[str, str]]:return [model.value for model in Model if model.name != "OLLAMA_MODELS"] + list(Model.OLLAMA_MODELS.value@@ -50,6 +57,9 @@ class LLM:model = self.model_id_to_enum_mapping()[self.model_id]+ if self.log_prompts:+ logger.debug(f"Prompt ({model}): --> {prompt}")+if model == "OLLAMA_MODELS":response = Ollama().inference(self.model_id, prompt).strip()elif "CLAUDE" in str(model):@@ -59,6 +69,9 @@ class LLM:else:raise ValueError(f"Model {model} not supported")+ if self.log_prompts:+ logger.debug(f"Response ({model}): --> {response}")+self.update_global_token_usage(response)return responsediff --git a/src/logger.py b/src/logger.pyindex def8faa..94ae2b0 100644--- a/src/logger.py+++ b/src/logger.py@@ -6,10 +6,10 @@ from flask import requestfrom src.config import Configclass Logger:- def __init__(self):+ def __init__(self, filename="devika_agent.log"):config = Config()logs_dir = config.get_logs_dir()- self.logger = LogInit(pathName=logs_dir + "/devika_agent.log", console=True, colors=True)+ self.logger = LogInit(pathName=logs_dir + "/" + filename, console=True, colors=True)def read_log_file(self) -> str:with open(self.logger.pathName, "r") as file:@@ -43,20 +43,25 @@ def route_logger(logger: Logger)::param logger: The logger instance to use for logging."""++ log_enabled = Config().get_logging_rest_api()+def decorator(func):@wraps(func)def wrapper(*args, **kwargs):# Log entry point- logger.info(f"{request.path} {request.method}")+ if log_enabled:+ logger.info(f"{request.path} {request.method}")# Call the actual route functionresponse = func(*args, **kwargs)# Log exit point, including response summary if possibletry:- response_summary = response.get_data(as_text=True)- logger.debug(f"{request.path} {request.method} - Response: {response_summary}")+ if log_enabled:+ response_summary = response.get_data(as_text=True)+ logger.debug(f"{request.path} {request.method} - Response: {response_summary}")except Exception as e:logger.exception(f"{request.path} {request.method} - {e})")