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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684

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
import os
from dotenv import load_dotenv
load_dotenv()

embed_model = FastEmbedEmbeddings(model_name="snowflake/snowflake-arctic-embed-m")

from groq import Groq
from langchain_groq import ChatGroq


llm = ChatGroq(temperature=0,
                      model_name="Llama3-8b-8192",
                      api_key=os.getenv("GROQ_API_KEY"),)

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)

print(len(doc_splits),doc_splits[0])

vectorstore = FAISS.from_documents(documents=doc_splits,embedding=embed_model)

from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import FlashrankRerank

compressor = FlashrankRerank()
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 20})
compression_retriever = ContextualCompressionRetriever(
    base_compressor=compressor, base_retriever=retriever
)

from operator import itemgetter
from langchain.prompts import PromptTemplate
from langchain.schema.runnable import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser


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()

)

def dummy_payroll_api_call(employee_id, month, year):

  data = {
    2023: {
        "MAY": {
            "employeeDetails": {
                "employeeId": "E2468",
                "firstName": "Sarah",
                "lastName": "Thompson",
                "designation": "Product Manager"
            },
            "paymentDetails": {
                "year": 2023,
                "month": "JAN",
                "basicSalary": 5500,
                "allowances": [
                    {
                        "type": "Housing Allowance",
                        "amount": 1500
                    },
                    {
                        "type": "Travel Allowance",
                        "amount": 800
                    }
                ],
                "deductions": [
                    {
                        "type": "Provident Fund",
                        "amount": 650
                    },
                    {
                        "type": "Health Insurance",
                        "amount": 300
                    }
                ],
                "taxes": [
                    {
                        "type": "Income Tax",
                        "amount": 1300
                    }
                ],
                "grossSalary": 7800,
                "totalDeductions": 2250,
                "netSalary": 6650
            },
            "companyDetails": {
                "companyName": "Tech Solutions Ltd.",
                "address": "789 Maple Avenue, City"
            }
        }
    },
    2024: {
        "JAN": {
            "employeeDetails": {
                "employeeId": "E2468",
                "firstName": "Sarah",
                "lastName": "Thompson",
                "designation": "Product Manager"
            },
            "paymentDetails": {
                "year": 2024,
                "month": "JAN",
                "basicSalary": 6500,
                "allowances": [
                    {
                        "type": "Housing Allowance",
                        "amount": 1500
                    },
                    {
                        "type": "Travel Allowance",
                        "amount": 800
                    }
                ],
                "deductions": [
                    {
                        "type": "Provident Fund",
                        "amount": 650
                    },
                    {
                        "type": "Health Insurance",
                        "amount": 300
                    }
                ],
                "taxes": [
                    {
                        "type": "Income Tax",
                        "amount": 1300
                    }
                ],
                "grossSalary": 8800,
                "totalDeductions": 2250,
                "netSalary": 6550
            },
            "companyDetails": {
                "companyName": "Tech Solutions Ltd.",
                "address": "789 Maple Avenue, City"
            }
        },
        "FEB": {
            "employeeDetails": {
                "employeeId": "E2468",
                "firstName": "Sarah",
                "lastName": "Thompson",
                "designation": "Product Manager"
            },
            "paymentDetails": {
                "year": 2024,
                "month": "FEB",
                "basicSalary": 6500,
                "allowances": [
                    {
                        "type": "Housing Allowance",
                        "amount": 1500
                    },
                    {
                        "type": "Travel Allowance",
                        "amount": 800
                    }
                ],
                "deductions": [
                    {
                        "type": "Provident Fund",
                        "amount": 650
                    },
                    {
                        "type": "Health Insurance",
                        "amount": 300
                    }
                ],
                "taxes": [
                    {
                        "type": "Income Tax",
                        "amount": 1300
                    }
                ],
                "grossSalary": 8800,
                "totalDeductions": 2250,
                "netSalary": 6550
            },
            "companyDetails": {
                "companyName": "Tech Solutions Ltd.",
                "address": "789 Maple Avenue, City"
            }
        },
                "MAY": {
            "employeeDetails": {
                "employeeId": "E2468",
                "firstName": "Sarah",
                "lastName": "Thompson",
                "designation": "Product Manager"
            },
            "paymentDetails": {
                "year": 2024,
                "month": "MAY",
                "basicSalary": 6500,
                "allowances": [
                    {
                        "type": "Housing Allowance",
                        "amount": 1500
                    },
                    {
                        "type": "Travel Allowance",
                        "amount": 800
                    }
                ],
                "deductions": [
                    {
                        "type": "Provident Fund",
                        "amount": 650
                    },
                    {
                        "type": "Health Insurance",
                        "amount": 300
                    }
                ],
                "taxes": [
                    {
                        "type": "Income Tax",
                        "amount": 1500
                    }
                ],
                "grossSalary": 8800,
                "totalDeductions": 2450,
                "netSalary": 6350
            },
            "companyDetails": {
                "companyName": "Tech Solutions Ltd.",
                "address": "789 Maple Avenue, City"
            }
        },
        "APR": {
            "employeeDetails": {
                "employeeId": "E2468",
                "firstName": "Sarah",
                "lastName": "Thompson",
                "designation": "Product Manager"
            },
            "paymentDetails": {
                "year": 2024,
                "month": "APR",
                "basicSalary": 6500,
                "allowances": [
                    {
                        "type": "Housing Allowance",
                        "amount": 1500
                    },
                    {
                        "type": "Travel Allowance",
                        "amount": 800
                    }
                ],
                "deductions": [
                    {
                        "type": "Provident Fund",
                        "amount": 650
                    },
                    {
                        "type": "Health Insurance",
                        "amount": 300
                    }
                ],
                "taxes": [
                    {
                        "type": "Income Tax",
                        "amount": 1500
                    }
                ],
                "grossSalary": 8800,
                "totalDeductions": 2450,
                "netSalary": 6350
            },
            "companyDetails": {
                "companyName": "Tech Solutions Ltd.",
                "address": "789 Maple Avenue, City"
            }
        }
    }
}
  year= 2024 if year == "CUR" else year
  year= 2023 if year == "PREV" else year

  month= "MAY" if month == "CUR" else month
  month= "APR" if month == "PREV" else month


  return data[year][month]

