-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantic.py
More file actions
61 lines (46 loc) · 1.87 KB
/
semantic.py
File metadata and controls
61 lines (46 loc) · 1.87 KB
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
import json
import time
import numpy as np
from SimplerVectors_core import VectorDatabase
from SimplerLLM.tools.generic_loader import load_content
from SimplerLLM.language.embeddings import EmbeddingsLLM, EmbeddingsProvider
def fetch_apis(filepath):
text_file = load_content(filepath)
content = json.loads(text_file.content)
return [(api['name'], api['description']) for api in content]
def get_embeddings(texts):
try:
embeddings_instance = EmbeddingsLLM.create(provider=EmbeddingsProvider.OPENAI, model_name="text-embedding-3-small")
response = embeddings_instance.generate_embeddings(texts)
embeddings = np.array([item.embedding for item in response])
return embeddings
except Exception as e:
print("An error occurred:", e)
return np.array([])
def find_best_api(user_query, apis):
db = VectorDatabase('VDB')
api_descriptions = [api[1] for api in apis]
query_embedding = get_embeddings([user_query])
description_embeddings = get_embeddings(api_descriptions)
for idx, emb in enumerate(description_embeddings):
db.add_vector(emb, {"api": apis[idx][0], "description": apis[idx][1]}, normalize=True)
query_embedding = db.normalize_vector(query_embedding[0])
best_sim = db.top_cosine_similarity(query_embedding, top_n=1)
top_match = best_sim[0]
if top_match[1] > 0.3:
return best_sim[0]
else:
return None
# Input:
filepath = 'apis.json'
apis = fetch_apis(filepath)
user_query = input("Enter your inquiry: ")
start_time = time.time() # Start timer
result = find_best_api(user_query, apis)
if result != None:
print(f"\nThe best API to use is: {result[0]["api"]}\n")
print(f"Info about this result: {result}\n")
else:
print("\nNo suitable API found.\n")
end_time = time.time() # End timer
print(f"Execution time: {end_time - start_time} seconds")