Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature LLMFileLoggingMiddleware #173

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions council/llm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
LLMMiddlewareChain,
LLMRetryMiddleware,
LLMLoggingMiddleware,
LLMFileLoggingMiddleware,
ExecuteLLMRequest,
)
from .llm_function import LLMFunction, LLMFunctionError, FunctionOutOfRetryError
Expand Down
35 changes: 35 additions & 0 deletions council/llm/llm_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,41 @@ def __call__(self, llm: LLMBase, execute: ExecuteLLMRequest, request: LLMRequest
return response


class LLMFileLoggingMiddleware:
"""Middleware for logging LLM requests and responses into a file."""

def __init__(self, log_file: str, component_name: str) -> None:
"""Initialize the middleware with the path to the log_file."""

self.log_file = log_file
self.component_name = component_name

def __call__(self, llm: LLMBase, execute: ExecuteLLMRequest, request: LLMRequest) -> LLMResponse:
self._log_llm_request(request)
response = execute(request)
self._log_llm_response(response)
return response

def _log_llm_request(self, request: LLMRequest) -> None:
messages_str = "\n\n".join(message.format() for message in request.messages)
self._log(f"LLM input for {self.component_name}:\n{messages_str}")

def _log_llm_response(self, response: LLMResponse) -> None:
if response.result is None:
self._log(f"LLM output for {self.component_name} is not available")
return
self._log(
f"LLM output for {self.component_name} Duration: {response.duration:.2f} Output:\n"
f"{response.result.first_choice}"
)

def _log(self, content: str) -> None:
Copy link
Contributor

@gkoch78 gkoch78 Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good for now and probably enough for testing, but I want to point out that this may not behave properly in context of multi-thread.
I'd suggest adding a lock for the _log function to ensure each write is done atomically

"""Append `content` to a current log file"""

with open(self.log_file, "a", encoding="utf-8") as file:
file.write(f"\n{content}")


class LLMRetryMiddleware:
"""
Middleware for implementing retry logic for LLM requests.
Expand Down
5 changes: 5 additions & 0 deletions docs/source/reference/llm/llm_middleware.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ LLMLoggingMiddleware

.. autoclass:: council.llm.LLMLoggingMiddleware

LLMFileLoggingMiddleware
------------------------

.. autoclass:: council.llm.LLMFileLoggingMiddleware

LLMRetryMiddleware
------------------

Expand Down
Loading