beriond commited on
Commit
adb5e9f
路
verified 路
1 Parent(s): 1f5fb8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -10,20 +10,20 @@ from langchain.memory import ConversationBufferMemory
10
  from langchain.agents import Tool, initialize_agent
11
  from langchain.agents.agent_types import AgentType
12
 
13
- # Pipeline com limite maior de tokens e truncamento ativado
14
  pipe = pipeline(
15
  "text2text-generation",
16
- model="google/flan-t5-small",
17
  max_new_tokens=100,
18
  truncation=True,
19
  max_length=512,
20
  )
21
  llm = HuggingFacePipeline(pipeline=pipe)
22
 
23
- # Mem贸ria limitada para 3 turnos
24
  memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True, k=3)
25
 
26
- # Carregamento do documento com chunks menores
27
  def carregar_documento_txt(path):
28
  loader = TextLoader(path, encoding="latin-1")
29
  documentos = loader.load()
@@ -35,14 +35,14 @@ def carregar_documento_txt(path):
35
 
36
  vectorstore = carregar_documento_txt("meu_documento.txt")
37
 
38
- # Cadeia de consulta com mem贸ria
39
  chat_chain = ConversationalRetrievalChain.from_llm(
40
  llm=llm,
41
  retriever=vectorstore.as_retriever(),
42
  memory=memory,
43
  )
44
 
45
- # Ferramentas para o agente
46
  def responder_documento(pergunta):
47
  return chat_chain.run({"question": pergunta})
48
 
@@ -53,15 +53,16 @@ tools = [
53
  Tool(
54
  name="Responder com Documento",
55
  func=responder_documento,
56
- description="Usa para responder perguntas espec铆ficas sobre o documento."
57
  ),
58
  Tool(
59
  name="Responder Geral",
60
  func=responder_geral,
61
- description="Usa para perguntas gerais ou bate-papo."
62
  )
63
  ]
64
 
 
65
  agent = initialize_agent(
66
  tools=tools,
67
  llm=llm,
@@ -81,9 +82,9 @@ with gr.Blocks() as demo:
81
  def interagir(msg, history):
82
  try:
83
  resposta = agent.invoke(msg)
84
- # Garantir resposta string para o chatbot
85
  if isinstance(resposta, dict):
86
- resposta = str(resposta)
87
  elif not isinstance(resposta, str):
88
  resposta = str(resposta)
89
  except Exception as e:
@@ -92,10 +93,8 @@ with gr.Blocks() as demo:
92
  return "", history
93
 
94
  entrada.submit(interagir, [entrada, estado_chat], [entrada, chatbot])
95
- enviar_btn = gr.Button("Enviar")
96
- enviar_btn.click(interagir, [entrada, estado_chat], [entrada, chatbot])
97
 
98
  demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
99
 
100
 
101
-
 
10
  from langchain.agents import Tool, initialize_agent
11
  from langchain.agents.agent_types import AgentType
12
 
13
+ # Modelo com melhor performance para agentes
14
  pipe = pipeline(
15
  "text2text-generation",
16
+ model="declare-lab/flan-alpaca-base",
17
  max_new_tokens=100,
18
  truncation=True,
19
  max_length=512,
20
  )
21
  llm = HuggingFacePipeline(pipeline=pipe)
22
 
23
+ # Mem贸ria limitada para evitar prompts muito longos
24
  memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True, k=3)
25
 
26
+ # Carrega e divide documento em partes menores
27
  def carregar_documento_txt(path):
28
  loader = TextLoader(path, encoding="latin-1")
29
  documentos = loader.load()
 
35
 
36
  vectorstore = carregar_documento_txt("meu_documento.txt")
37
 
38
+ # Cadeia de busca com mem贸ria
39
  chat_chain = ConversationalRetrievalChain.from_llm(
40
  llm=llm,
41
  retriever=vectorstore.as_retriever(),
42
  memory=memory,
43
  )
44
 
45
+ # Ferramentas do agente
46
  def responder_documento(pergunta):
47
  return chat_chain.run({"question": pergunta})
48
 
 
53
  Tool(
54
  name="Responder com Documento",
55
  func=responder_documento,
56
+ description="Use para responder perguntas espec铆ficas sobre o documento."
57
  ),
58
  Tool(
59
  name="Responder Geral",
60
  func=responder_geral,
61
+ description="Use para perguntas gerais ou bate-papo."
62
  )
63
  ]
64
 
65
+ # Agente com tratamento de parsing
66
  agent = initialize_agent(
67
  tools=tools,
68
  llm=llm,
 
82
  def interagir(msg, history):
83
  try:
84
  resposta = agent.invoke(msg)
85
+ # Extrai texto se for dict
86
  if isinstance(resposta, dict):
87
+ resposta = resposta.get('output', str(resposta))
88
  elif not isinstance(resposta, str):
89
  resposta = str(resposta)
90
  except Exception as e:
 
93
  return "", history
94
 
95
  entrada.submit(interagir, [entrada, estado_chat], [entrada, chatbot])
96
+ gr.Button("Enviar").click(interagir, [entrada, estado_chat], [entrada, chatbot])
 
97
 
98
  demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
99
 
100