27
27
from nemoguardrails .logging .explain import LLMCallInfo
28
28
from nemoguardrails .logging .processing_log import processing_log_var
29
29
from nemoguardrails .logging .stats import LLMStats
30
+ from nemoguardrails .utils import new_uuid
30
31
31
32
log = logging .getLogger (__name__ )
32
33
@@ -51,6 +52,8 @@ async def on_llm_start(
51
52
llm_call_info = LLMCallInfo ()
52
53
llm_call_info_var .set (llm_call_info )
53
54
55
+ llm_call_info .id = new_uuid ()
56
+
54
57
# We also add it to the explain object
55
58
explain_info = explain_info_var .get ()
56
59
if explain_info :
@@ -86,6 +89,8 @@ async def on_chat_model_start(
86
89
llm_call_info = LLMCallInfo ()
87
90
llm_call_info_var .set (llm_call_info )
88
91
92
+ llm_call_info .id = new_uuid ()
93
+
89
94
# We also add it to the explain object
90
95
explain_info = explain_info_var .get ()
91
96
if explain_info :
@@ -109,7 +114,7 @@ async def on_chat_model_start(
109
114
)
110
115
111
116
log .info ("Invocation Params :: %s" , kwargs .get ("invocation_params" , {}))
112
- log .info ("Prompt Messages :: %s" , prompt )
117
+ log .info ("Prompt Messages :: %s" , prompt , extra = { "id" : llm_call_info . id } )
113
118
llm_call_info .prompt = prompt
114
119
llm_call_info .started_at = time ()
115
120
@@ -143,12 +148,16 @@ async def on_llm_end(
143
148
** kwargs : Any ,
144
149
) -> None :
145
150
"""Run when LLM ends running."""
146
- log .info ("Completion :: %s" , response .generations [0 ][0 ].text )
147
151
llm_call_info = llm_call_info_var .get ()
148
152
if llm_call_info is None :
149
153
llm_call_info = LLMCallInfo ()
150
154
llm_call_info .completion = response .generations [0 ][0 ].text
151
155
llm_call_info .finished_at = time ()
156
+ log .info (
157
+ "Completion :: %s" ,
158
+ response .generations [0 ][0 ].text ,
159
+ extra = {"id" : llm_call_info .id },
160
+ )
152
161
153
162
llm_stats = llm_stats_var .get ()
154
163
if llm_stats is None :
@@ -159,7 +168,9 @@ async def on_llm_end(
159
168
if len (response .generations [0 ]) > 1 :
160
169
for i , generation in enumerate (response .generations [0 ][1 :]):
161
170
log .info ("--- :: Completion %d" , i + 2 )
162
- log .info ("Completion :: %s" , generation .text )
171
+ log .info (
172
+ "Completion :: %s" , generation .text , extra = {"id" : llm_call_info .id }
173
+ )
163
174
164
175
log .info ("Output Stats :: %s" , response .llm_output )
165
176
took = llm_call_info .finished_at - llm_call_info .started_at
0 commit comments