wakeupmh commited on
Commit
9d8a332
·
1 Parent(s): 459b2b5

pass prompt

Browse files
Files changed (1) hide show
  1. services/model_handler.py +43 -250
services/model_handler.py CHANGED
@@ -284,139 +284,6 @@ class LocalHuggingFaceModel(Model):
284
  error_message = str(e)
285
  return f"Error during generation: {error_message}"
286
 
287
- class DummyModel(Model):
288
- def __init__(self):
289
- super().__init__(id="dummy-model")
290
-
291
- async def ainvoke(self, prompt: str, **kwargs) -> str:
292
- """Async invoke method"""
293
- return await self.invoke(prompt=prompt, **kwargs)
294
-
295
- async def ainvoke_stream(self, prompt: str, **kwargs):
296
- """Async streaming invoke method"""
297
- result = await self.invoke(prompt=prompt, **kwargs)
298
- yield result
299
-
300
- def invoke(self, prompt: str, **kwargs) -> str:
301
- """Synchronous invoke method"""
302
- return Response("Sorry, the model is not available. Please try again later.")
303
-
304
- def invoke_stream(self, prompt: str, **kwargs):
305
- """Synchronous streaming invoke method"""
306
- result = self.invoke(prompt=prompt, **kwargs)
307
- yield result
308
-
309
- def parse_provider_response(self, response: str) -> str:
310
- """Parse the provider response"""
311
- return response
312
-
313
- def parse_provider_response_delta(self, delta: str) -> str:
314
- """Parse the provider response delta for streaming"""
315
- return delta
316
-
317
- async def aresponse(self, prompt=None, **kwargs):
318
- """Async response method - required abstract method"""
319
- try:
320
- # Log detalhado de todos os argumentos
321
- logging.info(f"aresponse args: prompt={prompt}, kwargs keys={list(kwargs.keys())}")
322
-
323
- # Extrair o prompt das mensagens se estiverem disponíveis
324
- if prompt is None and 'messages' in kwargs and kwargs['messages']:
325
- messages = kwargs['messages']
326
- # Procurar pela mensagem do usuário
327
- for message in messages:
328
- if hasattr(message, 'role') and message.role == 'user' and hasattr(message, 'content'):
329
- prompt = message.content
330
- logging.info(f"Extracted prompt from user message: {prompt[:100] if prompt and isinstance(prompt, str) else 'None'}")
331
- break
332
-
333
- # Verificar se o prompt está em kwargs['input']
334
- if prompt is None:
335
- if 'input' in kwargs:
336
- prompt = kwargs.get('input')
337
- logging.info(f"Found prompt in kwargs['input']: {prompt[:100] if prompt and isinstance(prompt, str) else 'None'}")
338
-
339
- logging.info(f"aresponse called with prompt: {prompt[:100] if prompt and isinstance(prompt, str) else 'None'}...")
340
-
341
- if not prompt or not isinstance(prompt, str) or not prompt.strip():
342
- logging.warning("Empty or invalid prompt in aresponse")
343
- return Response("No input provided. Please provide a valid prompt.")
344
-
345
- content = await self.ainvoke(prompt, **kwargs)
346
- return content if isinstance(content, Response) else Response(content)
347
- except Exception as e:
348
- logging.error(f"Error in aresponse: {str(e)}")
349
- return Response(f"Error in aresponse: {str(e)}")
350
-
351
- def response(self, prompt=None, **kwargs):
352
- """Synchronous response method - required abstract method"""
353
- try:
354
- # Log detalhado de todos os argumentos
355
- logging.info(f"response args: prompt={prompt}, kwargs keys={list(kwargs.keys())}")
356
-
357
- # Extrair o prompt das mensagens se estiverem disponíveis
358
- if prompt is None and 'messages' in kwargs and kwargs['messages']:
359
- messages = kwargs['messages']
360
- # Procurar pela mensagem do usuário
361
- for message in messages:
362
- if hasattr(message, 'role') and message.role == 'user' and hasattr(message, 'content'):
363
- prompt = message.content
364
- logging.info(f"Extracted prompt from user message: {prompt[:100] if prompt and isinstance(prompt, str) else 'None'}")
365
- break
366
-
367
- # Verificar se o prompt está em kwargs['input']
368
- if prompt is None:
369
- if 'input' in kwargs:
370
- prompt = kwargs.get('input')
371
- logging.info(f"Found prompt in kwargs['input']: {prompt[:100] if prompt and isinstance(prompt, str) else 'None'}")
372
-
373
- logging.info(f"response called with prompt: {prompt[:100] if prompt and isinstance(prompt, str) else 'None'}...")
374
-
375
- if not prompt or not isinstance(prompt, str) or not prompt.strip():
376
- logging.warning("Empty or invalid prompt in response")
377
- return Response("No input provided. Please provide a valid prompt.")
378
-
379
- content = self.invoke(prompt, **kwargs)
380
- return content if isinstance(content, Response) else Response(content)
381
- except Exception as e:
382
- logging.error(f"Error in response: {str(e)}")
383
- return Response(f"Error in response: {str(e)}")
384
-
385
- def response_stream(self, prompt=None, **kwargs):
386
- """Synchronous streaming response method - required abstract method"""
387
- try:
388
- # Log detalhado de todos os argumentos
389
- logging.info(f"response_stream args: prompt={prompt}, kwargs keys={list(kwargs.keys())}")
390
-
391
- # Extrair o prompt das mensagens se estiverem disponíveis
392
- if prompt is None and 'messages' in kwargs and kwargs['messages']:
393
- messages = kwargs['messages']
394
- # Procurar pela mensagem do usuário
395
- for message in messages:
396
- if hasattr(message, 'role') and message.role == 'user' and hasattr(message, 'content'):
397
- prompt = message.content
398
- logging.info(f"Extracted prompt from user message: {prompt[:100] if prompt and isinstance(prompt, str) else 'None'}")
399
- break
400
-
401
- # Verificar se o prompt está em kwargs['input']
402
- if prompt is None:
403
- if 'input' in kwargs:
404
- prompt = kwargs.get('input')
405
- logging.info(f"Found prompt in kwargs['input']: {prompt[:100] if prompt and isinstance(prompt, str) else 'None'}")
406
-
407
- logging.info(f"response_stream called with prompt: {prompt[:100] if prompt and isinstance(prompt, str) else 'None'}...")
408
-
409
- if not prompt or not isinstance(prompt, str) or not prompt.strip():
410
- logging.warning("Empty or invalid prompt in response_stream")
411
- yield Response("No input provided. Please provide a valid prompt.")
412
- return
413
-
414
- for chunk in self.invoke_stream(prompt, **kwargs):
415
- yield chunk if isinstance(chunk, Response) else Response(chunk)
416
- except Exception as e:
417
- logging.error(f"Error in response_stream: {str(e)}")
418
- yield Response(f"Error in response_stream: {str(e)}")
419
-
420
  class ModelHandler:
