kamkol commited on
Commit
25ae1f7
·
1 Parent(s): 06d2b05

Simplify OpenAI initialization for Hugging Face compatibility

Browse files
Files changed (1) hide show
  1. streamlit_app.py +78 -235
streamlit_app.py CHANGED
@@ -203,266 +203,109 @@ def load_document_chunks():
203
  def get_chat_model():
204
  """Get the chat model for initial RAG."""
205
  print("Initializing chat model...")
206
- # Try multiple approaches to initialize the model
207
  try:
208
- # Approach 1: Direct OpenAI client
209
- print("Trying direct OpenAI client approach")
210
- try:
211
- # Use direct OpenAI client to avoid proxy issues
212
- openai_client = OpenAI()
213
-
214
- # Create a wrapper that mimics LangChain's interface
215
- class SimpleOpenAIWrapper:
216
- def invoke(self, messages):
217
- print("Invoking SimpleOpenAIWrapper...")
218
- # Convert LangChain messages to OpenAI format
219
- openai_messages = []
220
- for msg in messages:
221
- role = "user"
222
- if hasattr(msg, "type"):
223
- role = "assistant" if msg.type == "ai" else "user"
224
- openai_messages.append({
225
- "role": role,
226
- "content": msg.content
227
- })
228
-
229
- # Log what we're sending to OpenAI
230
- print(f"Sending {len(openai_messages)} messages to OpenAI API")
231
-
232
- # Call API directly
233
  response = openai_client.chat.completions.create(
234
- model="gpt-4.1-mini",
235
- messages=openai_messages,
236
- temperature=0
237
  )
238
 
239
- # Create response object with content attribute
240
  class SimpleResponse:
241
  def __init__(self, content):
242
  self.content = content
243
 
244
  result = SimpleResponse(response.choices[0].message.content)
245
- print(f"Got response from OpenAI (length: {len(result.content)})")
246
  return result
247
-
248
- print("Successfully created SimpleOpenAIWrapper")
249
- return SimpleOpenAIWrapper()
250
- except Exception as e:
251
- print(f"Direct OpenAI client approach failed: {str(e)}")
252
- import traceback
253
- traceback.print_exc()
254
- raise
255
-
256
- except Exception as outer_e:
257
- print(f"First approach failed: {str(outer_e)}")
258
 
259
- # Approach 2: Standard LangChain
260
- try:
261
- print("Trying standard LangChain approach")
262
- model = ChatOpenAI(model="gpt-4.1-mini", temperature=0)
263
- print("Successfully created ChatOpenAI model")
264
- return model
265
- except Exception as e:
266
- print(f"Standard LangChain approach failed: {str(e)}")
267
-
268
- # Approach 3: Very minimal LangChain
269
- try:
270
- print("Trying minimal LangChain approach")
271
- model = ChatOpenAI(model="gpt-3.5-turbo")
272
- print("Successfully created minimal ChatOpenAI model")
273
- return model
274
- except Exception as e2:
275
- print(f"Minimal LangChain also failed: {str(e2)}")
276
-
277
- # Last resort: Dummy implementation
278
- print("Using dummy model as last resort")
279
- class DummyModel:
280
- def invoke(self, messages):
281
- print("WARNING: Using dummy model that returns fixed responses")
282
- class DummyResponse:
283
- def __init__(self):
284
- self.content = "I apologize, but I'm unable to process your query right now due to a technical issue. The system administrators have been notified."
285
- return DummyResponse()
286
-
287
- return DummyModel()
288
 
289
  @st.cache_resource
290
  def get_agent_model():
291
  """Get the more powerful model for agent and evaluation."""
292
  print("Initializing agent model...")
