Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: log the count of consumed prompt and completion tokens consumed #22

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions textbook/dataset_gen/dataset_gen.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import threading
from concurrent.futures import ThreadPoolExecutor
import json
import os
Expand All @@ -14,9 +15,14 @@
from rich.progress import (
Progress,
TimeElapsedColumn,
TextColumn,
)
import hashlib

THREAD_LOCK = threading.Lock()
PROMPT_TOKENS_CNT = 0
COMPLETION_TOKENS_CNT = 0


class Exercise(BaseModel):
problem: str
Expand Down Expand Up @@ -78,12 +84,17 @@ def __init__(
self.model = model

def generate(self, prompt: str) -> Result:
global PROMPT_TOKENS_CNT
global COMPLETION_TOKENS_CNT
chat_completion = openai.ChatCompletion.create(
model=self.model,
messages=[{"role": "user", "content": prompt}],
max_tokens=250,
timeout=60,
)
with THREAD_LOCK:
PROMPT_TOKENS_CNT += chat_completion.usage.prompt_tokens
COMPLETION_TOKENS_CNT += chat_completion.usage.completion_tokens
result = Result(
prompt=prompt, output=chat_completion.choices[0].message.content
)
Expand Down Expand Up @@ -190,12 +201,24 @@ def mass_generation(
*Progress.get_default_columns(),
"•",
TimeElapsedColumn(),
TextColumn("completion: [bold green]{task.fields[completion_tokens]}"),
TextColumn("prompt: [bold green]{task.fields[prompt_tokens]}"),
) as progress:
with ThreadPoolExecutor(max_workers=pool_size) as executor:
progress_task = progress.add_task("[red]Generating...", total=len(prompts))
progress_task = progress.add_task(
"[red]Generating...",
total=len(prompts),
completion_tokens=0,
prompt_tokens=0,
)

def update_progress():
progress.update(progress_task, advance=1)
progress.update(
progress_task,
advance=1,
completion_tokens=COMPLETION_TOKENS_CNT,
prompt_tokens=PROMPT_TOKENS_CNT,
)

tasks = []

Expand Down
Loading