File size: 2,359 Bytes
3fac7d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Semantic search implementation for educational standards."""

from __future__ import annotations

import json

from pinecone.exceptions import PineconeException

from src.pinecone_client import PineconeClient


def find_relevant_standards_impl(
    activity: str,
    max_results: int = 5,
    grade: str | None = None,
) -> str:
    """
    Implementation of semantic search over educational standards.

    Args:
        activity: Description of the learning activity
        max_results: Maximum number of standards to return (default: 5)
        grade: Optional grade level filter (e.g., "K", "01", "05", "09")

    Returns:
        JSON string with structured response containing matching standards
    """
    # Input validation
    if not activity or not activity.strip():
        return json.dumps(
            {
                "success": False,
                "results": [],
                "message": "Activity description cannot be empty",
                "error_type": "invalid_input",
            }
        )

    try:
        # Initialize client and perform search
        client = PineconeClient()
        results = client.search_standards(
            query_text=activity.strip(),
            top_k=max_results,
            grade=grade,
        )

        # Handle empty results
        if not results:
            return json.dumps(
                {
                    "success": False,
                    "results": [],
                    "message": "No matching standards found",
                    "error_type": "no_results",
                }
            )

        # Format successful results
        response = {
            "success": True,
            "results": results,
            "message": f"Found {len(results)} matching standards",
        }

        return json.dumps(response, indent=2)

    except PineconeException as e:
        return json.dumps(
            {
                "success": False,
                "results": [],
                "message": f"Pinecone API error: {str(e)}",
                "error_type": "api_error",
            }
        )
    except Exception as e:
        return json.dumps(
            {
                "success": False,
                "results": [],
                "message": f"Unexpected error: {str(e)}",
                "error_type": "api_error",
            }
        )