-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate_vector_index.py
289 lines (227 loc) · 10.6 KB
/
create_vector_index.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import requests
import openai
import logging
import os
import faiss
import json
import numpy as np
from dotenv import load_dotenv
from markdownify import markdownify
load_dotenv()
# Set up logging
logging_level = os.getenv('LOG_LEVEL', 'INFO')
logger = logging.basicConfig(level=logging_level)
client = openai.OpenAI()
# Directory where to save vector index
VECTOR_DIRECTORY = "./vector-database"
# Get documentation from OBP
obp_base_url = "https://apisandbox.openbankproject.com"
obp_version = "v5.1.0"
# If the swagger path is overriden by an environment variable, use that
obp_swagger_url_from_env = os.getenv('OBP_SWAGGER_URL')
if obp_swagger_url_from_env:
logging.info(f"Swagger URL Overriden from environment variable OBP_SWAGGER_URL: {obp_swagger_url_from_env}")
swagger_url = f"{obp_base_url}{obp_swagger_url_from_env}"
else:
swagger_url = "{}/obp/v5.1.0/resource-docs/{}/swagger?locale=en_GB".format(obp_base_url, obp_version)
logging.info(f"Swagger URL not overriden, using default: {swagger_url}")
# Get the _static_ swagger docs, we may want to change this if we give this to a bank that has lots of dynamic endpoints
logging.info(f"Requesting swagger docs from {swagger_url}")
try:
swagger_response = requests.get(swagger_url)
except Exception as e:
logging.error(f"Error fetching swagger docs: {e}")
logging.error(e.traceback.format_exc())
if swagger_response.status_code != 200:
logging.error(f"Error fetching swagger docs: {swagger_response.text}")
logging.info("If the swagger endpoint is broken, try overriding it with a known working one by setting OBP_SWAGGER_URL\n NOT RECOMMENDED FOR PRODUCTION")
raise Exception(f"Error fetching swagger docs: {swagger_response.text}")
else:
logging.info("Swagger docs fetched successfully")
swagger_json = swagger_response.json()
# get the glossary from OBP
glossary_url = "{}/obp/{}/api/glossary".format(obp_base_url, obp_version)
logging.info(f"Requesting glossary from {glossary_url}")
try:
glossary_response = requests.get(glossary_url)
except Exception as e:
logging.error(f"Error fetching glossary: {e}")
logging.error(e.traceback.format_exc())
if glossary_response.status_code != 200:
logging.error(f"Error fetching glossary: {glossary_response.text}")
raise Exception(f"Error fetching glossary: {glossary_response.text}")
else:
logging.info("Glossary fetched successfully")
glossary_json = glossary_response.json()
def resolve_reference(ref, definitions, resolved={}):
"""
Resolves a $ref to its definition, avoiding circular references.
Parameters:
ref (str): The reference to be resolved.
definitions (dict): A dictionary containing the definitions.
resolved (dict, optional): A dictionary containing the resolved references. Defaults to an empty dictionary.
Returns:
dict: The resolved definition.
"""
ref_name = ref.split('/')[-1]
if ref_name in resolved:
return resolved[ref_name]
if ref_name in definitions.keys():
definition = definitions[ref_name]
else:
definition = {}
resolved[ref_name] = definition
properties = definition.get('properties', {})
resolved_properties = resolve_properties(properties, definitions, resolved)
return {**definition, 'properties': resolved_properties}
def resolve_properties(properties, definitions, resolved):
"""
Resolves nested references in properties, avoiding circular references.
"""
resolved_properties = {}
for prop_name, prop_details in properties.items():
if '$ref' in prop_details:
resolved_properties[prop_name] = resolve_reference(prop_details['$ref'], definitions, resolved)
elif prop_details.get('type') == 'array' and 'items' in prop_details and '$ref' in prop_details['items']:
resolved_properties[prop_name] = {
"type": "array",
"items": resolve_reference(prop_details['items']['$ref'], definitions, resolved)
}
else:
resolved_properties[prop_name] = prop_details
return resolved_properties
def parse_swagger(swagger_json):
"""
Parses a Swagger JSON file and extracts information about the endpoints.
Args:
swagger_json (dict): The Swagger JSON object.
Returns:
list: A list of dictionaries, where each dictionary represents an endpoint and its details.
"""
try:
paths = swagger_json['paths']
except KeyError:
raise KeyError("no 'paths' key found in swagger JSON, ")
definitions = swagger_json['definitions']
endpoints = []
for path, methods in paths.items():
for method, details in methods.items():
endpoint_info = {
'path': path,
'method': method,
'summary': details.get('summary'),
'description': markdownify(details.get('description', '')),
'responses': [],
'parameters': {}
}
if ('parameters' in details) & (details['parameters'] != []):
endpoint_info["parameters"] = {
"type": "object",
"properties": {},
"required": [],
}
for param in details['parameters']:
if param['in'] == 'body' and '$ref' in param['schema']:
ref = param['schema']['$ref']
definition = resolve_reference(ref, definitions)
endpoint_info['parameters']['required'].extend(definition.get('required', []))
endpoint_info['parameters']['properties'].update(
resolve_properties(definition.get('properties', {}), definitions, {})
)
elif param['in'] == 'body' and '$ref' not in param['schema']:
# Right now, if the parameter does not have a reference (i.e. something that points to a swagger definition) we skip it
endpoint_info["parameters"] = param["schema"]
elif param['in'] == 'path':
endpoint_info['parameters']['required'].append(param['name'])
endpoint_info['parameters']['properties'][param['name']] = {
"type": param['type'],
"in": "path",
"description": param.get('description', '')
}
elif param['in'] == 'query':
endpoint_info['parameters']['properties'][param['name']] = {
"type": param['type'],
"description": param.get('description', '')
}
if param.get('required', False):
endpoint_info['parameters']['required'].append(param['name'])
if 'responses' in details:
for code, response in details['responses'].items():
if "schema" in response.keys() and ("$ref" in response['schema']):
ref = response['schema']['$ref']
definition_name = ref.split('/')[-1]
definition = resolve_reference(ref, definitions)
response_resolved = {
"code": code,
"body": resolve_properties(definition.get('properties', {}), definitions, {})
}
endpoint_info["responses"].append(response_resolved)
endpoints.append(endpoint_info)
return endpoints
endpoints = parse_swagger(swagger_json)
def parse_glossary(glossary_json):
"""
Parses the glossary JSON and extracts the title and description of each glossary item.
Args:
glossary_json (dict): The glossary JSON containing the glossary items.
Returns:
list: A list of dictionaries, where each dictionary represents a parsed glossary item
with 'title' and 'description' keys.
"""
glossary_items = glossary_json['glossary_items']
parsed_items = []
for item in glossary_items:
title = item.get('title', 'No title')
description_info = item.get('description', {})
# Get markdown description or else return no description
description = description_info.get('markdown', 'No description')
# do not add descriptions if they are empty
if description == "":
continue
parsed_items.append({
'title': title,
'description': description
})
return parsed_items
glossary_items = parse_glossary(glossary_json)
# Create vector embeddings
def get_embeddings(texts):
response = client.embeddings.create(
model="text-embedding-3-small",
input=texts
)
return [e.embedding for e in response.data]
def create_and_save_embedding_faiss(formatted_texts: list, json_metadata: list, filename: str):
"""
Creates and saves text embeddings and metadata for a given list of texts.
Args:
formatted_texts (list): A formatted list of texts for creating embeddings.
json_metadata (list): A list of dictionaries to pass as JSON metadata. Each dictionary represents metadata for a text.
filename (str): A prefix to attach to the saved index and metadata files.
Returns:
None
Raises:
None
Example usage:
create_and_save_embedding_faiss(formatted_texts, json_metadata, filename)
"""
embeddings = get_embeddings(formatted_texts)
# Convert embeddings to a numpy array
embeddings_np = np.array(embeddings).astype('float32')
# Create a FAISS index
index = faiss.IndexFlatL2(embeddings_np.shape[1]) # L2 distance index
index.add(embeddings_np)
# Optionally, save the index to disk for later use
faiss.write_index(index, f"{filename}_index.faiss")
# Save metadata for retrieval
with open(f"{filename}_metadata.json", 'w') as f:
json.dump(json_metadata, f)
glossary_texts = [f"{g['title']} - {g['description']}" for g in glossary_items]
if not os.path.isdir(VECTOR_DIRECTORY):
try:
os.mkdir(VECTOR_DIRECTORY)
except Exception as err:
print(f"Error creating directory {VECTOR_DIRECTORY}: {err}")
create_and_save_embedding_faiss(glossary_texts, glossary_items, os.path.join(VECTOR_DIRECTORY, "glossary"))
endpoint_texts = [f"{e['method'].upper()} {e['path']} - {e['description']}" for e in endpoints]
create_and_save_embedding_faiss(endpoint_texts, endpoints, os.path.join(VECTOR_DIRECTORY, "endpoint"))