55
66import tiktoken
77from mcp .types import Tool
8- from openai .types .chat import ChatCompletionTool
9- from openai .types .shared import FunctionDefinition
8+ from openai .types .chat import ChatCompletionToolParam
9+ from openai .types .shared_params import FunctionDefinition
1010
1111from .base import CompositeStatistic , ServerScoreCard , Statistic , StatisticValue
1212
1313logger = logging .getLogger (__name__ )
1414
1515
16- def num_tokens_for_tool (tool : ChatCompletionTool , model ):
16+ def num_tokens_for_tool (tool : ChatCompletionToolParam , model ):
1717 """From https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb"""
1818
1919 # Initialize function settings to 0
@@ -54,9 +54,9 @@ def num_tokens_for_tool(tool: ChatCompletionTool, model):
5454
5555 func_token_count = 0
5656 func_token_count += func_init # Add tokens for start of each function
57- function = tool . function
58- f_name = function . name
59- f_desc = function .description or ""
57+ function = tool [ " function" ]
58+ f_name = function [ " name" ]
59+ f_desc = function .get ( " description" , "" ) or ""
6060 if f_desc .endswith ("." ):
6161 f_desc = f_desc [:- 1 ]
6262 line = f_name + ":" + f_desc
@@ -65,8 +65,10 @@ def num_tokens_for_tool(tool: ChatCompletionTool, model):
6565 ) # Add tokens for set name and description
6666
6767 properties = {}
68- if function .parameters is not None and "properties" in function .parameters :
69- properties = cast (dict , function .parameters ["properties" ])
68+ if function .get ("parameters" ) is not None and "properties" in function .get (
69+ "parameters" , {}
70+ ):
71+ properties = cast (dict , function ["parameters" ]["properties" ])
7072
7173 if len (properties ) > 0 :
7274 func_token_count += prop_init # Add tokens for start of each property
@@ -100,7 +102,7 @@ def compute(self, server: ServerScoreCard) -> Generator[StatisticValue, None, No
100102
101103class ToolInputSchemaTokenCount (ToolStatistic ):
102104 def compute_tool (self , tool : Tool ) -> Generator [StatisticValue , None , None ]:
103- oai_tool = ChatCompletionTool (
105+ oai_tool = ChatCompletionToolParam (
104106 type = "function" ,
105107 function = FunctionDefinition (
106108 name = tool .name ,
0 commit comments