beriond commited on
Commit
f1fb8c6
路
verified 路
1 Parent(s): 74bab5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -10,15 +10,14 @@ from langchain.memory import ConversationBufferMemory
10
  from langchain.agents import Tool, initialize_agent
11
  from langchain.agents.agent_types import AgentType
12
 
13
-
14
- # 馃敼 Inicializa pipeline com modelo leve
15
- pipe = pipeline("text2text-generation", model="google/flan-t5-small", max_new_tokens=50)
16
  llm = HuggingFacePipeline(pipeline=pipe)
17
 
18
- # 馃敼 Mem贸ria de conversa para a cadeia de documentos
19
  memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
20
 
21
- # 馃敼 Carregamento e prepara莽茫o do documento
22
  def carregar_documento_txt(path):
23
  loader = TextLoader(path, encoding="latin-1")
24
  documentos = loader.load()
@@ -30,23 +29,19 @@ def carregar_documento_txt(path):
30
 
31
  vectorstore = carregar_documento_txt("meu_documento.txt")
32
 
33
- # 馃敼 Cadeia de busca com mem贸ria
34
  chat_chain = ConversationalRetrievalChain.from_llm(
35
  llm=llm,
36
  retriever=vectorstore.as_retriever(),
37
  memory=memory,
38
  )
39
 
40
- # 馃敼 Define ferramentas para o agente
41
-
42
- # 1. Responder baseado no documento
43
  def responder_documento(pergunta):
44
  return chat_chain.run({"question": pergunta})
45
 
46
- # 2. Responder de forma geral (chat simples)
47
  def responder_geral(pergunta):
48
- # Apenas usa o LLM para responder livremente
49
- return llm.run(pergunta)
50
 
51
  tools = [
52
  Tool(
@@ -61,15 +56,15 @@ tools = [
61
  )
62
  ]
63
 
64
- # Inicializa agente com as ferramentas acima
65
  agent = initialize_agent(
66
  tools=tools,
67
  llm=llm,
68
  agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
69
  verbose=False,
 
70
  )
71
 
72
- # Interface Gradio com hist贸rico
73
  with gr.Blocks() as demo:
74
  gr.Markdown("## 馃 Chat IA com Agente e Documento TXT")
75
 
@@ -78,7 +73,10 @@ with gr.Blocks() as demo:
78
  estado_chat = gr.State([])
79
 
80
  def interagir(msg, history):
81
- resposta = agent.run(msg)
 
 
 
82
  history = history + [(msg, resposta)]
83
  return "", history
84
 
 
10
  from langchain.agents import Tool, initialize_agent
11
  from langchain.agents.agent_types import AgentType
12
 
13
+ # Inicializa pipeline com modelo leve (aumentei max_new_tokens para evitar truncamento)
14
+ pipe = pipeline("text2text-generation", model="google/flan-t5-small", max_new_tokens=100)
 
15
  llm = HuggingFacePipeline(pipeline=pipe)
16
 
17
+ # Mem贸ria de conversa
18
  memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
19
 
20
+ # Fun莽茫o para carregar documento txt
21
  def carregar_documento_txt(path):
22
  loader = TextLoader(path, encoding="latin-1")
23
  documentos = loader.load()
 
29
 
30
  vectorstore = carregar_documento_txt("meu_documento.txt")
31
 
32
+ # Cadeia de busca com mem贸ria
33
  chat_chain = ConversationalRetrievalChain.from_llm(
34
  llm=llm,
35
  retriever=vectorstore.as_retriever(),
36
  memory=memory,
37
  )
38
 
39
+ # Ferramentas para o agente
 
 
40
  def responder_documento(pergunta):
41
  return chat_chain.run({"question": pergunta})
42
 
 
43
  def responder_geral(pergunta):
44
+ return llm.invoke(pergunta)
 
45
 
46
  tools = [
47
  Tool(
 
56
  )
57
  ]
58
 
 
59
  agent = initialize_agent(
60
  tools=tools,
61
  llm=llm,
62
  agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
63
  verbose=False,
64
+ handle_parsing_errors=True,
65
  )
66
 
67
+ # Interface Gradio
68
  with gr.Blocks() as demo:
69
  gr.Markdown("## 馃 Chat IA com Agente e Documento TXT")
70
 
 
73
  estado_chat = gr.State([])
74
 
75
  def interagir(msg, history):
76
+ try:
77
+ resposta = agent.invoke(msg)
78
+ except Exception as e:
79
+ resposta = f"Erro ao gerar resposta: {e}"
80
  history = history + [(msg, resposta)]
81
  return "", history
82