def _evaluate_with_llm(
        self, query: str, results: List[Dict[str, Any]], expected_content_keywords: List[str]
    ) -> Dict[str, Any]:
        """Use OpenAI's API to evaluate search results.

        Args:
            query: The search query
            results: List of search results
            expected_content_keywords: Keywords that should appear in good results

        Returns:
            Dict with evaluation metrics
        """
        try:
            # Check if OpenAI module is available
            if 'openai' not in globals():
                logger.error("OpenAI package not installed, falling back to simple evaluation")
                return self._simple_evaluation(query, results, expected_content_keywords)

            # Set up OpenAI client
            client = openai.OpenAI(api_key=self.llm_api_key)

            # Format results for better readability in the prompt
            formatted_results = json.dumps(results, indent=2)
            keywords_str = ", ".join(expected_content_keywords) if expected_content_keywords else "None specified"

            # Create the evaluation prompt
            prompt = f"""
            Evaluate the quality of these search results for the query: "{query}".

            Search Results:
            {formatted_results}

            Expected keywords that should appear in good results: {keywords_str}

            Please evaluate the results on the following metrics:
            1. Relevance (0-1): How relevant are the results to the query?
            2. Completeness (0-1): How well do the results cover the expected keywords and topics?
            3. Diversity (0-1): How diverse are the results in covering different aspects?
            4. Relevant Results Count: How many results are actually relevant to the query?

            Please provide your evaluation in JSON format with these fields:
            - relevance: float (0-1)
            - completeness: float (0-1)
            - diversity: float (0-1)
            - relevant_results: int
            - score: float (0-1, weighted average of other metrics)
            - feedback: string (brief explanation of the evaluation)
            """

            # Call the OpenAI API
            response = client.chat.completions.create(
                model="gpt-4o",  # Use an appropriate model
                messages=[{"role": "user", "content": prompt}],
                temperature=0.1,  # Low temperature for more consistent evaluations
                response_format={"type": "json_object"}
            )

            # Parse the response
            evaluation = json.loads(response.choices[0].message.content)

            # Ensure all required fields are present
            required_fields = ["relevance", "completeness", "diversity", "relevant_results", "score", "feedback"]
            for field in required_fields:
                if field not in evaluation:
                    logger.warning(f"Field {field} missing from LLM evaluation, adding default value")
                    if field == "relevant_results":
                        evaluation[field] = 0
                    elif field == "feedback":
                        evaluation[field] = "No feedback provided by LLM"
                    else:
                        evaluation[field] = 0.0  # Default value for missing metrics

            return evaluation

        except Exception as e:
            logger.error(f"Error calling OpenAI API: {e}")
            # Fall back to simple evaluation
            return self._simple_evaluation(query, results, expected_content_keywords)
Use OpenAI's API to evaluate search results.

        Args:
            query: The search query
            results: List of search results
            expected_content_keywords: Keywords that should appear in good results

        Returns:
            Dict with evaluation metrics
        
backend/tests/helpers/llm_judge.py