Spaces:
Sleeping
Sleeping
Fix OpenAIEmbeddings initialization and Qdrant query methods for Hugging Face compatibility
Browse files- streamlit_app.py +34 -15
streamlit_app.py
CHANGED
|
@@ -114,21 +114,27 @@ def get_embedding_model():
|
|
| 114 |
from langchain_openai import OpenAIEmbeddings
|
| 115 |
import os
|
| 116 |
|
|
|
|
| 117 |
try:
|
| 118 |
-
# First try the standard approach
|
| 119 |
return OpenAIEmbeddings(model="text-embedding-3-small")
|
| 120 |
except Exception as e:
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
return OpenAIEmbeddings(
|
| 124 |
model="text-embedding-3-small",
|
| 125 |
openai_api_key=os.environ.get("OPENAI_API_KEY"),
|
| 126 |
-
|
| 127 |
-
client_kwargs={"proxies": None, "timeout": 60}
|
| 128 |
)
|
| 129 |
-
else:
|
| 130 |
-
# Re-raise any other exceptions
|
| 131 |
-
raise
|
| 132 |
|
| 133 |
@st.cache_resource
|
| 134 |
def setup_qdrant_client():
|
|
@@ -172,15 +178,28 @@ def retrieve_documents(query, k=5):
|
|
| 172 |
query_vector=query_embedding,
|
| 173 |
limit=k
|
| 174 |
)
|
|
|
|
| 175 |
except Exception as e:
|
| 176 |
print(f"Error with query_points method: {str(e)}")
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
# Convert results to documents
|
| 186 |
documents = []
|
|
|
|
| 114 |
from langchain_openai import OpenAIEmbeddings
|
| 115 |
import os
|
| 116 |
|
| 117 |
+
# The simplest initialization possible - just model name
|
| 118 |
try:
|
|
|
|
| 119 |
return OpenAIEmbeddings(model="text-embedding-3-small")
|
| 120 |
except Exception as e:
|
| 121 |
+
print(f"OpenAIEmbeddings initialization error: {str(e)}")
|
| 122 |
+
|
| 123 |
+
# Try with just API key, no other parameters
|
| 124 |
+
try:
|
| 125 |
+
return OpenAIEmbeddings(
|
| 126 |
+
model="text-embedding-3-small",
|
| 127 |
+
openai_api_key=os.environ.get("OPENAI_API_KEY")
|
| 128 |
+
)
|
| 129 |
+
except Exception as e2:
|
| 130 |
+
print(f"Second attempt failed: {str(e2)}")
|
| 131 |
+
|
| 132 |
+
# Last resort - most minimal initialization
|
| 133 |
return OpenAIEmbeddings(
|
| 134 |
model="text-embedding-3-small",
|
| 135 |
openai_api_key=os.environ.get("OPENAI_API_KEY"),
|
| 136 |
+
client=None # Let the class create its own client
|
|
|
|
| 137 |
)
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
@st.cache_resource
|
| 140 |
def setup_qdrant_client():
|
|
|
|
| 178 |
query_vector=query_embedding,
|
| 179 |
limit=k
|
| 180 |
)
|
| 181 |
+
print("Successfully used query_points method")
|
| 182 |
except Exception as e:
|
| 183 |
print(f"Error with query_points method: {str(e)}")
|
| 184 |
+
try:
|
| 185 |
+
# Try a different parameter format
|
| 186 |
+
results = client.query_points(
|
| 187 |
+
collection_name="kohavi_ab_testing_pdf_collection",
|
| 188 |
+
query_vector=query_embedding,
|
| 189 |
+
with_payload=True,
|
| 190 |
+
with_vectors=False,
|
| 191 |
+
limit=k
|
| 192 |
+
)
|
| 193 |
+
print("Successfully used query_points with alternate parameters")
|
| 194 |
+
except Exception as e2:
|
| 195 |
+
print(f"Error with alternate query_points: {str(e2)}")
|
| 196 |
+
# Fall back to the deprecated method as last resort
|
| 197 |
+
results = client.search(
|
| 198 |
+
collection_name="kohavi_ab_testing_pdf_collection",
|
| 199 |
+
query_vector=query_embedding,
|
| 200 |
+
limit=k
|
| 201 |
+
)
|
| 202 |
+
print("Using deprecated search method")
|
| 203 |
|
| 204 |
# Convert results to documents
|
| 205 |
documents = []
|