421
  """
422
  Classe para gerenciar modelos e gerar respostas.
@@ -609,10 +476,6 @@ Output:"""
609
  """Initialize local model as fallback"""
610
  model, tokenizer = self._load_model()
611
 
612
- if model is None or tokenizer is None:
613
- # Create a dummy model that returns a helpful message
614
- return DummyModel()
615
-
616
  return LocalHuggingFaceModel(model, tokenizer)
617
 
618
  def generate_answer(self, query: str) -> str:
@@ -653,30 +516,26 @@ Output:"""
653
  logging.error("Empty translation result")
654
  return "Desculpe, não foi possível processar sua consulta. Por favor, tente novamente com uma pergunta diferente."
655
 
656
- # Se forçar resposta padrão, pular a pesquisa e usar diretamente a resposta padrão
657
- if self.force_default_response:
658
- logging.info("Forcing default response")
659
- research_content = self._get_default_research_content(translation_content)
660
- else:
661
- # Realizar a pesquisa
662
- research_prompt = self._format_prompt("research", translation_content)
663
- logging.info(f"Research prompt: {research_prompt}")
664
-
665
- research_result = self.researcher.run(research_prompt)
666
- logging.info(f"Research result type: {type(research_result)}")
667
-
668
- # Extrair o conteúdo da pesquisa
669
- research_content = self._extract_content(research_result)
670
- logging.info(f"Research content: {research_content}")
671
-
672
- # Verificar se a resposta da pesquisa é muito curta
673
- research_length = len(research_content.strip()) if research_content and isinstance(research_content, str) else 0
674
- logging.info(f"Research content length: {research_length} characters")
675
-
676
- if not research_content or not research_content.strip() or research_length < 150:
677
- logging.warning(f"Research result too short ({research_length} chars), trying with a more specific prompt")
678
- # Tentar novamente com um prompt mais específico
679
- enhanced_prompt = f"""Task: Detailed Research
680
 
681
  Instructions:
682
  Provide a comprehensive explanation about '{translation_content}'.
@@ -686,45 +545,34 @@ Be thorough and informative, covering all important aspects of the topic.
686
  Use clear and accessible language suitable for a general audience.
687
 
