-
Notifications
You must be signed in to change notification settings - Fork 13
/
init_search_1.py
182 lines (153 loc) · 5.52 KB
/
init_search_1.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""
Initializes an Azure Cognitive Search index with our custom data, using vector search
and semantic ranking.
To run this code, you must already have a "Cognitive Search" and an "OpenAI"
resource created in Azure.
"""
import os
import openai
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import (
HnswParameters,
HnswVectorSearchAlgorithmConfiguration,
PrioritizedFields,
SearchableField,
SearchField,
SearchFieldDataType,
SearchIndex,
SemanticConfiguration,
SemanticField,
SemanticSettings,
SimpleField,
VectorSearch,
)
from dotenv import load_dotenv
from langchain.document_loaders import DirectoryLoader, UnstructuredMarkdownLoader
from langchain.text_splitter import Language, RecursiveCharacterTextSplitter
# Config for Azure Search.
AZURE_SEARCH_ENDPOINT = os.getenv("AZURE_SEARCH_ENDPOINT")
AZURE_SEARCH_KEY = os.getenv("AZURE_SEARCH_KEY")
AZURE_SEARCH_INDEX_NAME = "products-index-1"
# Config for Azure OpenAI.
AZURE_OPENAI_API_TYPE = "azure"
AZURE_OPENAI_API_BASE = os.getenv("AZURE_OPENAI_API_BASE")
AZURE_OPENAI_API_VERSION = "2023-03-15-preview"
AZURE_OPENAI_API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
AZURE_OPENAI_EMBEDDING_DEPLOYMENT = os.getenv("AZURE_OPENAI_EMBEDDING_DEPLOYMENT")
DATA_DIR = "data/"
def load_and_split_documents() -> list[dict]:
"""
Loads our documents from disc and split them into chunks.
Returns a list of dictionaries.
"""
# Load our data.
loader = DirectoryLoader(
DATA_DIR, loader_cls=UnstructuredMarkdownLoader, show_progress=True
)
docs = loader.load()
# Split our documents.
splitter = RecursiveCharacterTextSplitter.from_language(
language=Language.MARKDOWN, chunk_size=6000, chunk_overlap=100
)
split_docs = splitter.split_documents(docs)
# Convert our LangChain Documents to a list of dictionaries.
final_docs = []
for i, doc in enumerate(split_docs):
doc_dict = {
"id": str(i),
"content": doc.page_content,
"sourcefile": os.path.basename(doc.metadata["source"]),
}
final_docs.append(doc_dict)
return final_docs
def get_index(name: str) -> SearchIndex:
"""
Returns an Azure Cognitive Search index with the given name.
"""
# The fields we want to index. The "embedding" field is a vector field that will
# be used for vector search.
fields = [
SimpleField(name="id", type=SearchFieldDataType.String, key=True),
SimpleField(name="sourcefile", type=SearchFieldDataType.String),
SearchableField(name="content", type=SearchFieldDataType.String),
SearchField(
name="embedding",
type=SearchFieldDataType.Collection(SearchFieldDataType.Single),
# Size of the vector created by the text-embedding-ada-002 model.
vector_search_dimensions=1536,
vector_search_configuration="default",
),
]
# The "content" field should be prioritized for semantic ranking.
semantic_settings = SemanticSettings(
configurations=[
SemanticConfiguration(
name="default",
prioritized_fields=PrioritizedFields(
title_field=None,
prioritized_content_fields=[SemanticField(field_name="content")],
),
)
]
)
# For vector search, we want to use the HNSW (Hierarchical Navigable Small World)
# algorithm (a type of approximate nearest neighbor search algorithm) with cosine
# distance.
vector_search = VectorSearch(
algorithm_configurations=[
HnswVectorSearchAlgorithmConfiguration(
name="default",
kind="hnsw",
parameters=HnswParameters(metric="cosine"),
)
]
)
# Create the search index.
index = SearchIndex(
name=name,
fields=fields,
semantic_settings=semantic_settings,
vector_search=vector_search,
)
return index
def initialize(search_index_client: SearchIndexClient):
"""
Initializes an Azure Cognitive Search index with our custom data, using vector
search.
"""
# Load our data.
docs = load_and_split_documents()
for doc in docs:
doc["embedding"] = openai.Embedding.create(
engine=AZURE_OPENAI_EMBEDDING_DEPLOYMENT, input=doc["content"]
)["data"][0]["embedding"]
# Create an Azure Cognitive Search index.
index = get_index(AZURE_SEARCH_INDEX_NAME)
search_index_client.create_or_update_index(index)
# Upload our data to the index.
search_client = SearchClient(
endpoint=AZURE_SEARCH_ENDPOINT,
index_name=AZURE_SEARCH_INDEX_NAME,
credential=AzureKeyCredential(AZURE_SEARCH_KEY),
)
search_client.upload_documents(docs)
def delete(search_index_client: SearchIndexClient):
"""
Deletes the Azure Cognitive Search index.
"""
search_index_client.delete_index(AZURE_SEARCH_INDEX_NAME)
def main():
load_dotenv()
openai.api_type = AZURE_OPENAI_API_TYPE
openai.api_base = AZURE_OPENAI_API_BASE
openai.api_version = AZURE_OPENAI_API_VERSION
openai.api_key = AZURE_OPENAI_API_KEY
search_index_client = SearchIndexClient(
AZURE_SEARCH_ENDPOINT, AzureKeyCredential(AZURE_SEARCH_KEY)
)
initialize(search_index_client)
# delete(search_index_client)
if __name__ == "__main__":
main()