Skip to content

Commit 81239a7

Browse files
committed
address comments'
1 parent 2249247 commit 81239a7

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

app/backend/approaches/chatreadretrieveread.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,29 +151,25 @@ def get_messages_from_history(self, prompt_override, follow_up_questions_prompt,
151151
break
152152
return messages
153153

154-
'''
155-
Source: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
156-
Adapted: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/chatgpt?pivots=programming-language-chat-completions#managing-conversations
157-
158-
Method takes in a single conversation and calculate prompt tokens
159-
for chat api
160-
161-
Keys role and content are accounted seperately.
162-
163-
Values of content are encoded by model type and calculated the length.
164-
165-
This gives close proximity of token length measurement used in gpt models
166-
167-
message = {"role":"assistant", "content":"how can I assist you?"}
168-
'''
169-
def num_tokens_from_messages(self, message: any, model: str):
154+
def num_tokens_from_messages(self, message: dict[str,str], model: str):
155+
"""
156+
Calculate the number of tokens required to encode a message.
157+
Args:
158+
message (any): The message to encode, represented as a dictionary.
159+
model (str): The name of the model to use for encoding.
160+
Returns:
161+
int: The total number of tokens required to encode the message.
162+
Example:
163+
message = {'role': 'user', 'name': 'John', 'content': 'Hello, how are you?'}
164+
model = 'gpt-3.5-turbo'
165+
num_tokens_from_messages(message, model)
166+
output: 11
167+
"""
170168
encoding = tiktoken.encoding_for_model(self.get_oai_chatmodel_tiktok(model))
171169
num_tokens = 0
172170
num_tokens += 2 # every message follows {role/name}\n{content}\n
173171
for key, value in message.items():
174172
num_tokens += len(encoding.encode(value))
175-
if key == "name": # if there's a name, the role is omitted
176-
num_tokens += -1 # role is always required and always 1 token
177173
return num_tokens
178174

179175
def get_oai_chatmodel_tiktok(self, aoaimodel: str):

0 commit comments

Comments
 (0)