Feat: Final changes to prompt

This commit is contained in:
2025-10-22 16:44:01 -03:00
parent f2df55f0f9
commit 42f9d22c77
32 changed files with 1122 additions and 1607 deletions

View File

@@ -1,37 +1,22 @@
import json
from langchain_core.tools import tool
from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser
from langchain_core.prompts import ChatPromptTemplate,MessagesPlaceholder, PromptTemplate
from langchain_core.messages import HumanMessage,AIMessage
from langchain_core.tools import render_text_description
from langchain.chains import create_history_aware_retriever
from langchain.chains.combine_documents import create_stuff_documents_chain
import langchain.chains
from langchain.chains import create_history_aware_retriever
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.agents.format_scratchpad import format_log_to_str
from langchain.chains import create_retrieval_chain
from langchain_aws import ChatBedrock
from langchain_aws.retrievers import AmazonKnowledgeBasesRetriever
from langchain.chains import ConversationalRetrievalChain
from typing import Union
from langchain_core.agents import AgentAction, AgentFinish
from langchain.agents.output_parsers import ReActSingleInputOutputParser
from langchain.tools import Tool
from langgraph.checkpoint.memory import MemorySaver
from langgraph.prebuilt import create_react_agent
import os
def find_tool_by_name(tools: list[Tool],tool_name:str):
for tool in tools:
if tool.name==tool_name:
print(tool.name)
print("\n\n")
return tool
raise ValueError(f"Tool with name {tool_name} not found")
def hello(event,context):
from langfuse import Langfuse
from langfuse.langchain import CallbackHandler
from tools import secrets,dynamo
import time
langfuse = Langfuse(
public_key=json.loads(secrets.get_secret())['api-langfuse-public'],
secret_key=json.loads(secrets.get_secret())['api-langfuse-secret'],
host="http://107.20.48.139:3000"
)
langfuse_handler = CallbackHandler()
def agent_call(event,context):
llm = ChatBedrock(
model_id="arn:aws:bedrock:us-east-1:654654422992:application-inference-profile/glvyppv11ds5",
model_id="us.anthropic.claude-sonnet-4-20250514-v1:0",
region_name="us-east-1",
#aws_access_key_id=os.environ["AWS_ACCESS_KEY_ID"],
#aws_secret_access_key=os.environ["AWS_SECRET_ACCESS_KEY"],
@@ -45,6 +30,10 @@ def hello(event,context):
retrieval_config={"vectorSearchConfiguration": {"numberOfResults": 4}},
)
if event['chat_history']==[]:
history=dynamo.read_memory(event['username'])
else:
history=event['chat_history']
memory = MemorySaver()
model = llm
tools = [retriever.as_tool()]
@@ -53,18 +42,47 @@ def hello(event,context):
Act like a human in Portuguese Brasil.
You are a assistant for students from diferent school campuses of the Instituto Federal de São Paulo.
Your objective is to answer diferent questions about editais de bolsas, which can be acessed by the AmazonKnowledgeBaseRetriever tool.
The editais change based on the student's campus, if he is a superior or médio student, and if he is already receiving any bolsa or not. Get these informations from the user, only prompting one per message to the user, before ansewering any questions.
The editais change based on the student's campus, if he is a superior or médio student, and if he is already receiving any type of "Bolsa PAP", not including "Iniciação Cientifica" or "bolsa de extensão" . Get these informations from the user, only prompting one per message to the user, before ansewering any questions.
The criteria for every type of "bolsa" is different, look at the editals to check. Been a student, who doesnt have a bolsa for any campi is not enough for any bolsa, so check for the extra conditions.
Retrieve the answer from the most recent edital that contemplates this type of student.
The chat history will be given, without the previous retrieved editais info.
If there are info or context missing ask the user before proceding with the document retrie
Also return the title of the source document, with edital number and year, not the file name.val.
Document with MTO are from "Campus Matão" and SPO from "Campus São Paulo".
Document with MTO are from "Campus Matão", SPO from "Campus São Paulo" and CBT from "Campus Cubatão".
If you are prompted after a campus that is not Matão, São Paulo or Cubatão, answer that you can only work with one of these.
Don't get information from diferent campus under no circunstance.
You cant evaluate if someone can receive a bolsa, only get the info from docs and send them to the procedure in the document.
If the prompt is too generic like or too short, like "sim" or "quero bolsa", ask for more context before retrieving from the documents.
If the user prompt you about wanting to get or to know more about a bolsa pap, ask wich of them (saúde,moradia,transporte,creche didático-pedagócio) they want to know more.
<\rules>
<glossary>
"não registrado" means trabalhador informal.
"banco inter" means "banco intermedium", if a user asks about inter, include in the answer this info.
<\glossary>
<chain_of_thought>
Use these questions as examples:
Quando a minha bolsa vai cair?
Resposta: Devido a complexidade da estrtura em volta do pagamento das bolsas, não existe um dia fixo, ele tende a variar dependendo do mês.
Não tenho conta bancária em meu nome, pode ser no nome do meu responsável?
Resposta: Não! A conta deve ser aberta no nome do estudante e pode ser em bancos digitais sem que seja necessário sair de casa, fazendo tudo pela internet!
Posso enviar a Documentação solicitada no Edital por e-mail?
Resposta: Não! Documentos enviados por e-mail não serão considerados para análise.
A comprovação de renda deve ser referente ao qual mês?
Resposta: agosto que é o mês mais recente em relação a inscrição.
Não tenho a Carteira de Trabalho impressa. Serve a digital?
Resposta: Sim!
Na carteira de trabalho consta o salário. Ela substitui o contracheque?
Resposta: Não, pois somente no contracheque encontramos o valor da renda BRUTA.
Trabalhador informal e trabalhador autônomo são sinônimos?
Resposta: Não! Trabalhadores informais são os que trabalham por conta própria, NÃO tem CNPJ e geralmente não pagam o carnê do INSS, não tendo nenhum direito trabalhista. Diferente deste, o profissional autônomo geralmente é especializado em algum segmento do mercado e atua por conta própria, geralmente sendo MEI Micro Empreendedores Individuais e tendo CNPJ.
<\chain_of_thought>
<general_info>
Tipos de bolsa:
Auxílio Alimentação: É um auxílio financeiro que tem por objetivo garantir e disponibilizar condições de que o estudante usufrua de ao menos uma refeição por dia. É pago mensalmente, e caso o aluno não frequente as aulas todos os dias ele poderá ser oferecido de forma parcial, calculado pela quantidade de dias que o aluno frequenta a aula.
@@ -99,13 +117,16 @@ Answer the following questions as best you can. You have access to the following
{tools}
Chat History:"""+str(event["chat_history"])
Chat History:"""+str(history)
agent_executor = create_react_agent(model, tools, checkpointer=memory, prompt=prompt)
config = {"configurable": {"thread_id": "abc123"}}
config = {"configurable": {"thread_id": "abc123"},"callbacks": [langfuse_handler]}
input_message = event["message"]
#input_message=[{"role":"user","content":"aluno superior, nunca recebi auxilio, campus são paulo, Meu pai não é registrado, como faço para ganhar auxilio?"}]
dict=input_message[0]
response=""
for step in agent_executor.stream({"messages": input_message}, config, stream_mode="values"):
response={"json":(step["messages"][-1].text())}
response['dynamo_reponse']=dynamo.write_memory(event['username'],int(time.time()),dict['role'],dict['content'])
#response['chat_history']=history
response['chat_history']=[]
return (response)