Fix fallback
Some checks failed
Run Tox Check / tox-py312 (pull_request) Successful in 20s
Run Tox Check / tox-pep8 (pull_request) Failing after 15s

This commit is contained in:
2026-03-03 11:27:37 +00:00
parent 553e3d7e19
commit 99f3ea4663

View File

@ -17,7 +17,6 @@ import argparse
import base64 import base64
import logging import logging
import pathlib import pathlib
import re
import requests import requests
import subprocess import subprocess
import sys import sys
@ -32,16 +31,8 @@ data = otc_metadata.services.Services()
api_session = requests.Session() api_session = requests.Session()
def remove_thinking_content(text):
"""Remove thinking process content between thinking markers."""
# Remove everything between <think> and </think> markers
text = re.sub(r'(?is)<think>.*?</think>', '', text, flags=re.DOTALL | re.IGNORECASE)
return text.strip()
def extract_description(result): def extract_description(result):
"""Extract description from API response and clean it.""" """Extract description from API response and clean it."""
# Chat completion format: choices[0].message.content
if "choices" in result and len(result["choices"]) > 0: if "choices" in result and len(result["choices"]) > 0:
message = result["choices"][0].get("message", {}) message = result["choices"][0].get("message", {})
description = message.get("content", "") description = message.get("content", "")
@ -52,7 +43,6 @@ def extract_description(result):
else: else:
return None return None
description = remove_thinking_content(description)
description = description.strip() description = description.strip()
if not description or description.isspace(): if not description or description.isspace():
@ -125,16 +115,20 @@ def generate_description_with_llm(text, service_title, llm_api_url, model_name,
except (KeyError, ValueError, IndexError) as e: except (KeyError, ValueError, IndexError) as e:
logging.warning(f"Attempt {attempt + 1}: LLM API response parsing failed: {e}. Retrying...") logging.warning(f"Attempt {attempt + 1}: LLM API response parsing failed: {e}. Retrying...")
# After all retries failed, use fallback # After all retries failed, use fallback - extract first headline
logging.warning("All LLM API retries failed. Using fallback description.") logging.warning("All LLM API retries failed. Using fallback description from first headline.")
lines = text.split("\n") lines = text.split("\n")
for line in lines: for i, line in enumerate(lines):
line = line.strip() line_stripped = line.strip()
if line and not line.startswith("-") and not line.startswith("#"): if line_stripped and not line_stripped.startswith("-") and not line_stripped.startswith("#"):
first_sentence = line.split(".")[0] + "." # Check if next line is a headline underline (=== or ---)
if len(first_sentence) > 160: if i + 1 < len(lines):
first_sentence = first_sentence[:157] + "..." next_line = lines[i + 1].strip()
return first_sentence if next_line and all(c in "=-" for c in next_line):
description = line_stripped
if len(description) > 160:
description = description[:157] + "..."
return description
return f"{service_title} documentation" return f"{service_title} documentation"