-
Notifications
You must be signed in to change notification settings - Fork 56
/
langchain.py
912 lines (809 loc) 路 30.7 KB
/
langchain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
import logging
import typing
import pydantic
try: # Test that langchain is installed before proceeding
import langchain # noqa
except ImportError as e:
log = logging.getLogger("langfuse")
log.error(
f"Could not import langchain. The langchain integration will not work. {e}"
)
from typing import Any, Dict, List, Optional, Sequence, Union
from uuid import UUID, uuid4
from langfuse.api.resources.ingestion.types.sdk_log_body import SdkLogBody
from langfuse.client import (
StatefulSpanClient,
StatefulTraceClient,
)
from langfuse.extract_model import _extract_model_name
from langfuse.utils import _get_timestamp
from langfuse.utils.base_callback_handler import LangfuseBaseCallbackHandler
try:
from langchain.callbacks.base import (
BaseCallbackHandler as LangchainBaseCallbackHandler,
)
from langchain.schema.agent import AgentAction, AgentFinish
from langchain.schema.document import Document
from langchain_core.outputs import (
ChatGeneration,
LLMResult,
)
from langchain_core.messages import (
AIMessage,
BaseMessage,
ChatMessage,
HumanMessage,
SystemMessage,
ToolMessage,
FunctionMessage,
)
except ImportError:
raise ModuleNotFoundError(
"Please install langchain to use the Langfuse langchain integration: 'pip install langchain'"
)
class LangchainCallbackHandler(
LangchainBaseCallbackHandler, LangfuseBaseCallbackHandler
):
log = logging.getLogger("langfuse")
next_span_id: Optional[str] = None
def __init__(
self,
public_key: Optional[str] = None,
secret_key: Optional[str] = None,
host: Optional[str] = None,
debug: bool = False,
stateful_client: Optional[
Union[StatefulTraceClient, StatefulSpanClient]
] = None,
update_stateful_client: bool = False,
session_id: Optional[str] = None,
user_id: Optional[str] = None,
trace_name: Optional[str] = None,
release: Optional[str] = None,
version: Optional[str] = None,
metadata: Optional[Dict[str, Any]] = None,
tags: Optional[List[str]] = None,
threads: Optional[int] = None,
flush_at: Optional[int] = None,
flush_interval: Optional[int] = None,
max_retries: Optional[int] = None,
timeout: Optional[int] = None,
sdk_integration: Optional[str] = None,
) -> None:
LangfuseBaseCallbackHandler.__init__(
self,
public_key=public_key,
secret_key=secret_key,
host=host,
debug=debug,
stateful_client=stateful_client,
update_stateful_client=update_stateful_client,
session_id=session_id,
user_id=user_id,
trace_name=trace_name,
release=release,
version=version,
metadata=metadata,
tags=tags,
threads=threads,
flush_at=flush_at,
flush_interval=flush_interval,
max_retries=max_retries,
timeout=timeout,
sdk_integration=sdk_integration or "langchain",
)
self.runs = {}
if stateful_client and isinstance(stateful_client, StatefulSpanClient):
self.runs[stateful_client.id] = stateful_client
def setNextSpan(self, id: str):
self.next_span_id = id
def on_llm_new_token(
self,
token: str,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
"""Run on new LLM token. Only available when streaming is enabled."""
# Nothing needs to happen here for langfuse. Once the streaming is done,
self.log.debug(
f"on llm new token: run_id: {run_id} parent_run_id: {parent_run_id}"
)
def get_langchain_run_name(self, serialized: Dict[str, Any], **kwargs: Any) -> str:
"""Retrieves the 'run_name' for an entity based on Langchain convention, prioritizing the 'name'
key in 'kwargs' or falling back to the 'name' or 'id' in 'serialized'. Defaults to "<unknown>"
if none are available.
Args:
serialized (Dict[str, Any]): A dictionary containing the entity's serialized data.
**kwargs (Any): Additional keyword arguments, potentially including the 'name' override.
Returns:
str: The determined Langchain run name for the entity.
"""
# Check if 'name' is in kwargs and not None, otherwise use default fallback logic
if "name" in kwargs and kwargs["name"] is not None:
return kwargs["name"]
# Fallback to serialized 'name', 'id', or "<unknown>"
return serialized.get("name", serialized.get("id", ["<unknown>"])[-1])
def on_retriever_error(
self,
error: Union[Exception, KeyboardInterrupt],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
"""Run when Retriever errors."""
try:
self._log_debug_event(
"on_retriever_error", run_id, parent_run_id, error=error
)
if run_id is None or run_id not in self.runs:
raise Exception("run not found")
self.runs[run_id] = self.runs[run_id].end(
level="ERROR",
status_message=str(error),
version=self.version,
input=kwargs.get("inputs"),
)
except Exception as e:
self.log.exception(e)
def on_chain_start(
self,
serialized: Dict[str, Any],
inputs: Dict[str, Any],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> Any:
try:
self._log_debug_event(
"on_chain_start", run_id, parent_run_id, inputs=inputs
)
self.__generate_trace_and_parent(
serialized=serialized,
inputs=inputs,
run_id=run_id,
parent_run_id=parent_run_id,
tags=tags,
metadata=metadata,
version=self.version,
**kwargs,
)
content = {
"id": self.next_span_id,
"trace_id": self.trace.id,
"name": self.get_langchain_run_name(serialized, **kwargs),
"metadata": self.__join_tags_and_metadata(tags, metadata),
"input": inputs,
"version": self.version,
}
if parent_run_id is None:
if self.root_span is None:
self.runs[run_id] = self.trace.span(**content)
else:
self.runs[run_id] = self.root_span.span(**content)
if parent_run_id is not None:
self.runs[run_id] = self.runs[parent_run_id].span(**content)
except Exception as e:
self.log.exception(e)
def __generate_trace_and_parent(
self,
serialized: Dict[str, Any],
inputs: Union[Dict[str, Any], List[str], str, None],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
try:
class_name = self.get_langchain_run_name(serialized, **kwargs)
# on a new invocation, and not user provided root, we want to initialise a new traceo
# parent_run_id is None when we are at the root of a langchain execution
if (
self.trace is not None
and parent_run_id is None
and self.langfuse is not None
):
self.trace = None
if (
self.trace is not None
and parent_run_id is None # We are at the root of a langchain execution
and self.langfuse is None # StatefulClient was provided by user
and self.update_stateful_client
):
params = {
"name": self.trace_name
if self.trace_name is not None
else class_name,
"metadata": self.__join_tags_and_metadata(
tags, metadata, trace_metadata=self.metadata
),
"version": self.version,
"session_id": self.session_id,
"user_id": self.user_id,
"tags": self.tags,
"input": inputs,
}
if self.root_span:
self.root_span.update(**params)
else:
self.trace.update(**params)
# if we are at a root, but langfuse exists, it means we do not have a
# root provided by a user. Initialise it by creating a trace and root span.
if self.trace is None and self.langfuse is not None:
trace = self.langfuse.trace(
id=str(run_id),
name=self.trace_name if self.trace_name is not None else class_name,
metadata=self.__join_tags_and_metadata(
tags, metadata, trace_metadata=self.metadata
),
version=self.version,
session_id=self.session_id,
user_id=self.user_id,
tags=self.tags,
input=inputs,
)
self.trace = trace
if parent_run_id is not None and parent_run_id in self.runs:
self.runs[run_id] = self.trace.span(
id=self.next_span_id,
trace_id=self.trace.id,
name=class_name,
metadata=self.__join_tags_and_metadata(tags, metadata),
input=inputs,
version=self.version,
)
return
except Exception as e:
self.log.exception(e)
def on_agent_action(
self,
action: AgentAction,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
"""Run on agent action."""
try:
self._log_debug_event(
"on_agent_action", run_id, parent_run_id, action=action
)
if run_id not in self.runs:
raise Exception("run not found")
self.runs[run_id] = self.runs[run_id].end(
output=action,
version=self.version,
input=kwargs.get("inputs"),
)
except Exception as e:
self.log.exception(e)
def on_agent_finish(
self,
finish: AgentFinish,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
try:
self._log_debug_event(
"on_agent_finish", run_id, parent_run_id, finish=finish
)
if run_id not in self.runs:
raise Exception("run not found")
self.runs[run_id] = self.runs[run_id].end(
output=finish,
version=self.version,
input=kwargs.get("inputs"),
)
# langchain sends same run_id for agent_finish and chain_end for the same agent interaction.
# Hence, we only delete at chain_end and not here.
self._update_trace_and_remove_state(
run_id, parent_run_id, finish, keep_state=True
)
except Exception as e:
self.log.exception(e)
def on_chain_end(
self,
outputs: Dict[str, Any],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
try:
self._log_debug_event(
"on_chain_end", run_id, parent_run_id, outputs=outputs
)
if run_id not in self.runs:
raise Exception("run not found")
self.runs[run_id] = self.runs[run_id].end(
output=outputs,
version=self.version,
input=kwargs.get("inputs"),
)
self._update_trace_and_remove_state(
run_id, parent_run_id, outputs, input=kwargs.get("inputs")
)
except Exception as e:
self.log.exception(e)
def on_chain_error(
self,
error: Union[Exception, KeyboardInterrupt],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
**kwargs: Any,
) -> None:
try:
self._log_debug_event("on_chain_error", run_id, parent_run_id, error=error)
self.runs[run_id] = self.runs[run_id].end(
level="ERROR",
status_message=str(error),
version=self.version,
input=kwargs.get("inputs"),
)
self._update_trace_and_remove_state(
run_id, parent_run_id, error, input=kwargs.get("inputs")
)
except Exception as e:
self.log.exception(e)
def on_chat_model_start(
self,
serialized: Dict[str, Any],
messages: List[List[BaseMessage]],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> Any:
try:
self._log_debug_event(
"on_chat_model_start", run_id, parent_run_id, messages=messages
)
self.__on_llm_action(
serialized,
run_id,
_flatten_comprehension(
[self._create_message_dicts(m) for m in messages]
),
parent_run_id,
tags=tags,
metadata=metadata,
**kwargs,
)
except Exception as e:
self.log.exception(e)
def on_llm_start(
self,
serialized: Dict[str, Any],
prompts: List[str],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> Any:
try:
self._log_debug_event(
"on_llm_start", run_id, parent_run_id, prompts=prompts
)
self.__on_llm_action(
serialized,
run_id,
prompts[0] if len(prompts) == 1 else prompts,
parent_run_id,
tags=tags,
metadata=metadata,
**kwargs,
)
except Exception as e:
self.log.exception(e)
def on_tool_start(
self,
serialized: Dict[str, Any],
input_str: str,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> Any:
try:
self._log_debug_event(
"on_tool_start", run_id, parent_run_id, input_str=input_str
)
if parent_run_id is None or parent_run_id not in self.runs:
raise Exception("parent run not found")
meta = self.__join_tags_and_metadata(tags, metadata)
if not meta:
meta = {}
meta.update(
{key: value for key, value in kwargs.items() if value is not None}
)
self.runs[run_id] = self.runs[parent_run_id].span(
id=self.next_span_id,
name=self.get_langchain_run_name(serialized, **kwargs),
input=input_str,
metadata=meta,
version=self.version,
)
self.next_span_id = None
except Exception as e:
self.log.exception(e)
def on_retriever_start(
self,
serialized: Dict[str, Any],
query: str,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> Any:
try:
self._log_debug_event(
"on_retriever_start", run_id, parent_run_id, query=query
)
if parent_run_id is None or parent_run_id not in self.runs:
raise Exception("parent run not found")
self.runs[run_id] = self.runs[parent_run_id].span(
id=self.next_span_id,
name=self.get_langchain_run_name(serialized, **kwargs),
input=query,
metadata=self.__join_tags_and_metadata(tags, metadata),
version=self.version,
)
self.next_span_id = None
except Exception as e:
self.log.exception(e)
def on_retriever_end(
self,
documents: Sequence[Document],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
try:
self._log_debug_event(
"on_retriever_end", run_id, parent_run_id, documents=documents
)
if run_id is None or run_id not in self.runs:
raise Exception("run not found")
self.runs[run_id] = self.runs[run_id].end(
output=documents,
version=self.version,
input=kwargs.get("inputs"),
)
self._update_trace_and_remove_state(run_id, parent_run_id, documents)
except Exception as e:
self.log.exception(e)
def on_tool_end(
self,
output: str,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
try:
self._log_debug_event("on_tool_end", run_id, parent_run_id, output=output)
if run_id is None or run_id not in self.runs:
raise Exception("run not found")
self.runs[run_id] = self.runs[run_id].end(
output=output,
version=self.version,
input=kwargs.get("inputs"),
)
self._update_trace_and_remove_state(run_id, parent_run_id, output)
except Exception as e:
self.log.exception(e)
def on_tool_error(
self,
error: Union[Exception, KeyboardInterrupt],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
try:
self._log_debug_event("on_tool_error", run_id, parent_run_id, error=error)
if run_id is None or run_id not in self.runs:
raise Exception("run not found")
self.runs[run_id] = self.runs[run_id].end(
status_message=str(error),
level="ERROR",
version=self.version,
input=kwargs.get("inputs"),
)
self._update_trace_and_remove_state(run_id, parent_run_id, error)
except Exception as e:
self.log.exception(e)
def __on_llm_action(
self,
serialized: Dict[str, Any],
run_id: UUID,
prompts: List[str],
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
**kwargs: Any,
):
try:
self.__generate_trace_and_parent(
serialized,
inputs=prompts[0] if len(prompts) == 1 else prompts,
run_id=run_id,
parent_run_id=parent_run_id,
tags=tags,
metadata=metadata,
version=self.version,
kwargs=kwargs,
)
model_name = None
model_name = self._parse_model_and_log_errors(serialized, kwargs)
content = {
"name": self.get_langchain_run_name(serialized, **kwargs),
"input": prompts,
"metadata": self.__join_tags_and_metadata(tags, metadata),
"model": model_name,
"model_parameters": {
key: value
for key, value in {
"temperature": kwargs["invocation_params"].get("temperature"),
"max_tokens": kwargs["invocation_params"].get("max_tokens"),
"top_p": kwargs["invocation_params"].get("top_p"),
"frequency_penalty": kwargs["invocation_params"].get(
"frequency_penalty"
),
"presence_penalty": kwargs["invocation_params"].get(
"presence_penalty"
),
"request_timeout": kwargs["invocation_params"].get(
"request_timeout"
),
}.items()
if value is not None
},
"version": self.version,
}
if parent_run_id in self.runs:
self.runs[run_id] = self.runs[parent_run_id].generation(**content)
elif self.root_span is not None and parent_run_id is None:
self.runs[run_id] = self.root_span.generation(**content)
else:
self.runs[run_id] = self.trace.generation(**content)
except Exception as e:
self.log.exception(e)
def _parse_model_and_log_errors(self, serialized, kwargs):
"""Parse the model name from the serialized object or kwargs. If it fails, send the error log to the server and return None."""
try:
model_name = _extract_model_name(serialized, **kwargs)
if model_name:
return model_name
if model_name is None:
self.log.warning(
"Langfuse was not able to parse the LLM model. The LLM call will be recorded without model name. Please create an issue so we can fix your integration: https://github.com/langfuse/langfuse/issues/new/choose"
)
self._report_error(
{
"log": "unable to parse model name",
"kwargs": str(kwargs),
"serialized": str(serialized),
}
)
except Exception as e:
self.log.exception(e)
self.log.warning(
"Langfuse was not able to parse the LLM model. The LLM call will be recorded without model name. Please create an issue so we can fix your integration: https://github.com/langfuse/langfuse/issues/new/choose"
)
self._report_error(
{
"log": "unable to parse model name",
"kwargs": str(kwargs),
"serialized": str(serialized),
"exception": str(e),
}
)
return None
def on_llm_end(
self,
response: LLMResult,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
try:
self._log_debug_event(
"on_llm_end", run_id, parent_run_id, response=response, kwargs=kwargs
)
if run_id not in self.runs:
raise Exception("Run not found, see docs what to do in this case.")
else:
generation = response.generations[-1][-1]
extracted_response = (
self._convert_message_to_dict(generation.message)
if isinstance(generation, ChatGeneration)
else _extract_raw_esponse(generation)
)
llm_usage = _parse_usage(response)
self.runs[run_id] = self.runs[run_id].end(
output=extracted_response,
usage=llm_usage,
version=self.version,
input=kwargs.get("inputs"),
)
self._update_trace_and_remove_state(
run_id, parent_run_id, extracted_response
)
except Exception as e:
self.log.exception(e)
def on_llm_error(
self,
error: Union[Exception, KeyboardInterrupt],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
try:
self._log_debug_event("on_llm_error", run_id, parent_run_id, error=error)
self.runs[run_id] = self.runs[run_id].end(
status_message=str(error),
level="ERROR",
version=self.version,
input=kwargs.get("inputs"),
)
self._update_trace_and_remove_state(run_id, parent_run_id, error)
except Exception as e:
self.log.exception(e)
def __join_tags_and_metadata(
self,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
trace_metadata: Optional[Dict[str, Any]] = None,
) -> Optional[Dict[str, Any]]:
final_dict = {}
if tags is not None and len(tags) > 0:
final_dict["tags"] = tags
if metadata is not None:
final_dict.update(metadata)
if trace_metadata is not None:
final_dict.update(trace_metadata)
return final_dict if final_dict != {} else None
def _report_error(self, error: dict):
event = SdkLogBody(log=error)
self._task_manager.add_task(
{
"id": str(uuid4()),
"type": "sdk-log",
"timestamp": _get_timestamp(),
"body": event.dict(),
}
)
def _update_trace_and_remove_state(
self,
run_id: str,
parent_run_id: Optional[str],
output: any,
*,
keep_state: bool = False,
**kwargs: Any,
):
"""Update the trace with the output of the current run. Called at every finish callback event."""
if (
parent_run_id
is None # If we are at the root of the langchain execution -> reached the end of the root
and self.trace is not None # We do have a trace available
and self.trace.id
== str(run_id) # The trace was generated by langchain and not by the user
):
self.trace = self.trace.update(output=output, **kwargs)
elif (
parent_run_id is None
and self.trace is not None # We have a user-provided parent
and self.update_stateful_client
):
if self.root_span is not None:
self.root_span = self.root_span.update(output=output, **kwargs)
else:
self.trace = self.trace.update(output=output, **kwargs)
if not keep_state:
del self.runs[run_id]
def _convert_message_to_dict(self, message: BaseMessage) -> Dict[str, Any]:
# assistant message
if isinstance(message, HumanMessage):
message_dict = {"role": "user", "content": message.content}
elif isinstance(message, AIMessage):
message_dict = {"role": "assistant", "content": message.content}
elif isinstance(message, SystemMessage):
message_dict = {"role": "system", "content": message.content}
elif isinstance(message, ToolMessage):
message_dict = {"role": "tool", "content": message.content}
elif isinstance(message, FunctionMessage):
message_dict = {"role": "function", "content": message.content}
elif isinstance(message, ChatMessage):
message_dict = {"role": message.role, "content": message.content}
else:
raise ValueError(f"Got unknown type {message}")
if "name" in message.additional_kwargs:
message_dict["name"] = message.additional_kwargs["name"]
if message.additional_kwargs:
message_dict["additional_kwargs"] = message.additional_kwargs
return message_dict
def _create_message_dicts(
self, messages: List[BaseMessage]
) -> List[Dict[str, Any]]:
return [self._convert_message_to_dict(m) for m in messages]
def _log_debug_event(
self,
event_name: str,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs,
):
kwargs_log = (
", " + ", ".join([f"{key}: {value}" for key, value in kwargs.items()])
if len(kwargs) > 0
else ""
)
self.log.debug(
f"Event: {event_name}, run_id: {str(run_id)[:5]}, parent_run_id: {str(parent_run_id)[:5]}"
+ kwargs_log
)
def _extract_raw_esponse(last_response):
"""Extract the response from the last response of the LLM call."""
# We return the text of the response if not empty
if last_response.text is not None and last_response.text.strip() != "":
return last_response.text.strip()
elif hasattr(last_response, 'message'):
# Additional kwargs contains the response in case of tool usage
return last_response.message.additional_kwargs
else:
# Not tool usage, some LLM responses can be simply empty
return ""
def _flatten_comprehension(matrix):
return [item for row in matrix for item in row]
def _parse_usage_model(usage: typing.Union[pydantic.BaseModel, dict]):
# maintains a list of key translations. For each key, the usage model is checked
# and a new object will be created with the new key if the key exists in the usage model
# All non matched keys will remain on the object.
if hasattr(usage, "__dict__"):
usage = usage.__dict__
conversion_list = [
# https://pypi.org/project/langchain-anthropic/
("input_tokens", "input"),
("output_tokens", "output"),
]
usage_model = usage.copy() # Copy all existing key-value pairs
for key, value in conversion_list:
if key in usage_model:
usage_model[value] = usage_model.pop(
key
) # Translate key and keep the value
return usage_model if usage_model else None
def _parse_usage(response: LLMResult):
# langchain-anthropic uses the usage field
llm_usage_keys = ["token_usage", "usage"]
llm_usage = None
if response.llm_output is not None:
for key in llm_usage_keys:
if key in response.llm_output and response.llm_output[key]:
llm_usage = _parse_usage_model(response.llm_output[key])
break
return llm_usage