Download example_rag_hybrid_usage.py from 0danielfonseca/Doninha-Artificial-Inteligence: direct link, hf CLI and curl.
- Browser
- Download file 16.6 kB
-
https://huggingface.co/0danielfonseca/Doninha-Artificial-Inteligence/resolve/main/example_rag_hybrid_usage.py
- Command line
-
hf download hf://0danielfonseca/Doninha-Artificial-Inteligence/example_rag_hybrid_usage.py
-
curl -L -o example_rag_hybrid_usage.py https://huggingface.co/0danielfonseca/Doninha-Artificial-Inteligence/resolve/main/example_rag_hybrid_usage.py
16.6 kB
| """ | |
| EXEMPLO DE USO: RAG HΓbrido com L1-L2 | |
| ====================================== | |
| Demonstra como usar o sistema completo: | |
| - RAG HΓbrido (Context Injection + Retrieval Seletivo) | |
| - IntegraΓ§Γ£o com L1 (Conceitos) e L2 (JuΓzos Kantianos) | |
| - Domain-aware Knowledge Base | |
| - Context Injection com system_prompt especializado | |
| """ | |
| from pathlib import Path | |
| import json | |
| from typing import Optional, Dict, Any | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 1. EXEMPLO BΓSICO: Apenas RAG HΓbrido | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def example_1_basic_rag(): | |
| """ | |
| Exemplo 1: Usa RAG hΓbrido para processar uma query. | |
| Demonstra: Context Injection + Retrieval Seletivo. | |
| """ | |
| print("\n" + "="*70) | |
| print("EXEMPLO 1: RAG HΓbrido BΓ‘sico") | |
| print("="*70) | |
| from rag_hybrid_context_injection import ( | |
| HybridRAGContextInjectionEngine, | |
| RetrievalStrategy, | |
| ) | |
| # Cria motor RAG | |
| rag_engine = HybridRAGContextInjectionEngine(verbose=True) | |
| # Query de teste | |
| query = "O que Γ© verdade em lΓ³gica paraconsistente?" | |
| # Processa com RAG hΓbrido | |
| rag_context = rag_engine.process( | |
| query=query, | |
| strategy=RetrievalStrategy.HYBRID, | |
| auto_detect_domain=True, | |
| ) | |
| print(f"\nβ DomΓnio detectado: {rag_context.domain}") | |
| print(f"β ConfianΓ§a: {rag_context.confidence_score:.2%}") | |
| print(f"β Documentos recuperados: {len(rag_context.retrieved_documents)}") | |
| print(f"\n--- Contexto Compilado (para injeΓ§Γ£o no LLM) ---") | |
| print(rag_context.compiled_context[:500] + "...") | |
| return rag_context | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 2. EXEMPLO: RAG + L1-L2 Pipeline Completo | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def example_2_l1_l2_rag_pipeline(): | |
| """ | |
| Exemplo 2: Pipeline completo L1-L2-RAG. | |
| Demonstra: Conceitos + JuΓzos + RAG HΓbrido integrados. | |
| """ | |
| print("\n" + "="*70) | |
| print("EXEMPLO 2: Pipeline Completo L1-L2-RAG") | |
| print("="*70) | |
| from l1_l2_rag_integration import create_l1_l2_rag_pipeline | |
| # Cria pipeline | |
| pipeline = create_l1_l2_rag_pipeline() | |
| # Query de teste | |
| query = "Qual Γ© a definiΓ§Γ£o epistemolΓ³gica de conhecimento justificado?" | |
| # Processa | |
| result = pipeline.process(query) | |
| print(f"\nβ DomΓnio: {result['domain']}") | |
| print(f"β ConfianΓ§a: {result['confidence']:.2%}") | |
| print(f"β Conceitos (L1): {len(result['l1_output'].concepts)}") | |
| print(f"β JuΓzos (L2): {len(result['l2_output'].judgments)}") | |
| print(f"\n--- L1 (Conceitos) ---") | |
| for concept in result['l1_output'].concepts[:3]: | |
| term = concept.term if hasattr(concept, "term") else str(concept) | |
| print(f" β’ {term}") | |
| print(f"\n--- L2 (Top Judgment) ---") | |
| if result['l2_output'].top_judgment: | |
| print(f" {str(result['l2_output'].top_judgment)[:200]}...") | |
| print(f"\n--- Contexto Compilado (para injeΓ§Γ£o) ---") | |
| print(result['compiled_context'][:600] + "...") | |
| return result | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 3. EXEMPLO: Domain Detection e Context Injection Seletiva | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def example_3_domain_specific_injection(): | |
| """ | |
| Exemplo 3: Detecta domΓnio automaticamente e usa system_prompt especializado. | |
| Demonstra: Domain-aware context injection. | |
| """ | |
| print("\n" + "="*70) | |
| print("EXEMPLO 3: Domain-Specific Context Injection") | |
| print("="*70) | |
| from rag_hybrid_context_injection import HybridRAGContextInjectionEngine | |
| rag_engine = HybridRAGContextInjectionEngine(verbose=True) | |
| # Queries de diferentes domΓnios | |
| queries = [ | |
| ("AristΓ³teles define a substΓ’ncia como categoria fundamental", "filosofia"), | |
| ("Na lΓ³gica paraconsistente, Γ© possΓvel ter P e Β¬P simultaneamente?", "lΓ³gica"), | |
| ("Como a justificaΓ§Γ£o interna diferencia-se da justificaΓ§Γ£o externa?", "epistemologia"), | |
| ] | |
| for query, expected_domain in queries: | |
| print(f"\n--- Query: {query[:60]}... ---") | |
| rag_context = rag_engine.process( | |
| query=query, | |
| auto_detect_domain=True, | |
| ) | |
| detected = rag_context.domain | |
| match = "β" if detected == expected_domain else "β" | |
| print(f"{match} DomΓnio detectado: {detected} (esperado: {expected_domain})") | |
| # Mostra system_prompt especializado | |
| domain_ctx = rag_engine.domains[detected] | |
| print(f"\nSystem Prompt (domΓnio {detected}):") | |
| print(f" {domain_ctx.system_prompt[:150]}...") | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 4. EXEMPLO: Retrieval Strategy Comparativo | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def example_4_strategy_comparison(): | |
| """ | |
| Exemplo 4: Compara diferentes estratΓ©gias de retrieval. | |
| Demonstra: InjeΓ§Γ£o direta vs Semantic Retrieval vs Hybrid. | |
| """ | |
| print("\n" + "="*70) | |
| print("EXEMPLO 4: EstratΓ©gias de Retrieval Comparativas") | |
| print("="*70) | |
| from rag_hybrid_context_injection import ( | |
| HybridRAGContextInjectionEngine, | |
| RetrievalStrategy, | |
| ) | |
| rag_engine = HybridRAGContextInjectionEngine(verbose=False) | |
| query = "O que Γ© uma proposiΓ§Γ£o numa lΓ³gica nΓ£o-clΓ‘ssica?" | |
| strategies = [ | |
| RetrievalStrategy.DIRECT_INJECTION, | |
| RetrievalStrategy.SEMANTIC_RETRIEVAL, | |
| RetrievalStrategy.HYBRID, | |
| ] | |
| for strategy in strategies: | |
| rag_context = rag_engine.process( | |
| query=query, | |
| strategy=strategy, | |
| auto_detect_domain=True, | |
| ) | |
| injected = sum(1 for d in rag_context.retrieved_documents if d.is_injected) | |
| retrieved = len(rag_context.retrieved_documents) - injected | |
| print(f"\n--- EstratΓ©gia: {strategy.value} ---") | |
| print(f" Injetados: {injected}") | |
| print(f" Recuperados: {retrieved}") | |
| print(f" ConfianΓ§a: {rag_context.confidence_score:.2%}") | |
| print(f" Contexto (chars): {len(rag_context.compiled_context)}") | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 5. EXEMPLO AVANΓADO: FormataΓ§Γ£o para LLM com System Prompt Customizado | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def example_5_llm_formatted_output(): | |
| """ | |
| Exemplo 5: Formata saΓda para injeΓ§Γ£o direta em LLM (ChatGPT, Claude, etc). | |
| Demonstra: Context Injection com system_prompt customizado. | |
| """ | |
| print("\n" + "="*70) | |
| print("EXEMPLO 5: FormataΓ§Γ£o para InjeΓ§Γ£o em LLM") | |
| print("="*70) | |
| from l1_l2_rag_integration import create_l1_l2_rag_pipeline | |
| pipeline = create_l1_l2_rag_pipeline() | |
| query = "Explique a diferenΓ§a entre conhecimento e opiniΓ£o justificada." | |
| result = pipeline.process(query) | |
| # System prompt customizado (fornecido pelo usuΓ‘rio) | |
| system_prompt_custom = """VocΓͺ Γ© um especialista rigoroso com acesso a uma base de conhecimento especializada. | |
| Responda sempre usando o contexto fornecido quando relevante. Seja preciso e cite fontes quando possΓvel.""" | |
| # Monta mensagens para LLM | |
| messages = [ | |
| { | |
| "role": "system", | |
| "content": system_prompt_custom, | |
| }, | |
| { | |
| "role": "user", | |
| "content": result["compiled_context"], | |
| }, | |
| ] | |
| print("\n--- Mensagens formatadas para LLM (JSON) ---") | |
| print(json.dumps(messages, indent=2, ensure_ascii=False)[:800] + "...") | |
| print("\n--- Como usar com OpenAI ---") | |
| print(""" | |
| from openai import OpenAI | |
| client = OpenAI(api_key="...") | |
| response = client.chat.completions.create( | |
| model="gpt-4", | |
| messages=messages, | |
| temperature=0.3, | |
| ) | |
| print(response.choices[0].message.content) | |
| """) | |
| print("\n--- Como usar com Anthropic Claude ---") | |
| print(""" | |
| import anthropic | |
| client = anthropic.Anthropic(api_key="...") | |
| response = client.messages.create( | |
| model="claude-3-opus-20240229", | |
| max_tokens=1024, | |
| system=system_prompt_custom, | |
| messages=[ | |
| {"role": "user", "content": result["compiled_context"]} | |
| ], | |
| ) | |
| print(response.content[0].text) | |
| """) | |
| return messages | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 6. EXEMPLO: Criando domΓnios customizados | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def example_6_custom_domains(): | |
| """ | |
| Exemplo 6: Cria e registra domΓnios customizados. | |
| Demonstra: Extensibilidade do sistema. | |
| """ | |
| print("\n" + "="*70) | |
| print("EXEMPLO 6: DomΓnios Customizados") | |
| print("="*70) | |
| from rag_hybrid_context_injection import ( | |
| HybridRAGContextInjectionEngine, | |
| DomainContext, | |
| ) | |
| rag_engine = HybridRAGContextInjectionEngine(verbose=True) | |
| # Define novo domΓnio customizado | |
| domain_direito = DomainContext( | |
| domain_name="direito", | |
| description="Direito civil, constitucional e penal", | |
| keywords=["lei", "cΓ³digo", "artigo", "direito", "obrigaΓ§Γ£o", "contrato"], | |
| kb_path="data/kb_direito.json", | |
| chroma_collection="direito_corpus", | |
| system_prompt="""VocΓͺ Γ© um especialista em direito com rigorosa base legal. | |
| Cite sempre artigos, precedentes e legislaΓ§Γ£o pertinente. Mantenha precisΓ£o tΓ©cnica e referencias Γ s leis.""", | |
| injection_weight=0.85, | |
| retrieval_weight=0.15, | |
| ) | |
| # Registra domΓnio | |
| rag_engine.register_domain(domain_direito) | |
| print(f"\nβ DomΓnio 'direito' registrado") | |
| print(f" Keywords: {', '.join(domain_direito.keywords[:3])}...") | |
| print(f" System Prompt: {domain_direito.system_prompt[:100]}...") | |
| # Testa com query de direito | |
| query = "Qual Γ© o prazo para prescriΓ§Γ£o de dΓ©bitos fiscais?" | |
| detected_domain, conf = rag_engine.detect_domain(query) | |
| print(f"\nβ Query sobre direito detectado como: {detected_domain}") | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 7. PIPELINE COMPLETO DE PONTA A PONTA | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def example_7_end_to_end_pipeline(): | |
| """ | |
| Exemplo 7: Pipeline completo de ponta a ponta. | |
| Demonstra: Fluxo completo desde query atΓ© resposta estruturada. | |
| """ | |
| print("\n" + "="*70) | |
| print("EXEMPLO 7: Pipeline de Ponta a Ponta") | |
| print("="*70) | |
| from l1_l2_rag_integration import create_l1_l2_rag_pipeline | |
| # Cria pipeline | |
| pipeline = create_l1_l2_rag_pipeline() | |
| # Query | |
| query = "Como Kant define o juΓzo analΓtico?" | |
| print(f"\n[1] Input: {query}") | |
| # Etapa 1: Processamento completo | |
| result = pipeline.process(query) | |
| print(f"\n[2] Domain Detection") | |
| print(f" β Domain: {result['domain']}") | |
| print(f" β Confidence: {result['confidence']:.2%}") | |
| print(f"\n[3] L1 (Conceitos) - ExtraΓ§Γ£o e Enriquecimento") | |
| print(f" β Conceitos extraΓdos: {len(result['l1_output'].concepts)}") | |
| for i, c in enumerate(result['l1_output'].concepts[:3], 1): | |
| print(f" {i}. {c.term if hasattr(c, 'term') else str(c)}") | |
| print(f"\n[4] L2 (JuΓzos Kantianos) - AnΓ‘lise e Enriquecimento") | |
| print(f" β JuΓzos gerados: {len(result['l2_output'].judgments)}") | |
| if result['l2_output'].top_judgment: | |
| judgment_str = str(result['l2_output'].top_judgment) | |
| print(f" β Top Judgment: {judgment_str[:120]}...") | |
| print(f"\n[5] Context Injection") | |
| print(f" β System Prompt: {result['system_prompt'][:100]}...") | |
| print(f" β Contexto compilado: {len(result['compiled_context'])} caracteres") | |
| print(f"\n[6] SaΓda Final (pronta para LLM)") | |
| print(f" β RAG Context Summary: {result['rag_context_summary']}") | |
| return result | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # MAIN: Executa todos os exemplos | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def main(): | |
| """Executa todos os exemplos.""" | |
| print("\n" + "="*70) | |
| print("DEMONSTRAΓΓO: RAG HΓbrido com Context Injection (L1-L2)") | |
| print("="*70) | |
| try: | |
| # Exemplo 1: RAG bΓ‘sico | |
| example_1_basic_rag() | |
| except Exception as e: | |
| print(f"\nβ Exemplo 1 falhou: {e}") | |
| try: | |
| # Exemplo 2: L1-L2-RAG pipeline | |
| example_2_l1_l2_rag_pipeline() | |
| except Exception as e: | |
| print(f"\nβ Exemplo 2 falhou: {e}") | |
| try: | |
| # Exemplo 3: Domain-specific injection | |
| example_3_domain_specific_injection() | |
| except Exception as e: | |
| print(f"\nβ Exemplo 3 falhou: {e}") | |
| try: | |
| # Exemplo 4: Strategy comparison | |
| example_4_strategy_comparison() | |
| except Exception as e: | |
| print(f"\nβ Exemplo 4 falhou: {e}") | |
| try: | |
| # Exemplo 5: LLM formatted output | |
| example_5_llm_formatted_output() | |
| except Exception as e: | |
| print(f"\nβ Exemplo 5 falhou: {e}") | |
| try: | |
| # Exemplo 6: Custom domains | |
| example_6_custom_domains() | |
| except Exception as e: | |
| print(f"\nβ Exemplo 6 falhou: {e}") | |
| try: | |
| # Exemplo 7: End-to-end | |
| example_7_end_to_end_pipeline() | |
| except Exception as e: | |
| print(f"\nβ Exemplo 7 falhou: {e}") | |
| print("\n" + "="*70) | |
| print("β DemonstraΓ§Γ£o concluΓda!") | |
| print("="*70 + "\n") | |
| if __name__ == "__main__": | |
| main() | |