Skip to content

Commit

Permalink
"Financial agent code commit"
Browse files Browse the repository at this point in the history
  • Loading branch information
PalakTolwani committed Jan 14, 2025
1 parent ff0c5d3 commit 2c8523c
Show file tree
Hide file tree
Showing 20 changed files with 3,999 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Budgeting/Agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# budgeting/agent.py
from langchain_core.messages import BaseMessage
from langchain_huggingface import HuggingFaceEndpoint

class BudgetingAgent:
def __init__(self, model_id="meta-llama/Llama-3.2-3B-Instruct"):
self.model = HuggingFaceEndpoint(
repo_id=model_id,
task="text_generation",
max_new_tokens=512,
temperature=0.3,
streaming=True,
)

def process_budgeting_query(self, state):
"""
Process the user query related to budgeting.
Args:
state (dict): The current state containing user messages.
"""
messages = state["messages"]
question = messages[0].content

response = self.model.invoke([{"content": question}])
return {"messages": [response]}
Binary file not shown.
4 changes: 4 additions & 0 deletions Budgeting/_init_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# budgeting/__init__.py
from .agent import BudgetingAgent
from .prompt import budgeting_prompt
from .workflow import budgeting_workflow
19 changes: 19 additions & 0 deletions Budgeting/budgeting_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Budgeting/budgeting_workflow.py
from prefect import task, flow

@task
def fetch_budget_data():
# Placeholder for fetching budget data
return {"budget": 5000, "expenses": 2000, "savings": 3000}

@task
def calculate_savings(data):
savings = data["budget"] - data["expenses"]
return savings

@flow
def budgeting_workflow(state):
# This flow orchestrates the budgeting process
budget_data = fetch_budget_data()
savings = calculate_savings(budget_data)
return {"messages": [{"content": f"Savings: {savings}"}]}
9 changes: 9 additions & 0 deletions Budgeting/prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# budgeting/prompt.py
from langchain.prompts import PromptTemplate

budgeting_prompt = PromptTemplate(
template="""You are a financial advisor helping the user with budgeting. \n
Here is the user's query: {question}\n
Provide actionable advice on how they can manage their budget effectively.""",
input_variables=["question"],
)
24 changes: 24 additions & 0 deletions Credit/Agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# credit/agent.py
from langchain_huggingface import HuggingFaceEndpoint

class CreditAgent:
def __init__(self, model_id="meta-llama/Llama-3.2-3B-Instruct"):
self.model = HuggingFaceEndpoint(
repo_id=model_id,
task="text_generation",
max_new_tokens=512,
temperature=0.3,
streaming=True,
)

def process_credit_query(self, state):
"""
Process the user query related to credit.
Args:
state (dict): The current state containing user messages.
"""
messages = state["messages"]
question = messages[0].content

response = self.model.invoke([{"content": question}])
return {"messages": [response]}
9 changes: 9 additions & 0 deletions Credit/Prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# credit/prompt.py
from langchain.prompts import PromptTemplate

credit_prompt = PromptTemplate(
template="""You are a credit advisor helping the user understand credit management. \n
Here is the user's query: {question}\n
Provide detailed guidance on credit management and options for the user.""",
input_variables=["question"],
)
Binary file added Credit/__pycache__/credit_workflow.cpython-312.pyc
Binary file not shown.
4 changes: 4 additions & 0 deletions Credit/_init_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# credit/__init__.py
from .agent import CreditAgent
from .prompt import credit_prompt
from .workflow import credit_workflow
22 changes: 22 additions & 0 deletions Credit/credit_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Credit/credit_workflow.py
from prefect import task, flow

@task
def fetch_credit_data(state):
# Simulated credit data
return {"credit_id": state, "limit": 5000, "used": 1200}

@task
def calculate_remaining_credit(data):
# Calculate remaining credit
limit = data["limit"]
used = data["used"]
remaining_credit = limit - used
return remaining_credit

