|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +from azure.search.documents.indexes.models import ( |
| 5 | + SearchFieldDataType, |
| 6 | + SearchField, |
| 7 | + SearchableField, |
| 8 | + SemanticField, |
| 9 | + SemanticPrioritizedFields, |
| 10 | + SemanticConfiguration, |
| 11 | + SemanticSearch, |
| 12 | + SimpleField, |
| 13 | + ComplexField, |
| 14 | +) |
| 15 | +from ai_search import AISearch |
| 16 | +from environment import ( |
| 17 | + IndexerType, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +class Text2SqlQueryCacheAISearch(AISearch): |
| 22 | + """This class is used to deploy the sql index.""" |
| 23 | + |
| 24 | + def __init__(self, suffix: str | None = None, rebuild: bool | None = False): |
| 25 | + """Initialize the Text2SqlAISearch class. This class implements the deployment of the sql index. |
| 26 | +
|
| 27 | + Args: |
| 28 | + suffix (str, optional): The suffix for the indexer. Defaults to None. If an suffix is provided, it is assumed to be a test indexer. |
| 29 | + rebuild (bool, optional): Whether to rebuild the index. Defaults to False. |
| 30 | + """ |
| 31 | + self.indexer_type = IndexerType.TEXT_2_SQL_QUERY_CACHE |
| 32 | + super().__init__(suffix, rebuild) |
| 33 | + |
| 34 | + def get_index_fields(self) -> list[SearchableField]: |
| 35 | + """This function returns the index fields for sql index. |
| 36 | +
|
| 37 | + Returns: |
| 38 | + list[SearchableField]: The index fields for sql index""" |
| 39 | + |
| 40 | + fields = [ |
| 41 | + SimpleField( |
| 42 | + name="Id", type=SearchFieldDataType.String, key=True, retrievable=False |
| 43 | + ), |
| 44 | + SearchableField( |
| 45 | + name="Question", |
| 46 | + type=SearchFieldDataType.String, |
| 47 | + analyzer_name="keyword", |
| 48 | + ), |
| 49 | + SearchField( |
| 50 | + name="QuestionEmbedding", |
| 51 | + type=SearchFieldDataType.Collection(SearchFieldDataType.Single), |
| 52 | + vector_search_dimensions=self.environment.open_ai_embedding_dimensions, |
| 53 | + vector_search_profile_name=self.vector_search_profile_name, |
| 54 | + ), |
| 55 | + SearchableField( |
| 56 | + name="Query", type=SearchFieldDataType.String, filterable=True |
| 57 | + ), |
| 58 | + SearchField( |
| 59 | + name="QueryEmbedding", |
| 60 | + type=SearchFieldDataType.Collection(SearchFieldDataType.Single), |
| 61 | + vector_search_dimensions=self.environment.open_ai_embedding_dimensions, |
| 62 | + vector_search_profile_name=self.vector_search_profile_name, |
| 63 | + ), |
| 64 | + ComplexField( |
| 65 | + name="Schemas", |
| 66 | + collection=True, |
| 67 | + fields=[ |
| 68 | + SearchableField( |
| 69 | + name="Entity", |
| 70 | + type=SearchFieldDataType.String, |
| 71 | + filterable=True, |
| 72 | + ), |
| 73 | + ComplexField( |
| 74 | + name="Columns", |
| 75 | + collection=True, |
| 76 | + fields=[ |
| 77 | + SearchableField( |
| 78 | + name="Name", type=SearchFieldDataType.String |
| 79 | + ), |
| 80 | + SearchableField( |
| 81 | + name="Definition", type=SearchFieldDataType.String |
| 82 | + ), |
| 83 | + SearchableField( |
| 84 | + name="Type", type=SearchFieldDataType.String |
| 85 | + ), |
| 86 | + SimpleField( |
| 87 | + name="AllowedValues", |
| 88 | + type=SearchFieldDataType.String, |
| 89 | + collection=True, |
| 90 | + ), |
| 91 | + SimpleField( |
| 92 | + name="SampleValues", |
| 93 | + type=SearchFieldDataType.String, |
| 94 | + collection=True, |
| 95 | + ), |
| 96 | + ], |
| 97 | + ), |
| 98 | + ], |
| 99 | + ), |
| 100 | + ] |
| 101 | + |
| 102 | + return fields |
| 103 | + |
| 104 | + def get_semantic_search(self) -> SemanticSearch: |
| 105 | + """This function returns the semantic search configuration for sql index |
| 106 | +
|
| 107 | + Returns: |
| 108 | + SemanticSearch: The semantic search configuration""" |
| 109 | + |
| 110 | + semantic_config = SemanticConfiguration( |
| 111 | + name=self.semantic_config_name, |
| 112 | + prioritized_fields=SemanticPrioritizedFields( |
| 113 | + title_field=SemanticField(field_name="Question"), |
| 114 | + keywords_fields=[ |
| 115 | + SemanticField(field_name="Query"), |
| 116 | + ], |
| 117 | + ), |
| 118 | + ) |
| 119 | + |
| 120 | + semantic_search = SemanticSearch(configurations=[semantic_config]) |
| 121 | + |
| 122 | + return semantic_search |
0 commit comments