688
  Output:"""
689
- logging.info(f"Enhanced research prompt: {enhanced_prompt}")
690
- research_result = self.researcher.run(enhanced_prompt)
691
- research_content = self._extract_content(research_result)
692
- research_length = len(research_content.strip()) if research_content and isinstance(research_content, str) else 0
693
- logging.info(f"Enhanced research content: {research_content}")
694
- logging.info(f"Enhanced research content length: {research_length} characters")
695
-
696
- # Se ainda estiver vazio ou muito curto, usar uma resposta padrão
697
  if not research_content or not research_content.strip() or research_length < 150:
698
  logging.warning(f"Research result still too short ({research_length} chars), using default response")
699
  # Usar resposta padrão
700
  logging.info("Using default research content")
701
  research_content = self._get_default_research_content(translation_content)
702
 
703
- # Se forçar resposta padrão, pular a apresentação e usar diretamente a resposta padrão
704
- if self.force_default_response:
705
- logging.info("Forcing default presentation")
706
- presentation_content = self._get_default_presentation_content()
707
- else:
708
- # Apresentar os resultados
709
- presentation_prompt = self._format_prompt("presentation", research_content)
710
- logging.info(f"Presentation prompt: {presentation_prompt}")
711
-
712
- presentation_result = self.presenter.run(presentation_prompt)
713
- logging.info(f"Presentation type: {type(presentation_result)}")
714
-
715
- # Extrair o conteúdo da apresentação
716
- presentation_content = self._extract_content(presentation_result)
717
- logging.info(f"Presentation content: {presentation_content}")
718
-
719
- # Verificar se a apresentação é muito curta
720
- presentation_length = len(presentation_content.strip()) if presentation_content and isinstance(presentation_content, str) else 0
721
- logging.info(f"Presentation content length: {presentation_length} characters")
722
-
723
- if not presentation_content or not presentation_content.strip() or presentation_length < 150:
724
- logging.warning(f"Presentation result too short ({presentation_length} chars), using default presentation")
725
- # Usar apresentação padrão
726
- logging.info("Using default presentation content")
727
- presentation_content = self._get_default_presentation_content()
728
 
729
  logging.info("Answer generated successfully")
730
  return presentation_content
@@ -736,59 +584,4 @@ Output:"""
736
  except Exception as e:
737
  logging.error(f"Unexpected error in generate_answer: {str(e)}")
738
  return "Desculpe, ocorreu um erro inesperado. Por favor, tente novamente mais tarde."
739
-
740
- def _get_default_research_content(self, topic: str) -> str:
741
- """
742
- Retorna um conteúdo de pesquisa padrão para o tópico.
743
-
744
- Args:
745
- topic: O tópico da pesquisa
746
-
747
- Returns:
748
- Conteúdo de pesquisa padrão
749
- """
750
- return f"""Information about {topic}:
751
-
752
- Autism is a complex neurodevelopmental disorder that affects communication, social interaction, and behavior. It is characterized by challenges with social skills, repetitive behaviors, speech, and nonverbal communication.
753
-
754
- The condition is part of a broader category called autism spectrum disorder (ASD), which reflects the wide variation in challenges and strengths possessed by each person with autism. Some individuals with autism may require significant support in their daily lives, while others may need less support and, in some cases, live entirely independently.
755
-
756
- Autism is believed to be caused by a combination of genetic and environmental factors. Research suggests that certain genetic mutations may increase the risk of autism, as well as various environmental factors that influence early brain development. There is no single cause for autism, making it a complex condition to understand and treat.
757
-
758
- Early diagnosis and intervention are important for improving outcomes for individuals with autism. Various therapies and support strategies can help people with autism develop skills and cope with challenges. These may include behavioral therapy, speech therapy, occupational therapy, and educational support.
759
-
760
- It's important to note that autism is not a disease to be cured but a different way of experiencing and interacting with the world. Many people with autism have exceptional abilities in visual skills, music, math, and art, among other areas."""
761
-
762
- def _get_default_presentation_content(self) -> str:
763
- """
764
- Retorna um conteúdo de apresentação padrão.
765
-
766
- Returns:
767
- Conteúdo de apresentação padrão
768
- """
769
- return """🧠 **Autismo: Entendendo o Espectro** 🧠
770
-
771
- ## O que é o Autismo?
772
- O autismo é uma condição neurológica complexa que afeta a comunicação, interação social e comportamento. É caracterizado por desafios com habilidades sociais, comportamentos repetitivos, fala e comunicação não verbal.
773
-
774
- ## Características Principais:
775
- - 🔄 Comportamentos repetitivos e interesses restritos
776
- - 🗣️ Dificuldades na comunicação verbal e não verbal
777
- - 👥 Desafios nas interações sociais
778
- - 🎭 Dificuldade em entender expressões faciais e emoções
779
- - 🔊 Sensibilidade sensorial (sons, luzes, texturas)
780
-
781
- ## Causas e Origens:
782
- O autismo é causado por uma combinação de fatores genéticos e ambientais. Pesquisas sugerem que certas mutações genéticas podem aumentar o risco, assim como vários fatores ambientais que influenciam o desenvolvimento inicial do cérebro.
783
-
784
- ## Pontos Importantes:
785
- - 📊 O autismo afeta cada pessoa de maneira diferente (por isso é chamado de "espectro")
786
- - 🧩 Diagnóstico precoce e intervenção melhoram os resultados
787
- - 💪 Muitas pessoas com autismo têm habilidades excepcionais em áreas específicas
788
- - 🌈 O autismo não é uma doença a ser curada, mas uma forma diferente de experimentar o mundo
789
-
790
- ## Conclusão:
791
- Compreender o autismo é essencial para criar uma sociedade mais inclusiva. Cada pessoa com autismo tem suas próprias forças e desafios únicos, e merece apoio e aceitação.
792
-
793
- *Fonte: Pesquisas científicas atuais sobre transtornos do espectro autista*
794
- """
 
