Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 169 additions & 22 deletions examples/email_reply_agent.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 1,
"metadata": {},
"outputs": [
{
Expand All @@ -11,15 +11,15 @@
"True"
]
},
"execution_count": 15,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from flo_ai import Flo\n",
"from flo_ai import FloSession\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain_openai import ChatOpenAI, OpenAIEmbeddings\n",
"\n",
"from dotenv import load_dotenv\n",
"load_dotenv()"
Expand All @@ -34,7 +34,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -76,7 +76,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -111,17 +111,104 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Register the tools"
"# Setup a retriver db"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/vizsatiz/Documents/hub/flo/.venv/lib/python3.11/site-packages/sentence_transformers/cross_encoder/CrossEncoder.py:11: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from tqdm.autonotebook import tqdm, trange\n"
]
}
],
"source": [
"# import\n",
"from langchain_chroma import Chroma\n",
"from langchain_community.document_loaders import TextLoader\n",
"from langchain_community.embeddings.sentence_transformer import (\n",
" SentenceTransformerEmbeddings,\n",
")\n",
"from langchain_text_splitters import CharacterTextSplitter\n",
"\n",
"# load the document and split it into chunks\n",
"loader = TextLoader(\"./rag_document.txt\")\n",
"documents = loader.load()\n",
"\n",
"# split it into chunks\n",
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
"docs = text_splitter.split_documents(documents)\n",
"\n",
"# create the open-source embedding function\n",
"embedding_function = SentenceTransformerEmbeddings(model_name=\"all-MiniLM-L6-v2\")\n",
"\n",
"# load it into Chroma\n",
"db = Chroma.from_documents(docs, embedding_function)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<flo_ai.state.flo_session.FloSession at 0x15d90ca90>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from flo_ai.retrievers.flo_retriever import FloRagBuilder\n",
"from flo_ai.retrievers.flo_compression_pipeline import FloCompressionPipeline\n",
"\n",
"llm = ChatOpenAI(temperature=0, model_name='gpt-4o')\n",
"session = FloSession(llm).register_tool(\n",
"session = FloSession(llm)\n",
"builder = FloRagBuilder(session, db.as_retriever())\n",
"compression_pipeline = FloCompressionPipeline(OpenAIEmbeddings(model=\"text-embedding-3-small\"))\n",
"compression_pipeline.add_embedding_reduntant_filter()\n",
"compression_pipeline.add_embedding_relevant_filter()\n",
"\n",
"retriever_tool = builder.with_multi_query().with_compression(compression_pipeline).build_retriever_tool(name=\"HousingLoanRetreiver\",\n",
" description=\"Tool to fetch data around housing loans\")\n",
"session.register_tool(name=\"HousingLoanTool\", tool=retriever_tool)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Register the tools"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<flo_ai.state.flo_session.FloSession at 0x15d90ca90>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"session.register_tool(\n",
" name=\"SendEmailTool\", \n",
" tool=SendEmailTool()\n",
").register_tool(\n",
Expand All @@ -139,7 +226,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -161,6 +248,10 @@
" prompt: You are capable of fetching any kind of transactions from the database given transaction reference id\n",
" tools:\n",
" - name: FetchTransactionTool\n",
" - name: HousingLoanTeamLead\n",
" prompt: Fetch the housing loan information from the db and answer the question\n",
" tools:\n",
" - name: HousingLoanTool\n",
"\"\"\""
]
},
Expand All @@ -176,36 +267,60 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"email_input = \"\"\"\n",
"email_input_for_transaction_without_id = \"\"\"\n",
"Hi Tom,\n",
"\n",
"We have a failed transaction. Can you tell me more about why this failed.\n",
"\n",
"Thanks\n",
"Vishnu\n",
"\"\"\"\n",
"\n",
"email_input_for_transaction_with_id = \"\"\"\n",
"Hi Tom,\n",
"\n",
"We have a failed transaction. Can you tell me more about why this failed.\n",
"\n",
"Transaction ID: 12123123432\n",
"\n",
"Thanks\n",
"Vishnu\n",
"\"\"\"\n",
"\n",
"email_input_for_housing_loan = \"\"\"\n",
"Hi Tom,\n",
"\n",
"I am looking for a housing loan, is the interest percentage on the loan\n",
"\n",
"The tenure is 8 years\n",
"Amount am looking for is 85 lakhs\n",
"And am looking for fixed rate housing loan\n",
"\n",
"Thanks\n",
"Vishnu\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"input_prompt = \"\"\"\n",
"You are a support ticket handling agent. You are expected to understand the email, and perform the following steps:\n",
"\n",
"1. Reply asking for more information if there is missing information in the email\n",
"2. Perform the neccessary steps to required to answer the users question and reply the user query.\n",
"2. Perform the neccessary steps to required to answer the users question and reply the user query as email.\n",
"3. If are not able to come up with a answer just reply I dont know over the email\n",
"\n",
"Email:\n",
"\n",
"\"\"\" + email_input"
"\"\"\" + email_input_for_transaction_with_id"
]
},
{
Expand All @@ -217,33 +332,65 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'SupportSupervisor-x7llc': {'next': 'TransactionFetcher-YdHZo'}}\n",
"{'SupportSupervisor-0XuY9': {'next': 'TransactionFetcher-7pEar'}}\n",
"----\n",
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3mHi Vishnu,\n",
"\u001b[32;1m\u001b[1;3m\n",
"Invoking: `fetch_transactions` with `{'reference_number': '12123123432'}`\n",
"\n",
"\n",
"\u001b[0m\u001b[36;1m\u001b[1;3mThe transaction happened on 23/07/2024 IST and it failed because there was not enough balance in the account\u001b[0m\u001b[32;1m\u001b[1;3mSubject: Re: Failed Transaction Inquiry\n",
"\n",
"Hi Vishnu,\n",
"\n",
"Thank you for reaching out.\n",
"\n",
"Could you please provide me with the transaction reference number for the failed transaction? This will help me to look into the details and provide you with the necessary information.\n",
"The transaction with ID 12123123432, which occurred on 23/07/2024 IST, failed due to insufficient balance in the account.\n",
"\n",
"Looking forward to your response.\n",
"If you have any further questions or need additional assistance, please feel free to ask.\n",
"\n",
"Best regards, \n",
"Tom\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"{'TransactionFetcher-YdHZo': {'messages': [HumanMessage(content='Hi Vishnu,\\n\\nThank you for reaching out.\\n\\nCould you please provide me with the transaction reference number for the failed transaction? This will help me to look into the details and provide you with the necessary information.\\n\\nLooking forward to your response.\\n\\nBest regards, \\nTom', name='TransactionFetcher-YdHZo')]}}\n",
"{'TransactionFetcher-7pEar': {'messages': [HumanMessage(content='Subject: Re: Failed Transaction Inquiry\\n\\nHi Vishnu,\\n\\nThank you for reaching out.\\n\\nThe transaction with ID 12123123432, which occurred on 23/07/2024 IST, failed due to insufficient balance in the account.\\n\\nIf you have any further questions or need additional assistance, please feel free to ask.\\n\\nBest regards, \\nTom', name='TransactionFetcher-7pEar')]}}\n",
"----\n",
"{'SupportSupervisor-0XuY9': {'next': 'EmailSender-YJNuR'}}\n",
"----\n",
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m\n",
"Invoking: `email_triage` with `{'email': 'Subject: Re: Failed Transaction Inquiry\\n\\nHi Vishnu,\\n\\nThank you for reaching out.\\n\\nThe transaction with ID 12123123432, which occurred on 23/07/2024 IST, failed due to insufficient balance in the account.\\n\\nIf you have any further questions or need additional assistance, please feel free to ask.\\n\\nBest regards,\\nTom'}`\n",
"\n",
"\n",
"\u001b[0mSubject: Re: Failed Transaction Inquiry\n",
"\n",
"Hi Vishnu,\n",
"\n",
"Thank you for reaching out.\n",
"\n",
"The transaction with ID 12123123432, which occurred on 23/07/2024 IST, failed due to insufficient balance in the account.\n",
"\n",
"If you have any further questions or need additional assistance, please feel free to ask.\n",
"\n",
"Best regards,\n",
"Tom\n",
"\u001b[36;1m\u001b[1;3mEmail sent successfully\u001b[0m\u001b[32;1m\u001b[1;3mI have sent the email to Vishnu with the details regarding the failed transaction. If there are any further questions or additional assistance needed, Vishnu can reach out again.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"{'EmailSender-YJNuR': {'messages': [HumanMessage(content='I have sent the email to Vishnu with the details regarding the failed transaction. If there are any further questions or additional assistance needed, Vishnu can reach out again.', name='EmailSender-YJNuR')]}}\n",
"----\n",
"{'SupportSupervisor-x7llc': {'next': 'FINISH'}}\n",
"{'SupportSupervisor-0XuY9': {'next': 'FINISH'}}\n",
"----\n"
]
}
Expand Down
4 changes: 2 additions & 2 deletions examples/history_aware_rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
logging.basicConfig()
logging.getLogger("langchain.retrievers.multi_query").setLevel(logging.INFO)

