File size: 8,328 Bytes
45fb393 c8e6eb5 45fb393 c8e6eb5 45fb393 c8e6eb5 45fb393 c8e6eb5 45fb393 c8e6eb5 45fb393 c8e6eb5 45fb393 c8e6eb5 45fb393 c8e6eb5 45fb393 310f427 45fb393 310f427 45fb393 310f427 45fb393 c8e6eb5 45fb393 c8e6eb5 45fb393 c8e6eb5 45fb393 c8e6eb5 45fb393 c8e6eb5 45fb393 310f427 45fb393 c8e6eb5 310f427 45fb393 310f427 45fb393 310f427 45fb393 0fa51ba 45fb393 310f427 45fb393 310f427 45fb393 310f427 45fb393 310f427 45fb393 c8e6eb5 45fb393 0fa51ba 45fb393 310f427 45fb393 310f427 45fb393 310f427 45fb393 |
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
import os
import time
from dotenv import load_dotenv
from operator import itemgetter
from typing_extensions import TypedDict
from typing import List
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.output_parsers import StrOutputParser
from langchain_community.vectorstores import FAISS
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import PyMuPDFLoader
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings.fastembed import FastEmbedEmbeddings
from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import FlashrankRerank
from langchain.schema import Document
from langgraph.graph import END, StateGraph
from groq import Groq
from langchain_groq import ChatGroq
from utils import get_payroll_api_schema, dummy_payroll_api_call
load_dotenv()
# Setup the models
embed_model = FastEmbedEmbeddings(model_name="snowflake/snowflake-arctic-embed-m")
llm = ChatGroq(temperature=0,
model_name="Llama3-8b-8192",
api_key=os.getenv("GROQ_API_KEY"),)
# Load the documents
loader = PyMuPDFLoader("https://home.synise.com/HRUtility/Documents/HRA/UmaP/Synise%20Handbook.pdf")
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1500, chunk_overlap=200
)
doc_splits = text_splitter.split_documents(documents)
vectorstore = FAISS.from_documents(documents=doc_splits,embedding=embed_model)
# Setup the retriever
compressor = FlashrankRerank()
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 20})
compression_retriever = ContextualCompressionRetriever(
base_compressor=compressor, base_retriever=retriever
)
# Define RAG Chain
RAG_PROMPT_TEMPLATE = """
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
Answer the question based only on the provided context. If you cannot answer the question with the provided context, please respond with 'I don't know" without any preamble, explanation, or additional text.
Context:
{context}
Question:
{question}
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
rag_prompt = PromptTemplate(
template=RAG_PROMPT_TEMPLATE, input_variables=["question", "context"]
)
response_chain = (rag_prompt
| llm
| StrOutputParser()
)
# Setup Router Chain
ROUTER_AGENT_PROMPT_TEMPLATE = """
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are an expert at delegating user questions to one of the most appropriate agents 'raqa' or 'payroll'.
Use the following criteria to determine the appropriate agents to answer the user que:
- If the query is regarding payslips, salary, tax deductions, basepay of a given month, use 'payroll'.
- If the question is closely related to general human resource queries, organisational policies, prompt engineering, or adversarial attacks, even if the keywords are not explicitly mentioned, use the 'raqa'.
Your output should be a JSON object with a single key 'agent' and a value of either 'raqa' or 'payroll'. Do not include any preamble, explanation, or additional text.
User's Question: {question}
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
router_prompt = PromptTemplate(
template=ROUTER_AGENT_PROMPT_TEMPLATE, input_variables=["question"]
)
router_chain = router_prompt | llm | JsonOutputParser()
payroll_schema = get_payroll_api_schema()
# Define Filter Extraction Chain
FILTER_EXTTRACTION_PROMPT = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>
Extract the month and year from a given user question about payroll. Use the following schema instructions to guide your extraction.
Instructions:
1. Your output should be a JSON object with only two keys, 'month' and 'year'.
2. 'month' key shall have value ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]
3. 'year' shall be a number between 2020 and 2024.
4. If the user is suggesting current year or month, respond with "CUR" for 'month' and 'year' keys accordingly
5. If the user is suggesting previous year or month, respond with "PREV" for 'month' and 'year' keys accordingly
Do not include any preamble, explanation, or additional text.
User Question: {question}
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
filter_extraction_prompt = PromptTemplate(
template=FILTER_EXTTRACTION_PROMPT, input_variables=["question"]
)
fiter_extraction_chain = filter_extraction_prompt | llm | JsonOutputParser()
# Define Payroll QA Chain
PAYROLL_QA_PROMPT = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>
Answer the user query given the provided payroll data in json form. Use the provided schema to understand the payroll data structure. If you cannot answer the question with the provided information, please respond with 'I don't know" without any preamble, explanation, or additional text
SCHEMA:
{schema}
PAYROLL DATA
{data}
PAYROLL DATA:
{data}
User Question: {question}
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
payroll_qa_prompt = PromptTemplate(
template=PAYROLL_QA_PROMPT, input_variables=["question", "data", "schema"]
)
########### Create Nodes Actions ###########
class AgentState(TypedDict):
question : str
answer : str
documents : List[str]
def route_question(state):
"""
Route question to payroll_agent or policy_agent to retrieve reevant data
Args:
state (dict): The current graph state
Returns:
str: Next node to call
"""
print("---ROUTING---")
question = state["question"]
result = router_chain.invoke({"question": question})
return result["agent"]
state = AgentState(question="What is my salary on jan 2024 ?", answer="", documents=None)
route_question(state)
def retrieve(state):
"""
Retrieve documents from vectorstore
Args:
state (dict): The current graph state
Returns:
state (dict): New key added to state, documents, that contains retrieved documents
"""
print("---RETRIEVE DOCUMENTS---")
question = state["question"]
documents = compression_retriever.invoke(question)
return {"documents": documents, "question": question}
# state = AgentState(question="What is leave policy?", answer="", documents=None)
# retrieve_policy(state)
def generate(state):
"""
Generate answer using retrieved data
Args:
state (dict): The current graph state
Returns:
state (dict): New key added to state, generation, that contains LLM generation
"""
print("---GENERATE ANSWER---")
question = state["question"]
documents = state["documents"]
answer = response_chain.invoke({"context": documents, "question": question})
return {"documents": documents, "question": question, "answer": answer}
def payroll(state):
"""
Query payroll api to retrieve payroll data
Args:
state (dict): The current graph state
Returns:
state (dict): Updated state with retrived payroll data
"""
print("---QUERY PAYROLL API---")
question = state["question"]
payroll_query_filters = fiter_extraction_chain.invoke({"question":question})
payroll_api_query_results = dummy_payroll_api_call(1234, payroll_query_filters["month"], payroll_query_filters["year"])
context = context = 'PAYROLL DATA SCHEMA: \n {payroll_schema} \n PAYROLL DATA: {payroll_api_query_results}'.format(
payroll_schema=payroll_schema, payroll_api_query_results=payroll_api_query_results)
documents = [Document(page_content=context)]
return {"documents": documents, "question": question}
########### Build Execution Graph ###########
workflow = StateGraph(AgentState)
# Define the nodes
workflow.add_node("payroll", payroll)
workflow.add_node("retrieve", retrieve)
workflow.add_node("generate", generate)
workflow.set_conditional_entry_point(
route_question,
{
"payroll": "payroll",
"raqa": "retrieve",
},
)
workflow.add_edge("payroll", "generate")
workflow.add_edge("retrieve", "generate")
workflow.add_edge("generate", END)
app = workflow.compile()
|