print(dummy_payroll_api_call(1234, 'CUR', 2024))

import time
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.output_parsers import StrOutputParser

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 'policy_agent' or 'payroll_agent'.

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_agent'.
- 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 'policyagent'.

Your output should be a JSON object with a single key 'agent' and a value of either 'policy_agent' or 'payroll_agent'. 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()

print(router_chain.invoke({"question":"What is my salary on 6 2024 ?"}))

print(router_chain.invoke({"question":"What is leave policy ?"}))

payroll_schema= {
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Monthly Payslip",
  "description": "A schema for a monthly payslip",
  "type": "object",
  "properties": {
    "employeeDetails": {
      "type": "object",
      "properties": {
        "employeeId": {
          "type": "string",
          "description": "Unique identifier for the employee"
        },
        "firstName": {
          "type": "string",
          "description": "First name of the employee"
        },
        "lastName": {
          "type": "string",
          "description": "Last name of the employee"
        },
        "designation": {
          "type": "string",
          "description": "Designation or job title of the employee"
        }
      },
      "required": ["employeeId", "firstName", "lastName", "designation"]
    },
    "paymentDetails": {
      "type": "object",
      "properties": {
        "year": {
          "type": "integer",
          "description": "Year of the pay period"
        },
        "month": {
          "type": "string",
          "enum": ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"],
          "description": "Month of the pay period"
        },
        "basicSalary": {
          "type": "number",
          "description": "Basic salary of the employee"
        },
        "allowances": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "type": {
                "type": "string",
                "description": "Type of allowance"
              },
              "amount": {
                "type": "number",
                "description": "Amount of the allowance"
              }
            },
            "required": ["type", "amount"]
          }
        },
        "deductions": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "type": {
                "type": "string",
                "description": "Type of deduction"
              },
              "amount": {
                "type": "number",
                "description": "Amount of the deduction"
              }
            },
            "required": ["type", "amount"]
          }
        },
        "taxes": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "type": {
                "type": "string",
                "description": "Type of tax"
              },
              "amount": {
                "type": "number",
                "description": "Amount of the tax"
              }
            },
            "required": ["type", "amount"]
          }
        },
        "grossSalary": {
          "type": "number",
          "description": "Gross salary (basic salary + allowances)"
        },
        "totalDeductions": {
          "type": "number",
          "description": "Total deductions (including taxes)"
        },
        "netSalary": {
          "type": "number",
          "description": "Net salary (gross salary - total deductions)"
        }
      },
      "required": ["year", "month", "basicSalary", "allowances", "deductions", "taxes", "grossSalary", "totalDeductions", "netSalary"]
    },
    "companyDetails": {
      "type": "object",
      "properties": {
        "companyName": {
          "type": "string",
          "description": "Name of the company"
        },
        "address": {
          "type": "string",
          "description": "Address of the company"
        }
      },
      "required": ["companyName", "address"]
    }
  },
  "required": ["employeeDetails", "paymentDetails", "companyDetails"]
}