rag = rag_builder.with_multi_query().build()
print(rag.invoke({ "question": "Tell me about corporate loans" }))
rag_tool = rag_builder.with_multi_query().build_rag_tool(name="RAGTool", description="RAG to answer question by looking at db")
print(rag_tool.invoke({"query": "What is the interest rate on housing loans"}))

61 changes: 61 additions & 0 deletions examples/rag_document.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Housing Loan

Your dream home is never far away! Get hassle free home loans from Federal Bank to turn your dream home into reality. We assist you to realize your dream home. Avail your Housing Loan from us at competitive interest rates. The loan scheme assists borrowers for construction of house, acquisition of land & construction of house, repairs / renovation / remodeling / extension of house, reimbursement of debt incurred for construction / purchase / furnishing / beautification / purchase of flat / villa / house plots / takeover of housing loans / supplementary housing loan to employees of well-run companies / purchase of house plot for subsequent construction of house etc.



Maximum funding for your dream house -85% of the project cost.
Loan amount up to Rs. 1500 Lakhs
Longer repayment period up to 360 months
Repayment holiday up to 36 months.
Loan can be availed in overdraft format
Minimal paperwork
Speedy loan approval
No pre closure charges
Low processing fees
Optional funding for life insurance
Easy top up loans in future.
Repayment of the loan is made as Equated Monthly Installments (EMI).


Eligibility
For Residents:



For NRIs:

NRI individuals including salaried people, self-employed and business persons are eligible for Housing Loan.
Persons of Indian Origin (PIO) are also eligible for Housing Loan subject to following conditions:
The loan is covered by primary / collateral security of immovable property in India in the name of PIO.
The PIO should hold a valid PIO Card accompanied by a valid foreign passport.
A close relative (as defined in Companies Act) residing in India should join as co-obligant to the loan.
Age of the borrower should not exceed 55 years at the end of loan tenure.
Monthly income should be not less than Rs. 50000/-.
To avail the loan, you can apply online in simple steps. After submitting the application, bank will process your application and will be informing further details. You can track the progress of application too.


Repayment of the loan is made as Equated Monthly Installments (EMI).

You can use any one of the ways to repay the loan:

Cheques
Standing instructions at your branch
FedNet - Internet Banking
Automated Payment through ECS
Mobile Banking

Housing loan interest rates will change subject to the changes made by Bank/RBI from time to time.

Present Repo Rate 6.50% (p.a)


Loan scheme

Interest Rate (%) *

Home Loan

8.80 (Repo Rate+ 2.30) Onwards

*T&C Apply
Loading