Skip to content

Commit b1f4eab

Browse files
authored
Merge pull request #25 from kaikreuzer/ChatCompletionToolParam
fix: Update OpenAI API imports for compatibility with v1.107+
2 parents 1f9fb8d + 415d40e commit b1f4eab

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

  • src/mcp_interviewer/statistics

src/mcp_interviewer/statistics/tool.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
import tiktoken
77
from 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

1111
from .base import CompositeStatistic, ServerScoreCard, Statistic, StatisticValue
1212

1313
logger = 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,9 @@ 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+
function_parameters = function.get("parameters")
69+
if function_parameters is not None and "properties" in function_parameters:
70+
properties = cast(dict, function_parameters["properties"])
7071

7172
if len(properties) > 0:
7273
func_token_count += prop_init # Add tokens for start of each property
@@ -100,11 +101,11 @@ def compute(self, server: ServerScoreCard) -> Generator[StatisticValue, None, No
100101

101102
class ToolInputSchemaTokenCount(ToolStatistic):
102103
def compute_tool(self, tool: Tool) -> Generator[StatisticValue, None, None]:
103-
oai_tool = ChatCompletionTool(
104+
oai_tool = ChatCompletionToolParam(
104105
type="function",
105106
function=FunctionDefinition(
106107
name=tool.name,
107-
description=tool.description,
108+
description=tool.description or "",
108109
parameters=tool.inputSchema,
109110
),
110111
)
@@ -135,7 +136,7 @@ class ToolInputSchemaMaxDepthCount(ToolStatistic):
135136
def compute_tool(self, tool: Tool) -> Generator[StatisticValue, None, None]:
136137
def get_max_depth(o, depth=0):
137138
max_depth = depth
138-
if isinstance(o, list | tuple):
139+
if isinstance(o, (list, tuple)): # noqa: UP038
139140
for item in o:
140141
max_depth = max(get_max_depth(item, depth + 1), max_depth)
141142
elif isinstance(o, dict):

0 commit comments

Comments
 (0)