-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRAG.py
More file actions
41 lines (34 loc) · 1.3 KB
/
RAG.py
File metadata and controls
41 lines (34 loc) · 1.3 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
import requests
### --------------- Funcion generadora de embeddings -------------------------------------------------------------
#
def search_query(client, query, search_api_key, search_service_endpoint, index_name, api_search_version):
# Generar el embedding del query
response = client.embeddings.create(
input=query,
model = "text-embedding-ada-002"
)
embeddings=response.model_dump()
query_embedding = embeddings['data'][0]['embedding']
# Construir consulta híbrida
payload = {
"search": query,
"select": "title, subtitle, content, category",
"queryLanguage": "en-us",
"vectorQueries": [
{
"kind": "vector",
"vector": query_embedding,
"fields": "contentVector",
"k": 3
}
],
"top": 10}
headers = {
"Content-Type": "application/json",
"api-key": search_api_key}
# Enviar consulta a Azure AI Search
url = f"{search_service_endpoint}/indexes/{index_name}/docs/search?api-version={api_search_version}"
response = requests.post(url, headers=headers, json=payload)
results = response.json()['value']
context = " ".join([doc["category"] + ", " + doc["title"] + ", " + doc["subtitle"] + ", " + doc["content"] for doc in results])
return context