print(str(payroll_schema))

import time
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.output_parsers import StrOutputParser

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()

print(fiter_extraction_chain.invoke({"question":"What is my salary on 6 2024 ?"}))

import time
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.output_parsers import StrOutputParser

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"]
)

payroll_qa_chain = payroll_qa_prompt | llm | StrOutputParser()

result = fiter_extraction_chain.invoke({"question":"What is my salary on jan 2024 ?"})

result

api_result = dummy_payroll_api_call(1234, result["month"], result["year"])

api_result

payroll_qa_chain.invoke({"question":"What is my salary on jan 2024 ?", "data":api_result, "schema":payroll_schema})

from typing_extensions import TypedDict
from typing import List

### State

class AgentState(TypedDict):
    question : str
    answer : str
    documents : List[str]

import logging as log

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
    """

    question = state["question"]
    result = router_chain.invoke({"question": question})

    log.debug('Routing to {}....'.format(result["agent"]))

    if result['agent'] == 'payroll_agent':
        log.debug('Routing to {}....'.format(result["agent"]))
        return "payroll_agent"
    elif result['agent'] == 'policy_agent':
        log.debug('Routing to {}....'.format(result["agent"]))
        return "policy_agent"

state = AgentState(question="What is my salary on jan 2024 ?", answer="", documents=None)
route_question(state)

from langchain.schema import Document
def retrieve_policy(state):
    """
    Retrieve policy documents from vectorstore

    Args:
        state (dict): The current graph state

    Returns:
        state (dict): New key added to state, documents, that contains retrieved documents
    """
    log.debug("Retreiving policy 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_answer(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
    """
    log.debug("Generating answer.......")
    question = state["question"]
    documents = state["documents"]


    answer = response_chain.invoke({"context": documents, "question": question})

    return {"documents": documents, "question": question, "answer": answer}

state = AgentState(question="What is leave policy?", answer="", documents=[Document(page_content="According to leave policy, there are two types of leaves 1: PL 2: CL")])
generate_answer(state)

def query_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
    """


    question = state["question"]
    payroll_query_filters = fiter_extraction_chain.invoke({"question":question})
    payroll_api_query_results = dummy_payroll_api_call(1234, result["month"], result["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}

state = AgentState(question="Tell me salary for Jan 2024?", answer="", documents=None)
query_payroll(state)

from langgraph.graph import END, StateGraph
workflow = StateGraph(AgentState)

# Define the nodes
workflow.add_node("payroll_agent", query_payroll)
workflow.add_node("policy_agent", retrieve_policy)
workflow.add_node("generator_agent", generate_answer)

workflow.set_conditional_entry_point(
    route_question,
    {
        "payroll_agent": "payroll_agent",
        "policy_agent": "policy_agent",
    },
)
workflow.add_edge("payroll_agent", "generator_agent")
workflow.add_edge("policy_agent", "generator_agent")
workflow.add_edge("generator_agent", END)

app = workflow.compile()


from langchain_core.runnables import chain
@chain
def sage_chain(question):
  inputs = {"question": question}
  return app.invoke(inputs)