accountant swarm fixes

pull/162/head
Kye 1 year ago
parent 9c92517b5c
commit 42b2cd6313

@ -1,35 +1,96 @@
import re
from swarms.models.nougat import Nougat
from swarms.structs import Flow
from swarms.models import OpenAIChat
from swarms.models import LayoutLMDocumentQA
from swarms.models import OpenAIChat, Anthropic
from typing import List
# # URL of the image of the financial document
IMAGE_OF_FINANCIAL_DOC_URL = "bank_statement_2.jpg"
# Example usage
api_key = ""
# Base llms
llm1 = OpenAIChat()
llm2 = Anthropic()
nougat = Nougat()
# Initialize the language flow
llm = OpenAIChat(
openai_api_key=api_key,
)
# LayoutLM Document QA
pdf_analyzer = LayoutLMDocumentQA()
# Prompts for each agent
SUMMARY_AGENT_PROMPT = """
Generate an actionable summary of this financial document be very specific and precise, provide bulletpoints be very specific provide methods of lowering expenses: {answer}"
"""
question = "What is the total amount of expenses?"
answer = pdf_analyzer(
question,
IMAGE_OF_FINANCIAL_DOC_URL,
# Agents
user_consultant_agent = Flow(
llm=llm1,
)
doc_analyzer_agent = Flow(
llm=llm1,
)
summary_generator_agent = Flow(
llm=llm2,
)
fraud_detection_agent = Flow(
llm=llm2,
)
decision_making_support_agent = Flow(
llm=llm2,
)
# Initialize the Flow with the language flow
agent = Flow(llm=llm)
SUMMARY_AGENT_PROMPT = f"""
Generate an actionable summary of this financial document be very specific and precise, provide bulletpoints be very specific provide methods of lowering expenses: {answer}"
"""
# Add tasks to the workflow
summary_agent = agent.run(SUMMARY_AGENT_PROMPT)
print(summary_agent)
class AccountantSwarms:
"""
Accountant Swarms is a collection of agents that work together to help
accountants with their work.
Flow: analyze doc -> detect fraud -> generate summary -> decision making support
The agents are:
- User Consultant: Asks the user many questions
- Document Analyzer: Extracts text from the image of the financial document
- Fraud Detection: Detects fraud in the document
- Summary Agent: Generates an actionable summary of the document
- Decision Making Support: Provides decision making support to the accountant
The agents are connected together in a workflow that is defined in the
run method.
The workflow is as follows:
1. The Document Analyzer agent extracts text from the image of the
financial document.
2. The Fraud Detection agent detects fraud in the document.
3. The Summary Agent generates an actionable summary of the document.
4. The Decision Making Support agent provides decision making support
to the accountant.
Example:
>>> accountant_swarms = AccountantSwarms(
"""
def __init__(
self,
financial_document_img: str,
financial_document_list_img: List[str] = None,
fraud_detection_instructions: str = None,
summary_agent_instructions: str = None,
decision_making_support_agent_instructions: str = None,
):
super().__init__()
self.financial_document_img = financial_document_img
self.fraud_detection_instructions = fraud_detection_instructions
self.summary_agent_instructions = summary_agent_instructions
def run(self):
# Extract text from the image
analyzed_doc = self.nougat(self.financial_document_img)
# Detect fraud in the document
fraud_detection_agent_output = self.fraud_detection_agent(analyzed_doc)
# Generate an actionable summary of the document
summary_agent_output = self.summary_agent(fraud_detection_agent_output)
# Provide decision making support to the accountant
decision_making_support_agent_output = self.decision_making_support_agent(
summary_agent_output
)
return decision_making_support_agent_output

@ -0,0 +1,19 @@
from swarms.structs import Flow, SequentialWorkflow
from swarms.models import OpenAIChat, Anthropic
# llm
llm = OpenAIChat()
llm2 = Anthropic()
# 2 Flows, one that creates an algorithmic pseuedocode and another that creates the pytorch code
flow1 = Flow(llm2, max_loops=1)
flow2 = Flow(llm, max_loops=1)
# SequentialWorkflow
workflow = SequentialWorkflow(
[flow1, flow2],
max_loops=1,
name="Paper to Code",
autosave=True,
description="This workflow takes a paper and converts it to code.",
)

@ -0,0 +1,10 @@
from swarms import Flow, Fuyu
llm = Fuyu()
flow = Flow(max_loops="auto", llm=llm)
flow.run(
task="Describe this image in a few sentences: ",
img="https://unsplash.com/photos/0pIC5ByPpZY",
)

@ -0,0 +1,14 @@
# This might not work in the beginning but it's a starting point
from swarms.structs import Flow, GPT4V
llm = GPT4V()
flow = Flow(
max_loops="auto",
llm=llm,
)
flow.run(
task="Describe this image in a few sentences: ",
img="https://unsplash.com/photos/0pIC5ByPpZY",
)

@ -46,6 +46,7 @@ commands: {
}
}
-------------TOOLS---------------------------
{tools}
"""
@ -149,14 +150,16 @@ class Flow:
dynamic_loops: Optional[bool] = False,
interactive: bool = False,
dashboard: bool = False,
agent_name: str = "Flow agent",
agent_name: str = " Autonomous Agent XYZ1B",
system_prompt: str = FLOW_SYSTEM_PROMPT,
# tools: List[Any] = None,
dynamic_temperature: bool = False,
SOP: str = None,
saved_state_path: Optional[str] = "flow_state.json",
autosave: bool = False,
context_length: int = 8192,
user_name: str = "Human",
user_name: str = "Human:",
self_healing: bool = False,
**kwargs: Any,
):
self.llm = llm
@ -175,6 +178,9 @@ class Flow:
self.dynamic_temperature = dynamic_temperature
self.dynamic_loops = dynamic_loops
self.user_name = user_name
self.context_length = context_length
# SOPS to inject into the system prompt
self.SOP = SOP
# The max_loops will be set dynamically if the dynamic_loop
if self.dynamic_loops:
self.max_loops = "auto"
@ -184,6 +190,7 @@ class Flow:
self.saved_state_path = saved_state_path
self.autosave = autosave
self.response_filters = []
self.self_healing = self_healing
def provide_feedback(self, feedback: str) -> None:
"""Allow users to provide feedback on the responses."""
@ -688,14 +695,6 @@ class Flow:
return "Timeout"
return response
# def backup_memory_to_s3(self, bucket_name: str, object_name: str):
# """Backup the memory to S3"""
# import boto3
# s3 = boto3.client("s3")
# s3.put_object(Bucket=bucket_name, Key=object_name, Body=json.dumps(self.memory))
# print(f"Backed up memory to S3: {bucket_name}/{object_name}")
def analyze_feedback(self):
"""Analyze the feedback for issues"""
feedback_counts = {}
@ -920,3 +919,40 @@ class Flow:
def update_retry_interval(self, retry_interval: int):
"""Update the retry interval"""
self.retry_interval = retry_interval
def self_healing(self, **kwargs):
"""
Self healing by debugging errors and refactoring its own code
Args:
**kwargs (Any): Any additional keyword arguments
"""
# Run the flow
response = self.run_with_timeout("flow")
# If an error occurs, save the state
if not self.validate_response(response):
self.save_state("previous_state.txt")
# Refactor the code
self.refactor_code()
# Run the flow again
response = self.run_with_timeout("flow")
# If the error occurs again, revert to the previous state
if not self.validate_response(response):
self.load_state("previous_state.txt")
# If the error does not occur, continue
else:
print("Self-healing successful! Bug fixed!")
return response
def refactor_code(self):
"""
Refactor the code
"""
# Add your code here to refactor the code
pass

@ -107,6 +107,8 @@ class SequentialWorkflow:
tasks: List[Task] = field(default_factory=list)
max_loops: int = 1
autosave: bool = False
name: str = (None,)
description: str = (None,)
saved_state_filepath: Optional[str] = "sequential_workflow_state.json"
restore_state_filepath: Optional[str] = None
dashboard: bool = False
@ -248,6 +250,8 @@ class SequentialWorkflow:
f"""
Sequential Workflow Dashboard
--------------------------------
Name: {self.name}
Description: {self.description}
Tasks: {len(self.tasks)}
Max Loops: {self.max_loops}
Autosave: {self.autosave}

Loading…
Cancel
Save