async def search_with_completion(
        self,
        db: AsyncSession,
        query: str,
        readable_id: str,
        current_user: schemas.User,
        response_type: ResponseType = ResponseType.RAW,
    ) -> schemas.SearchResponse:
        """Search and optionally generate AI completion for results.

        Args:
            db: The database session
            query: The search query text
            readable_id: Readable ID of the collection to search in
            current_user: The current user
            response_type: Type of response (raw results or AI completion)

        Returns:
            dict: A dictionary containing search results or AI completion
        """
        # Lazy import to avoid circular dependency
        from airweave.core.chat_service import chat_service

        # Note: search() now returns cleaned results
        results = await self.search(
            db=db,
            query=query,
            readable_id=readable_id,
            current_user=current_user,
        )

        if response_type == ResponseType.RAW:
            return schemas.SearchResponse(
                results=results, response_type=response_type, status=SearchStatus.SUCCESS
            )

        # If no results found, return a more specific message
        if not results:
            return schemas.SearchResponse(
                results=[],
                completion=(
                    "I couldn't find any relevant information for that query. "
                    "Try asking about something in your data collection."
                ),
                response_type=ResponseType.COMPLETION,
                status=SearchStatus.NO_RESULTS,
            )

        # For low-quality results (where scores are low), add this check:
        has_relevant_results = any(result.get("score", 0) > 0.25 for result in results)
        if not has_relevant_results:
            return schemas.SearchResponse(
                results=results,
                completion=(
                    "Your query didn't match anything meaningful in the database. "
                    "Please try a different question related to your data."
                ),
                response_type=ResponseType.COMPLETION,
                status=SearchStatus.NO_RELEVANT_RESULTS,
            )

        # For completion generation, we need full content (not truncated)
        # So fetch results again but with less aggressive cleaning
        raw_results = await self._get_raw_search_results(
            db=db,
            query=query,
            readable_id=readable_id,
            current_user=current_user,
        )

        # Clean raw results but don't truncate content
        context_results = self._clean_search_results(raw_results, for_display=False)

        # Modify the context prompt to be more specific about only answering based on context
        messages = [
            {
                "role": "system",
                "content": chat_service.CONTEXT_PROMPT.format(
                    context=str(context_results),
                    additional_instruction=(
                        "If the provided context doesn't contain information to answer "
                        "the query directly, respond with 'I don't have enough information to "
                        "answer that question based on the available data.'"
                    ),
                ),
            },
            {"role": "user", "content": query},
        ]

        # Generate completion
        model = chat_service.DEFAULT_MODEL
        model_settings = chat_service.DEFAULT_MODEL_SETTINGS.copy()

        # Remove streaming setting if present
        if "stream" in model_settings:
            model_settings.pop("stream")

        try:
            response = await chat_service.client.chat.completions.create(
                model=model, messages=messages, **model_settings
            )

            completion = (
                response.choices[0].message.content
                if response.choices
                else "Unable to generate completion."
            )
        except Exception as e:
            completion = f"Error generating completion: {str(e)}"

        # Return the display-friendly cleaned results along with completion
        return schemas.SearchResponse(
            results=results,
            completion=completion,
            response_type=response_type,
            status=SearchStatus.SUCCESS,
        )
Search and optionally generate AI completion for results.

        Args:
            db: The database session
            query: The search query text
            readable_id: Readable ID of the collection to search in
            current_user: The current user
            response_type: Type of response (raw results or AI completion)

        Returns:
            dict: A dictionary containing search results or AI completion
        
backend/airweave/core/search_service.py