Use the LLM to classify the problem type, ensuring the result is one of the valid types. Args: content: The query/problem to classify client: LLM client for making API calls model: Model identifier Returns: str: The problem type classification (
(content: str, client, model: str)
| 23 | logger = logging.getLogger(__name__) |
| 24 | |
| 25 | def classify_problem(content: str, client, model: str) -> str: |
| 26 | """ |
| 27 | Use the LLM to classify the problem type, ensuring the result is one of the valid types. |
| 28 | |
| 29 | Args: |
| 30 | content: The query/problem to classify |
| 31 | client: LLM client for making API calls |
| 32 | model: Model identifier |
| 33 | |
| 34 | Returns: |
| 35 | str: The problem type classification (always a valid type) |
| 36 | """ |
| 37 | # Format problem types as a comma-separated list |
| 38 | problem_types_str = ", ".join(VALID_PROBLEM_TYPES[:-1]) # Exclude the general_problem fallback |
| 39 | |
| 40 | try: |
| 41 | messages = [ |
| 42 | { |
| 43 | "role": "system", |
| 44 | "content": PROBLEM_CLASSIFICATION_PROMPT.format(problem_types=problem_types_str) |
| 45 | }, |
| 46 | { |
| 47 | "role": "user", |
| 48 | "content": ( |
| 49 | f"Classify the following problem into ONE of these types: {problem_types_str}\n\n" |
| 50 | f"Problem: {content}" |
| 51 | ) |
| 52 | } |
| 53 | ] |
| 54 | |
| 55 | response = client.chat.completions.create( |
| 56 | model=model, |
| 57 | messages=messages, |
| 58 | temperature=0.1, # Low temperature for more deterministic output |
| 59 | max_tokens=DEFAULT_MAX_TOKENS # Increased token limit for reasoning LLMs |
| 60 | ) |
| 61 | |
| 62 | # Extract final response and thinking content |
| 63 | raw_response = response.choices[0].message.content |
| 64 | final_response, thinking = extract_thinking(raw_response) |
| 65 | |
| 66 | # Clean and normalize the response |
| 67 | final_response = final_response.strip().lower() |
| 68 | |
| 69 | logger.debug(f"Problem classification - raw response: '{raw_response}'") |
| 70 | logger.debug(f"Problem classification - final response after removing thinking: '{final_response}'") |
| 71 | |
| 72 | # Find the exact match from our list of valid types |
| 73 | for valid_type in VALID_PROBLEM_TYPES: |
| 74 | if valid_type.lower() == final_response: |
| 75 | logger.info(f"Classified problem as '{valid_type}' (exact match)") |
| 76 | return valid_type |
| 77 | |
| 78 | # If no exact match, look for partial matches |
| 79 | for valid_type in VALID_PROBLEM_TYPES: |
| 80 | if valid_type.lower() in final_response: |
| 81 | logger.info(f"Classified problem as '{valid_type}' (partial match from '{final_response}')") |
| 82 | return valid_type |
no test coverage detected