/
Paff
/
declarant
Обзор
Документация
Войти
/
Paff
/
declarant
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
test_deepseek_fix.py
102 строки
4 KB
1pash1985-gif
Unified doc list UI + AI prompt for FORM_A
05 авг 2026, 10:09
05 авг 2026, 10:09
e439f08
Код
Авторство
О чём код?
"""Test fixes for deepseek-v4-flash reasoning token exhaustion.""" import sys, os, re, json, logging sys.path.insert(0, '/app/web') os.chdir('/app/web') logging.basicConfig(level=logging.INFO, format='%(name)s - %(levelname)s - %(message)s') from ai_parser import extract_text_from_excel, client, DEEPSEEK_MODEL, INVOICE_SYSTEM_PROMPT, _get_corrections_context xls_path = '/app/web/uploads/reports/2026-07-27/444/sources/invoice_444_1.xls' raw_text = extract_text_from_excel(xls_path) # Fix 1: Strip URLs (they waste tokens and aren't useful for invoice parsing) text_no_urls = re.sub(r'https?://\S+', '', raw_text) # Clean up multiple spaces left behind text_no_urls = re.sub(r' +', ' ', text_no_urls) text_no_urls = re.sub(r' \| $', '', text_no_urls) # Remove trailing " | " on lines print(f'Raw text: {len(raw_text)} chars') print(f'URL-stripped text: {len(text_no_urls)} chars ({len(text_no_urls) - len(raw_text)} chars saved)') url_pattern = r'https?://\S+' print(f'URLs removed: {len(re.findall(url_pattern, raw_text))}') print('---') system_prompt = INVOICE_SYSTEM_PROMPT + _get_corrections_context() # Test 1: max_tokens=16384 with original text print('=== TEST 1: max_tokens=16384, original text ===') try: response = client.chat.completions.create( model=DEEPSEEK_MODEL, messages=[ {'role': 'system', 'content': system_prompt}, {'role': 'user', 'content': f'Вот текст инвойса:\n\n{raw_text}'}, ], temperature=0.1, max_tokens=16384, response_format={'type': 'json_object'}, ) fr = response.choices[0].finish_reason cl = len(response.choices[0].message.content or '') ct = response.usage.completion_tokens if response.usage else 0 rt = getattr(response.usage.completion_tokens_details, 'reasoning_tokens', '?') if response.usage and hasattr(response.usage, 'completion_tokens_details') else '?' print(f'finish_reason={fr}, content_len={cl}, completion_tokens={ct}, reasoning_tokens={rt}') if cl > 0: print(f'Content preview: {(response.choices[0].message.content or "")[:200]}') except Exception as e: print(f'ERROR: {e}') print() # Test 2: max_tokens=16384 with URL-stripped text print('=== TEST 2: max_tokens=16384, URL-stripped text ===') try: response = client.chat.completions.create( model=DEEPSEEK_MODEL, messages=[ {'role': 'system', 'content': system_prompt}, {'role': 'user', 'content': f'Вот текст инвойса:\n\n{text_no_urls}'}, ], temperature=0.1, max_tokens=16384, response_format={'type': 'json_object'}, ) fr = response.choices[0].finish_reason cl = len(response.choices[0].message.content or '') ct = response.usage.completion_tokens if response.usage else 0 rt = getattr(response.usage.completion_tokens_details, 'reasoning_tokens', '?') if response.usage and hasattr(response.usage, 'completion_tokens_details') else '?' print(f'finish_reason={fr}, content_len={cl}, completion_tokens={ct}, reasoning_tokens={rt}') if cl > 0: print(f'Content preview: {(response.choices[0].message.content or "")[:200]}') except Exception as e: print(f'ERROR: {e}') print() # Test 3: max_tokens=32768 with URL-stripped text print('=== TEST 3: max_tokens=32768, URL-stripped text ===') try: response = client.chat.completions.create( model=DEEPSEEK_MODEL, messages=[ {'role': 'system', 'content': system_prompt}, {'role': 'user', 'content': f'Вот текст инвойса:\n\n{text_no_urls}'}, ], temperature=0.1, max_tokens=32768, response_format={'type': 'json_object'}, ) fr = response.choices[0].finish_reason cl = len(response.choices[0].message.content or '') ct = response.usage.completion_tokens if response.usage else 0 rt = getattr(response.usage.completion_tokens_details, 'reasoning_tokens', '?') if response.usage and hasattr(response.usage, 'completion_tokens_details') else '?' print(f'finish_reason={fr}, content_len={cl}, completion_tokens={ct}, reasoning_tokens={rt}') if cl > 0: content = response.choices[0].message.content or '' print(f'Content preview: {content[:300]}') parsed = json.loads(content) if content else None if parsed: print(f'Parsed OK! Supplier: {parsed.get("supplier_name", "N/A")}') except Exception as e: print(f'ERROR: {e}')