File size: 2,717 Bytes
8752160
46e8b1c
8752160
 
46e8b1c
8752160
 
 
 
 
 
 
 
46e8b1c
 
 
 
 
 
 
8752160
 
 
 
 
 
46e8b1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8752160
 
 
46e8b1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8752160
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import json
from dotenv import load_dotenv
from retriever import db_chroma
from prompts import PROMPT_TEMPLATE, CLASSIFIER_PROMPT
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

load_dotenv()

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

def main():
    # Bom modelo para classificação e respostas
    model = ChatOpenAI(model_name='gpt-4o-mini', openai_api_key=os.getenv("OPENAI_API_KEY"))

    with open('know_graph.json', 'r', encoding='utf-8') as f:
        knowledge_graph_data = json.dumps(json.load(f), ensure_ascii=False)

    chat_history = ""  # Histórico inicial 
    
    while True:
        query = input("Digite sua pergunta (para sair digite 'sair'): ")
        if query.lower() == 'sair':
            break

        # Classificação inicial
        prompt_classifier = ChatPromptTemplate.from_template(CLASSIFIER_PROMPT)
        classifier_input = prompt_classifier.format(
            knowledge_graph=knowledge_graph_data,
            chat_history=chat_history,
            question=query
        )

        classification_response = model.invoke(classifier_input).content

        cleaned_response = classification_response.replace("```json", "").replace("```", "").strip()
        try:
            classification = json.loads(cleaned_response)
        except:
            classification = {"estagio": "Nao identificado", "objecao_id": "nenhuma", "argumento_base": ""}

        # Busca de contexto relevante
        docs_chroma = db_chroma.similarity_search(query, k=5)
        context_text = "\n\n".join([doc.page_content for doc in docs_chroma])

        diretriz_venda = f"\n[DIRETRIZ ESTRATÉGICA]: Estágio: {classification.get('estagio')}. " \
                         f"Objeção: {classification.get('objecao_id')}. " \
                         f"Argumento sugerido: {classification.get('argumento_base')}"
        
        full_context = context_text + diretriz_venda

        prompt_final = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
        final_input = prompt_final.format(
            context=full_context,
            chat_history=chat_history,
            question=query
        )

        response = model.invoke(final_input)
        
        print(f"\n--- LOG DE VENDAS ---")
        print(f"📍 Estágio: {classification.get('estagio')}")
        print(f"⚠️ Objeção: {classification.get('objecao_id')}")
        print(f"---------------------\n")
        print('***Resposta:*** \n\n' + response.content)

        # Atualizar histórico (simplificado)
        chat_history += f"\nUsuário: {query}\nAssistente: {response.content}"


        print("\n" + "-"*50 + "\n")
if __name__ == "__main__":
    main()