284
  error_message = str(e)
285
  return f"Error during generation: {error_message}"
286
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
  class ModelHandler:
288
  """
289
  Classe para gerenciar modelos e gerar respostas.
 
476
  """Initialize local model as fallback"""
477
  model, tokenizer = self._load_model()
478
 
 
 
 
 
479
  return LocalHuggingFaceModel(model, tokenizer)
480
 
481
  def generate_answer(self, query: str) -> str:
 
516
  logging.error("Empty translation result")
517
  return "Desculpe, não foi possível processar sua consulta. Por favor, tente novamente com uma pergunta diferente."
518
 
519
+
520
+ # Realizar a pesquisa
521
+ research_prompt = self._format_prompt("research", translation_content)
522
+ logging.info(f"Research prompt: {research_prompt}")
523
+
524
+ research_result = self.researcher.run(research_prompt)
525
+ logging.info(f"Research result type: {type(research_result)}")
526
+
527
+ # Extrair o conteúdo da pesquisa
528
+ research_content = self._extract_content(research_result)
529
+ logging.info(f"Research content: {research_content}")
530
+
531
+ # Verificar se a resposta da pesquisa é muito curta
532
+ research_length = len(research_content.strip()) if research_content and isinstance(research_content, str) else 0
533
+ logging.info(f"Research content length: {research_length} characters")
534
+
535
+ if not research_content or not research_content.strip() or research_length < 150:
536
+ logging.warning(f"Research result too short ({research_length} chars), trying with a more specific prompt")
537
+ # Tentar novamente com um prompt mais específico
538
+ enhanced_prompt = f"""Task: Detailed Research
 
 
 
 
539
 
540
  Instructions:
541
  Provide a comprehensive explanation about '{translation_content}'.
 
545
  Use clear and accessible language suitable for a general audience.
546
 
547
  Output:"""
548
+ logging.info(f"Enhanced research prompt: {enhanced_prompt}")
549
+ research_result = self.researcher.run(enhanced_prompt)
550
+ research_content = self._extract_content(research_result)
551
+ research_length = len(research_content.strip()) if research_content and isinstance(research_content, str) else 0
552
+ logging.info(f"Enhanced research content: {research_content}")
553
+ logging.info(f"Enhanced research content length: {research_length} characters")
554
+
 
555
  if not research_content or not research_content.strip() or research_length < 150:
556
  logging.warning(f"Research result still too short ({research_length} chars), using default response")
557
  # Usar resposta padrão
558
  logging.info("Using default research content")
559
  research_content = self._get_default_research_content(translation_content)
560
 
561
+
562
+ presentation_prompt = self._format_prompt("presentation", research_content)
563
+ logging.info(f"Presentation prompt: {presentation_prompt}")
564
+
565
+ presentation_result = self.presenter.run(presentation_prompt)
566
+ logging.info(f"Presentation type: {type(presentation_result)}")
567
+
568
+ presentation_content = self._extract_content(presentation_result)
569
+ logging.info(f"Presentation content: {presentation_content}")
570
+
571
+ presentation_length = len(presentation_content.strip()) if presentation_content and isinstance(presentation_content, str) else 0
572
+ logging.info(f"Presentation content length: {presentation_length} characters")
573
+
574
+ if not presentation_content or not presentation_content.strip() or presentation_length < 150:
575
+ logging.warning(f"Presentation result too short ({presentation_length} chars), using default presentation")
 
 
 
 
 
 
 
 
 
 
576
 
577
  logging.info("Answer generated successfully")
578
  return presentation_content
 
584
  except Exception as e:
585
  logging.error(f"Unexpected error in generate_answer: {str(e)}")
586
  return "Desculpe, ocorreu um erro inesperado. Por favor, tente novamente mais tarde."
587
+