Skip to content

Commit db2aa29

Browse files
committed
Make mypy happy
1 parent 7c4242b commit db2aa29

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [0.1.7] - Aug 3, 2024
6+
7+
- Fix bug where you couldn't pass in example tool calls in `few_shots` to `build_messages`.
8+
9+
## [0.1.6] - Aug 2, 2024
10+
11+
- Fix bug where you couldn't pass in `tools` and `default_to_cl100k` to True with a non-OpenAI model.
12+
513
## [0.1.5] - June 4, 2024
614

715
- Remove spurious `print` call when counting tokens for function calling.

CONTRIBUTING.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ python3 -m pytest
1717

1818
## Publishing
1919

20-
1. Update the version number in pyproject.toml
20+
1. Update the CHANGELOG with description of changes
2121

22-
2. Push the changes to the main branch
22+
2. Update the version number in pyproject.toml
2323

24-
3. Publish to PyPi:
24+
3. Push the changes to the main branch
25+
26+
4. Publish to PyPi:
2527

2628
```shell
2729
export FLIT_USERNAME=__token__

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[project]
22
name = "openai-messages-token-helper"
33
description = "A helper library for estimating tokens used by messages sent through OpenAI Chat Completions API."
4-
version = "0.1.6"
4+
version = "0.1.7"
55
authors = [{name = "Pamela Fox"}]
66
requires-python = ">=3.9"
77
readme = "README.md"

src/openai_messages_token_helper/message_builder.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
ChatCompletionAssistantMessageParam,
88
ChatCompletionContentPartParam,
99
ChatCompletionMessageParam,
10+
ChatCompletionMessageToolCallParam,
1011
ChatCompletionNamedToolChoiceParam,
1112
ChatCompletionRole,
1213
ChatCompletionSystemMessageParam,
@@ -18,7 +19,9 @@
1819
from .model_helper import count_tokens_for_message, count_tokens_for_system_and_tools, get_token_limit
1920

2021

21-
def normalize_content(content: Union[str, Iterable[ChatCompletionContentPartParam]]):
22+
def normalize_content(content: Union[str, Iterable[ChatCompletionContentPartParam], None]):
23+
if content is None:
24+
return None
2225
if isinstance(content, str):
2326
return unicodedata.normalize("NFC", content)
2427
else:
@@ -51,9 +54,9 @@ def all_messages(self) -> list[ChatCompletionMessageParam]:
5154
def insert_message(
5255
self,
5356
role: ChatCompletionRole,
54-
content: Union[str, Iterable[ChatCompletionContentPartParam]],
57+
content: Union[str, Iterable[ChatCompletionContentPartParam], None],
5558
index: int = 0,
56-
tool_calls: Optional[list[ChatCompletionToolParam]] = None,
59+
tool_calls: Optional[Iterable[ChatCompletionMessageToolCallParam]] = None,
5760
tool_call_id: Optional[str] = None,
5861
):
5962
"""
@@ -116,8 +119,14 @@ def build_messages(
116119
for shot in reversed(few_shots):
117120
if shot["role"] is None or (shot.get("content") is None and shot.get("tool_calls") is None):
118121
raise ValueError("Few-shot messages must have role and either content or tool_calls")
122+
tool_call_id = shot.get("tool_call_id")
123+
if tool_call_id is not None and not isinstance(tool_call_id, str):
124+
raise ValueError("tool_call_id must be a string value")
125+
tool_calls = shot.get("tool_calls")
126+
if tool_calls is not None and not isinstance(tool_calls, Iterable):
127+
raise ValueError("tool_calls must be a list of tool calls")
119128
message_builder.insert_message(
120-
shot["role"], shot.get("content"), tool_calls=shot.get("tool_calls"), tool_call_id=shot.get("tool_call_id")
129+
shot["role"], shot.get("content"), tool_calls=tool_calls, tool_call_id=tool_call_id
121130
)
122131

123132
append_index = len(few_shots)

0 commit comments

Comments
 (0)