Skip to content

Commit 413891f

Browse files
authored
Token Level Log (#3238)
1 parent 7a0a4d4 commit 413891f

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

backend/danswer/configs/app_configs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,9 @@
422422
LOG_DANSWER_MODEL_INTERACTIONS = (
423423
os.environ.get("LOG_DANSWER_MODEL_INTERACTIONS", "").lower() == "true"
424424
)
425+
LOG_INDIVIDUAL_MODEL_TOKENS = (
426+
os.environ.get("LOG_INDIVIDUAL_MODEL_TOKENS", "").lower() == "true"
427+
)
425428
# If set to `true` will enable additional logs about Vespa query performance
426429
# (time spent on finding the right docs + time spent fetching summaries from disk)
427430
LOG_VESPA_TIMING_INFORMATION = (

backend/danswer/llm/answering/stream_processing/answer_response_handler.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
QuotesProcessor,
1414
)
1515
from danswer.llm.answering.stream_processing.utils import DocumentIdOrderMapping
16+
from danswer.utils.logger import setup_logger
17+
18+
logger = setup_logger()
1619

1720

1821
class AnswerResponseHandler(abc.ABC):
@@ -48,6 +51,9 @@ def __init__(
4851
self.processed_text = ""
4952
self.citations: list[CitationInfo] = []
5053

54+
# TODO remove this after citation issue is resolved
55+
logger.debug(f"Document to ranking map {self.doc_id_to_rank_map}")
56+
5157
def handle_response_part(
5258
self,
5359
response_item: BaseMessage | None,

backend/danswer/llm/interfaces.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from danswer.configs.app_configs import DISABLE_GENERATIVE_AI
1111
from danswer.configs.app_configs import LOG_DANSWER_MODEL_INTERACTIONS
12+
from danswer.configs.app_configs import LOG_INDIVIDUAL_MODEL_TOKENS
1213
from danswer.utils.logger import setup_logger
1314

1415

@@ -117,10 +118,19 @@ def stream(
117118
self._precall(prompt)
118119
# TODO add a postcall to log model outputs independent of concrete class
119120
# implementation
120-
return self._stream_implementation(
121+
messages = self._stream_implementation(
121122
prompt, tools, tool_choice, structured_response_format
122123
)
123124

125+
tokens = []
126+
for message in messages:
127+
if LOG_INDIVIDUAL_MODEL_TOKENS:
128+
tokens.append(message.content)
129+
yield message
130+
131+
if LOG_INDIVIDUAL_MODEL_TOKENS and tokens:
132+
logger.debug(f"Model Tokens: {tokens}")
133+
124134
@abc.abstractmethod
125135
def _stream_implementation(
126136
self,

deployment/docker_compose/docker-compose.dev.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ services:
8383
- LOG_ALL_MODEL_INTERACTIONS=${LOG_ALL_MODEL_INTERACTIONS:-} # LiteLLM Verbose Logging
8484
# Log all of Danswer prompts and interactions with the LLM
8585
- LOG_DANSWER_MODEL_INTERACTIONS=${LOG_DANSWER_MODEL_INTERACTIONS:-}
86+
- LOG_INDIVIDUAL_MODEL_TOKENS=${LOG_INDIVIDUAL_MODEL_TOKENS:-}
8687
# If set to `true` will enable additional logs about Vespa query performance
8788
# (time spent on finding the right docs + time spent fetching summaries from disk)
8889
- LOG_VESPA_TIMING_INFORMATION=${LOG_VESPA_TIMING_INFORMATION:-}
@@ -204,6 +205,7 @@ services:
204205
- LOG_ALL_MODEL_INTERACTIONS=${LOG_ALL_MODEL_INTERACTIONS:-} # LiteLLM Verbose Logging
205206
# Log all of Danswer prompts and interactions with the LLM
206207
- LOG_DANSWER_MODEL_INTERACTIONS=${LOG_DANSWER_MODEL_INTERACTIONS:-}
208+
- LOG_INDIVIDUAL_MODEL_TOKENS=${LOG_INDIVIDUAL_MODEL_TOKENS:-}
207209
- LOG_VESPA_TIMING_INFORMATION=${LOG_VESPA_TIMING_INFORMATION:-}
208210

209211
# Analytics Configs

0 commit comments

Comments
 (0)