| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7ca837a commit 8008b9c
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -149,6 +149,7 @@ def _get_ai_system(all_params: "Dict[str, Any]") -> "Optional[str]": | |||
| 149 | 149 | ||
| 150 | 150 | DATA_FIELDS = { | |
| 151 | 151 | "frequency_penalty": SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY, | |
| 152 | + # "function_call" is an OpenAI convention for the now-legacy Chat Completions API field | ||
| 152 | 153 | "function_call": SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, | |
| 153 | 154 | "max_tokens": SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, | |
| 154 | 155 | "presence_penalty": SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY, | |
@@ -422,6 +423,12 @@ def on_llm_start( | |||
| 422 | 423 | ||
| 423 | 424 | for key, attribute in DATA_FIELDS.items(): | |
| 424 | 425 | if key in all_params and all_params[key] is not None: | |
| 426 | + # This is correctly gated on "inputs" at the moment because the | ||
| 427 | + # "on_llm_start" method is the start of a request. | ||
| 428 | + # | ||
| 429 | + # TODO: GEN_AI_RESPONSE_TOOL_CALLS will need to be | ||
| 430 | + # transitioned to non-deprecated tool call attributes | ||
| 431 | + | ||
| 425 | 432 | if ( | |
| 426 | 433 | attribute == SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS | |
| 427 | 434 | and has_data_collection_enabled(client.options) | |
@@ -521,6 +528,11 @@ def on_chat_model_start( | |||
| 521 | 528 | for key, attribute in DATA_FIELDS.items(): | |
| 522 | 529 | if key in all_params and all_params[key] is not None: | |
| 523 | 530 | if ( | |
| 531 | + # This is correctly gated on "inputs" at the moment because the | ||
| 532 | + # "on_chat_model_start" method is the start of a request. | ||
| 533 | + # | ||
| 534 | + # TODO: GEN_AI_RESPONSE_TOOL_CALLS will need to be | ||
| 535 | + # transitioned to non-deprecated tool call attributes | ||
| 524 | 536 | attribute == SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS | |
| 525 | 537 | and has_data_collection_enabled(client.options) | |
| 526 | 538 | and not client.options["data_collection"]["gen_ai"]["inputs"] | |
@@ -620,14 +632,11 @@ def on_llm_end( | |||
| 620 | 632 | ||
| 621 | 633 | client = sentry_sdk.get_client() | |
| 622 | 634 | ||
| 623 | - record_inputs = False | ||
| 624 | 635 | record_outputs = False | |
| 625 | 636 | if has_data_collection_enabled(client.options): | |
| 626 | - record_inputs = client.options["data_collection"]["gen_ai"]["inputs"] | ||
| 627 | 637 | record_outputs = client.options["data_collection"]["gen_ai"]["outputs"] | |
| 628 | 638 | elif should_send_default_pii() and self.include_prompts: | |
| 629 | 639 | # TODO: Remove this branch once `send_default_pii` is deprecated | |
| 630 | - record_inputs = True | ||
| 631 | 640 | record_outputs = True | |
| 632 | 641 | ||
| 633 | 642 | try: | |
@@ -652,7 +661,7 @@ def on_llm_end( | |||
| 652 | 661 | if response_model is not None: | |
| 653 | 662 | set_on_span(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model) | |
| 654 | 663 | ||
| 655 | - if record_inputs: | ||
| 664 | + if record_outputs: | ||
| 656 | 665 | tool_calls = getattr(generation.message, "tool_calls", None) | |
| 657 | 666 | if tool_calls is not None and tool_calls != []: | |
| 658 | 667 | set_data_normalized( | |
@@ -1053,17 +1062,19 @@ def _set_tools_on_span(span: "Union[Span, StreamedSpan]", tools: "Any") -> None: | |||
| 1053 | 1062 | return | |
| 1054 | 1063 | ||
| 1055 | 1064 | client = sentry_sdk.get_client() | |
| 1065 | + attribute_name = SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS | ||
| 1056 | 1066 | if has_data_collection_enabled(client.options): | |
| 1057 | 1067 | if not client.options["data_collection"]["gen_ai"]["inputs"]: | |
| 1058 | 1068 | return | |
| 1059 | - | ||
| 1069 | + else: | ||
| 1070 | + attribute_name = SPANDATA.GEN_AI_TOOL_DEFINITIONS | ||
| 1060 | 1071 | # Before data collection was introduced this was set unconditionally, so it | |
| 1061 | 1072 | # stays that way when data collection is not configured. | |
| 1062 | 1073 | simplified_tools = _simplify_langchain_tools(tools) | |
| 1063 | 1074 | if simplified_tools: | |
| 1064 | 1075 | set_data_normalized( | |
| 1065 | 1076 | span, | |
| 1066 | - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, | ||
| 1077 | + attribute_name, | ||
| 1067 | 1078 | simplified_tools, | |
| 1068 | 1079 | unpack=False, | |
| 1069 | 1080 | ) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6497,10 +6497,10 @@ def test_langchain_text_completion_data_collection( | |||
| 6497 | 6497 | False, | |
| 6498 | 6498 | False, | |
| 6499 | 6499 | [ | |
| 6500 | - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, | ||
| 6500 | + SPANDATA.GEN_AI_TOOL_DEFINITIONS, | ||
| 6501 | 6501 | SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, | |
| 6502 | 6502 | ], | |
| 6503 | - [], | ||
| 6503 | + [SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS], | ||
| 6504 | 6504 | id="gen-ai-inputs-and-outputs-enabled-tools-collected", | |
| 6505 | 6505 | ), | |
| 6506 | 6506 | pytest.param( | |
@@ -6509,6 +6509,7 @@ def test_langchain_text_completion_data_collection( | |||
| 6509 | 6509 | True, | |
| 6510 | 6510 | [], | |
| 6511 | 6511 | [ | |
| 6512 | + SPANDATA.GEN_AI_TOOL_DEFINITIONS, | ||
| 6512 | 6513 | SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, | |
| 6513 | 6514 | SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, | |
| 6514 | 6515 | ], | |
@@ -6518,33 +6519,34 @@ def test_langchain_text_completion_data_collection( | |||
| 6518 | 6519 | {"gen_ai": {"inputs": True, "outputs": False}}, | |
| 6519 | 6520 | False, | |
| 6520 | 6521 | False, | |
| 6522 | + [SPANDATA.GEN_AI_TOOL_DEFINITIONS], | ||
| 6521 | 6523 | [ | |
| 6524 | + # REQUEST_AVAILABLE_TOOLS is the legacy value set when data collection is not enabled | ||
| 6522 | 6525 | SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, | |
| 6523 | 6526 | SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, | |
| 6524 | 6527 | ], | |
| 6525 | - [], | ||
| 6526 | - id="gen-ai-inputs-enabled-outputs-disabled-tools-collected", | ||
| 6528 | + id="gen-ai-inputs-enabled-outputs-disabled-only-tool-definitions-collected", | ||
| 6527 | 6529 | ), | |
| 6528 | 6530 | pytest.param( | |
| 6529 | 6531 | {"gen_ai": {"inputs": False, "outputs": True}}, | |
| 6530 | 6532 | True, | |
| 6531 | 6533 | True, | |
| 6532 | - [], | ||
| 6534 | + [SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS], | ||
| 6533 | 6535 | [ | |
| 6536 | + SPANDATA.GEN_AI_TOOL_DEFINITIONS, | ||
| 6534 | 6537 | SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, | |
| 6535 | - SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, | ||
| 6536 | 6538 | ], | |
| 6537 | - id="gen-ai-outputs-enabled-inputs-disabled-tools-not-collected", | ||
| 6539 | + id="gen-ai-outputs-enabled-inputs-disabled-only-response-tool-calls-collected", | ||
| 6538 | 6540 | ), | |
| 6539 | 6541 | pytest.param( | |
| 6540 | 6542 | {"gen_ai": {}}, | |
| 6541 | 6543 | False, | |
| 6542 | 6544 | False, | |
| 6543 | 6545 | [ | |
| 6544 | - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, | ||
| 6546 | + SPANDATA.GEN_AI_TOOL_DEFINITIONS, | ||
| 6545 | 6547 | SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, | |
| 6546 | 6548 | ], | |
| 6547 | - [], | ||
| 6549 | + [SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS], | ||
| 6548 | 6550 | id="gen-ai-inputs-and-outputs-omitted-default-to-enabled", | |
| 6549 | 6551 | ), | |
| 6550 | 6552 | pytest.param( | |
@@ -6555,15 +6557,18 @@ def test_langchain_text_completion_data_collection( | |||
| 6555 | 6557 | SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, | |
| 6556 | 6558 | SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, | |
| 6557 | 6559 | ], | |
| 6558 | - [], | ||
| 6560 | + [SPANDATA.GEN_AI_TOOL_DEFINITIONS], | ||
| 6559 | 6561 | id="no-gen-ai-config-legacy-pii-and-include-prompts-enabled", | |
| 6560 | 6562 | ), | |
| 6561 | 6563 | pytest.param( | |
| 6562 | 6564 | None, | |
| 6563 | 6565 | False, | |
| 6564 | 6566 | False, | |
| 6565 | 6567 | [SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS], | |
| 6566 | - [SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS], | ||
| 6568 | + [ | ||
| 6569 | + SPANDATA.GEN_AI_TOOL_DEFINITIONS, | ||
| 6570 | + SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, | ||
| 6571 | + ], | ||
| 6567 | 6572 | id="no-gen-ai-config-available-tools-collected-regardless-of-pii", | |
| 6568 | 6573 | ), | |
| 6569 | 6574 | ], | |
@@ -6664,16 +6669,14 @@ def test_langchain_data_collection_tools( | |||
| 6664 | 6669 | for key in expected_absent: | |
| 6665 | 6670 | assert key not in chat_spans[0], f"{key} should not have been collected" | |
| 6666 | 6671 | ||
| 6667 | - # Available tools are gated the same way on every span they are set on | ||
| 6668 | - available_tools_collected = ( | ||
| 6669 | - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS in expected_present | ||
| 6670 | - ) | ||
| 6671 | - assert ( | ||
| 6672 | - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS in chat_spans[1] | ||
| 6673 | - ) is available_tools_collected | ||
| 6674 | - assert ( | ||
| 6675 | - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS in invoke_agent_span | ||
| 6676 | - ) is available_tools_collected | ||
| 6672 | + # Tool definitions are gated the same way on every span they are set on | ||
| 6673 | + for key in ( | ||
| 6674 | + SPANDATA.GEN_AI_TOOL_DEFINITIONS, | ||
| 6675 | + SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, | ||
| 6676 | + ): | ||
| 6677 | + collected = key in expected_present | ||
| 6678 | + assert (key in chat_spans[1]) is collected | ||
| 6679 | + assert (key in invoke_agent_span) is collected | ||
| 6677 | 6680 | ||
| 6678 | 6681 | ||
| 6679 | 6682 | @pytest.mark.parametrize("span_streaming", [True, False]) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments