gen_lesson_figures prints prompt/completion tokens

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-07 16:34:22 +03:30
parent ce629e32e4
commit 25b3d24eb4

View File

@@ -54,7 +54,7 @@ class Command(BaseCommand):
raise CommandError('درسی پیدا نشد.') raise CommandError('درسی پیدا نشد.')
model = chat_model_for('') model = chat_model_for('')
client = clients.get_openai_client() client = clients.get_openai_client()
spent = 0 spent = prompt_t = out_t = 0
for lesson in lessons: for lesson in lessons:
body = (f'عنوان: {lesson.title}\nتوضیح: {lesson.intro_text}\nمثال: {lesson.example}\n' body = (f'عنوان: {lesson.title}\nتوضیح: {lesson.intro_text}\nمثال: {lesson.example}\n'
+ '\n'.join(f'قدم {i}: {s}' for i, s in enumerate(lesson.steps or []))) + '\n'.join(f'قدم {i}: {s}' for i, s in enumerate(lesson.steps or [])))
@@ -64,6 +64,8 @@ class Command(BaseCommand):
{'role': 'user', 'content': body}]) {'role': 'user', 'content': body}])
u = getattr(r, 'usage', None) u = getattr(r, 'usage', None)
spent += (getattr(u, 'total_tokens', 0) or 0) spent += (getattr(u, 'total_tokens', 0) or 0)
prompt_t += (getattr(u, 'prompt_tokens', 0) or 0)
out_t += (getattr(u, 'completion_tokens', 0) or 0)
raw = re.sub(r'^```(?:json)?|```$', '', (r.choices[0].message.content or '').strip()).strip() raw = re.sub(r'^```(?:json)?|```$', '', (r.choices[0].message.content or '').strip()).strip()
try: try:
start = raw.index('[') start = raw.index('[')
@@ -90,4 +92,4 @@ class Command(BaseCommand):
if figures and not o['dry_run']: if figures and not o['dry_run']:
lesson.figures = figures lesson.figures = figures
lesson.save(update_fields=['figures']) lesson.save(update_fields=['figures'])
self.stdout.write(f'توکنِ مصرفی: {spent}') self.stdout.write(f'توکنِ مصرفی: {spent} (ورودی {prompt_t}، خروجی {out_t})')