From 793d2bc77a2d18b15ff779236c10fc143130a99c Mon Sep 17 00:00:00 2001 From: Cyber MacGeddon Date: Wed, 30 Jul 2025 22:50:38 +0100 Subject: [PATCH] Logging strategy updates --- .../model/text_completion/azure/llm.py | 18 +++++++++++------- .../model/text_completion/azure_openai/llm.py | 18 +++++++++++------- .../model/text_completion/claude/llm.py | 14 +++++++++----- .../model/text_completion/cohere/llm.py | 14 +++++++++----- .../text_completion/googleaistudio/llm.py | 17 ++++++++++------- .../model/text_completion/llamafile/llm.py | 14 +++++++++----- .../model/text_completion/lmstudio/llm.py | 18 +++++++++++------- .../model/text_completion/mistral/llm.py | 14 +++++++++----- .../model/text_completion/ollama/llm.py | 10 +++++++--- .../model/text_completion/tgi/llm.py | 17 ++++++++++------- .../model/text_completion/vllm/llm.py | 17 ++++++++++------- 11 files changed, 106 insertions(+), 65 deletions(-) diff --git a/trustgraph-flow/trustgraph/model/text_completion/azure/llm.py b/trustgraph-flow/trustgraph/model/text_completion/azure/llm.py index 70b07606..388ac7c1 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/azure/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/azure/llm.py @@ -8,10 +8,14 @@ import requests import json from prometheus_client import Histogram import os +import logging from .... exceptions import TooManyRequests from .... base import LlmService, LlmResult +# Module logger +logger = logging.getLogger(__name__) + default_ident = "text-completion" default_temperature = 0.0 @@ -111,11 +115,11 @@ class Processor(LlmService): inputtokens = response['usage']['prompt_tokens'] outputtokens = response['usage']['completion_tokens'] - print(resp, flush=True) - print(f"Input Tokens: {inputtokens}", flush=True) - print(f"Output Tokens: {outputtokens}", flush=True) + logger.debug(f"LLM response: {resp}") + logger.info(f"Input Tokens: {inputtokens}") + logger.info(f"Output Tokens: {outputtokens}") - print("Send response...", flush=True) + logger.debug("Sending response...") resp = LlmResult( text = resp, @@ -128,7 +132,7 @@ class Processor(LlmService): except TooManyRequests: - print("Rate limit...") + logger.warning("Rate limit exceeded") # Leave rate limit retries to the base handler raise TooManyRequests() @@ -137,10 +141,10 @@ class Processor(LlmService): # Apart from rate limits, treat all exceptions as unrecoverable - print(f"Exception: {e}") + logger.error(f"Azure LLM exception ({type(e).__name__}): {e}", exc_info=True) raise e - print("Done.", flush=True) + logger.debug("Azure LLM processing complete") @staticmethod def add_args(parser): diff --git a/trustgraph-flow/trustgraph/model/text_completion/azure_openai/llm.py b/trustgraph-flow/trustgraph/model/text_completion/azure_openai/llm.py index c5dd097c..11376426 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/azure_openai/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/azure_openai/llm.py @@ -8,6 +8,10 @@ import json from prometheus_client import Histogram from openai import AzureOpenAI, RateLimitError import os +import logging + +# Module logger +logger = logging.getLogger(__name__) from .... exceptions import TooManyRequests from .... base import LlmService, LlmResult @@ -84,10 +88,10 @@ class Processor(LlmService): inputtokens = resp.usage.prompt_tokens outputtokens = resp.usage.completion_tokens - print(resp.choices[0].message.content, flush=True) - print(f"Input Tokens: {inputtokens}", flush=True) - print(f"Output Tokens: {outputtokens}", flush=True) - print("Send response...", flush=True) + logger.debug(f"LLM response: {resp.choices[0].message.content}") + logger.info(f"Input Tokens: {inputtokens}") + logger.info(f"Output Tokens: {outputtokens}") + logger.debug("Sending response...") r = LlmResult( text = resp.choices[0].message.content, @@ -100,7 +104,7 @@ class Processor(LlmService): except RateLimitError: - print("Send rate limit response...", flush=True) + logger.warning("Rate limit exceeded") # Leave rate limit retries to the base handler raise TooManyRequests() @@ -108,10 +112,10 @@ class Processor(LlmService): except Exception as e: # Apart from rate limits, treat all exceptions as unrecoverable - print(f"Exception: {e}") + logger.error(f"Azure OpenAI LLM exception ({type(e).__name__}): {e}", exc_info=True) raise e - print("Done.", flush=True) + logger.debug("Azure OpenAI LLM processing complete") @staticmethod def add_args(parser): diff --git a/trustgraph-flow/trustgraph/model/text_completion/claude/llm.py b/trustgraph-flow/trustgraph/model/text_completion/claude/llm.py index e69c2095..87b611f4 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/claude/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/claude/llm.py @@ -6,10 +6,14 @@ Input is prompt, output is response. import anthropic import os +import logging from .... exceptions import TooManyRequests from .... base import LlmService, LlmResult +# Module logger +logger = logging.getLogger(__name__) + default_ident = "text-completion" default_model = 'claude-3-5-sonnet-20240620' @@ -42,7 +46,7 @@ class Processor(LlmService): self.temperature = temperature self.max_output = max_output - print("Initialised", flush=True) + logger.info("Claude LLM service initialized") async def generate_content(self, system, prompt): @@ -69,9 +73,9 @@ class Processor(LlmService): resp = response.content[0].text inputtokens = response.usage.input_tokens outputtokens = response.usage.output_tokens - print(resp, flush=True) - print(f"Input Tokens: {inputtokens}", flush=True) - print(f"Output Tokens: {outputtokens}", flush=True) + logger.debug(f"LLM response: {resp}") + logger.info(f"Input Tokens: {inputtokens}") + logger.info(f"Output Tokens: {outputtokens}") resp = LlmResult( text = resp, @@ -91,7 +95,7 @@ class Processor(LlmService): # Apart from rate limits, treat all exceptions as unrecoverable - print(f"Exception: {e}") + logger.error(f"Claude LLM exception ({type(e).__name__}): {e}", exc_info=True) raise e @staticmethod diff --git a/trustgraph-flow/trustgraph/model/text_completion/cohere/llm.py b/trustgraph-flow/trustgraph/model/text_completion/cohere/llm.py index 8e583040..df2c1143 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/cohere/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/cohere/llm.py @@ -7,6 +7,10 @@ Input is prompt, output is response. import cohere from prometheus_client import Histogram import os +import logging + +# Module logger +logger = logging.getLogger(__name__) from .... exceptions import TooManyRequests from .... base import LlmService, LlmResult @@ -39,7 +43,7 @@ class Processor(LlmService): self.temperature = temperature self.cohere = cohere.Client(api_key=api_key) - print("Initialised", flush=True) + logger.info("Cohere LLM service initialized") async def generate_content(self, system, prompt): @@ -59,9 +63,9 @@ class Processor(LlmService): inputtokens = int(output.meta.billed_units.input_tokens) outputtokens = int(output.meta.billed_units.output_tokens) - print(resp, flush=True) - print(f"Input Tokens: {inputtokens}", flush=True) - print(f"Output Tokens: {outputtokens}", flush=True) + logger.debug(f"LLM response: {resp}") + logger.info(f"Input Tokens: {inputtokens}") + logger.info(f"Output Tokens: {outputtokens}") resp = LlmResult( text = resp, @@ -83,7 +87,7 @@ class Processor(LlmService): # Apart from rate limits, treat all exceptions as unrecoverable - print(f"Exception: {e}") + logger.error(f"Cohere LLM exception ({type(e).__name__}): {e}", exc_info=True) raise e @staticmethod diff --git a/trustgraph-flow/trustgraph/model/text_completion/googleaistudio/llm.py b/trustgraph-flow/trustgraph/model/text_completion/googleaistudio/llm.py index ec568e61..6170490a 100644 --- a/trustgraph-flow/trustgraph/model/text_completion/googleaistudio/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/googleaistudio/llm.py @@ -17,6 +17,10 @@ from google.genai import types from google.genai.types import HarmCategory, HarmBlockThreshold from google.api_core.exceptions import ResourceExhausted import os +import logging + +# Module logger +logger = logging.getLogger(__name__) from .... exceptions import TooManyRequests from .... base import LlmService, LlmResult @@ -77,7 +81,7 @@ class Processor(LlmService): # HarmCategory.HARM_CATEGORY_CIVIC_INTEGRITY: block_level, ] - print("Initialised", flush=True) + logger.info("GoogleAIStudio LLM service initialized") async def generate_content(self, system, prompt): @@ -102,9 +106,9 @@ class Processor(LlmService): resp = response.text inputtokens = int(response.usage_metadata.prompt_token_count) outputtokens = int(response.usage_metadata.candidates_token_count) - print(resp, flush=True) - print(f"Input Tokens: {inputtokens}", flush=True) - print(f"Output Tokens: {outputtokens}", flush=True) + logger.debug(f"LLM response: {resp}") + logger.info(f"Input Tokens: {inputtokens}") + logger.info(f"Output Tokens: {outputtokens}") resp = LlmResult( text = resp, @@ -117,7 +121,7 @@ class Processor(LlmService): except ResourceExhausted as e: - print("Hit rate limit:", e, flush=True) + logger.warning("Rate limit exceeded") # Leave rate limit retries to the default handler raise TooManyRequests() @@ -126,8 +130,7 @@ class Processor(LlmService): # Apart from rate limits, treat all exceptions as unrecoverable - print(type(e), flush=True) - print(f"Exception: {e}", flush=True) + logger.error(f"GoogleAIStudio LLM exception ({type(e).__name__}): {e}", exc_info=True) raise e @staticmethod diff --git a/trustgraph-flow/trustgraph/model/text_completion/llamafile/llm.py b/trustgraph-flow/trustgraph/model/text_completion/llamafile/llm.py index baede64c..d769248c 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/llamafile/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/llamafile/llm.py @@ -6,6 +6,10 @@ Input is prompt, output is response. from openai import OpenAI import os +import logging + +# Module logger +logger = logging.getLogger(__name__) from .... exceptions import TooManyRequests from .... base import LlmService, LlmResult @@ -44,7 +48,7 @@ class Processor(LlmService): api_key = "sk-no-key-required", ) - print("Initialised", flush=True) + logger.info("Llamafile LLM service initialized") async def generate_content(self, system, prompt): @@ -70,9 +74,9 @@ class Processor(LlmService): inputtokens = resp.usage.prompt_tokens outputtokens = resp.usage.completion_tokens - print(resp.choices[0].message.content, flush=True) - print(f"Input Tokens: {inputtokens}", flush=True) - print(f"Output Tokens: {outputtokens}", flush=True) + logger.debug(f"LLM response: {resp.choices[0].message.content}") + logger.info(f"Input Tokens: {inputtokens}") + logger.info(f"Output Tokens: {outputtokens}") resp = LlmResult( text = resp.choices[0].message.content, @@ -87,7 +91,7 @@ class Processor(LlmService): except Exception as e: - print(f"Exception: {e}") + logger.error(f"Llamafile LLM exception ({type(e).__name__}): {e}", exc_info=True) raise e @staticmethod diff --git a/trustgraph-flow/trustgraph/model/text_completion/lmstudio/llm.py b/trustgraph-flow/trustgraph/model/text_completion/lmstudio/llm.py index db1ec00e..16dcfdda 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/lmstudio/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/lmstudio/llm.py @@ -6,6 +6,10 @@ Input is prompt, output is response. from openai import OpenAI import os +import logging + +# Module logger +logger = logging.getLogger(__name__) from .... exceptions import TooManyRequests from .... base import LlmService, LlmResult @@ -44,7 +48,7 @@ class Processor(LlmService): api_key = "sk-no-key-required", ) - print("Initialised", flush=True) + logger.info("LMStudio LLM service initialized") async def generate_content(self, system, prompt): @@ -52,7 +56,7 @@ class Processor(LlmService): try: - print(prompt) + logger.debug(f"Prompt: {prompt}") resp = self.openai.chat.completions.create( model=self.model, @@ -69,14 +73,14 @@ class Processor(LlmService): #} ) - print(resp) + logger.debug(f"Full response: {resp}") inputtokens = resp.usage.prompt_tokens outputtokens = resp.usage.completion_tokens - print(resp.choices[0].message.content, flush=True) - print(f"Input Tokens: {inputtokens}", flush=True) - print(f"Output Tokens: {outputtokens}", flush=True) + logger.debug(f"LLM response: {resp.choices[0].message.content}") + logger.info(f"Input Tokens: {inputtokens}") + logger.info(f"Output Tokens: {outputtokens}") resp = LlmResult( text = resp.choices[0].message.content, @@ -91,7 +95,7 @@ class Processor(LlmService): except Exception as e: - print(f"Exception: {e}") + logger.error(f"LMStudio LLM exception ({type(e).__name__}): {e}", exc_info=True) raise e @staticmethod diff --git a/trustgraph-flow/trustgraph/model/text_completion/mistral/llm.py b/trustgraph-flow/trustgraph/model/text_completion/mistral/llm.py index 0c5c1430..6dfd2656 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/mistral/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/mistral/llm.py @@ -6,6 +6,10 @@ Input is prompt, output is response. from mistralai import Mistral import os +import logging + +# Module logger +logger = logging.getLogger(__name__) from .... exceptions import TooManyRequests from .... base import LlmService, LlmResult @@ -42,7 +46,7 @@ class Processor(LlmService): self.max_output = max_output self.mistral = Mistral(api_key=api_key) - print("Initialised", flush=True) + logger.info("Mistral LLM service initialized") async def generate_content(self, system, prompt): @@ -75,9 +79,9 @@ class Processor(LlmService): inputtokens = resp.usage.prompt_tokens outputtokens = resp.usage.completion_tokens - print(resp.choices[0].message.content, flush=True) - print(f"Input Tokens: {inputtokens}", flush=True) - print(f"Output Tokens: {outputtokens}", flush=True) + logger.debug(f"LLM response: {resp.choices[0].message.content}") + logger.info(f"Input Tokens: {inputtokens}") + logger.info(f"Output Tokens: {outputtokens}") resp = LlmResult( text = resp.choices[0].message.content, @@ -105,7 +109,7 @@ class Processor(LlmService): # Apart from rate limits, treat all exceptions as unrecoverable - print(f"Exception: {e}") + logger.error(f"Mistral LLM exception ({type(e).__name__}): {e}", exc_info=True) raise e @staticmethod diff --git a/trustgraph-flow/trustgraph/model/text_completion/ollama/llm.py b/trustgraph-flow/trustgraph/model/text_completion/ollama/llm.py index 6afe0aea..97ed7d15 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/ollama/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/ollama/llm.py @@ -6,6 +6,10 @@ Input is prompt, output is response. from ollama import Client import os +import logging + +# Module logger +logger = logging.getLogger(__name__) from .... exceptions import TooManyRequests from .... base import LlmService, LlmResult @@ -41,8 +45,8 @@ class Processor(LlmService): response = self.llm.generate(self.model, prompt) response_text = response['response'] - print("Send response...", flush=True) - print(response_text, flush=True) + logger.debug("Sending response...") + logger.debug(f"LLM response: {response_text}") inputtokens = int(response['prompt_eval_count']) outputtokens = int(response['eval_count']) @@ -60,7 +64,7 @@ class Processor(LlmService): except Exception as e: - print(f"Exception: {e}") + logger.error(f"Ollama LLM exception ({type(e).__name__}): {e}", exc_info=True) raise e @staticmethod diff --git a/trustgraph-flow/trustgraph/model/text_completion/tgi/llm.py b/trustgraph-flow/trustgraph/model/text_completion/tgi/llm.py index fa7c15c0..09286405 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/tgi/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/tgi/llm.py @@ -6,6 +6,10 @@ Input is prompt, output is response. import os import aiohttp +import logging + +# Module logger +logger = logging.getLogger(__name__) from .... exceptions import TooManyRequests from .... base import LlmService, LlmResult @@ -41,9 +45,8 @@ class Processor(LlmService): self.session = aiohttp.ClientSession() - print("Using TGI service at", base_url) - - print("Initialised", flush=True) + logger.info(f"Using TGI service at {base_url}") + logger.info("TGI LLM service initialized") async def generate_content(self, system, prompt): @@ -85,9 +88,9 @@ class Processor(LlmService): inputtokens = resp["usage"]["prompt_tokens"] outputtokens = resp["usage"]["completion_tokens"] ans = resp["choices"][0]["message"]["content"] - print(f"Input Tokens: {inputtokens}", flush=True) - print(f"Output Tokens: {outputtokens}", flush=True) - print(ans, flush=True) + logger.info(f"Input Tokens: {inputtokens}") + logger.info(f"Output Tokens: {outputtokens}") + logger.debug(f"LLM response: {ans}") resp = LlmResult( text = ans, @@ -104,7 +107,7 @@ class Processor(LlmService): # Apart from rate limits, treat all exceptions as unrecoverable - print(f"Exception: {type(e)} {e}") + logger.error(f"TGI LLM exception ({type(e).__name__}): {e}", exc_info=True) raise e @staticmethod diff --git a/trustgraph-flow/trustgraph/model/text_completion/vllm/llm.py b/trustgraph-flow/trustgraph/model/text_completion/vllm/llm.py index 96b232e8..f194dc86 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/vllm/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/vllm/llm.py @@ -6,6 +6,10 @@ Input is prompt, output is response. import os import aiohttp +import logging + +# Module logger +logger = logging.getLogger(__name__) from .... exceptions import TooManyRequests from .... base import LlmService, LlmResult @@ -45,9 +49,8 @@ class Processor(LlmService): self.session = aiohttp.ClientSession() - print("Using vLLM service at", base_url) - - print("Initialised", flush=True) + logger.info(f"Using vLLM service at {base_url}") + logger.info("vLLM LLM service initialized") async def generate_content(self, system, prompt): @@ -80,9 +83,9 @@ class Processor(LlmService): inputtokens = resp["usage"]["prompt_tokens"] outputtokens = resp["usage"]["completion_tokens"] ans = resp["choices"][0]["text"] - print(f"Input Tokens: {inputtokens}", flush=True) - print(f"Output Tokens: {outputtokens}", flush=True) - print(ans, flush=True) + logger.info(f"Input Tokens: {inputtokens}") + logger.info(f"Output Tokens: {outputtokens}") + logger.debug(f"LLM response: {ans}") resp = LlmResult( text = ans, @@ -99,7 +102,7 @@ class Processor(LlmService): # Apart from rate limits, treat all exceptions as unrecoverable - print(f"Exception: {type(e)} {e}") + logger.error(f"vLLM LLM exception ({type(e).__name__}): {e}", exc_info=True) raise e @staticmethod