@flow
def credit_workflow(state):
credit_data = fetch_credit_data(state)
remaining_credit = calculate_remaining_credit(credit_data)
# Return in the expected format
return {"messages": [{"content": f"Remaining credit amount is: {remaining_credit}"}]}
24 changes: 24 additions & 0 deletions Loan/Agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# loan/agent.py
from langchain_huggingface import HuggingFaceEndpoint

class LoanAgent:
def __init__(self, model_id="meta-llama/Llama-3.2-3B-Instruct"):
self.model = HuggingFaceEndpoint(
repo_id=model_id,
task="text_generation",
max_new_tokens=512,
temperature=0.3,
streaming=True,
)

def process_loan_query(self, state):
"""
Process the user query related to loans.
Args:
state (dict): The current state containing user messages.
"""
messages = state["messages"]
question = messages[0].content

response = self.model.invoke([{"content": question}])
return {"messages": [response]}
8 changes: 8 additions & 0 deletions Loan/Financial-Agent.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders": [
{
"path": ".."
}
],
"settings": {}
}
9 changes: 9 additions & 0 deletions Loan/Prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# loan/prompt.py
from langchain.prompts import PromptTemplate

loan_prompt = PromptTemplate(
template="""You are a loan advisor assisting the user with their loan inquiries. \n
Here is the user's query: {question}\n
Provide a detailed response regarding the loan options, eligibility, and best advice.""",
input_variables=["question"],
)
Binary file added Loan/__pycache__/loan_workflow.cpython-312.pyc
Binary file not shown.
4 changes: 4 additions & 0 deletions Loan/_init_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# loan/__init__.py
from .agent import LoanAgent
from .prompt import loan_prompt
from .workflow import loan_workflow
32 changes: 32 additions & 0 deletions Loan/loan_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Loan/loan_workflow.py

from prefect import task, flow

# Define a Prefect task for loan data processing
@task
def fetch_loan_data(loan_id):
# Placeholder for fetching loan data (could be from a database or API)
loan_data = {"loan_id": loan_id, "amount": 10000, "interest_rate": 5.5}
#print(f"Fetched loan data: {loan_data}")
return loan_data

# Define a task to calculate loan repayment
@task
def calculate_repayment(loan_data):
amount = loan_data["amount"]
interest_rate = loan_data["interest_rate"]
repayment = amount * (1 + interest_rate / 100)
#print(f"Calculated repayment amount: {repayment}")
return repayment

# Define a Prefect flow to tie tasks together
@flow
def loan_workflow(loan_id):
loan_data = fetch_loan_data(loan_id)
repayment = calculate_repayment(loan_data)
#print(f"Final repayment for loan {loan_id}: {repayment}")
return {"messages": [{"content": f"Loan repayment amount is: {repayment}"}]}

# Call the workflow in main block
if __name__ == "__main__":
loan_workflow(12345) # Pass a sample loan ID
5 changes: 5 additions & 0 deletions budget_report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

Budget Report:
--------------
Remaining Budget: 1000

37 changes: 37 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# main.py
import logging
from Budgeting.budgeting_workflow import budgeting_workflow
from Loan.loan_workflow import loan_workflow
from Credit.credit_workflow import credit_workflow

logging.getLogger("prefect").setLevel(logging.CRITICAL)

def handle_query(query):
state = {"messages": [{"content": query}]}

if "budget" in query.lower():
response = budgeting_workflow(state) # Call the function here
elif "loan" in query.lower():
response = loan_workflow(state)
elif "credit" in query.lower():
response = credit_workflow(state)
else:
response = {"messages": [{"content": "I'm not sure about that."}]}

return response

def main():
print("Welcome to the Financial Agent Chatbot! ")
while True:
user_input = input("You: ")

if user_input.lower() in ["exit", "quit"]:
print("Bot: Goodbye! Have a great day!")
break

response = handle_query(user_input)
print(f"Bot: {response['messages'][0]['content']}")


if __name__ == "__main__":
main()
Loading

0 comments on commit 2c8523c

Please sign in to comment.