Skip to content

Commit

Permalink
openai[patch]: fix get_num_tokens for function calls (#25785)
Browse files Browse the repository at this point in the history
Closes #25784

See additional discussion
[here](0a4ee86#r145147380).
  • Loading branch information
ccurme authored Aug 27, 2024
1 parent 2aa35d8 commit 2e5c379
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion libs/partners/openai/langchain_openai/chat_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
else:
# Cast str(value) in case the message value is not a string
# This occurs with function messages
num_tokens += len(encoding.encode(value))
num_tokens += len(encoding.encode(str(value)))
if key == "name":
num_tokens += tokens_per_name
# every reply is primed with <im_start>assistant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,10 @@ def test_get_num_tokens_from_messages() -> None:
AIMessage(
"",
additional_kwargs={
"function_call": json.dumps({"arguments": "old", "name": "fun"})
"function_call": {
"arguments": json.dumps({"arg1": "arg1"}),
"name": "fun",
}
},
),
AIMessage(
Expand All @@ -688,6 +691,6 @@ def test_get_num_tokens_from_messages() -> None:
),
ToolMessage("foobar", tool_call_id="foo"),
]
expected = 170
expected = 176
actual = llm.get_num_tokens_from_messages(messages)
assert expected == actual

0 comments on commit 2e5c379

Please sign in to comment.