293
- # Try multiple approaches to initialize the model
294
- try:
295
- # Approach 1: Direct OpenAI client
296
- print("Trying direct OpenAI client approach for agent model")
297
- try:
298
- # Use direct OpenAI client to avoid proxy issues
299
- openai_client = OpenAI()
300
-
301
- # Create a wrapper that mimics LangChain's interface
302
- class SimpleOpenAIWrapper:
303
- def invoke(self, messages):
304
- print("Invoking agent SimpleOpenAIWrapper...")
305
- # Convert LangChain messages to OpenAI format
306
- openai_messages = []
307
- for msg in messages:
308
- role = "user"
309
- if hasattr(msg, "type"):
310
- role = "assistant" if msg.type == "ai" else "user"
311
- openai_messages.append({
312
- "role": role,
313
- "content": msg.content
314
- })
315
-
316
- # Log what we're sending to OpenAI
317
- print(f"Sending {len(openai_messages)} messages to OpenAI API (agent)")
318
-
319
- # Call API directly with a more powerful model
320
- response = openai_client.chat.completions.create(
321
- model="gpt-4.1",
322
- messages=openai_messages,
323
- temperature=0
324
- )
325
-
326
- class SimpleResponse:
327
- def __init__(self, content):
328
- self.content = content
329
-
330
- result = SimpleResponse(response.choices[0].message.content)
331
- print(f"Got agent response from OpenAI (length: {len(result.content)})")
332
- return result
333
-
334
- print("Successfully created agent SimpleOpenAIWrapper")
335
- return SimpleOpenAIWrapper()
336
- except Exception as e:
337
- print(f"Direct OpenAI client approach for agent failed: {str(e)}")
338
- import traceback
339
- traceback.print_exc()
340
- raise
341
-
342
- except Exception as outer_e:
343
- print(f"First agent approach failed: {str(outer_e)}")
344
-
345
- # Approach 2: Standard LangChain
346
- try:
347
- print("Trying standard LangChain approach for agent")
348
- model = ChatOpenAI(model="gpt-4.1", temperature=0)
349
- print("Successfully created agent ChatOpenAI model")
350
- return model
351
- except Exception as e:
352
- print(f"Standard LangChain approach for agent failed: {str(e)}")
353
-
354
- # Approach 3: Very minimal LangChain with fallback model
355
- try:
356
- print("Trying minimal LangChain approach for agent")
357
- model = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
358
- print("Successfully created minimal agent ChatOpenAI model")
359
- return model
360
- except Exception as e2:
361
- print(f"Minimal LangChain for agent also failed: {str(e2)}")
362
-
363
- # Last resort: Dummy implementation
364
- print("Using dummy agent model as last resort")
365
- class DummyModel:
366
- def invoke(self, messages):
367
- print("WARNING: Using dummy agent model that returns fixed responses")
368
- class DummyResponse:
369
- def __init__(self):
370
- self.content = "I apologize, but I'm unable to process complex queries right now due to a technical issue."
371
- return DummyResponse()
372
-
373
- return DummyModel()
374
 
375
  @st.cache_resource
376
  def get_embedding_model():
377
  """Get the embedding model."""
378
  print("Initializing embedding model...")
379
  try:
380
- # Approach 1: Direct OpenAI client
381
- print("Trying direct OpenAI client approach for embeddings")
382
- try:
383
- # Create an OpenAI client directly
384
- openai_client = OpenAI()
385
-
386
- # Create a wrapper class that matches the interface LangChain expects
387
- class SimpleEmbeddings:
388
- def embed_query(self, text):
389
- print(f"Embedding query text (length: {len(text)})")
390
- try:
391
- response = openai_client.embeddings.create(
392
- model="text-embedding-3-small",
393
- input=text
394
- )
395
- print("Successfully got embedding from OpenAI API")
396
- return response.data[0].embedding
397
- except Exception as e:
398
- print(f"Error in embed_query: {str(e)}")
399
- import traceback
400
- traceback.print_exc()
401
- # Return a dummy embedding of the right size
402
- print("WARNING: Returning dummy embedding vector")
403
- return [0.0] * 1536 # Standard size for embeddings
404
-
405
- def embed_documents(self, texts):
406
- print(f"Embedding {len(texts)} documents")
407
- try:
408
- if not texts:
409
- return []
410
-
411
- # Embed each text individually to avoid batch size issues
412
- results = []
413
- for i, text in enumerate(texts):
414
- print(f"Embedding document {i+1}/{len(texts)}")
415
- results.append(self.embed_query(text))
416
- return results
417
- except Exception as e:
418
- print(f"Error in embed_documents: {str(e)}")
419
- import traceback
420
- traceback.print_exc()
421
- # Return dummy embeddings
422
- print("WARNING: Returning dummy document embeddings")
423
- return [[0.0] * 1536 for _ in range(len(texts))]
424
 
425
- print("Successfully created SimpleEmbeddings")
426
- return SimpleEmbeddings()
427
- except Exception as e:
428
- print(f"Direct OpenAI client approach for embeddings failed: {str(e)}")
429
- import traceback
430
- traceback.print_exc()
431
- raise
 
 
 
 
 
 
 
 
432
 
433
- except Exception as outer_e:
434
- print(f"First embedding approach failed: {str(outer_e)}")
435
 
436
- # Approach 2: Standard LangChain OpenAIEmbeddings
437
- try:
438
- print("Trying standard LangChain approach for embeddings")
439
- embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
440
- print("Successfully created OpenAIEmbeddings")
441
- return embeddings
442
- except Exception as e:
443
- print(f"Standard OpenAIEmbeddings failed: {str(e)}")
444
-
445
- # Approach 3: Very minimal OpenAIEmbeddings
446
- try:
447
- print("Trying minimal OpenAIEmbeddings")
448
- embeddings = OpenAIEmbeddings()
449
- print("Successfully created minimal OpenAIEmbeddings")
450
- return embeddings
451
- except Exception as e2:
452
- print(f"Minimal OpenAIEmbeddings failed: {str(e2)}")
453
-
454
- # Last resort: Dummy implementation
455
- print("Using dummy embeddings as last resort")
456
- class DummyEmbeddings:
457
- def embed_query(self, text):
458
- print("WARNING: Using dummy embeddings")
459
- return [0.0] * 1536
460
-
461
- def embed_documents(self, texts):
462
- print("WARNING: Using dummy document embeddings")
463
- return [[0.0] * 1536 for _ in range(len(texts))]
464
-
465
- return DummyEmbeddings()
466
 
