Spaces:
Running
Running
app.py for consuming hsmw serialized model
Browse files
app.py
CHANGED
@@ -1,16 +1,17 @@
|
|
1 |
-
|
2 |
import streamlit as st
|
3 |
import joblib
|
4 |
import numpy as np
|
5 |
-
import
|
6 |
import os
|
7 |
from openai import OpenAI
|
8 |
|
9 |
-
# Initialize OpenAI client using
|
10 |
client = OpenAI(api_key=os.getenv("POCJujitsu"))
|
11 |
|
12 |
-
# Load serialized
|
13 |
-
|
|
|
|
|
14 |
|
15 |
# Embed query using OpenAI embedding API
|
16 |
def embed_query(text):
|
@@ -20,23 +21,13 @@ def embed_query(text):
|
|
20 |
)
|
21 |
return np.array(response.data[0].embedding, dtype=np.float32).reshape(1, -1)
|
22 |
|
23 |
-
# Semantic search using
|
24 |
-
|
25 |
-
# Semantic search with fallback handling
|
26 |
-
|
27 |
-
# Semantic search using FAISS - strictly for older API with preallocated arrays
|
28 |
def search(query, k=3):
|
29 |
query_vec = embed_query(query).astype(np.float32)
|
30 |
-
|
31 |
-
# Preallocate arrays (required for FAISS IndexFlatL2 in older versions)
|
32 |
-
distances = np.empty((1, k), dtype=np.float32)
|
33 |
-
labels = np.empty((1, k), dtype=np.int64)
|
34 |
-
|
35 |
-
# Call FAISS with all required arguments
|
36 |
-
index.search(query_vec, k, distances, labels)
|
37 |
-
|
38 |
return [chunks[i] for i in labels[0]]
|
39 |
|
|
|
40 |
def chat_no_rag(question):
|
41 |
response = client.chat.completions.create(
|
42 |
model="gpt-3.5-turbo",
|
@@ -48,8 +39,12 @@ def chat_no_rag(question):
|
|
48 |
|
49 |
def chat_with_rag(question, context_chunks):
|
50 |
context = "\n".join(context_chunks)
|
51 |
-
prompt =
|
52 |
-
|
|
|
|
|
|
|
|
|
53 |
response = client.chat.completions.create(
|
54 |
model="gpt-3.5-turbo",
|
55 |
messages=[{"role": "user", "content": prompt}],
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import joblib
|
3 |
import numpy as np
|
4 |
+
import hnswlib
|
5 |
import os
|
6 |
from openai import OpenAI
|
7 |
|
8 |
+
# Initialize OpenAI client using secret from Hugging Face Spaces
|
9 |
client = OpenAI(api_key=os.getenv("POCJujitsu"))
|
10 |
|
11 |
+
# Load serialized HNSW index and document chunks
|
12 |
+
model_data = joblib.load("rag_model_hnsw.joblib")
|
13 |
+
chunks = model_data["chunks"]
|
14 |
+
index = model_data["index"]
|
15 |
|
16 |
# Embed query using OpenAI embedding API
|
17 |
def embed_query(text):
|
|
|
21 |
)
|
22 |
return np.array(response.data[0].embedding, dtype=np.float32).reshape(1, -1)
|
23 |
|
24 |
+
# Semantic search using HNSWlib
|
|
|
|
|
|
|
|
|
25 |
def search(query, k=3):
|
26 |
query_vec = embed_query(query).astype(np.float32)
|
27 |
+
labels, distances = index.knn_query(query_vec, k=k)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
return [chunks[i] for i in labels[0]]
|
29 |
|
30 |
+
# Chat modes
|
31 |
def chat_no_rag(question):
|
32 |
response = client.chat.completions.create(
|
33 |
model="gpt-3.5-turbo",
|
|
|
39 |
|
40 |
def chat_with_rag(question, context_chunks):
|
41 |
context = "\n".join(context_chunks)
|
42 |
+
prompt = (
|
43 |
+
"Usa el siguiente contexto como referencia para responder la pregunta. "
|
44 |
+
"Puedes complementar con tus propios conocimientos si es necesario.\n\n"
|
45 |
+
f"Contexto:\n{context}\n\n"
|
46 |
+
f"Pregunta: {question}\nRespuesta:"
|
47 |
+
)
|
48 |
response = client.chat.completions.create(
|
49 |
model="gpt-3.5-turbo",
|
50 |
messages=[{"role": "user", "content": prompt}],
|