Logging strategy updates

This commit is contained in:
Cyber MacGeddon 2025-07-30 22:13:19 +01:00
parent c28ba58591
commit 905e17ac36
3 changed files with 41 additions and 29 deletions

View file

@ -4,10 +4,14 @@ Simple token counter for each LLM response.
from prometheus_client import Counter
import json
import logging
from .. schema import TextCompletionResponse, Error
from .. base import FlowProcessor, ConsumerSpec
# Module logger
logger = logging.getLogger(__name__)
default_ident = "metering"
class Processor(FlowProcessor):
@ -59,10 +63,10 @@ class Processor(FlowProcessor):
# Load token costs from the config service
async def on_cost_config(self, config, version):
print("Loading configuration version", version)
logger.info(f"Loading metering configuration version {version}")
if self.config_key not in config:
print(f"No key {self.config_key} in config", flush=True)
logger.warning(f"No key {self.config_key} in config")
return
config = config[self.config_key]
@ -102,9 +106,9 @@ class Processor(FlowProcessor):
__class__.input_cost_metric.inc(cost_in)
__class__.output_cost_metric.inc(cost_out)
print(f"Input Tokens: {num_in}", flush=True)
print(f"Output Tokens: {num_out}", flush=True)
print(f"Cost for call: ${cost_per_call}", flush=True)
logger.info(f"Input Tokens: {num_in}")
logger.info(f"Output Tokens: {num_out}")
logger.info(f"Cost for call: ${cost_per_call}")
@staticmethod
def add_args(parser):

View file

@ -6,10 +6,14 @@ Input is prompt, output is response.
from openai import OpenAI, RateLimitError
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 = 'gpt-3.5-turbo'
@ -52,7 +56,7 @@ class Processor(LlmService):
else:
self.openai = OpenAI(api_key=api_key)
print("Initialised", flush=True)
logger.info("OpenAI LLM service initialized")
async def generate_content(self, system, prompt):
@ -85,9 +89,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,
@ -109,7 +113,7 @@ class Processor(LlmService):
# Apart from rate limits, treat all exceptions as unrecoverable
print(f"Exception: {type(e)} {e}")
logger.error(f"OpenAI LLM exception ({type(e).__name__}): {e}", exc_info=True)
raise e
@staticmethod

View file

@ -6,6 +6,7 @@ Language service abstracts prompt engineering from LLM.
import asyncio
import json
import re
import logging
from ...schema import Definition, Relationship, Triple
from ...schema import Topic
@ -17,6 +18,9 @@ from ...base import ProducerSpec, ConsumerSpec, TextCompletionClientSpec
from ...template import PromptManager
# Module logger
logger = logging.getLogger(__name__)
default_ident = "prompt"
default_concurrency = 1
@ -68,10 +72,10 @@ class Processor(FlowProcessor):
async def on_prompt_config(self, config, version):
print("Loading configuration version", version)
logger.info(f"Loading prompt configuration version {version}")
if self.config_key not in config:
print(f"No key {self.config_key} in config", flush=True)
logger.warning(f"No key {self.config_key} in config")
return
config = config[self.config_key]
@ -80,12 +84,12 @@ class Processor(FlowProcessor):
self.manager.load_config(config)
print("Prompt configuration reloaded.", flush=True)
logger.info("Prompt configuration reloaded")
except Exception as e:
print("Exception:", e, flush=True)
print("Configuration reload failed", flush=True)
logger.error(f"Prompt configuration exception: {e}", exc_info=True)
logger.error("Configuration reload failed")
async def on_request(self, msg, consumer, flow):
@ -99,19 +103,19 @@ class Processor(FlowProcessor):
try:
print(v.terms, flush=True)
logger.debug(f"Prompt terms: {v.terms}")
input = {
k: json.loads(v)
for k, v in v.terms.items()
}
print(f"Handling kind {kind}...", flush=True)
logger.debug(f"Handling prompt kind {kind}...")
async def llm(system, prompt):
print(system, flush=True)
print(prompt, flush=True)
logger.debug(f"System prompt: {system}")
logger.debug(f"User prompt: {prompt}")
resp = await flow("text-completion-request").text_completion(
system = system, prompt = prompt,
@ -120,20 +124,20 @@ class Processor(FlowProcessor):
try:
return resp
except Exception as e:
print("LLM Exception:", e, flush=True)
logger.error(f"LLM Exception: {e}", exc_info=True)
return None
try:
resp = await self.manager.invoke(kind, input, llm)
except Exception as e:
print("Invocation exception:", e, flush=True)
logger.error(f"Prompt invocation exception: {e}", exc_info=True)
raise e
print(resp, flush=True)
logger.debug(f"Prompt response: {resp}")
if isinstance(resp, str):
print("Send text response...", flush=True)
logger.debug("Sending text response...")
r = PromptResponse(
text=resp,
@ -147,8 +151,8 @@ class Processor(FlowProcessor):
else:
print("Send object response...", flush=True)
print(json.dumps(resp, indent=4), flush=True)
logger.debug("Sending object response...")
logger.debug(f"Response object: {json.dumps(resp, indent=4)}")
r = PromptResponse(
text=None,
@ -162,9 +166,9 @@ class Processor(FlowProcessor):
except Exception as e:
print(f"Exception: {e}", flush=True)
logger.error(f"Prompt service exception: {e}", exc_info=True)
print("Send error response...", flush=True)
logger.debug("Sending error response...")
r = PromptResponse(
error=Error(
@ -178,9 +182,9 @@ class Processor(FlowProcessor):
except Exception as e:
print(f"Exception: {e}", flush=True)
logger.error(f"Prompt service exception: {e}", exc_info=True)
print("Send error response...", flush=True)
logger.debug("Sending error response...")
r = PromptResponse(
error=Error(