467
  @st.cache_resource
468
  def setup_qdrant_client():
 
203
  def get_chat_model():
204
  """Get the chat model for initial RAG."""
205
  print("Initializing chat model...")
 
206
  try:
207
+ # Very minimal OpenAI initialization for Hugging Face compatibility
208
+ openai_api_key = os.environ.get("OPENAI_API_KEY", "")
209
+ openai_client = OpenAI(api_key=openai_api_key)
210
+
211
+ # Create a simplified wrapper that avoids any problematic parameters
212
+ class SimpleOpenAIWrapper:
213
+ def invoke(self, messages):
214
+ print("Invoking chat model...")
215
+ # Convert LangChain messages to OpenAI format
216
+ openai_messages = []
217
+ for msg in messages:
218
+ role = "user"
219
+ if hasattr(msg, "type"):
220
+ role = "assistant" if msg.type == "ai" else "user"
221
+ openai_messages.append({
222
+ "role": role,
223
+ "content": msg.content
224
+ })
225
+
226
+ # Call API directly with absolutely minimal parameters
227
+ try:
 
 
 
 
228
  response = openai_client.chat.completions.create(
229
+ model="gpt-3.5-turbo", # Use a minimal, widely supported model
230
+ messages=openai_messages
 
231
  )
232
 
233
+ # Create response object
234
  class SimpleResponse:
235
  def __init__(self, content):
236
  self.content = content
237
 
238
  result = SimpleResponse(response.choices[0].message.content)
239
+ print(f"Got response of length: {len(result.content)}")
240
  return result
241
+ except Exception as e:
242
+ print(f"Error calling OpenAI API: {str(e)}")
243
+ raise
 
 
 
 
 
 
 
 
244
 
245
+ return SimpleOpenAIWrapper()
246
+ except Exception as e:
247
+ print(f"Error initializing chat model: {str(e)}")
248
+ # Create dummy for testing
249
+ class DummyModel:
250
+ def invoke(self, messages):
251
+ print("WARNING: Using dummy model!")
252
+ return type('obj', (object,), {'content': 'I apologize, but I cannot access the necessary data to answer this question.'})
253
+
254
+ return DummyModel()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255
 
256
  @st.cache_resource
257
  def get_agent_model():
258
  """Get the more powerful model for agent and evaluation."""
259
  print("Initializing agent model...")
260
+ # Use the exact same approach as the chat model for consistency
261
+ return get_chat_model()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
 
263
  @st.cache_resource
264
  def get_embedding_model():
265
  """Get the embedding model."""
266
  print("Initializing embedding model...")
267
  try:
268
+ # Very minimal OpenAI initialization for Hugging Face compatibility
269
+ openai_api_key = os.environ.get("OPENAI_API_KEY", "")
270
+ openai_client = OpenAI(api_key=openai_api_key)
271
+
272
+ # Create a wrapper that avoids any problematic parameters
273
+ class SimpleEmbeddings:
274
+ def embed_query(self, text):
275
+ print(f"Embedding query of length: {len(text)}")
276
+ try:
277
+ response = openai_client.embeddings.create(
278
+ model="text-embedding-ada-002", # Use older, more compatible model
279
+ input=text
280
+ )
281
+ print("Successfully got embedding")
282
+ return response.data[0].embedding
283
+ except Exception as e:
284
+ print(f"Error in embed_query: {str(e)}")
285
+ # Return a dummy embedding
286
+ print("WARNING: Returning dummy embedding!")
287
+ return [0.0] * 1536
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
 
289
+ def embed_documents(self, texts):
290
+ print(f"Embedding {len(texts)} documents")
291
+ results = []
292
+ for i, text in enumerate(texts):
293
+ results.append(self.embed_query(text))
294
+ return results
295
+
296
+ return SimpleEmbeddings()
297
+ except Exception as e:
298
+ print(f"Error initializing embedding model: {str(e)}")
299
+ # Create dummy for testing
300
+ class DummyEmbeddings:
301
+ def embed_query(self, text):
302
+ print("WARNING: Using dummy embeddings!")
303
+ return [0.0] * 1536
304
 
305
+ def embed_documents(self, texts):
306
+ return [[0.0] * 1536 for _ in range(len(texts))]
307
 
308
+ return DummyEmbeddings()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
 
310
  @st.cache_resource
311
  def setup_qdrant_client():