Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Capture top_k - `gen_ai.request.top_k` and choice count - `gen_ai.request.choice.count` on chat
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ def on_chat_model_start(
request_model = request_model.removeprefix("models/")

# Initialize variables with default values to avoid "possibly unbound" errors
request_choice_count = None
top_k = None
top_p = None
frequency_penalty = None
presence_penalty = None
Expand All @@ -300,6 +302,8 @@ def on_chat_model_start(
max_tokens = None

if params is not None:
request_choice_count = params.get("n")
top_k = params.get("top_k")
top_p = params.get("top_p")
frequency_penalty = params.get("frequency_penalty")
presence_penalty = params.get("presence_penalty")
Expand Down Expand Up @@ -343,6 +347,8 @@ def on_chat_model_start(
llm_invocation.conversation_id = _conversation_id(metadata)
llm_invocation.input_messages = input_messages
llm_invocation.top_p = top_p
llm_invocation.top_k = top_k
Comment thread
rads-1996 marked this conversation as resolved.
llm_invocation.request_choice_count = request_choice_count
llm_invocation.frequency_penalty = frequency_penalty
llm_invocation.presence_penalty = presence_penalty
llm_invocation.stop_sequences = stop_sequences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2799,6 +2799,42 @@ def test_on_chat_model_start_captures_input_messages_when_content_enabled():
assert blob.content == _REAL_PNG_BYTES


def test_on_chat_model_start_captures_top_k_and_choice_count():
run_id = _run_id()
handler, _, llm_inv = _make_handler_with_llm_invocation(run_id)

handler.on_chat_model_start(
serialized={},
messages=[[HumanMessage(content="Hello")]],
run_id=run_id,
invocation_params={
"params": {
"model_name": "gpt-4o",
"top_k": 40,
"n": 3,
}
},
)

assert llm_inv.top_k == 40
assert llm_inv.request_choice_count == 3
Comment thread
rads-1996 marked this conversation as resolved.


def test_on_chat_model_start_defaults_top_k_and_choice_count_to_none():
run_id = _run_id()
handler, _, llm_inv = _make_handler_with_llm_invocation(run_id)

handler.on_chat_model_start(
serialized={},
messages=[[HumanMessage(content="Hello")]],
run_id=run_id,
invocation_params={"model_name": "gpt-4o"},
)

assert llm_inv.top_k is None
assert llm_inv.request_choice_count is None


def test_on_chat_model_start_preserves_message_name():
run_id = _run_id()
handler, telemetry, llm_inv = _make_handler_with_llm_invocation(run_id)
Expand Down