Skip to content

Commit

Permalink
Keep track of prompt and completion tokens separately
Browse files Browse the repository at this point in the history
  • Loading branch information
rlouf committed Dec 4, 2023
1 parent 8a1430f commit 9ce5092
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions outlines/models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,14 @@ def __init__(
self.client = openai.AsyncOpenAI(
api_key=api_key, max_retries=max_retries, timeout=timeout
)
self.total_tokens = 0
self.last_tokens: int
self.system_prompt = system_prompt

# We count the total number of prompt and generated tokens as returned
# by the OpenAI API, summed over all the requests performed with this
# model instance.
self.prompt_tokens = 0
self.completion_tokens = 0

def __call__(
self,
prompt: Union[str, List[str]],
Expand Down Expand Up @@ -170,10 +174,12 @@ def __call__(
)
)
if "gpt-" in self.config.model:
response, self.last_tokens = generate_chat(
response, usage = generate_chat(
prompt, self.system_prompt, self.client, config
)
self.total_tokens += self.last_tokens
self.prompt_tokens += usage.prompt_tokens
self.completion_tokens += usage.completion_tokens

return response

def generate_choice(
Expand Down Expand Up @@ -227,10 +233,11 @@ def generate_choice(

config = replace(config, logit_bias=mask, max_tokens=max_tokens_left)

response, self.last_tokens = generate_chat(
response, usage = generate_chat(
prompt, self.system_prompt, self.client, config
)
self.total_tokens += self.last_tokens
self.completion_tokens += usage.completion_tokens
self.prompt_tokens += usage.completion_tokens

encoded_response = tokenizer.encode(response)

Expand Down

0 comments on commit 9ce5092

Please sign in to comment.