[CODE QUALITY]

pull/207/merge
Kye 1 year ago
parent 87e8b90b0f
commit dc55006f8f

@ -4,14 +4,16 @@ load_dict = {"ImageCaptioning": "cuda"}
node = MultiModalAgent(load_dict)
text = node.run_text("What is your name? Generate a picture of yourself")
text = node.run_text(
"What is your name? Generate a picture of yourself"
)
img = node.run_img("/image1", "What is this image about?")
chat = node.chat(
(
"What is your name? Generate a picture of yourself. What is this image"
" about?"
"What is your name? Generate a picture of yourself. What is"
" this image about?"
),
streaming=True,
)

@ -53,7 +53,8 @@ decision_making_support_agent = Agent(
pdf_path = "bankstatement.pdf"
fraud_detection_instructions = "Detect fraud in the document"
summary_agent_instructions = (
"Generate an actionable summary of the document with action steps to take"
"Generate an actionable summary of the document with action steps"
" to take"
)
decision_making_support_agent_instructions = (
"Provide decision making support to the business owner:"
@ -77,5 +78,6 @@ summary_agent_output = summary_generator_agent.run(
# Provide decision making support to the accountant
decision_making_support_agent_output = decision_making_support_agent.run(
f"{decision_making_support_agent_instructions}: {summary_agent_output}"
f"{decision_making_support_agent_instructions}:"
f" {summary_agent_output}"
)

@ -81,7 +81,9 @@ class AccountantSwarms:
super().__init__()
self.pdf_path = pdf_path
self.list_pdfs = list_pdfs
self.fraud_detection_instructions = fraud_detection_instructions
self.fraud_detection_instructions = (
fraud_detection_instructions
)
self.summary_agent_instructions = summary_agent_instructions
self.decision_making_support_agent_instructions = (
decision_making_support_agent_instructions
@ -98,7 +100,8 @@ class AccountantSwarms:
# Generate an actionable summary of the document
summary_agent_output = summary_generator_agent.run(
f"{self.summary_agent_instructions}: {fraud_detection_agent_output}"
f"{self.summary_agent_instructions}:"
f" {fraud_detection_agent_output}"
)
# Provide decision making support to the accountant
@ -113,7 +116,9 @@ class AccountantSwarms:
swarm = AccountantSwarms(
pdf_path="tesla.pdf",
fraud_detection_instructions="Detect fraud in the document",
summary_agent_instructions="Generate an actionable summary of the document",
summary_agent_instructions=(
"Generate an actionable summary of the document"
),
decision_making_support_agent_instructions=(
"Provide decision making support to the business owner:"
),

@ -30,15 +30,34 @@ class ProductAdConceptGenerator:
"in a luxurious setting", "in a playful and colorful background", "in an ice cave setting",
"in a serene and calm landscape"
]
<<<<<<< HEAD
=======
self.contexts = [
"high realism product ad (extremely creative)"
]
>>>>>>> 831147e ([CODE QUALITY])
def generate_concept(self):
theme = random.choice(self.themes)
context = random.choice(self.contexts)
<<<<<<< HEAD
return f"An ad for {self.product_name} that embodies a {theme} theme {context}"
# User input
product_name = input("Enter a product name for ad creation (e.g., 'PS5', 'AirPods', 'Kirkland Vodka'): ")
social_media_platform = input("Enter a social media platform (e.g., 'Facebook', 'Twitter', 'Instagram'): ")
=======
return (
f"{theme} inside a {style} {self.product_name}, {context}"
)
# User input
product_name = input(
"Enter a product name for ad creation (e.g., 'PS5', 'AirPods',"
" 'Kirkland Vodka'): "
)
>>>>>>> 831147e ([CODE QUALITY])
# Generate creative concept
concept_generator = ProductAdConceptGenerator(product_name)
@ -53,6 +72,16 @@ ad_copy_prompt = f"Write a compelling {social_media_platform} ad copy for a prod
ad_copy = ad_copy_agent.run(task=ad_copy_prompt)
# Output the results
<<<<<<< HEAD
print("Creative Concept:", creative_concept)
print("Image Path:", image_paths[0] if image_paths else "No image generated")
print("Ad Copy:", ad_copy)
=======
print("Creative Concept:", concept_result)
print("Design Ideas:", design_result)
print("Ad Copy:", copywriting_result)
print(
"Image Path:",
image_paths[0] if image_paths else "No image generated",
)
>>>>>>> 831147e ([CODE QUALITY])

@ -48,7 +48,9 @@ paper_implementor_agent = Agent(
paper = pdf_to_text(PDF_PATH)
algorithmic_psuedocode_agent = paper_summarizer_agent.run(
"Focus on creating the algorithmic pseudocode for the novel method in this"
f" paper: {paper}"
"Focus on creating the algorithmic pseudocode for the novel"
f" method in this paper: {paper}"
)
pytorch_code = paper_implementor_agent.run(
algorithmic_psuedocode_agent
)
pytorch_code = paper_implementor_agent.run(algorithmic_psuedocode_agent)

@ -7,10 +7,10 @@ from swarms.prompts.multi_modal_autonomous_instruction_prompt import (
llm = GPT4VisionAPI()
task = (
"Analyze this image of an assembly line and identify any issues such as"
" misaligned parts, defects, or deviations from the standard assembly"
" process. IF there is anything unsafe in the image, explain why it is"
" unsafe and how it could be improved."
"Analyze this image of an assembly line and identify any issues"
" such as misaligned parts, defects, or deviations from the"
" standard assembly process. IF there is anything unsafe in the"
" image, explain why it is unsafe and how it could be improved."
)
img = "assembly_line.jpg"

@ -9,7 +9,8 @@ from swarms.prompts.autobloggen import (
# Prompts
topic_selection_task = (
"Generate 10 topics on gaining mental clarity using ancient practices"
"Generate 10 topics on gaining mental clarity using ancient"
" practices"
)
@ -54,7 +55,9 @@ class AutoBlogGenSwarm:
):
self.llm = llm()
self.topic_selection_task = topic_selection_task
self.topic_selection_agent_prompt = topic_selection_agent_prompt
self.topic_selection_agent_prompt = (
topic_selection_agent_prompt
)
self.objective = objective
self.iterations = iterations
self.max_retries = max_retries
@ -90,7 +93,9 @@ class AutoBlogGenSwarm:
def step(self):
"""Steps through the task"""
topic_selection_agent = self.llm(self.topic_selection_agent_prompt)
topic_selection_agent = self.llm(
self.topic_selection_agent_prompt
)
topic_selection_agent = self.print_beautifully(
"Topic Selection Agent", topic_selection_agent
)
@ -100,7 +105,9 @@ class AutoBlogGenSwarm:
# Agent that reviews the draft
review_agent = self.llm(self.get_review_prompt(draft_blog))
review_agent = self.print_beautifully("Review Agent", review_agent)
review_agent = self.print_beautifully(
"Review Agent", review_agent
)
# Agent that publishes on social media
distribution_agent = self.llm(
@ -119,7 +126,11 @@ class AutoBlogGenSwarm:
except Exception as error:
print(
colored(
f"Error while running AutoBlogGenSwarm {error}", "red"
(
"Error while running AutoBlogGenSwarm"
f" {error}"
),
"red",
)
)
if attempt == self.retry_attempts - 1:

@ -47,7 +47,11 @@ class AutoTemp:
"""
score_text = self.llm(eval_prompt, temperature=0.5)
score_match = re.search(r"\b\d+(\.\d)?\b", score_text)
return round(float(score_match.group()), 1) if score_match else 0.0
return (
round(float(score_match.group()), 1)
if score_match
else 0.0
)
def run(self, prompt, temperature_string):
print("Starting generation process...")

@ -12,7 +12,9 @@ class BlogGen:
blog_topic,
temperature_range: str = "0.4,0.6,0.8,1.0,1.2",
): # Add blog_topic as an argument
self.openai_chat = OpenAIChat(openai_api_key=api_key, temperature=0.8)
self.openai_chat = OpenAIChat(
openai_api_key=api_key, temperature=0.8
)
self.auto_temp = AutoTemp(api_key)
self.temperature_range = temperature_range
self.workflow = SequentialWorkflow(max_loops=5)
@ -52,11 +54,15 @@ class BlogGen:
)
chosen_topic = topic_output.split("\n")[0]
print(colored("Selected topic: " + chosen_topic, "yellow"))
print(
colored("Selected topic: " + chosen_topic, "yellow")
)
# Initial draft generation with AutoTemp
initial_draft_prompt = self.DRAFT_WRITER_SYSTEM_PROMPT.replace(
"{{CHOSEN_TOPIC}}", chosen_topic
initial_draft_prompt = (
self.DRAFT_WRITER_SYSTEM_PROMPT.replace(
"{{CHOSEN_TOPIC}}", chosen_topic
)
)
auto_temp_output = self.auto_temp.run(
initial_draft_prompt, self.temperature_range
@ -89,13 +95,17 @@ class BlogGen:
)
# Distribution preparation using OpenAIChat
distribution_prompt = self.DISTRIBUTION_AGENT_SYSTEM_PROMPT.replace(
"{{ARTICLE_TOPIC}}", chosen_topic
distribution_prompt = (
self.DISTRIBUTION_AGENT_SYSTEM_PROMPT.replace(
"{{ARTICLE_TOPIC}}", chosen_topic
)
)
distribution_result = self.openai_chat.generate(
[distribution_prompt]
)
distribution_output = distribution_result.generations[0][0].text
distribution_output = distribution_result.generations[0][
0
].text
print(
colored(
(

@ -5,7 +5,9 @@ from blog_gen import BlogGen
def main():
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("OPENAI_API_KEY environment variable not set.")
raise ValueError(
"OPENAI_API_KEY environment variable not set."
)
blog_topic = input("Enter the topic for the blog generation: ")

@ -37,12 +37,18 @@ llm = OpenAIChat(openai_api_key=api_key, max_tokens=5000)
# Documentation agent
documentation_agent = Agent(
llm=llm, sop=DOCUMENTATION_SOP, max_loops=1,
llm=llm,
sop=DOCUMENTATION_SOP,
max_loops=1,
)
# Tests agent
tests_agent = Agent(llm=llm, sop=TEST_SOP, max_loops=2,)
tests_agent = Agent(
llm=llm,
sop=TEST_SOP,
max_loops=2,
)
# Run the documentation agent
@ -52,6 +58,6 @@ documentation = documentation_agent.run(
# Run the tests agent
tests = tests_agent.run(
f"Write tests for the following code:{TASK} here is the documentation:"
f" {documentation}"
f"Write tests for the following code:{TASK} here is the"
f" documentation: {documentation}"
)

@ -12,14 +12,15 @@ openai_api_key = os.getenv("OPENAI_API_KEY")
# Define prompts for various tasks
MEAL_PLAN_PROMPT = (
"Based on the following user preferences: dietary restrictions as"
" vegetarian, preferred cuisines as Italian and Indian, a total caloric"
" intake of around 2000 calories per day, and an exclusion of legumes,"
" create a detailed weekly meal plan. Include a variety of meals for"
" breakfast, lunch, dinner, and optional snacks."
" vegetarian, preferred cuisines as Italian and Indian, a total"
" caloric intake of around 2000 calories per day, and an"
" exclusion of legumes, create a detailed weekly meal plan."
" Include a variety of meals for breakfast, lunch, dinner, and"
" optional snacks."
)
IMAGE_ANALYSIS_PROMPT = (
"Identify the items in this fridge, including their quantities and"
" condition."
"Identify the items in this fridge, including their quantities"
" and condition."
)
@ -74,12 +75,15 @@ def generate_integrated_shopping_list(
meal_plan_output, image_analysis, user_preferences
):
# Prepare the prompt for the LLM
fridge_contents = image_analysis["choices"][0]["message"]["content"]
fridge_contents = image_analysis["choices"][0]["message"][
"content"
]
prompt = (
f"Based on this meal plan: {meal_plan_output}, and the following items"
f" in the fridge: {fridge_contents}, considering dietary preferences as"
" vegetarian with a preference for Italian and Indian cuisines,"
" generate a comprehensive shopping list that includes only the items"
f"Based on this meal plan: {meal_plan_output}, and the"
f" following items in the fridge: {fridge_contents},"
" considering dietary preferences as vegetarian with a"
" preference for Italian and Indian cuisines, generate a"
" comprehensive shopping list that includes only the items"
" needed."
)
@ -124,6 +128,10 @@ print("Integrated Shopping List:", integrated_shopping_list)
with open("nutrition_output.txt", "w") as file:
file.write("Meal Plan:\n" + meal_plan_output + "\n\n")
file.write("Integrated Shopping List:\n" + integrated_shopping_list + "\n")
file.write(
"Integrated Shopping List:\n"
+ integrated_shopping_list
+ "\n"
)
print("Outputs have been saved to nutrition_output.txt")

@ -41,7 +41,9 @@ def get_review_prompt(article):
return prompt
def social_media_prompt(article: str, goal: str = "Clicks and engagement"):
def social_media_prompt(
article: str, goal: str = "Clicks and engagement"
):
prompt = SOCIAL_MEDIA_SYSTEM_PROMPT_AGENT.replace(
"{{ARTICLE}}", article
).replace("{{GOAL}}", goal)
@ -50,11 +52,12 @@ def social_media_prompt(article: str, goal: str = "Clicks and engagement"):
# Agent that generates topics
topic_selection_task = (
"Generate 10 topics on gaining mental clarity using ancient practices"
"Generate 10 topics on gaining mental clarity using ancient"
" practices"
)
topics = llm(
f"Your System Instructions: {TOPIC_GENERATOR_SYSTEM_PROMPT}, Your current"
f" task: {topic_selection_task}"
f"Your System Instructions: {TOPIC_GENERATOR_SYSTEM_PROMPT}, Your"
f" current task: {topic_selection_task}"
)
dashboard = print(

@ -24,8 +24,12 @@ api_key = os.getenv("OPENAI_API_KEY")
llm = GPT4VisionAPI(openai_api_key=api_key)
assembly_line = "playground/demos/swarm_of_mma_manufacturing/assembly_line.jpg"
red_robots = "playground/demos/swarm_of_mma_manufacturing/red_robots.jpg"
assembly_line = (
"playground/demos/swarm_of_mma_manufacturing/assembly_line.jpg"
)
red_robots = (
"playground/demos/swarm_of_mma_manufacturing/red_robots.jpg"
)
robots = "playground/demos/swarm_of_mma_manufacturing/robots.jpg"
tesla_assembly_line = (
"playground/demos/swarm_of_mma_manufacturing/tesla_assembly.jpg"
@ -35,29 +39,31 @@ tesla_assembly_line = (
# Define detailed prompts for each agent
tasks = {
"health_safety": (
"Analyze the factory's working environment for health safety. Focus on"
" cleanliness, ventilation, spacing between workstations, and personal"
" protective equipment availability."
"Analyze the factory's working environment for health safety."
" Focus on cleanliness, ventilation, spacing between"
" workstations, and personal protective equipment"
" availability."
),
"productivity": (
"Review the factory's workflow efficiency, machine utilization, and"
" employee engagement. Identify operational delays or bottlenecks."
"Review the factory's workflow efficiency, machine"
" utilization, and employee engagement. Identify operational"
" delays or bottlenecks."
),
"safety": (
"Analyze the factory's safety measures, including fire exits, safety"
" signage, and emergency response equipment."
"Analyze the factory's safety measures, including fire exits,"
" safety signage, and emergency response equipment."
),
"security": (
"Evaluate the factory's security systems, entry/exit controls, and"
" potential vulnerabilities."
"Evaluate the factory's security systems, entry/exit"
" controls, and potential vulnerabilities."
),
"sustainability": (
"Inspect the factory's sustainability practices, including waste"
" management, energy usage, and eco-friendly processes."
"Inspect the factory's sustainability practices, including"
" waste management, energy usage, and eco-friendly processes."
),
"efficiency": (
"Assess the manufacturing process's efficiency, considering the layout,"
" logistics, and automation level."
"Assess the manufacturing process's efficiency, considering"
" the layout, logistics, and automation level."
),
}
@ -73,7 +79,10 @@ efficiency_prompt = tasks["efficiency"]
# Health security agent
health_security_agent = Agent(
llm=llm, sop_list=health_safety_prompt, max_loops=2, multi_modal=True
llm=llm,
sop_list=health_safety_prompt,
max_loops=2,
multi_modal=True,
)
# Quality control agent
@ -98,10 +107,14 @@ health_check = health_security_agent.run(
)
# Add the third task to the productivity_check_agent
productivity_check = productivity_check_agent.run(health_check, assembly_line)
productivity_check = productivity_check_agent.run(
health_check, assembly_line
)
# Add the fourth task to the security_check_agent
security_check = security_check_agent.add(productivity_check, red_robots)
security_check = security_check_agent.add(
productivity_check, red_robots
)
# Add the fifth task to the efficiency_check_agent
efficiency_check = efficiency_check_agent.run(

@ -2,7 +2,8 @@ from langchain.document_loaders import CSVLoader
from swarms.memory import qdrant
loader = CSVLoader(
file_path="../document_parsing/aipg/aipg.csv", encoding="utf-8-sig"
file_path="../document_parsing/aipg/aipg.csv",
encoding="utf-8-sig",
)
docs = loader.load()

@ -24,7 +24,9 @@ llm = OpenAIChat(
)
# Initialize the Worker with the custom tool
worker = Worker(llm=llm, ai_name="EdgeGPT Worker", external_tools=[edgegpt])
worker = Worker(
llm=llm, ai_name="EdgeGPT Worker", external_tools=[edgegpt]
)
# Use the worker to process a task
task = "Hello, my name is ChatGPT"

@ -17,5 +17,8 @@ labels = [
]
result = clip("swarms.jpeg", labels)
metadata = {"filename": "images/.jpg".split("/")[-1], "top_probs": result}
metadata = {
"filename": "images/.jpg".split("/")[-1],
"top_probs": result,
}
clip.plot_image_with_metadata("swarms.jpeg", metadata)

@ -7,4 +7,6 @@ model_wrapper = DistilWhisperModel()
transcription = model_wrapper("path/to/audio.mp3")
# For async usage
transcription = asyncio.run(model_wrapper.async_transcribe("path/to/audio.mp3"))
transcription = asyncio.run(
model_wrapper.async_transcribe("path/to/audio.mp3")
)

@ -1,7 +1,9 @@
from swarms.models.mpt import MPT
mpt_instance = MPT(
"mosaicml/mpt-7b-storywriter", "EleutherAI/gpt-neox-20b", max_tokens=150
"mosaicml/mpt-7b-storywriter",
"EleutherAI/gpt-neox-20b",
max_tokens=150,
)
mpt_instance.generate("Once upon a time in a land far, far away...")

@ -31,7 +31,9 @@ async def async_load_playwright(url: str) -> str:
text = soup.get_text()
lines = (line.strip() for line in text.splitlines())
chunks = (
phrase.strip() for line in lines for phrase in line.split(" ")
phrase.strip()
for line in lines
for phrase in line.split(" ")
)
results = "\n".join(chunk for chunk in chunks if chunk)
except Exception as e:
@ -60,6 +62,6 @@ agent = Agent(
)
out = agent.run(
"Generate a 10,000 word blog on mental clarity and the benefits of"
" meditation."
"Generate a 10,000 word blog on mental clarity and the benefits"
" of meditation."
)

@ -18,7 +18,9 @@ flow2 = Agent(llm=llm, max_loops=1, dashboard=False)
workflow = SequentialWorkflow(max_loops=1)
# Add tasks to the workflow
workflow.add("Generate a 10,000 word blog on health and wellness.", flow1)
workflow.add(
"Generate a 10,000 word blog on health and wellness.", flow1
)
# Suppose the next task takes the output of the first task as input
workflow.add("Summarize the generated blog", flow2)

@ -1,7 +1,11 @@
from swarms import Orchestrator, Worker
# Instantiate the Orchestrator with 10 agents
orchestrator = Orchestrator(Worker, agent_list=[Worker] * 10, task_queue=[])
orchestrator = Orchestrator(
Worker, agent_list=[Worker] * 10, task_queue=[]
)
# Agent 1 sends a message to Agent 2
orchestrator.chat(sender_id=1, receiver_id=2, message="Hello, Agent 2!")
orchestrator.chat(
sender_id=1, receiver_id=2, message="Hello, Agent 2!"
)

@ -37,7 +37,9 @@ class DialogueAgent:
[
self.system_message,
HumanMessage(
content="\n".join(self.message_history + [self.prefix])
content="\n".join(
self.message_history + [self.prefix]
)
),
]
)
@ -76,7 +78,9 @@ class DialogueSimulator:
def step(self) -> tuple[str, str]:
# 1. choose the next speaker
speaker_idx = self.select_next_speaker(self._step, self.agents)
speaker_idx = self.select_next_speaker(
self._step, self.agents
)
speaker = self.agents[speaker_idx]
# 2. next speaker sends message
@ -114,7 +118,9 @@ class BiddingDialogueAgent(DialogueAgent):
message_history="\n".join(self.message_history),
recent_message=self.message_history[-1],
)
bid_string = self.model([SystemMessage(content=prompt)]).content
bid_string = self.model(
[SystemMessage(content=prompt)]
).content
return bid_string
@ -127,7 +133,8 @@ The presidential candidates are: {', '.join(character_names)}."""
player_descriptor_system_message = SystemMessage(
content=(
"You can add detail to the description of each presidential candidate."
"You can add detail to the description of each presidential"
" candidate."
)
)
@ -156,7 +163,9 @@ Your goal is to be as creative as possible and make the voters think you are the
"""
def generate_character_system_message(character_name, character_header):
def generate_character_system_message(
character_name, character_header
):
return SystemMessage(content=f"""{character_header}
You will speak in the style of {character_name}, and exaggerate their personality.
You will come up with creative ideas related to {topic}.
@ -183,7 +192,9 @@ character_headers = [
)
]
character_system_messages = [
generate_character_system_message(character_name, character_headers)
generate_character_system_message(
character_name, character_headers
)
for character_name, character_headers in zip(
character_names, character_headers
)
@ -209,8 +220,8 @@ for (
class BidOutputParser(RegexParser):
def get_format_instructions(self) -> str:
return (
"Your response should be an integer delimited by angled brackets,"
" like this: <int>."
"Your response should be an integer delimited by angled"
" brackets, like this: <int>."
)
@ -262,7 +273,9 @@ topic_specifier_prompt = [
Speak directly to the presidential candidates: {*character_names,}.
Do not add anything else."""),
]
specified_topic = ChatOpenAI(temperature=1.0)(topic_specifier_prompt).content
specified_topic = ChatOpenAI(temperature=1.0)(
topic_specifier_prompt
).content
print(f"Original topic:\n{topic}\n")
print(f"Detailed topic:\n{specified_topic}\n")
@ -273,7 +286,8 @@ print(f"Detailed topic:\n{specified_topic}\n")
wait=tenacity.wait_none(), # No waiting time between retries
retry=tenacity.retry_if_exception_type(ValueError),
before_sleep=lambda retry_state: print(
f"ValueError occurred: {retry_state.outcome.exception()}, retrying..."
f"ValueError occurred: {retry_state.outcome.exception()},"
" retrying..."
),
retry_error_callback=lambda retry_state: 0,
) # Default value when all retries are exhausted
@ -286,7 +300,9 @@ def ask_for_bid(agent) -> str:
return bid
def select_next_speaker(step: int, agents: List[DialogueAgent]) -> int:
def select_next_speaker(
step: int, agents: List[DialogueAgent]
) -> int:
bids = []
for agent in agents:
bid = ask_for_bid(agent)
@ -309,7 +325,9 @@ def select_next_speaker(step: int, agents: List[DialogueAgent]) -> int:
characters = []
for character_name, character_system_message, bidding_template in zip(
character_names, character_system_messages, character_bidding_templates
character_names,
character_system_messages,
character_bidding_templates,
):
characters.append(
BiddingDialogueAgent(

@ -2,7 +2,9 @@ from swarms.swarms import DialogueSimulator
from swarms.workers.worker import Worker
from swarms.models import OpenAIChat
llm = OpenAIChat(model_name="gpt-4", openai_api_key="api-key", temperature=0.5)
llm = OpenAIChat(
model_name="gpt-4", openai_api_key="api-key", temperature=0.5
)
worker1 = Worker(
llm=llm,

@ -45,5 +45,7 @@ manager = Agent(
agents = [flow1, flow2, flow3]
group_chat = GroupChat(agents=agents, messages=[], max_round=10)
chat_manager = GroupChatManager(groupchat=group_chat, selector=manager)
chat_manager = GroupChatManager(
groupchat=group_chat, selector=manager
)
chat_history = chat_manager("Write me a riddle")

@ -1,4 +1,7 @@
from swarms.swarms.multi_agent_debate import MultiAgentDebate, select_speaker
from swarms.swarms.multi_agent_debate import (
MultiAgentDebate,
select_speaker,
)
from swarms.workers.worker import Worker
from swarms.models import OpenAIChat
@ -37,9 +40,9 @@ debate = MultiAgentDebate(agents, select_speaker)
# Run task
task = (
"What were the winning boston marathon times for the past 5 years (ending"
" in 2022)? Generate a table of the year, name, country of origin, and"
" times."
"What were the winning boston marathon times for the past 5 years"
" (ending in 2022)? Generate a table of the year, name, country"
" of origin, and times."
)
results = debate.run(task, max_iters=4)

@ -7,9 +7,13 @@ node = Worker(
# Instantiate the Orchestrator with 10 agents
orchestrator = Orchestrator(node, agent_list=[node] * 10, task_queue=[])
orchestrator = Orchestrator(
node, agent_list=[node] * 10, task_queue=[]
)
# Agent 7 sends a message to Agent 9
orchestrator.chat(
sender_id=7, receiver_id=9, message="Can you help me with this task?"
sender_id=7,
receiver_id=9,
message="Can you help me with this task?",
)

@ -7,9 +7,13 @@ node = Worker(
# Instantiate the Orchestrator with 10 agents
orchestrator = Orchestrator(node, agent_list=[node] * 10, task_queue=[])
orchestrator = Orchestrator(
node, agent_list=[node] * 10, task_queue=[]
)
# Agent 7 sends a message to Agent 9
orchestrator.chat(
sender_id=7, receiver_id=9, message="Can you help me with this task?"
sender_id=7,
receiver_id=9,
message="Can you help me with this task?",
)

@ -8,8 +8,8 @@ swarm = HierarchicalSwarm(api_key)
# Define an objective
objective = (
"Find 20 potential customers for a HierarchicalSwarm based AI Agent"
" automation infrastructure"
"Find 20 potential customers for a HierarchicalSwarm based AI"
" Agent automation infrastructure"
)
# Run HierarchicalSwarm

@ -77,17 +77,17 @@ mypy-protobuf = "^3.0.0"
[tool.autopep8]
max_line_length = 80
max_line_length = 70
ignore = "E501,W6" # or ["E501", "W6"]
in-place = true
recursive = true
aggressive = 3
[tool.ruff]
line-length = 80
line-length = 70
[tool.black]
line-length = 80
line-length = 70
target-version = ['py38']
preview = true

@ -33,12 +33,16 @@ agent3 = Agent(llm=biochat, max_loops=1, dashboard=False)
workflow = SequentialWorkflow(max_loops=1)
# Add tasks to the workflow
workflow.add("Generate a 10,000 word blog on health and wellness.", agent1)
workflow.add(
"Generate a 10,000 word blog on health and wellness.", agent1
)
# Suppose the next task takes the output of the first task as input
workflow.add("Summarize the generated blog", agent2)
workflow.add("Create a references sheet of materials for the curriculm", agent3)
workflow.add(
"Create a references sheet of materials for the curriculm", agent3
)
# Run the workflow
workflow.run()

@ -28,7 +28,8 @@ class BaseVectorStore(ABC):
embedding_driver: Any
futures_executor: futures.Executor = field(
default=Factory(lambda: futures.ThreadPoolExecutor()), kw_only=True
default=Factory(lambda: futures.ThreadPoolExecutor()),
kw_only=True,
)
def upsert_text_artifacts(
@ -40,7 +41,11 @@ class BaseVectorStore(ABC):
execute_futures_dict(
{
namespace: self.futures_executor.submit(
self.upsert_text_artifact, a, namespace, meta, **kwargs
self.upsert_text_artifact,
a,
namespace,
meta,
**kwargs,
)
for namespace, artifact_list in artifacts.items()
for a in artifact_list
@ -62,7 +67,9 @@ class BaseVectorStore(ABC):
if artifact.embedding:
vector = artifact.embedding
else:
vector = artifact.generate_embedding(self.embedding_driver)
vector = artifact.generate_embedding(
self.embedding_driver
)
return self.upsert_vector(
vector,
@ -106,7 +113,9 @@ class BaseVectorStore(ABC):
...
@abstractmethod
def load_entries(self, namespace: Optional[str] = None) -> list[Entry]:
def load_entries(
self, namespace: Optional[str] = None
) -> list[Entry]:
...
@abstractmethod

@ -35,11 +35,18 @@ def _results_to_docs(results: Any) -> List[Document]:
return [doc for doc, _ in _results_to_docs_and_scores(results)]
def _results_to_docs_and_scores(results: Any) -> List[Tuple[Document, float]]:
def _results_to_docs_and_scores(
results: Any,
) -> List[Tuple[Document, float]]:
return [
# TODO: Chroma can do batch querying,
# we shouldn't hard code to the 1st result
(Document(page_content=result[0], metadata=result[1] or {}), result[2])
(
Document(
page_content=result[0], metadata=result[1] or {}
),
result[2],
)
for result in zip(
results["documents"][0],
results["metadatas"][0],
@ -94,13 +101,16 @@ class Chroma(VectorStore):
# If client_settings is provided with persist_directory specified,
# then it is "in-memory and persisting to disk" mode.
client_settings.persist_directory = (
persist_directory or client_settings.persist_directory
persist_directory
or client_settings.persist_directory
)
if client_settings.persist_directory is not None:
# Maintain backwards compatibility with chromadb < 0.4.0
major, minor, _ = chromadb.__version__.split(".")
if int(major) == 0 and int(minor) < 4:
client_settings.chroma_db_impl = "duckdb+parquet"
client_settings.chroma_db_impl = (
"duckdb+parquet"
)
_client_settings = client_settings
elif persist_directory:
@ -120,7 +130,8 @@ class Chroma(VectorStore):
self._client_settings = _client_settings
self._client = chromadb.Client(_client_settings)
self._persist_directory = (
_client_settings.persist_directory or persist_directory
_client_settings.persist_directory
or persist_directory
)
self._embedding_function = embedding_function
@ -189,7 +200,9 @@ class Chroma(VectorStore):
embeddings = None
texts = list(texts)
if self._embedding_function is not None:
embeddings = self._embedding_function.embed_documents(texts)
embeddings = self._embedding_function.embed_documents(
texts
)
if metadatas:
# fill metadatas with empty dicts if somebody
# did not specify metadata for all texts
@ -205,13 +218,17 @@ class Chroma(VectorStore):
empty_ids.append(idx)
if non_empty_ids:
metadatas = [metadatas[idx] for idx in non_empty_ids]
texts_with_metadatas = [texts[idx] for idx in non_empty_ids]
texts_with_metadatas = [
texts[idx] for idx in non_empty_ids
]
embeddings_with_metadatas = (
[embeddings[idx] for idx in non_empty_ids]
if embeddings
else None
)
ids_with_metadata = [ids[idx] for idx in non_empty_ids]
ids_with_metadata = [
ids[idx] for idx in non_empty_ids
]
try:
self._collection.upsert(
metadatas=metadatas,
@ -222,7 +239,8 @@ class Chroma(VectorStore):
except ValueError as e:
if "Expected metadata value to be" in str(e):
msg = (
"Try filtering complex metadata from the document"
"Try filtering complex metadata from the"
" document"
" using "
"langchain.vectorstores.utils.filter_complex_metadata."
)
@ -230,9 +248,13 @@ class Chroma(VectorStore):
else:
raise e
if empty_ids:
texts_without_metadatas = [texts[j] for j in empty_ids]
texts_without_metadatas = [
texts[j] for j in empty_ids
]
embeddings_without_metadatas = (
[embeddings[j] for j in empty_ids] if embeddings else None
[embeddings[j] for j in empty_ids]
if embeddings
else None
)
ids_without_metadatas = [ids[j] for j in empty_ids]
self._collection.upsert(
@ -351,7 +373,9 @@ class Chroma(VectorStore):
where_document=where_document,
)
else:
query_embedding = self._embedding_function.embed_query(query)
query_embedding = self._embedding_function.embed_query(
query
)
results = self.__query_collection(
query_embeddings=[query_embedding],
n_results=k,
@ -388,9 +412,9 @@ class Chroma(VectorStore):
return self._max_inner_product_relevance_score_fn
else:
raise ValueError(
"No supported normalization function"
f" for distance metric of type: {distance}."
"Consider providing relevance_score_fn to Chroma constructor."
"No supported normalization function for distance"
f" metric of type: {distance}.Consider providing"
" relevance_score_fn to Chroma constructor."
)
def max_marginal_relevance_search_by_vector(
@ -426,7 +450,12 @@ class Chroma(VectorStore):
n_results=fetch_k,
where=filter,
where_document=where_document,
include=["metadatas", "documents", "distances", "embeddings"],
include=[
"metadatas",
"documents",
"distances",
"embeddings",
],
)
mmr_selected = maximal_marginal_relevance(
np.array(embedding, dtype=np.float32),
@ -471,8 +500,8 @@ class Chroma(VectorStore):
"""
if self._embedding_function is None:
raise ValueError(
"For MMR search, you must specify an embedding function"
" oncreation."
"For MMR search, you must specify an embedding"
" function oncreation."
)
embedding = self._embedding_function.embed_query(query)
@ -546,7 +575,9 @@ class Chroma(VectorStore):
if int(major) == 0 and int(minor) < 4:
self._client.persist()
def update_document(self, document_id: str, document: Document) -> None:
def update_document(
self, document_id: str, document: Document
) -> None:
"""Update a document in the collection.
Args:
@ -568,8 +599,8 @@ class Chroma(VectorStore):
metadata = [document.metadata for document in documents]
if self._embedding_function is None:
raise ValueError(
"For update, you must specify an embedding function on"
" creation."
"For update, you must specify an embedding function"
" on creation."
)
embeddings = self._embedding_function.embed_documents(text)
@ -711,7 +742,9 @@ class Chroma(VectorStore):
**kwargs,
)
def delete(self, ids: Optional[List[str]] = None, **kwargs: Any) -> None:
def delete(
self, ids: Optional[List[str]] = None, **kwargs: Any
) -> None:
"""Delete by vector IDs.
Args:

@ -18,8 +18,8 @@ def cosine_similarity(X: Matrix, Y: Matrix) -> np.ndarray:
Y = np.array(Y)
if X.shape[1] != Y.shape[1]:
raise ValueError(
"Number of columns in X and Y must be the same. X has shape"
f" {X.shape} and Y has shape {Y.shape}."
"Number of columns in X and Y must be the same. X has"
f" shape {X.shape} and Y has shape {Y.shape}."
)
try:
import simsimd as simd
@ -32,9 +32,9 @@ def cosine_similarity(X: Matrix, Y: Matrix) -> np.ndarray:
return Z
except ImportError:
logger.info(
"Unable to import simsimd, defaulting to NumPy implementation. If"
" you want to use simsimd please install with `pip install"
" simsimd`."
"Unable to import simsimd, defaulting to NumPy"
" implementation. If you want to use simsimd please"
" install with `pip install simsimd`."
)
X_norm = np.linalg.norm(X, axis=1)
Y_norm = np.linalg.norm(Y, axis=1)
@ -68,9 +68,15 @@ def cosine_similarity_top_k(
score_array = cosine_similarity(X, Y)
score_threshold = score_threshold or -1.0
score_array[score_array < score_threshold] = 0
top_k = min(top_k or len(score_array), np.count_nonzero(score_array))
top_k_idxs = np.argpartition(score_array, -top_k, axis=None)[-top_k:]
top_k_idxs = top_k_idxs[np.argsort(score_array.ravel()[top_k_idxs])][::-1]
top_k = min(
top_k or len(score_array), np.count_nonzero(score_array)
)
top_k_idxs = np.argpartition(score_array, -top_k, axis=None)[
-top_k:
]
top_k_idxs = top_k_idxs[
np.argsort(score_array.ravel()[top_k_idxs])
][::-1]
ret_idxs = np.unravel_index(top_k_idxs, score_array.shape)
scores = score_array.ravel()[top_k_idxs].tolist()
return list(zip(*ret_idxs)), scores # type: ignore

@ -84,7 +84,9 @@ class PgVectorVectorStore(BaseVectorStore):
"""
connection_string: Optional[str] = field(default=None, kw_only=True)
connection_string: Optional[str] = field(
default=None, kw_only=True
)
create_engine_params: dict = field(factory=dict, kw_only=True)
engine: Optional[Engine] = field(default=None, kw_only=True)
table_name: str = field(kw_only=True)
@ -104,12 +106,14 @@ class PgVectorVectorStore(BaseVectorStore):
# If an engine is not provided, a connection string is required.
if connection_string is None:
raise ValueError("An engine or connection string is required")
raise ValueError(
"An engine or connection string is required"
)
if not connection_string.startswith("postgresql://"):
raise ValueError(
"The connection string must describe a Postgres database"
" connection"
"The connection string must describe a Postgres"
" database connection"
)
@engine.validator
@ -120,7 +124,9 @@ class PgVectorVectorStore(BaseVectorStore):
# If a connection string is not provided, an engine is required.
if engine is None:
raise ValueError("An engine or connection string is required")
raise ValueError(
"An engine or connection string is required"
)
def __attrs_post_init__(self) -> None:
"""If a an engine is provided, it will be used to connect to the database.
@ -139,10 +145,14 @@ class PgVectorVectorStore(BaseVectorStore):
) -> None:
"""Provides a mechanism to initialize the database schema and extensions."""
if install_uuid_extension:
self.engine.execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";')
self.engine.execute(
'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";'
)
if install_vector_extension:
self.engine.execute('CREATE EXTENSION IF NOT EXISTS "vector";')
self.engine.execute(
'CREATE EXTENSION IF NOT EXISTS "vector";'
)
if create_schema:
self._model.metadata.create_all(self.engine)
@ -246,7 +256,9 @@ class PgVectorVectorStore(BaseVectorStore):
return [
BaseVectorStore.QueryResult(
id=str(result[0].id),
vector=result[0].vector if include_vectors else None,
vector=(
result[0].vector if include_vectors else None
),
score=result[1],
meta=result[0].meta,
namespace=result[0].namespace,

@ -111,7 +111,9 @@ class PineconeVectorStoreStore(BaseVectorStore):
**kwargs,
) -> str:
"""Upsert vector"""
vector_id = vector_id if vector_id else str_to_hash(str(vector))
vector_id = (
vector_id if vector_id else str_to_hash(str(vector))
)
params = {"namespace": namespace} | kwargs
@ -179,7 +181,11 @@ class PineconeVectorStoreStore(BaseVectorStore):
vector = self.embedding_driver.embed_string(query)
params = {
"top_k": count if count else BaseVectorStore.DEFAULT_QUERY_COUNT,
"top_k": (
count
if count
else BaseVectorStore.DEFAULT_QUERY_COUNT
),
"namespace": namespace,
"include_values": include_vectors,
"include_metadata": include_metadata,

@ -2,7 +2,11 @@ from typing import List
from sentence_transformers import SentenceTransformer
from httpx import RequestError
from qdrant_client import QdrantClient
from qdrant_client.http.models import Distance, VectorParams, PointStruct
from qdrant_client.http.models import (
Distance,
VectorParams,
PointStruct,
)
class Qdrant:
@ -33,7 +37,9 @@ class Qdrant:
https: bool = True,
):
try:
self.client = QdrantClient(url=host, port=port, api_key=api_key)
self.client = QdrantClient(
url=host, port=port, api_key=api_key
)
self.collection_name = collection_name
self._load_embedding_model(model_name)
self._setup_collection()
@ -56,7 +62,10 @@ class Qdrant:
try:
exists = self.client.get_collection(self.collection_name)
if exists:
print(f"Collection '{self.collection_name}' already exists.")
print(
f"Collection '{self.collection_name}' already"
" exists."
)
except Exception as e:
self.client.create_collection(
collection_name=self.collection_name,
@ -93,7 +102,8 @@ class Qdrant:
)
else:
print(
f"Document at index {i} is missing 'page_content' key"
f"Document at index {i} is missing"
" 'page_content' key"
)
except Exception as e:
print(f"Error processing document at index {i}: {e}")
@ -121,7 +131,9 @@ class Qdrant:
SearchResult or None: Returns the search results if successful, otherwise None.
"""
try:
query_vector = self.model.encode(query, normalize_embeddings=True)
query_vector = self.model.encode(
query, normalize_embeddings=True
)
search_result = self.client.search(
collection_name=self.collection_name,
query_vector=query_vector,

@ -9,7 +9,9 @@ from pydantic import BaseModel, Field
class TaskInput(BaseModel):
__root__: Any = Field(
...,
description="The input parameters for the task. Any value is allowed.",
description=(
"The input parameters for the task. Any value is allowed."
),
example='{\n"debug": false,\n"mode": "benchmarks"\n}',
)
@ -25,7 +27,9 @@ class Artifact(BaseModel):
)
relative_path: Optional[str] = Field(
None,
description="Relative path of the artifact in the agent's workspace",
description=(
"Relative path of the artifact in the agent's workspace"
),
example="python/code/",
)
@ -34,7 +38,9 @@ class ArtifactUpload(BaseModel):
file: bytes = Field(..., description="File to upload")
relative_path: Optional[str] = Field(
None,
description="Relative path of the artifact in the agent's workspace",
description=(
"Relative path of the artifact in the agent's workspace"
),
example="python/code/",
)
@ -42,7 +48,10 @@ class ArtifactUpload(BaseModel):
class StepInput(BaseModel):
__root__: Any = Field(
...,
description="Input parameters for the task step. Any value is allowed.",
description=(
"Input parameters for the task step. Any value is"
" allowed."
),
example='{\n"file_to_refactor": "models.py"\n}',
)
@ -51,7 +60,8 @@ class StepOutput(BaseModel):
__root__: Any = Field(
...,
description=(
"Output that the task step has produced. Any value is allowed."
"Output that the task step has produced. Any value is"
" allowed."
),
example='{\n"tokens": 7894,\n"estimated_cost": "0,24$"\n}',
)
@ -61,7 +71,9 @@ class TaskRequestBody(BaseModel):
input: Optional[str] = Field(
None,
description="Input prompt for the task.",
example="Write the words you receive to the file 'output.txt'.",
example=(
"Write the words you receive to the file 'output.txt'."
),
)
additional_input: Optional[TaskInput] = None
@ -84,7 +96,9 @@ class Task(TaskRequestBody):
class StepRequestBody(BaseModel):
input: Optional[str] = Field(
None, description="Input prompt for the step.", example="Washington"
None,
description="Input prompt for the step.",
example="Washington",
)
additional_input: Optional[StepInput] = None
@ -107,22 +121,28 @@ class Step(StepRequestBody):
example="6bb1801a-fd80-45e8-899a-4dd723cc602e",
)
name: Optional[str] = Field(
None, description="The name of the task step.", example="Write to file"
None,
description="The name of the task step.",
example="Write to file",
)
status: Status = Field(
..., description="The status of the task step."
)
status: Status = Field(..., description="The status of the task step.")
output: Optional[str] = Field(
None,
description="Output of the task step.",
example=(
"I am going to use the write_to_file command and write Washington"
" to a file called output.txt <write_to_file('output.txt',"
" 'Washington')"
"I am going to use the write_to_file command and write"
" Washington to a file called output.txt"
" <write_to_file('output.txt', 'Washington')"
),
)
additional_output: Optional[StepOutput] = None
artifacts: List[Artifact] = Field(
[], description="A list of artifacts that the step has produced."
[],
description="A list of artifacts that the step has produced.",
)
is_last: Optional[bool] = Field(
False, description="Whether this is the last step in the task."
False,
description="Whether this is the last step in the task.",
)

@ -31,26 +31,33 @@ def maximal_marginal_relevance(
return []
if query_embedding.ndim == 1:
query_embedding = np.expand_dims(query_embedding, axis=0)
similarity_to_query = cosine_similarity(query_embedding, embedding_list)[0]
similarity_to_query = cosine_similarity(
query_embedding, embedding_list
)[0]
most_similar = int(np.argmax(similarity_to_query))
idxs = [most_similar]
selected = np.array([embedding_list[most_similar]])
while len(idxs) < min(k, len(embedding_list)):
best_score = -np.inf
idx_to_add = -1
similarity_to_selected = cosine_similarity(embedding_list, selected)
similarity_to_selected = cosine_similarity(
embedding_list, selected
)
for i, query_score in enumerate(similarity_to_query):
if i in idxs:
continue
redundant_score = max(similarity_to_selected[i])
equation_score = (
lambda_mult * query_score - (1 - lambda_mult) * redundant_score
lambda_mult * query_score
- (1 - lambda_mult) * redundant_score
)
if equation_score > best_score:
best_score = equation_score
idx_to_add = i
idxs.append(idx_to_add)
selected = np.append(selected, [embedding_list[idx_to_add]], axis=0)
selected = np.append(
selected, [embedding_list[idx_to_add]], axis=0
)
return idxs

@ -10,14 +10,18 @@ from swarms.models.openai_models import (
from swarms.models.zephyr import Zephyr # noqa: E402
from swarms.models.biogpt import BioGPT # noqa: E402
from swarms.models.huggingface import HuggingfaceLLM # noqa: E402
from swarms.models.wizard_storytelling import WizardLLMStoryTeller # noqa: E402
from swarms.models.wizard_storytelling import (
WizardLLMStoryTeller,
) # noqa: E402
from swarms.models.mpt import MPT7B # noqa: E402
# MultiModal Models
from swarms.models.idefics import Idefics # noqa: E402
from swarms.models.vilt import Vilt # noqa: E402
from swarms.models.nougat import Nougat # noqa: E402
from swarms.models.layoutlm_document_qa import LayoutLMDocumentQA # noqa: E402
from swarms.models.layoutlm_document_qa import (
LayoutLMDocumentQA,
) # noqa: E402
from swarms.models.gpt4_vision_api import GPT4VisionAPI # noqa: E402
# from swarms.models.gpt4v import GPT4Vision

@ -45,10 +45,16 @@ def xor_args(*arg_groups: Tuple[str, ...]) -> Callable:
def wrapper(*args: Any, **kwargs: Any) -> Any:
"""Validate exactly one arg in each group is not None."""
counts = [
sum(1 for arg in arg_group if kwargs.get(arg) is not None)
sum(
1
for arg in arg_group
if kwargs.get(arg) is not None
)
for arg_group in arg_groups
]
invalid_groups = [i for i, count in enumerate(counts) if count != 1]
invalid_groups = [
i for i, count in enumerate(counts) if count != 1
]
if invalid_groups:
invalid_group_names = [
", ".join(arg_groups[i]) for i in invalid_groups
@ -119,8 +125,9 @@ def guard_import(
module = importlib.import_module(module_name, package)
except ImportError:
raise ImportError(
f"Could not import {module_name} python package. "
f"Please install it with `pip install {pip_name or module_name}`."
f"Could not import {module_name} python package. Please"
" install it with `pip install"
f" {pip_name or module_name}`."
)
return module
@ -134,25 +141,33 @@ def check_package_version(
) -> None:
"""Check the version of a package."""
imported_version = parse(version(package))
if lt_version is not None and imported_version >= parse(lt_version):
if lt_version is not None and imported_version >= parse(
lt_version
):
raise ValueError(
f"Expected {package} version to be < {lt_version}. Received "
f"{imported_version}."
f"Expected {package} version to be < {lt_version}."
f" Received {imported_version}."
)
if lte_version is not None and imported_version > parse(lte_version):
if lte_version is not None and imported_version > parse(
lte_version
):
raise ValueError(
f"Expected {package} version to be <= {lte_version}. Received "
f"{imported_version}."
f"Expected {package} version to be <= {lte_version}."
f" Received {imported_version}."
)
if gt_version is not None and imported_version <= parse(gt_version):
if gt_version is not None and imported_version <= parse(
gt_version
):
raise ValueError(
f"Expected {package} version to be > {gt_version}. Received "
f"{imported_version}."
f"Expected {package} version to be > {gt_version}."
f" Received {imported_version}."
)
if gte_version is not None and imported_version < parse(gte_version):
if gte_version is not None and imported_version < parse(
gte_version
):
raise ValueError(
f"Expected {package} version to be >= {gte_version}. Received "
f"{imported_version}."
f"Expected {package} version to be >= {gte_version}."
f" Received {imported_version}."
)
@ -185,9 +200,11 @@ def build_extra_kwargs(
if field_name in extra_kwargs:
raise ValueError(f"Found {field_name} supplied twice.")
if field_name not in all_required_field_names:
warnings.warn(f"""WARNING! {field_name} is not default parameter.
warnings.warn(
f"""WARNING! {field_name} is not default parameter.
{field_name} was transferred to model_kwargs.
Please confirm that {field_name} is what you intended.""")
Please confirm that {field_name} is what you intended."""
)
extra_kwargs[field_name] = values.pop(field_name)
invalid_model_kwargs = all_required_field_names.intersection(
@ -195,8 +212,9 @@ def build_extra_kwargs(
)
if invalid_model_kwargs:
raise ValueError(
f"Parameters {invalid_model_kwargs} should be specified explicitly."
" Instead they were passed in as part of `model_kwargs` parameter."
f"Parameters {invalid_model_kwargs} should be specified"
" explicitly. Instead they were passed in as part of"
" `model_kwargs` parameter."
)
return extra_kwargs
@ -273,12 +291,16 @@ class _AnthropicCommon(BaseLanguageModel):
check_package_version("anthropic", gte_version="0.3")
values["client"] = anthropic.Anthropic(
base_url=values["anthropic_api_url"],
api_key=values["anthropic_api_key"].get_secret_value(),
api_key=values[
"anthropic_api_key"
].get_secret_value(),
timeout=values["default_request_timeout"],
)
values["async_client"] = anthropic.AsyncAnthropic(
base_url=values["anthropic_api_url"],
api_key=values["anthropic_api_key"].get_secret_value(),
api_key=values[
"anthropic_api_key"
].get_secret_value(),
timeout=values["default_request_timeout"],
)
values["HUMAN_PROMPT"] = anthropic.HUMAN_PROMPT
@ -316,7 +338,9 @@ class _AnthropicCommon(BaseLanguageModel):
self, stop: Optional[List[str]] = None
) -> List[str]:
if not self.HUMAN_PROMPT or not self.AI_PROMPT:
raise NameError("Please ensure the anthropic package is loaded")
raise NameError(
"Please ensure the anthropic package is loaded"
)
if stop is None:
stop = []
@ -375,7 +399,9 @@ class Anthropic(LLM, _AnthropicCommon):
def _wrap_prompt(self, prompt: str) -> str:
if not self.HUMAN_PROMPT or not self.AI_PROMPT:
raise NameError("Please ensure the anthropic package is loaded")
raise NameError(
"Please ensure the anthropic package is loaded"
)
if prompt.startswith(self.HUMAN_PROMPT):
return prompt # Already wrapped.
@ -389,7 +415,8 @@ class Anthropic(LLM, _AnthropicCommon):
# As a last resort, wrap the prompt ourselves to emulate instruct-style.
return (
f"{self.HUMAN_PROMPT} {prompt}{self.AI_PROMPT} Sure, here you go:\n"
f"{self.HUMAN_PROMPT} {prompt}{self.AI_PROMPT} Sure, here"
" you go:\n"
)
def _call(
@ -419,7 +446,10 @@ class Anthropic(LLM, _AnthropicCommon):
if self.streaming:
completion = ""
for chunk in self._stream(
prompt=prompt, stop=stop, run_manager=run_manager, **kwargs
prompt=prompt,
stop=stop,
run_manager=run_manager,
**kwargs,
):
completion += chunk.text
return completion
@ -447,7 +477,10 @@ class Anthropic(LLM, _AnthropicCommon):
if self.streaming:
completion = ""
async for chunk in self._astream(
prompt=prompt, stop=stop, run_manager=run_manager, **kwargs
prompt=prompt,
stop=stop,
run_manager=run_manager,
**kwargs,
):
completion += chunk.text
return completion
@ -533,10 +566,14 @@ class Anthropic(LLM, _AnthropicCommon):
chunk = GenerationChunk(text=token.completion)
yield chunk
if run_manager:
await run_manager.on_llm_new_token(chunk.text, chunk=chunk)
await run_manager.on_llm_new_token(
chunk.text, chunk=chunk
)
def get_num_tokens(self, text: str) -> int:
"""Calculate number of tokens."""
if not self.count_tokens:
raise NameError("Please ensure the anthropic package is loaded")
raise NameError(
"Please ensure the anthropic package is loaded"
)
return self.count_tokens(text)

@ -27,7 +27,9 @@ class AbstractModel(ABC):
def chat(self, task: str, history: str = "") -> str:
"""Chat with the model"""
complete_task = task + " | " + history # Delimiter for clarity
complete_task = (
task + " | " + history
) # Delimiter for clarity
return self.run(complete_task)
def __call__(self, task: str) -> str:

@ -107,7 +107,9 @@ class BaseMultiModalModel:
image_pil = Image.open(BytesIO(response.content))
return image_pil
except requests.RequestException as error:
print(f"Error fetching image from {img} and error: {error}")
print(
f"Error fetching image from {img} and error: {error}"
)
return None
def encode_img(self, img: str):
@ -142,14 +144,18 @@ class BaseMultiModalModel:
"""
# Instantiate the thread pool executor
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
with ThreadPoolExecutor(
max_workers=self.max_workers
) as executor:
results = executor.map(self.run, tasks, imgs)
# Print the results for debugging
for result in results:
print(result)
def run_batch(self, tasks_images: List[Tuple[str, str]]) -> List[str]:
def run_batch(
self, tasks_images: List[Tuple[str, str]]
) -> List[str]:
"""Process a batch of tasks and images"""
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [
@ -176,7 +182,9 @@ class BaseMultiModalModel:
"""Process a batch of tasks and images asynchronously with retries"""
loop = asyncio.get_event_loop()
futures = [
loop.run_in_executor(None, self.run_with_retries, task, img)
loop.run_in_executor(
None, self.run_with_retries, task, img
)
for task, img in tasks_images
]
return await asyncio.gather(*futures)
@ -194,7 +202,9 @@ class BaseMultiModalModel:
print(f"Error with the request {error}")
continue
def run_batch_with_retries(self, tasks_images: List[Tuple[str, str]]):
def run_batch_with_retries(
self, tasks_images: List[Tuple[str, str]]
):
"""Run the model with retries"""
for i in range(self.retries):
try:

@ -112,11 +112,12 @@ class BioClip:
template: str = "this is a photo of ",
context_length: int = 256,
):
image = torch.stack([self.preprocess_val(Image.open(img_path))]).to(
self.device
)
image = torch.stack(
[self.preprocess_val(Image.open(img_path))]
).to(self.device)
texts = self.tokenizer(
[template + l for l in labels], context_length=context_length
[template + l for l in labels],
context_length=context_length,
).to(self.device)
with torch.no_grad():
@ -128,7 +129,9 @@ class BioClip:
.detach()
.softmax(dim=-1)
)
sorted_indices = torch.argsort(logits, dim=-1, descending=True)
sorted_indices = torch.argsort(
logits, dim=-1, descending=True
)
logits = logits.cpu().numpy()
sorted_indices = sorted_indices.cpu().numpy()
@ -149,7 +152,10 @@ class BioClip:
metadata["filename"]
+ "\n"
+ "\n".join(
[f"{k}: {v*100:.1f}" for k, v in metadata["top_probs"].items()]
[
f"{k}: {v*100:.1f}"
for k, v in metadata["top_probs"].items()
]
)
)
ax.set_title(title, fontsize=14)

@ -34,7 +34,12 @@ advantage of BioGPT on biomedical literature to generate fluent descriptions for
"""
import torch
from transformers import pipeline, set_seed, BioGptTokenizer, BioGptForCausalLM
from transformers import (
pipeline,
set_seed,
BioGptTokenizer,
BioGptForCausalLM,
)
class BioGPT:
@ -85,8 +90,12 @@ class BioGPT:
self.do_sample = do_sample
self.min_length = min_length
self.model = BioGptForCausalLM.from_pretrained(self.model_name)
self.tokenizer = BioGptTokenizer.from_pretrained(self.model_name)
self.model = BioGptForCausalLM.from_pretrained(
self.model_name
)
self.tokenizer = BioGptTokenizer.from_pretrained(
self.model_name
)
def __call__(self, text: str):
"""
@ -103,7 +112,9 @@ class BioGPT:
"""
set_seed(42)
generator = pipeline(
"text-generation", model=self.model, tokenizer=self.tokenizer
"text-generation",
model=self.model,
tokenizer=self.tokenizer,
)
out = generator(
text,
@ -156,7 +167,9 @@ class BioGPT:
num_beams=num_beams,
early_stopping=early_stopping,
)
return self.tokenizer.decode(beam_output[0], skip_special_tokens=True)
return self.tokenizer.decode(
beam_output[0], skip_special_tokens=True
)
# Feature 1: Set a new tokenizer and model
def set_pretrained_model(self, model_name):
@ -167,8 +180,12 @@ class BioGPT:
model_name (str): Name of the pretrained model.
"""
self.model_name = model_name
self.model = BioGptForCausalLM.from_pretrained(self.model_name)
self.tokenizer = BioGptTokenizer.from_pretrained(self.model_name)
self.model = BioGptForCausalLM.from_pretrained(
self.model_name
)
self.tokenizer = BioGptTokenizer.from_pretrained(
self.model_name
)
# Feature 2: Get the model's config details
def get_config(self):

@ -32,7 +32,9 @@ def _create_retry_decorator(llm) -> Callable[[Any], Any]:
return retry(
reraise=True,
stop=stop_after_attempt(llm.max_retries),
wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds),
wait=wait_exponential(
multiplier=1, min=min_seconds, max=max_seconds
),
retry=(retry_if_exception_type(cohere.error.CohereError)),
before_sleep=before_sleep_log(logger, logging.WARNING),
)
@ -65,7 +67,9 @@ class BaseCohere(Serializable):
client: Any #: :meta private:
async_client: Any #: :meta private:
model: Optional[str] = Field(default=None, description="Model name to use.")
model: Optional[str] = Field(
default=None, description="Model name to use."
)
"""Model name to use."""
temperature: float = 0.75

@ -116,7 +116,9 @@ class Dalle3:
byte_array = byte_stream.getvalue()
return byte_array
@backoff.on_exception(backoff.expo, Exception, max_time=max_time_seconds)
@backoff.on_exception(
backoff.expo, Exception, max_time=max_time_seconds
)
def __call__(self, task: str):
"""
Text to image conversion using the Dalle3 API
@ -169,8 +171,8 @@ class Dalle3:
print(
colored(
(
f"Error running Dalle3: {error} try optimizing your api"
" key and or try again"
f"Error running Dalle3: {error} try"
" optimizing your api key and or try again"
),
"red",
)
@ -198,7 +200,9 @@ class Dalle3:
with open(full_path, "wb") as file:
file.write(response.content)
else:
raise ValueError(f"Failed to download image from {img_url}")
raise ValueError(
f"Failed to download image from {img_url}"
)
def create_variations(self, img: str):
"""
@ -234,14 +238,21 @@ class Dalle3:
print(
colored(
(
f"Error running Dalle3: {error} try optimizing your api"
" key and or try again"
f"Error running Dalle3: {error} try"
" optimizing your api key and or try again"
),
"red",
)
)
print(colored(f"Error running Dalle3: {error.http_status}", "red"))
print(colored(f"Error running Dalle3: {error.error}", "red"))
print(
colored(
f"Error running Dalle3: {error.http_status}",
"red",
)
)
print(
colored(f"Error running Dalle3: {error.error}", "red")
)
raise error
def print_dashboard(self):
@ -300,7 +311,9 @@ class Dalle3:
executor.submit(self, task): task for task in tasks
}
results = []
for future in concurrent.futures.as_completed(future_to_task):
for future in concurrent.futures.as_completed(
future_to_task
):
task = future_to_task[future]
try:
img = future.result()
@ -311,19 +324,27 @@ class Dalle3:
print(
colored(
(
f"Error running Dalle3: {error} try optimizing"
" your api key and or try again"
f"Error running Dalle3: {error} try"
" optimizing your api key and or try"
" again"
),
"red",
)
)
print(
colored(
f"Error running Dalle3: {error.http_status}", "red"
(
"Error running Dalle3:"
f" {error.http_status}"
),
"red",
)
)
print(
colored(f"Error running Dalle3: {error.error}", "red")
colored(
f"Error running Dalle3: {error.error}",
"red",
)
)
raise error
@ -339,7 +360,9 @@ class Dalle3:
"""Str method for the Dalle3 class"""
return f"Dalle3(image_url={self.image_url})"
@backoff.on_exception(backoff.expo, Exception, max_tries=max_retries)
@backoff.on_exception(
backoff.expo, Exception, max_tries=max_retries
)
def rate_limited_call(self, task: str):
"""Rate limited call to the Dalle3 API"""
return self.__call__(task)

@ -6,7 +6,11 @@ from typing import Union
import torch
from termcolor import colored
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from transformers import (
AutoModelForSpeechSeq2Seq,
AutoProcessor,
pipeline,
)
def async_retry(max_retries=3, exceptions=(Exception,), delay=1):
@ -29,8 +33,8 @@ def async_retry(max_retries=3, exceptions=(Exception,), delay=1):
if retries <= 0:
raise
print(
f"Retry after exception: {e}, Attempts remaining:"
f" {retries}"
f"Retry after exception: {e}, Attempts"
f" remaining: {retries}"
)
await asyncio.sleep(delay)
@ -66,7 +70,9 @@ class DistilWhisperModel:
def __init__(self, model_id="distil-whisper/distil-large-v2"):
self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
self.torch_dtype = (
torch.float16 if torch.cuda.is_available() else torch.float32
torch.float16
if torch.cuda.is_available()
else torch.float32
)
self.model_id = model_id
self.model = AutoModelForSpeechSeq2Seq.from_pretrained(
@ -106,7 +112,9 @@ class DistilWhisperModel:
:return: The transcribed text.
"""
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, self.transcribe, inputs)
return await loop.run_in_executor(
None, self.transcribe, inputs
)
def real_time_transcribe(self, audio_file_path, chunk_duration=5):
"""
@ -130,13 +138,21 @@ class DistilWhisperModel:
sample_rate = audio_input.sampling_rate
len(audio_input.array) / sample_rate
chunks = [
audio_input.array[i : i + sample_rate * chunk_duration]
audio_input.array[
i : i + sample_rate * chunk_duration
]
for i in range(
0, len(audio_input.array), sample_rate * chunk_duration
0,
len(audio_input.array),
sample_rate * chunk_duration,
)
]
print(colored("Starting real-time transcription...", "green"))
print(
colored(
"Starting real-time transcription...", "green"
)
)
for i, chunk in enumerate(chunks):
# Process the current chunk
@ -146,8 +162,8 @@ class DistilWhisperModel:
return_tensors="pt",
padding=True,
)
processed_inputs = processed_inputs.input_values.to(
self.device
processed_inputs = (
processed_inputs.input_values.to(self.device)
)
# Generate transcription for the chunk
@ -158,7 +174,9 @@ class DistilWhisperModel:
# Print the chunk's transcription
print(
colored(f"Chunk {i+1}/{len(chunks)}: ", "yellow")
colored(
f"Chunk {i+1}/{len(chunks)}: ", "yellow"
)
+ transcription
)
@ -167,5 +185,8 @@ class DistilWhisperModel:
except Exception as e:
print(
colored(f"An error occurred during transcription: {e}", "red")
colored(
f"An error occurred during transcription: {e}",
"red",
)
)

@ -13,7 +13,8 @@ def _import_elevenlabs() -> Any:
import elevenlabs
except ImportError as e:
raise ImportError(
"Cannot import elevenlabs, please install `pip install elevenlabs`."
"Cannot import elevenlabs, please install `pip install"
" elevenlabs`."
) from e
return elevenlabs
@ -52,16 +53,18 @@ class ElevenLabsText2SpeechTool(BaseTool):
name: str = "eleven_labs_text2speech"
description: str = (
"A wrapper around Eleven Labs Text2Speech. "
"Useful for when you need to convert text to speech. "
"It supports multiple languages, including English, German, Polish, "
"Spanish, Italian, French, Portuguese, and Hindi. "
"A wrapper around Eleven Labs Text2Speech. Useful for when"
" you need to convert text to speech. It supports multiple"
" languages, including English, German, Polish, Spanish,"
" Italian, French, Portuguese, and Hindi. "
)
@root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key exists in environment."""
_ = get_from_dict_or_env(values, "eleven_api_key", "ELEVEN_API_KEY")
_ = get_from_dict_or_env(
values, "eleven_api_key", "ELEVEN_API_KEY"
)
return values
@ -102,7 +105,9 @@ class ElevenLabsText2SpeechTool(BaseTool):
def save(self, speech_file: str, path: str) -> None:
"""Save the speech file to a path."""
raise NotImplementedError("Saving not implemented for this tool.")
raise NotImplementedError(
"Saving not implemented for this tool."
)
def __str__(self):
return "ElevenLabsText2SpeechTool"

@ -14,7 +14,9 @@ class Embeddings(ABC):
def embed_query(self, text: str) -> List[float]:
"""Embed query text."""
async def aembed_documents(self, texts: List[str]) -> List[List[float]]:
async def aembed_documents(
self, texts: List[str]
) -> List[List[float]]:
"""Embed search docs."""
raise NotImplementedError

@ -83,4 +83,6 @@ class FastViT:
top_classes = top_classes.cpu().numpy().tolist()
# top_class_labels = [FASTVIT_IMAGENET_1K_CLASSES[i] for i in top_classes] # Uncomment if class labels are needed
return ClassificationResult(class_id=top_classes, confidence=top_probs)
return ClassificationResult(
class_id=top_classes, confidence=top_probs
)

@ -43,7 +43,9 @@ class Fuyu:
self.device_map = device_map
self.max_new_tokens = max_new_tokens
self.tokenizer = AutoTokenizer.from_pretrained(pretrained_path)
self.tokenizer = AutoTokenizer.from_pretrained(
pretrained_path
)
self.image_processor = FuyuImageProcessor()
self.processor = FuyuProcessor(
image_processor=self.image_processor,
@ -87,5 +89,7 @@ class Fuyu:
image_pil = Image.open(BytesIO(response.content))
return image_pil
except requests.RequestException as error:
print(f"Error fetching image from {img} and error: {error}")
print(
f"Error fetching image from {img} and error: {error}"
)
return None

@ -15,7 +15,10 @@ from termcolor import colored
try:
import cv2
except ImportError:
print("OpenCV not installed. Please install OpenCV to use this model.")
print(
"OpenCV not installed. Please install OpenCV to use this"
" model."
)
raise ImportError
# Load environment variables
@ -127,7 +130,10 @@ class GPT4VisionAPI:
payload = {
"model": self.model_name,
"messages": [
{"role": "system", "content": [self.system_prompt]},
{
"role": "system",
"content": [self.system_prompt],
},
{
"role": "user",
"content": [
@ -135,9 +141,7 @@ class GPT4VisionAPI:
{
"type": "image_url",
"image_url": {
"url": (
f"data:image/jpeg;base64,{base64_image}"
)
"url": f"data:image/jpeg;base64,{base64_image}"
},
},
],
@ -241,7 +245,9 @@ class GPT4VisionAPI:
if not success:
break
_, buffer = cv2.imencode(".jpg", frame)
base64_frames.append(base64.b64encode(buffer).decode("utf-8"))
base64_frames.append(
base64.b64encode(buffer).decode("utf-8")
)
video.release()
print(len(base64_frames), "frames read.")
@ -266,7 +272,10 @@ class GPT4VisionAPI:
payload = {
"model": self.model_name,
"messages": [
{"role": "system", "content": [self.system_prompt]},
{
"role": "system",
"content": [self.system_prompt],
},
{
"role": "user",
"content": [
@ -274,9 +283,7 @@ class GPT4VisionAPI:
{
"type": "image_url",
"image_url": {
"url": (
f"data:image/jpeg;base64,{base64_image}"
)
"url": f"data:image/jpeg;base64,{base64_image}"
},
},
],
@ -326,7 +333,9 @@ class GPT4VisionAPI:
"""
# Instantiate the thread pool executor
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
with ThreadPoolExecutor(
max_workers=self.max_workers
) as executor:
results = executor.map(self.run, tasks, imgs)
# Print the results for debugging
@ -372,9 +381,7 @@ class GPT4VisionAPI:
{
"type": "image_url",
"image_url": {
"url": (
f"data:image/jpeg;base64,{base64_image}"
)
"url": f"data:image/jpeg;base64,{base64_image}"
},
},
],
@ -384,7 +391,9 @@ class GPT4VisionAPI:
}
async with aiohttp.ClientSession() as session:
async with session.post(
self.openai_proxy, headers=headers, data=json.dumps(payload)
self.openai_proxy,
headers=headers,
data=json.dumps(payload),
) as response:
out = await response.json()
content = out["choices"][0]["message"]["content"]
@ -393,7 +402,9 @@ class GPT4VisionAPI:
print(f"Error with the request {error}")
raise error
def run_batch(self, tasks_images: List[Tuple[str, str]]) -> List[str]:
def run_batch(
self, tasks_images: List[Tuple[str, str]]
) -> List[str]:
"""Process a batch of tasks and images"""
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [
@ -420,7 +431,9 @@ class GPT4VisionAPI:
"""Process a batch of tasks and images asynchronously with retries"""
loop = asyncio.get_event_loop()
futures = [
loop.run_in_executor(None, self.run_with_retries, task, img)
loop.run_in_executor(
None, self.run_with_retries, task, img
)
for task, img in tasks_images
]
return await asyncio.gather(*futures)
@ -428,7 +441,9 @@ class GPT4VisionAPI:
def health_check(self):
"""Health check for the GPT4Vision model"""
try:
response = requests.get("https://api.openai.com/v1/engines")
response = requests.get(
"https://api.openai.com/v1/engines"
)
return response.status_code == 200
except requests.RequestException as error:
print(f"Health check failed: {error}")

@ -65,7 +65,9 @@ class GPT4Vision:
model: str = "gpt-4-vision-preview"
backoff_factor: float = 2.0
timeout_seconds: int = 10
openai_api_key: Optional[str] = None or os.getenv("OPENAI_API_KEY")
openai_api_key: Optional[str] = None or os.getenv(
"OPENAI_API_KEY"
)
# 'Low' or 'High' for respesctively fast or high quality, but high more token usage
quality: str = "low"
# Max tokens to use for the API request, the maximum might be 3,000 but we don't know
@ -131,9 +133,14 @@ class GPT4Vision:
return out
except openai.OpenAIError as e:
# logger.error(f"OpenAI API error: {e}")
return f"OpenAI API error: Could not process the image. {e}"
return (
f"OpenAI API error: Could not process the image. {e}"
)
except Exception as e:
return f"Unexpected error occurred while processing the image. {e}"
return (
"Unexpected error occurred while processing the"
f" image. {e}"
)
def clean_output(self, output: str):
# Regex pattern to find the Choice object representation in the output
@ -182,11 +189,18 @@ class GPT4Vision:
return print(response.choices[0])
except openai.OpenAIError as e:
# logger.error(f"OpenAI API error: {e}")
return f"OpenAI API error: Could not process the image. {e}"
return (
f"OpenAI API error: Could not process the image. {e}"
)
except Exception as e:
return f"Unexpected error occurred while processing the image. {e}"
return (
"Unexpected error occurred while processing the"
f" image. {e}"
)
def run_batch(self, tasks_images: List[Tuple[str, str]]) -> List[str]:
def run_batch(
self, tasks_images: List[Tuple[str, str]]
) -> List[str]:
"""Process a batch of tasks and images"""
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [
@ -213,7 +227,9 @@ class GPT4Vision:
"""Process a batch of tasks and images asynchronously with retries"""
loop = asyncio.get_event_loop()
futures = [
loop.run_in_executor(None, self.run_with_retries, task, img)
loop.run_in_executor(
None, self.run_with_retries, task, img
)
for task, img in tasks_images
]
return await asyncio.gather(*futures)
@ -240,7 +256,9 @@ class GPT4Vision:
def health_check(self):
"""Health check for the GPT4Vision model"""
try:
response = requests.get("https://api.openai.com/v1/engines")
response = requests.get(
"https://api.openai.com/v1/engines"
)
return response.status_code == 200
except requests.RequestException as error:
print(f"Health check failed: {error}")

@ -7,7 +7,11 @@ from typing import List, Tuple
import torch
from termcolor import colored
from torch.nn.parallel import DistributedDataParallel as DDP
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
)
class HuggingfaceLLM:
@ -173,7 +177,10 @@ class HuggingfaceLLM:
self.model_id, *args, **kwargs
)
self.model = AutoModelForCausalLM.from_pretrained(
self.model_id, quantization_config=bnb_config, *args, **kwargs
self.model_id,
quantization_config=bnb_config,
*args,
**kwargs,
)
self.model # .to(self.device)
@ -182,7 +189,11 @@ class HuggingfaceLLM:
# raise
print(
colored(
f"Failed to load the model and or the tokenizer: {e}", "red"
(
"Failed to load the model and or the"
f" tokenizer: {e}"
),
"red",
)
)
@ -198,7 +209,9 @@ class HuggingfaceLLM:
"""Load the model"""
if not self.model or not self.tokenizer:
try:
self.tokenizer = AutoTokenizer.from_pretrained(self.model_id)
self.tokenizer = AutoTokenizer.from_pretrained(
self.model_id
)
bnb_config = (
BitsAndBytesConfig(**self.quantization_config)
@ -214,7 +227,8 @@ class HuggingfaceLLM:
self.model = DDP(self.model)
except Exception as error:
self.logger.error(
f"Failed to load the model or the tokenizer: {error}"
"Failed to load the model or the tokenizer:"
f" {error}"
)
raise
@ -226,7 +240,9 @@ class HuggingfaceLLM:
results = list(executor.map(self.run, tasks))
return results
def run_batch(self, tasks_images: List[Tuple[str, str]]) -> List[str]:
def run_batch(
self, tasks_images: List[Tuple[str, str]]
) -> List[str]:
"""Process a batch of tasks and images"""
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [
@ -254,9 +270,9 @@ class HuggingfaceLLM:
self.print_dashboard(task)
try:
inputs = self.tokenizer.encode(task, return_tensors="pt").to(
self.device
)
inputs = self.tokenizer.encode(
task, return_tensors="pt"
).to(self.device)
# self.log.start()
@ -266,7 +282,9 @@ class HuggingfaceLLM:
output_sequence = []
outputs = self.model.generate(
inputs, max_length=len(inputs) + 1, do_sample=True
inputs,
max_length=len(inputs) + 1,
do_sample=True,
)
output_tokens = outputs[0][-1]
output_sequence.append(output_tokens.item())
@ -274,7 +292,8 @@ class HuggingfaceLLM:
# print token in real-time
print(
self.tokenizer.decode(
[output_tokens], skip_special_tokens=True
[output_tokens],
skip_special_tokens=True,
),
end="",
flush=True,
@ -287,13 +306,16 @@ class HuggingfaceLLM:
)
del inputs
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
return self.tokenizer.decode(
outputs[0], skip_special_tokens=True
)
except Exception as e:
print(
colored(
(
"HuggingfaceLLM could not generate text because of"
f" error: {e}, try optimizing your arguments"
"HuggingfaceLLM could not generate text"
f" because of error: {e}, try optimizing your"
" arguments"
),
"red",
)
@ -318,9 +340,9 @@ class HuggingfaceLLM:
self.print_dashboard(task)
try:
inputs = self.tokenizer.encode(task, return_tensors="pt").to(
self.device
)
inputs = self.tokenizer.encode(
task, return_tensors="pt"
).to(self.device)
# self.log.start()
@ -330,7 +352,9 @@ class HuggingfaceLLM:
output_sequence = []
outputs = self.model.generate(
inputs, max_length=len(inputs) + 1, do_sample=True
inputs,
max_length=len(inputs) + 1,
do_sample=True,
)
output_tokens = outputs[0][-1]
output_sequence.append(output_tokens.item())
@ -338,7 +362,8 @@ class HuggingfaceLLM:
# print token in real-time
print(
self.tokenizer.decode(
[output_tokens], skip_special_tokens=True
[output_tokens],
skip_special_tokens=True,
),
end="",
flush=True,
@ -352,7 +377,9 @@ class HuggingfaceLLM:
del inputs
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
return self.tokenizer.decode(
outputs[0], skip_special_tokens=True
)
except Exception as e:
self.logger.error(f"Failed to generate the text: {e}")
raise

@ -100,10 +100,14 @@ class Idefics:
"""
inputs = (
self.processor(
prompts, add_end_of_utterance_token=False, return_tensors="pt"
prompts,
add_end_of_utterance_token=False,
return_tensors="pt",
).to(self.device)
if batched_mode
else self.processor(prompts[0], return_tensors="pt").to(self.device)
else self.processor(prompts[0], return_tensors="pt").to(
self.device
)
)
exit_condition = self.processor.tokenizer(
@ -111,7 +115,8 @@ class Idefics:
).input_ids
bad_words_ids = self.processor.tokenizer(
["<image>", "<fake_token_around_image"], add_special_tokens=False
["<image>", "<fake_token_around_image"],
add_special_tokens=False,
).input_ids
generated_ids = self.model.generate(
@ -145,10 +150,14 @@ class Idefics:
"""
inputs = (
self.processor(
prompts, add_end_of_utterance_token=False, return_tensors="pt"
prompts,
add_end_of_utterance_token=False,
return_tensors="pt",
).to(self.device)
if batched_mode
else self.processor(prompts[0], return_tensors="pt").to(self.device)
else self.processor(prompts[0], return_tensors="pt").to(
self.device
)
)
exit_condition = self.processor.tokenizer(
@ -156,7 +165,8 @@ class Idefics:
).input_ids
bad_words_ids = self.processor.tokenizer(
["<image>", "<fake_token_around_image"], add_special_tokens=False
["<image>", "<fake_token_around_image"],
add_special_tokens=False,
).input_ids
generated_ids = self.model.generate(

@ -3,7 +3,11 @@ import logging
import torch
from numpy.linalg import norm
from torch.nn.parallel import DistributedDataParallel as DDP
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
)
def cos_sim(a, b):
@ -92,14 +96,18 @@ class JinaEmbeddings:
self.model # .to(self.device)
except Exception as e:
self.logger.error(f"Failed to load the model or the tokenizer: {e}")
self.logger.error(
f"Failed to load the model or the tokenizer: {e}"
)
raise
def load_model(self):
"""Load the model"""
if not self.model or not self.tokenizer:
try:
self.tokenizer = AutoTokenizer.from_pretrained(self.model_id)
self.tokenizer = AutoTokenizer.from_pretrained(
self.model_id
)
bnb_config = (
BitsAndBytesConfig(**self.quantization_config)
@ -117,7 +125,8 @@ class JinaEmbeddings:
self.model = DDP(self.model)
except Exception as error:
self.logger.error(
f"Failed to load the model or the tokenizer: {error}"
"Failed to load the model or the tokenizer:"
f" {error}"
)
raise
@ -137,7 +146,9 @@ class JinaEmbeddings:
max_length = self.max_length
try:
embeddings = self.model.encode([task], max_length=max_length)
embeddings = self.model.encode(
[task], max_length=max_length
)
if self.cos_sim:
print(cos_sim(embeddings[0], embeddings[1]))
@ -186,7 +197,9 @@ class JinaEmbeddings:
max_length = self.max_length
try:
embeddings = self.model.encode([task], max_length=max_length)
embeddings = self.model.encode(
[task], max_length=max_length
)
if self.cos_sim:
print(cos_sim(embeddings[0], embeddings[1]))

@ -20,7 +20,9 @@ class Detections(BaseModel):
), "All fields must have the same length."
return values
@validator("xyxy", "class_id", "confidence", pre=True, each_item=True)
@validator(
"xyxy", "class_id", "confidence", pre=True, each_item=True
)
def check_not_empty(cls, v):
if isinstance(v, list) and len(v) == 0:
raise ValueError("List must not be empty")
@ -69,7 +71,9 @@ class Kosmos2(BaseModel):
image = Image.open(img)
prompt = "<grounding>An image of"
inputs = self.processor(text=prompt, images=image, return_tensors="pt")
inputs = self.processor(
text=prompt, images=image, return_tensors="pt"
)
outputs = self.model.generate(
**inputs, use_cache=True, max_new_tokens=64
)
@ -83,7 +87,9 @@ class Kosmos2(BaseModel):
entities = self.extract_entities(generated_text)
# Convert entities to detections format
detections = self.process_entities_to_detections(entities, image)
detections = self.process_entities_to_detections(
entities, image
)
return detections
def extract_entities(

@ -51,7 +51,9 @@ class Kosmos:
def run(self, prompt, image):
"""Run Kosmos"""
inputs = self.processor(text=prompt, images=image, return_tensors="pt")
inputs = self.processor(
text=prompt, images=image, return_tensors="pt"
)
generated_ids = self.model.generate(
pixel_values=inputs["pixel_values"],
input_ids=inputs["input_ids"][:, :-1],
@ -65,13 +67,15 @@ class Kosmos:
generated_ids,
skip_special_tokens=True,
)[0]
processed_text, entities = self.processor.post_process_generation(
generated_texts
processed_text, entities = (
self.processor.post_process_generation(generated_texts)
)
def __call__(self, prompt, image):
"""Run call"""
inputs = self.processor(text=prompt, images=image, return_tensors="pt")
inputs = self.processor(
text=prompt, images=image, return_tensors="pt"
)
generated_ids = self.model.generate(
pixel_values=inputs["pixel_values"],
input_ids=inputs["input_ids"][:, :-1],
@ -85,8 +89,8 @@ class Kosmos:
generated_ids,
skip_special_tokens=True,
)[0]
processed_text, entities = self.processor.post_process_generation(
generated_texts
processed_text, entities = (
self.processor.post_process_generation(generated_texts)
)
# tasks
@ -117,7 +121,9 @@ class Kosmos:
prompt = "<grounding> Describe this image in detail"
self.run(prompt, image_url)
def draw_entity_boxes_on_image(image, entities, show=False, save_path=None):
def draw_entity_boxes_on_image(
image, entities, show=False, save_path=None
):
"""_summary_
Args:
image (_type_): image or image path
@ -144,13 +150,17 @@ class Kosmos:
reverse_norm_std = torch.tensor(
[0.26862954, 0.26130258, 0.27577711]
)[:, None, None]
image_tensor = image_tensor * reverse_norm_std + reverse_norm_mean
image_tensor = (
image_tensor * reverse_norm_std + reverse_norm_mean
)
pil_img = T.ToPILImage()(image_tensor)
image_h = pil_img.height
image_w = pil_img.width
image = np.array(pil_img)[:, :, [2, 1, 0]]
else:
raise ValueError(f"invaild image format, {type(image)} for {image}")
raise ValueError(
f"invaild image format, {type(image)} for {image}"
)
if len(entities) == 0:
return image
@ -179,7 +189,9 @@ class Kosmos:
)
# draw bbox
# random color
color = tuple(np.random.randint(0, 255, size=3).tolist())
color = tuple(
np.random.randint(0, 255, size=3).tolist()
)
new_image = cv2.rectangle(
new_image,
(orig_x1, orig_y1),
@ -196,7 +208,12 @@ class Kosmos:
x1 = orig_x1 - l_o
y1 = orig_y1 - l_o
if y1 < text_height + text_offset_original + 2 * text_spaces:
if (
y1
< text_height
+ text_offset_original
+ 2 * text_spaces
):
y1 = (
orig_y1
+ r_o
@ -215,24 +232,40 @@ class Kosmos:
)
text_bg_x1, text_bg_y1, text_bg_x2, text_bg_y2 = (
x1,
y1 - (text_height + text_offset_original + 2 * text_spaces),
y1
- (
text_height
+ text_offset_original
+ 2 * text_spaces
),
x1 + text_width,
y1,
)
for prev_bbox in previous_bboxes:
while is_overlapping(
(text_bg_x1, text_bg_y1, text_bg_x2, text_bg_y2),
(
text_bg_x1,
text_bg_y1,
text_bg_x2,
text_bg_y2,
),
prev_bbox,
):
text_bg_y1 += (
text_height + text_offset_original + 2 * text_spaces
text_height
+ text_offset_original
+ 2 * text_spaces
)
text_bg_y2 += (
text_height + text_offset_original + 2 * text_spaces
text_height
+ text_offset_original
+ 2 * text_spaces
)
y1 += (
text_height + text_offset_original + 2 * text_spaces
text_height
+ text_offset_original
+ 2 * text_spaces
)
if text_bg_y2 >= image_h:

@ -170,7 +170,9 @@ class LlamaFunctionCaller:
prompt = f"{task}\n\n"
# Encode and send to the model
inputs = self.tokenizer([prompt], return_tensors="pt").to(self.runtime)
inputs = self.tokenizer([prompt], return_tensors="pt").to(
self.runtime
)
streamer = TextStreamer(self.tokenizer)

@ -70,7 +70,10 @@ class MultiModalLlava:
def chat(self):
"""Interactive chat in terminal"""
print("Starting chat with LlavaModel. Type 'exit' to end the session.")
print(
"Starting chat with LlavaModel. Type 'exit' to end the"
" session."
)
while True:
user_input = input("You: ")
if user_input.lower() == "exit":

@ -50,7 +50,8 @@ class Mistral:
# Check if the specified device is available
if not torch.cuda.is_available() and device == "cuda":
raise ValueError(
"CUDA is not available. Please choose a different device."
"CUDA is not available. Please choose a different"
" device."
)
# Load the model and tokenizer
@ -62,19 +63,25 @@ class Mistral:
def load_model(self):
try:
self.model = AutoModelForCausalLM.from_pretrained(self.model_name)
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
self.model = AutoModelForCausalLM.from_pretrained(
self.model_name
)
self.tokenizer = AutoTokenizer.from_pretrained(
self.model_name
)
self.model.to(self.device)
except Exception as e:
raise ValueError(f"Error loading the Mistral model: {str(e)}")
raise ValueError(
f"Error loading the Mistral model: {str(e)}"
)
def run(self, task: str):
"""Run the model on a given task."""
try:
model_inputs = self.tokenizer([task], return_tensors="pt").to(
self.device
)
model_inputs = self.tokenizer(
[task], return_tensors="pt"
).to(self.device)
generated_ids = self.model.generate(
**model_inputs,
max_length=self.max_length,
@ -82,7 +89,9 @@ class Mistral:
temperature=self.temperature,
max_new_tokens=self.max_length,
)
output_text = self.tokenizer.batch_decode(generated_ids)[0]
output_text = self.tokenizer.batch_decode(generated_ids)[
0
]
return output_text
except Exception as e:
raise ValueError(f"Error running the model: {str(e)}")
@ -91,9 +100,9 @@ class Mistral:
"""Run the model on a given task."""
try:
model_inputs = self.tokenizer([task], return_tensors="pt").to(
self.device
)
model_inputs = self.tokenizer(
[task], return_tensors="pt"
).to(self.device)
generated_ids = self.model.generate(
**model_inputs,
max_length=self.max_length,
@ -101,7 +110,9 @@ class Mistral:
temperature=self.temperature,
max_new_tokens=self.max_length,
)
output_text = self.tokenizer.batch_decode(generated_ids)[0]
output_text = self.tokenizer.batch_decode(generated_ids)[
0
]
return output_text
except Exception as e:
raise ValueError(f"Error running the model: {str(e)}")

@ -30,7 +30,10 @@ class MPT7B:
"""
def __init__(
self, model_name: str, tokenizer_name: str, max_tokens: int = 100
self,
model_name: str,
tokenizer_name: str,
max_tokens: int = 100,
):
# Loading model and tokenizer details
self.model_name = model_name
@ -138,9 +141,13 @@ class MPT7B:
"""Call the model asynchronously""" ""
return await self.run_async(task, *args, **kwargs)
def batch_generate(self, prompts: list, temperature: float = 1.0) -> list:
def batch_generate(
self, prompts: list, temperature: float = 1.0
) -> list:
"""Batch generate text"""
self.logger.info(f"Generating text for {len(prompts)} prompts...")
self.logger.info(
f"Generating text for {len(prompts)} prompts..."
)
results = []
with torch.autocast("cuda", dtype=torch.bfloat16):
for prompt in prompts:

@ -61,7 +61,9 @@ class Nougat:
def __call__(self, img: str):
"""Call the model with an image_path str as an input"""
image = Image.open(img)
pixel_values = self.processor(image, return_tensors="pt").pixel_values
pixel_values = self.processor(
image, return_tensors="pt"
).pixel_values
# Generate transcriptions, here we only generate 30 tokens
outputs = self.model.generate(
@ -92,7 +94,9 @@ class Nougat:
# Convert the matches to a readable format
cleaned_data = [
"Date: {}, Amount: {}".format(date, amount.replace(",", ""))
"Date: {}, Amount: {}".format(
date, amount.replace(",", "")
)
for date, amount in matches
]

@ -55,13 +55,17 @@ def _create_retry_decorator(
return retry(
reraise=True,
stop=stop_after_attempt(embeddings.max_retries),
wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds),
wait=wait_exponential(
multiplier=1, min=min_seconds, max=max_seconds
),
retry=(
retry_if_exception_type(llm.error.Timeout)
| retry_if_exception_type(llm.error.APIError)
| retry_if_exception_type(llm.error.APIConnectionError)
| retry_if_exception_type(llm.error.RateLimitError)
| retry_if_exception_type(llm.error.ServiceUnavailableError)
| retry_if_exception_type(
llm.error.ServiceUnavailableError
)
),
before_sleep=before_sleep_log(logger, logging.WARNING),
)
@ -77,13 +81,17 @@ def _async_retry_decorator(embeddings: OpenAIEmbeddings) -> Any:
async_retrying = AsyncRetrying(
reraise=True,
stop=stop_after_attempt(embeddings.max_retries),
wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds),
wait=wait_exponential(
multiplier=1, min=min_seconds, max=max_seconds
),
retry=(
retry_if_exception_type(llm.error.Timeout)
| retry_if_exception_type(llm.error.APIError)
| retry_if_exception_type(llm.error.APIConnectionError)
| retry_if_exception_type(llm.error.RateLimitError)
| retry_if_exception_type(llm.error.ServiceUnavailableError)
| retry_if_exception_type(
llm.error.ServiceUnavailableError
)
),
before_sleep=before_sleep_log(logger, logging.WARNING),
)
@ -104,11 +112,15 @@ def _check_response(response: dict) -> dict:
if any(len(d["embedding"]) == 1 for d in response["data"]):
import llm
raise llm.error.APIError("OpenAI API returned an empty embedding")
raise llm.error.APIError(
"OpenAI API returned an empty embedding"
)
return response
def embed_with_retry(embeddings: OpenAIEmbeddings, **kwargs: Any) -> Any:
def embed_with_retry(
embeddings: OpenAIEmbeddings, **kwargs: Any
) -> Any:
"""Use tenacity to retry the embedding call."""
retry_decorator = _create_retry_decorator(embeddings)
@ -176,9 +188,7 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
client: Any #: :meta private:
model: str = "text-embedding-ada-002"
deployment: str = (
model # to support Azure OpenAI Service custom deployment names
)
deployment: str = model # to support Azure OpenAI Service custom deployment names
openai_api_version: Optional[str] = None
# to support Azure OpenAI Service custom endpoints
openai_api_base: Optional[str] = None
@ -191,12 +201,16 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
openai_api_key: Optional[str] = None
openai_organization: Optional[str] = None
allowed_special: Union[Literal["all"], Set[str]] = set()
disallowed_special: Union[Literal["all"], Set[str], Sequence[str]] = "all"
disallowed_special: Union[
Literal["all"], Set[str], Sequence[str]
] = "all"
chunk_size: int = 1000
"""Maximum number of texts to embed in each batch"""
max_retries: int = 6
"""Maximum number of retries to make when generating."""
request_timeout: Optional[Union[float, Tuple[float, float]]] = None
request_timeout: Optional[Union[float, Tuple[float, float]]] = (
None
)
"""Timeout in seconds for the OpenAPI request."""
headers: Any = None
tiktoken_model_name: Optional[str] = None
@ -226,7 +240,9 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
extra = values.get("model_kwargs", {})
for field_name in list(values):
if field_name in extra:
raise ValueError(f"Found {field_name} supplied twice.")
raise ValueError(
f"Found {field_name} supplied twice."
)
if field_name not in all_required_field_names:
warnings.warn(
f"""WARNING! {field_name} is not default parameter.
@ -240,9 +256,9 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
)
if invalid_model_kwargs:
raise ValueError(
f"Parameters {invalid_model_kwargs} should be specified"
" explicitly. Instead they were passed in as part of"
" `model_kwargs` parameter."
f"Parameters {invalid_model_kwargs} should be"
" specified explicitly. Instead they were passed in"
" as part of `model_kwargs` parameter."
)
values["model_kwargs"] = extra
@ -272,7 +288,11 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
"OPENAI_PROXY",
default="",
)
if values["openai_api_type"] in ("azure", "azure_ad", "azuread"):
if values["openai_api_type"] in (
"azure",
"azure_ad",
"azuread",
):
default_api_version = "2022-12-01"
else:
default_api_version = ""
@ -324,9 +344,15 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
return openai_args
def _get_len_safe_embeddings(
self, texts: List[str], *, engine: str, chunk_size: Optional[int] = None
self,
texts: List[str],
*,
engine: str,
chunk_size: Optional[int] = None,
) -> List[List[float]]:
embeddings: List[List[float]] = [[] for _ in range(len(texts))]
embeddings: List[List[float]] = [
[] for _ in range(len(texts))
]
try:
import tiktoken
except ImportError:
@ -343,7 +369,8 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
encoding = tiktoken.encoding_for_model(model_name)
except KeyError:
logger.warning(
"Warning: model not found. Using cl100k_base encoding."
"Warning: model not found. Using cl100k_base"
" encoding."
)
model = "cl100k_base"
encoding = tiktoken.get_encoding(model)
@ -358,7 +385,9 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
disallowed_special=self.disallowed_special,
)
for j in range(0, len(token), self.embedding_ctx_length):
tokens.append(token[j : j + self.embedding_ctx_length])
tokens.append(
token[j : j + self.embedding_ctx_length]
)
indices.append(i)
batched_embeddings: List[List[float]] = []
@ -380,10 +409,16 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
input=tokens[i : i + _chunk_size],
**self._invocation_params,
)
batched_embeddings.extend(r["embedding"] for r in response["data"])
batched_embeddings.extend(
r["embedding"] for r in response["data"]
)
results: List[List[List[float]]] = [[] for _ in range(len(texts))]
num_tokens_in_batch: List[List[int]] = [[] for _ in range(len(texts))]
results: List[List[List[float]]] = [
[] for _ in range(len(texts))
]
num_tokens_in_batch: List[List[int]] = [
[] for _ in range(len(texts))
]
for i in range(len(indices)):
results[indices[i]].append(batched_embeddings[i])
num_tokens_in_batch[indices[i]].append(len(tokens[i]))
@ -400,16 +435,24 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
average = np.average(
_result, axis=0, weights=num_tokens_in_batch[i]
)
embeddings[i] = (average / np.linalg.norm(average)).tolist()
embeddings[i] = (
average / np.linalg.norm(average)
).tolist()
return embeddings
# please refer to
# https://github.com/openai/openai-cookbook/blob/main/examples/Embedding_long_inputs.ipynb
async def _aget_len_safe_embeddings(
self, texts: List[str], *, engine: str, chunk_size: Optional[int] = None
self,
texts: List[str],
*,
engine: str,
chunk_size: Optional[int] = None,
) -> List[List[float]]:
embeddings: List[List[float]] = [[] for _ in range(len(texts))]
embeddings: List[List[float]] = [
[] for _ in range(len(texts))
]
try:
import tiktoken
except ImportError:
@ -426,7 +469,8 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
encoding = tiktoken.encoding_for_model(model_name)
except KeyError:
logger.warning(
"Warning: model not found. Using cl100k_base encoding."
"Warning: model not found. Using cl100k_base"
" encoding."
)
model = "cl100k_base"
encoding = tiktoken.get_encoding(model)
@ -441,7 +485,9 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
disallowed_special=self.disallowed_special,
)
for j in range(0, len(token), self.embedding_ctx_length):
tokens.append(token[j : j + self.embedding_ctx_length])
tokens.append(
token[j : j + self.embedding_ctx_length]
)
indices.append(i)
batched_embeddings: List[List[float]] = []
@ -452,10 +498,16 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
input=tokens[i : i + _chunk_size],
**self._invocation_params,
)
batched_embeddings.extend(r["embedding"] for r in response["data"])
batched_embeddings.extend(
r["embedding"] for r in response["data"]
)
results: List[List[List[float]]] = [[] for _ in range(len(texts))]
num_tokens_in_batch: List[List[int]] = [[] for _ in range(len(texts))]
results: List[List[List[float]]] = [
[] for _ in range(len(texts))
]
num_tokens_in_batch: List[List[int]] = [
[] for _ in range(len(texts))
]
for i in range(len(indices)):
results[indices[i]].append(batched_embeddings[i])
num_tokens_in_batch[indices[i]].append(len(tokens[i]))
@ -474,7 +526,9 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
average = np.average(
_result, axis=0, weights=num_tokens_in_batch[i]
)
embeddings[i] = (average / np.linalg.norm(average)).tolist()
embeddings[i] = (
average / np.linalg.norm(average)
).tolist()
return embeddings
@ -493,7 +547,9 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
"""
# NOTE: to keep things simple, we assume the list may contain texts longer
# than the maximum context and use length-safe embedding function.
return self._get_len_safe_embeddings(texts, engine=self.deployment)
return self._get_len_safe_embeddings(
texts, engine=self.deployment
)
async def aembed_documents(
self, texts: List[str], chunk_size: Optional[int] = 0

@ -3,7 +3,11 @@ from typing import Any, Dict, List, Optional, Union
import openai
import requests
from pydantic import BaseModel, validator
from tenacity import retry, stop_after_attempt, wait_random_exponential
from tenacity import (
retry,
stop_after_attempt,
wait_random_exponential,
)
from termcolor import colored
@ -100,7 +104,9 @@ class FunctionSpecification(BaseModel):
for req_param in self.required or []:
if req_param not in params:
raise ValueError(f"Missing required parameter: {req_param}")
raise ValueError(
f"Missing required parameter: {req_param}"
)
class OpenAIFunctionCaller:
@ -220,7 +226,10 @@ class OpenAIFunctionCaller:
elif message["role"] == "tool":
print(
colored(
f"function ({message['name']}): {message['content']}\n",
(
f"function ({message['name']}):"
f" {message['content']}\n"
),
role_to_color[message["role"]],
)
)

@ -27,7 +27,10 @@ from langchain.llms.base import BaseLLM, create_base_retry_decorator
from langchain.pydantic_v1 import Field, root_validator
from langchain.schema import Generation, LLMResult
from langchain.schema.output import GenerationChunk
from langchain.utils import get_from_dict_or_env, get_pydantic_field_names
from langchain.utils import (
get_from_dict_or_env,
get_pydantic_field_names,
)
from langchain.utils.utils import build_extra_kwargs
@ -44,7 +47,9 @@ def is_openai_v1() -> bool:
def update_token_usage(
keys: Set[str], response: Dict[str, Any], token_usage: Dict[str, Any]
keys: Set[str],
response: Dict[str, Any],
token_usage: Dict[str, Any],
) -> None:
"""Update token usage."""
_keys_to_use = keys.intersection(response["usage"])
@ -65,7 +70,9 @@ def _stream_response_to_generation_chunk(
finish_reason=stream_response["choices"][0].get(
"finish_reason", None
),
logprobs=stream_response["choices"][0].get("logprobs", None),
logprobs=stream_response["choices"][0].get(
"logprobs", None
),
),
)
@ -74,13 +81,15 @@ def _update_response(
response: Dict[str, Any], stream_response: Dict[str, Any]
) -> None:
"""Update response from the stream response."""
response["choices"][0]["text"] += stream_response["choices"][0]["text"]
response["choices"][0]["finish_reason"] = stream_response["choices"][0].get(
"finish_reason", None
)
response["choices"][0]["logprobs"] = stream_response["choices"][0][
"logprobs"
response["choices"][0]["text"] += stream_response["choices"][0][
"text"
]
response["choices"][0]["finish_reason"] = stream_response[
"choices"
][0].get("finish_reason", None)
response["choices"][0]["logprobs"] = stream_response["choices"][
0
]["logprobs"]
def _streaming_response_template() -> Dict[str, Any]:
@ -111,7 +120,9 @@ def _create_retry_decorator(
openai.error.ServiceUnavailableError,
]
return create_base_retry_decorator(
error_types=errors, max_retries=llm.max_retries, run_manager=run_manager
error_types=errors,
max_retries=llm.max_retries,
run_manager=run_manager,
)
@ -121,7 +132,9 @@ def completion_with_retry(
**kwargs: Any,
) -> Any:
"""Use tenacity to retry the completion call."""
retry_decorator = _create_retry_decorator(llm, run_manager=run_manager)
retry_decorator = _create_retry_decorator(
llm, run_manager=run_manager
)
@retry_decorator
def _completion_with_retry(**kwargs: Any) -> Any:
@ -136,7 +149,9 @@ async def acompletion_with_retry(
**kwargs: Any,
) -> Any:
"""Use tenacity to retry the async completion call."""
retry_decorator = _create_retry_decorator(llm, run_manager=run_manager)
retry_decorator = _create_retry_decorator(
llm, run_manager=run_manager
)
@retry_decorator
async def _completion_with_retry(**kwargs: Any) -> Any:
@ -160,7 +175,9 @@ class BaseOpenAI(BaseLLM):
attributes["openai_api_base"] = self.openai_api_base
if self.openai_organization != "":
attributes["openai_organization"] = self.openai_organization
attributes["openai_organization"] = (
self.openai_organization
)
if self.openai_proxy != "":
attributes["openai_proxy"] = self.openai_proxy
@ -199,9 +216,13 @@ class BaseOpenAI(BaseLLM):
openai_proxy: Optional[str] = None
batch_size: int = 20
"""Batch size to use when passing multiple documents to generate."""
request_timeout: Optional[Union[float, Tuple[float, float]]] = None
request_timeout: Optional[Union[float, Tuple[float, float]]] = (
None
)
"""Timeout for requests to OpenAI completion API. Default is 600 seconds."""
logit_bias: Optional[Dict[str, float]] = Field(default_factory=dict)
logit_bias: Optional[Dict[str, float]] = Field(
default_factory=dict
)
"""Adjust the probability of specific tokens being generated."""
max_retries: int = 6
"""Maximum number of retries to make when generating."""
@ -278,7 +299,9 @@ class BaseOpenAI(BaseLLM):
if values["streaming"] and values["n"] > 1:
raise ValueError("Cannot stream results when n > 1.")
if values["streaming"] and values["best_of"] > 1:
raise ValueError("Cannot stream results when best_of > 1.")
raise ValueError(
"Cannot stream results when best_of > 1."
)
return values
@property
@ -310,7 +333,9 @@ class BaseOpenAI(BaseLLM):
**kwargs: Any,
) -> Iterator[GenerationChunk]:
params = {**self._invocation_params, **kwargs, "stream": True}
self.get_sub_prompts(params, [prompt], stop) # this mutates params
self.get_sub_prompts(
params, [prompt], stop
) # this mutates params
for stream_resp in completion_with_retry(
self, prompt=prompt, run_manager=run_manager, **params
):
@ -336,7 +361,9 @@ class BaseOpenAI(BaseLLM):
**kwargs: Any,
) -> AsyncIterator[GenerationChunk]:
params = {**self._invocation_params, **kwargs, "stream": True}
self.get_sub_prompts(params, [prompt], stop) # this mutate params
self.get_sub_prompts(
params, [prompt], stop
) # this mutate params
async for stream_resp in await acompletion_with_retry(
self, prompt=prompt, run_manager=run_manager, **params
):
@ -404,7 +431,9 @@ class BaseOpenAI(BaseLLM):
{
"text": generation.text,
"finish_reason": (
generation.generation_info.get("finish_reason")
generation.generation_info.get(
"finish_reason"
)
if generation.generation_info
else None
),
@ -417,7 +446,10 @@ class BaseOpenAI(BaseLLM):
)
else:
response = completion_with_retry(
self, prompt=_prompts, run_manager=run_manager, **params
self,
prompt=_prompts,
run_manager=run_manager,
**params,
)
choices.extend(response["choices"])
update_token_usage(_keys, response, token_usage)
@ -459,7 +491,9 @@ class BaseOpenAI(BaseLLM):
{
"text": generation.text,
"finish_reason": (
generation.generation_info.get("finish_reason")
generation.generation_info.get(
"finish_reason"
)
if generation.generation_info
else None
),
@ -472,7 +506,10 @@ class BaseOpenAI(BaseLLM):
)
else:
response = await acompletion_with_retry(
self, prompt=_prompts, run_manager=run_manager, **params
self,
prompt=_prompts,
run_manager=run_manager,
**params,
)
choices.extend(response["choices"])
update_token_usage(_keys, response, token_usage)
@ -488,15 +525,19 @@ class BaseOpenAI(BaseLLM):
if stop is not None:
if "stop" in params:
raise ValueError(
"`stop` found in both the input and default params."
"`stop` found in both the input and default"
" params."
)
params["stop"] = stop
if params["max_tokens"] == -1:
if len(prompts) != 1:
raise ValueError(
"max_tokens set to -1 not supported for multiple inputs."
"max_tokens set to -1 not supported for multiple"
" inputs."
)
params["max_tokens"] = self.max_tokens_for_prompt(prompts[0])
params["max_tokens"] = self.max_tokens_for_prompt(
prompts[0]
)
sub_prompts = [
prompts[i : i + self.batch_size]
for i in range(0, len(prompts), self.batch_size)
@ -504,7 +545,10 @@ class BaseOpenAI(BaseLLM):
return sub_prompts
def create_llm_result(
self, choices: Any, prompts: List[str], token_usage: Dict[str, int]
self,
choices: Any,
prompts: List[str],
token_usage: Dict[str, int],
) -> LLMResult:
"""Create the LLMResult from the choices and prompts."""
generations = []
@ -522,8 +566,13 @@ class BaseOpenAI(BaseLLM):
for choice in sub_choices
]
)
llm_output = {"token_usage": token_usage, "model_name": self.model_name}
return LLMResult(generations=generations, llm_output=llm_output)
llm_output = {
"token_usage": token_usage,
"model_name": self.model_name,
}
return LLMResult(
generations=generations, llm_output=llm_output
)
@property
def _invocation_params(self) -> Dict[str, Any]:
@ -542,7 +591,10 @@ class BaseOpenAI(BaseLLM):
@property
def _identifying_params(self) -> Mapping[str, Any]:
"""Get the identifying parameters."""
return {**{"model_name": self.model_name}, **self._default_params}
return {
**{"model_name": self.model_name},
**self._default_params,
}
@property
def _llm_type(self) -> str:
@ -558,9 +610,9 @@ class BaseOpenAI(BaseLLM):
import tiktoken
except ImportError:
raise ImportError(
"Could not import tiktoken python package. "
"This is needed in order to calculate get_num_tokens. "
"Please install it with `pip install tiktoken`."
"Could not import tiktoken python package. This is"
" needed in order to calculate get_num_tokens. Please"
" install it with `pip install tiktoken`."
)
model_name = self.tiktoken_model_name or self.model_name
@ -568,7 +620,8 @@ class BaseOpenAI(BaseLLM):
enc = tiktoken.encoding_for_model(model_name)
except KeyError:
logger.warning(
"Warning: model not found. Using cl100k_base encoding."
"Warning: model not found. Using cl100k_base"
" encoding."
)
model = "cl100k_base"
enc = tiktoken.get_encoding(model)
@ -630,8 +683,8 @@ class BaseOpenAI(BaseLLM):
if context_size is None:
raise ValueError(
f"Unknown model: {modelname}. Please provide a valid OpenAI"
" model name.Known models are: "
f"Unknown model: {modelname}. Please provide a valid"
" OpenAI model name.Known models are: "
+ ", ".join(model_token_mapping.keys())
)
@ -678,7 +731,10 @@ class OpenAI(BaseOpenAI):
@property
def _invocation_params(self) -> Dict[str, Any]:
return {**{"model": self.model_name}, **super()._invocation_params}
return {
**{"model": self.model_name},
**super()._invocation_params,
}
class AzureOpenAI(BaseOpenAI):
@ -802,7 +858,9 @@ class OpenAIChat(BaseLLM):
for field_name in list(values):
if field_name not in all_required_field_names:
if field_name in extra:
raise ValueError(f"Found {field_name} supplied twice.")
raise ValueError(
f"Found {field_name} supplied twice."
)
extra[field_name] = values.pop(field_name)
values["model_kwargs"] = extra
return values
@ -826,7 +884,10 @@ class OpenAIChat(BaseLLM):
default="",
)
openai_organization = get_from_dict_or_env(
values, "openai_organization", "OPENAI_ORGANIZATION", default=""
values,
"openai_organization",
"OPENAI_ORGANIZATION",
default="",
)
try:
import openai
@ -847,9 +908,10 @@ class OpenAIChat(BaseLLM):
values["client"] = openai.ChatCompletion
except AttributeError:
raise ValueError(
"`openai` has no `ChatCompletion` attribute, this is likely "
"due to an old version of the openai package. Try upgrading it "
"with `pip install --upgrade openai`."
"`openai` has no `ChatCompletion` attribute, this is"
" likely due to an old version of the openai package."
" Try upgrading it with `pip install --upgrade"
" openai`."
)
return values
@ -863,8 +925,8 @@ class OpenAIChat(BaseLLM):
) -> Tuple:
if len(prompts) > 1:
raise ValueError(
"OpenAIChat currently only supports single prompt, got"
f" {prompts}"
"OpenAIChat currently only supports single prompt,"
f" got {prompts}"
)
messages = self.prefix_messages + [
{"role": "user", "content": prompts[0]}
@ -876,7 +938,8 @@ class OpenAIChat(BaseLLM):
if stop is not None:
if "stop" in params:
raise ValueError(
"`stop` found in both the input and default params."
"`stop` found in both the input and default"
" params."
)
params["stop"] = stop
if params.get("max_tokens") == -1:
@ -896,7 +959,9 @@ class OpenAIChat(BaseLLM):
for stream_resp in completion_with_retry(
self, messages=messages, run_manager=run_manager, **params
):
token = stream_resp["choices"][0]["delta"].get("content", "")
token = stream_resp["choices"][0]["delta"].get(
"content", ""
)
chunk = GenerationChunk(text=token)
yield chunk
if run_manager:
@ -914,7 +979,9 @@ class OpenAIChat(BaseLLM):
async for stream_resp in await acompletion_with_retry(
self, messages=messages, run_manager=run_manager, **params
):
token = stream_resp["choices"][0]["delta"].get("content", "")
token = stream_resp["choices"][0]["delta"].get(
"content", ""
)
chunk = GenerationChunk(text=token)
yield chunk
if run_manager:
@ -929,7 +996,9 @@ class OpenAIChat(BaseLLM):
) -> LLMResult:
if self.streaming:
generation: Optional[GenerationChunk] = None
for chunk in self._stream(prompts[0], stop, run_manager, **kwargs):
for chunk in self._stream(
prompts[0], stop, run_manager, **kwargs
):
if generation is None:
generation = chunk
else:
@ -950,7 +1019,9 @@ class OpenAIChat(BaseLLM):
generations=[
[
Generation(
text=full_response["choices"][0]["message"]["content"]
text=full_response["choices"][0]["message"][
"content"
]
)
]
],
@ -989,7 +1060,9 @@ class OpenAIChat(BaseLLM):
generations=[
[
Generation(
text=full_response["choices"][0]["message"]["content"]
text=full_response["choices"][0]["message"][
"content"
]
)
]
],
@ -999,7 +1072,10 @@ class OpenAIChat(BaseLLM):
@property
def _identifying_params(self) -> Mapping[str, Any]:
"""Get the identifying parameters."""
return {**{"model_name": self.model_name}, **self._default_params}
return {
**{"model_name": self.model_name},
**self._default_params,
}
@property
def _llm_type(self) -> str:
@ -1015,9 +1091,9 @@ class OpenAIChat(BaseLLM):
import tiktoken
except ImportError:
raise ImportError(
"Could not import tiktoken python package. "
"This is needed in order to calculate get_num_tokens. "
"Please install it with `pip install tiktoken`."
"Could not import tiktoken python package. This is"
" needed in order to calculate get_num_tokens. Please"
" install it with `pip install tiktoken`."
)
enc = tiktoken.encoding_for_model(self.model_name)

@ -47,7 +47,9 @@ def _create_retry_decorator() -> Callable[[Any], Any]:
| retry_if_exception_type(
google.api_core.exceptions.ServiceUnavailable
)
| retry_if_exception_type(google.api_core.exceptions.GoogleAPIError)
| retry_if_exception_type(
google.api_core.exceptions.GoogleAPIError
)
),
before_sleep=before_sleep_log(logger, logging.WARNING),
)
@ -114,8 +116,9 @@ class GooglePalm(BaseLLM, BaseModel):
genai.configure(api_key=google_api_key)
except ImportError:
raise ImportError(
"Could not import google-generativeai python package. "
"Please install it with `pip install google-generativeai`."
"Could not import google-generativeai python package."
" Please install it with `pip install"
" google-generativeai`."
)
values["client"] = genai
@ -124,9 +127,14 @@ class GooglePalm(BaseLLM, BaseModel):
values["temperature"] is not None
and not 0 <= values["temperature"] <= 1
):
raise ValueError("temperature must be in the range [0.0, 1.0]")
raise ValueError(
"temperature must be in the range [0.0, 1.0]"
)
if values["top_p"] is not None and not 0 <= values["top_p"] <= 1:
if (
values["top_p"] is not None
and not 0 <= values["top_p"] <= 1
):
raise ValueError("top_p must be in the range [0.0, 1.0]")
if values["top_k"] is not None and values["top_k"] <= 0:
@ -136,7 +144,9 @@ class GooglePalm(BaseLLM, BaseModel):
values["max_output_tokens"] is not None
and values["max_output_tokens"] <= 0
):
raise ValueError("max_output_tokens must be greater than zero")
raise ValueError(
"max_output_tokens must be greater than zero"
)
return values
@ -165,8 +175,12 @@ class GooglePalm(BaseLLM, BaseModel):
prompt_generations = []
for candidate in completion.candidates:
raw_text = candidate["output"]
stripped_text = _strip_erroneous_leading_spaces(raw_text)
prompt_generations.append(Generation(text=stripped_text))
stripped_text = _strip_erroneous_leading_spaces(
raw_text
)
prompt_generations.append(
Generation(text=stripped_text)
)
generations.append(prompt_generations)
return LLMResult(generations=generations)

@ -34,16 +34,22 @@ class PegasusEmbedding:
"""
def __init__(
self, modality: str, multi_process: bool = False, n_processes: int = 4
self,
modality: str,
multi_process: bool = False,
n_processes: int = 4,
):
self.modality = modality
self.multi_process = multi_process
self.n_processes = n_processes
try:
self.pegasus = Pegasus(modality, multi_process, n_processes)
self.pegasus = Pegasus(
modality, multi_process, n_processes
)
except Exception as e:
logging.error(
f"Failed to initialize Pegasus with modality: {modality}: {e}"
"Failed to initialize Pegasus with modality:"
f" {modality}: {e}"
)
raise
@ -52,5 +58,7 @@ class PegasusEmbedding:
try:
return self.pegasus.embed(data)
except Exception as e:
logging.error(f"Failed to generate embeddings. Error: {e}")
logging.error(
f"Failed to generate embeddings. Error: {e}"
)
raise

@ -38,6 +38,8 @@ class Petals:
def __call__(self, prompt):
"""Generate text using the Petals API."""
params = self._default_params()
inputs = self.tokenizer(prompt, return_tensors="pt")["input_ids"]
inputs = self.tokenizer(prompt, return_tensors="pt")[
"input_ids"
]
outputs = self.model.generate(inputs, **params)
return self.tokenizer.decode(outputs[0])

@ -1,7 +1,12 @@
import cv2
import numpy as np
from PIL import Image
from transformers import SamImageProcessor, SamModel, SamProcessor, pipeline
from transformers import (
SamImageProcessor,
SamModel,
SamProcessor,
pipeline,
)
try:
import cv2
@ -44,16 +49,18 @@ def compute_mask_iou_vectorized(masks: np.ndarray) -> np.ndarray:
"""
if np.any(masks.sum(axis=(1, 2)) == 0):
raise ValueError(
"One or more masks are empty. Please filter out empty masks before"
" using `compute_iou_vectorized` function."
"One or more masks are empty. Please filter out empty"
" masks before using `compute_iou_vectorized` function."
)
masks_bool = masks.astype(bool)
masks_flat = masks_bool.reshape(masks.shape[0], -1)
intersection = np.logical_and(masks_flat[:, None], masks_flat[None, :]).sum(
axis=2
)
union = np.logical_or(masks_flat[:, None], masks_flat[None, :]).sum(axis=2)
intersection = np.logical_and(
masks_flat[:, None], masks_flat[None, :]
).sum(axis=2)
union = np.logical_or(
masks_flat[:, None], masks_flat[None, :]
).sum(axis=2)
iou_matrix = intersection / union
return iou_matrix
@ -96,7 +103,9 @@ def mask_non_max_suppression(
def filter_masks_by_relative_area(
masks: np.ndarray, minimum_area: float = 0.01, maximum_area: float = 1.0
masks: np.ndarray,
minimum_area: float = 0.01,
maximum_area: float = 1.0,
) -> np.ndarray:
"""
Filters masks based on their relative area within the total area of each mask.
@ -123,18 +132,21 @@ def filter_masks_by_relative_area(
if not (0 <= minimum_area <= 1) or not (0 <= maximum_area <= 1):
raise ValueError(
"`minimum_area` and `maximum_area` must be between 0 and 1."
"`minimum_area` and `maximum_area` must be between 0"
" and 1."
)
if minimum_area > maximum_area:
raise ValueError(
"`minimum_area` must be less than or equal to `maximum_area`."
"`minimum_area` must be less than or equal to"
" `maximum_area`."
)
total_area = masks.shape[1] * masks.shape[2]
relative_areas = masks.sum(axis=(1, 2)) / total_area
return masks[
(relative_areas >= minimum_area) & (relative_areas <= maximum_area)
(relative_areas >= minimum_area)
& (relative_areas <= maximum_area)
]
@ -170,7 +182,9 @@ def adjust_mask_features_by_relative_area(
if feature_type == FeatureType.ISLAND
else cv2.RETR_CCOMP
)
contours, _ = cv2.findContours(mask, operation, cv2.CHAIN_APPROX_SIMPLE)
contours, _ = cv2.findContours(
mask, operation, cv2.CHAIN_APPROX_SIMPLE
)
for contour in contours:
area = cv2.contourArea(contour)
@ -180,7 +194,9 @@ def adjust_mask_features_by_relative_area(
image=mask,
contours=[contour],
contourIdx=-1,
color=(0 if feature_type == FeatureType.ISLAND else 255),
color=(
0 if feature_type == FeatureType.ISLAND else 255
),
thickness=-1,
)
return np.where(mask > 0, 1, 0).astype(bool)
@ -198,7 +214,9 @@ def masks_to_marks(masks: np.ndarray) -> sv.Detections:
sv.Detections: An object containing the masks and their bounding box
coordinates.
"""
return sv.Detections(mask=masks, xyxy=sv.mask_to_xyxy(masks=masks))
return sv.Detections(
mask=masks, xyxy=sv.mask_to_xyxy(masks=masks)
)
def refine_marks(
@ -262,11 +280,15 @@ class SegmentAnythingMarkGenerator:
"""
def __init__(
self, device: str = "cpu", model_name: str = "facebook/sam-vit-huge"
self,
device: str = "cpu",
model_name: str = "facebook/sam-vit-huge",
):
self.model = SamModel.from_pretrained(model_name).to(device)
self.processor = SamProcessor.from_pretrained(model_name)
self.image_processor = SamImageProcessor.from_pretrained(model_name)
self.image_processor = SamImageProcessor.from_pretrained(
model_name
)
self.pipeline = pipeline(
task="mask-generation",
model=self.model,
@ -285,7 +307,9 @@ class SegmentAnythingMarkGenerator:
sv.Detections: An object containing the segmentation masks and their
corresponding bounding box coordinates.
"""
image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
image = Image.fromarray(
cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
)
outputs = self.pipeline(image, points_per_batch=64)
masks = np.array(outputs["masks"])
return masks_to_marks(masks=masks)

@ -4,7 +4,9 @@ from openai import OpenAI
client = OpenAI()
def get_ada_embeddings(text: str, model: str = "text-embedding-ada-002"):
def get_ada_embeddings(
text: str, model: str = "text-embedding-ada-002"
):
"""
Simple function to get embeddings from ada
@ -16,6 +18,6 @@ def get_ada_embeddings(text: str, model: str = "text-embedding-ada-002"):
text = text.replace("\n", " ")
return client.embeddings.create(input=[text], model=model)["data"][0][
"embedding"
]
return client.embeddings.create(input=[text], model=model)[
"data"
][0]["embedding"]

@ -87,9 +87,15 @@ class SpeechT5:
self.model_name = model_name
self.vocoder_name = vocoder_name
self.dataset_name = dataset_name
self.processor = SpeechT5Processor.from_pretrained(self.model_name)
self.model = SpeechT5ForTextToSpeech.from_pretrained(self.model_name)
self.vocoder = SpeechT5HifiGan.from_pretrained(self.vocoder_name)
self.processor = SpeechT5Processor.from_pretrained(
self.model_name
)
self.model = SpeechT5ForTextToSpeech.from_pretrained(
self.model_name
)
self.vocoder = SpeechT5HifiGan.from_pretrained(
self.vocoder_name
)
self.embeddings_dataset = load_dataset(
self.dataset_name, split="validation"
)
@ -101,7 +107,9 @@ class SpeechT5:
).unsqueeze(0)
inputs = self.processor(text=text, return_tensors="pt")
speech = self.model.generate_speech(
inputs["input_ids"], speaker_embedding, vocoder=self.vocoder
inputs["input_ids"],
speaker_embedding,
vocoder=self.vocoder,
)
return speech
@ -112,13 +120,19 @@ class SpeechT5:
def set_model(self, model_name: str):
"""Set the model to a new model."""
self.model_name = model_name
self.processor = SpeechT5Processor.from_pretrained(self.model_name)
self.model = SpeechT5ForTextToSpeech.from_pretrained(self.model_name)
self.processor = SpeechT5Processor.from_pretrained(
self.model_name
)
self.model = SpeechT5ForTextToSpeech.from_pretrained(
self.model_name
)
def set_vocoder(self, vocoder_name):
"""Set the vocoder to a new vocoder."""
self.vocoder_name = vocoder_name
self.vocoder = SpeechT5HifiGan.from_pretrained(self.vocoder_name)
self.vocoder = SpeechT5HifiGan.from_pretrained(
self.vocoder_name
)
def set_embeddings_dataset(self, dataset_name):
"""Set the embeddings dataset to a new dataset."""
@ -148,7 +162,9 @@ class SpeechT5:
# Feature 4: Change dataset split (train, validation, test)
def change_dataset_split(self, split="train"):
"""Change dataset split (train, validation, test)."""
self.embeddings_dataset = load_dataset(self.dataset_name, split=split)
self.embeddings_dataset = load_dataset(
self.dataset_name, split=split
)
# Feature 5: Load a custom speaker embedding (xvector) for the text
def load_custom_embedding(self, xvector):

@ -96,7 +96,9 @@ class SSD1B:
byte_array = byte_stream.getvalue()
return byte_array
@backoff.on_exception(backoff.expo, Exception, max_time=max_time_seconds)
@backoff.on_exception(
backoff.expo, Exception, max_time=max_time_seconds
)
def __call__(self, task: str, neg_prompt: str):
"""
Text to image conversion using the SSD1B API
@ -124,7 +126,9 @@ class SSD1B:
if task in self.cache:
return self.cache[task]
try:
img = self.pipe(prompt=task, neg_prompt=neg_prompt).images[0]
img = self.pipe(
prompt=task, neg_prompt=neg_prompt
).images[0]
# Generate a unique filename for the image
img_name = f"{uuid.uuid4()}.{self.image_format}"
@ -141,8 +145,8 @@ class SSD1B:
print(
colored(
(
f"Error running SSD1B: {error} try optimizing your api"
" key and or try again"
f"Error running SSD1B: {error} try optimizing"
" your api key and or try again"
),
"red",
)
@ -218,7 +222,9 @@ class SSD1B:
executor.submit(self, task): task for task in tasks
}
results = []
for future in concurrent.futures.as_completed(future_to_task):
for future in concurrent.futures.as_completed(
future_to_task
):
task = future_to_task[future]
try:
img = future.result()
@ -229,18 +235,28 @@ class SSD1B:
print(
colored(
(
f"Error running SSD1B: {error} try optimizing"
" your api key and or try again"
f"Error running SSD1B: {error} try"
" optimizing your api key and or try"
" again"
),
"red",
)
)
print(
colored(
f"Error running SSD1B: {error.http_status}", "red"
(
"Error running SSD1B:"
f" {error.http_status}"
),
"red",
)
)
print(
colored(
f"Error running SSD1B: {error.error}",
"red",
)
)
print(colored(f"Error running SSD1B: {error.error}", "red"))
raise error
def _generate_uuid(self):
@ -255,7 +271,9 @@ class SSD1B:
"""Str method for the SSD1B class"""
return f"SSD1B(image_url={self.image_url})"
@backoff.on_exception(backoff.expo, Exception, max_tries=max_retries)
@backoff.on_exception(
backoff.expo, Exception, max_tries=max_retries
)
def rate_limited_call(self, task: str):
"""Rate limited call to the SSD1B API"""
return self.__call__(task)

@ -34,7 +34,9 @@ class TimmModel:
"""Retrieve the list of supported models from timm."""
return timm.list_models()
def _create_model(self, model_info: TimmModelInfo) -> torch.nn.Module:
def _create_model(
self, model_info: TimmModelInfo
) -> torch.nn.Module:
"""
Create a model instance from timm with specified parameters.

@ -93,7 +93,9 @@ class WhisperX:
try:
segments = result["segments"]
transcription = " ".join(segment["text"] for segment in segments)
transcription = " ".join(
segment["text"] for segment in segments
)
return transcription
except KeyError:
print("The key 'segments' is not found in the result.")
@ -128,7 +130,9 @@ class WhisperX:
try:
segments = result["segments"]
transcription = " ".join(segment["text"] for segment in segments)
transcription = " ".join(
segment["text"] for segment in segments
)
return transcription
except KeyError:
print("The key 'segments' is not found in the result.")

@ -2,7 +2,11 @@ import logging
import torch
from torch.nn.parallel import DistributedDataParallel as DDP
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
)
class WizardLLMStoryTeller:
@ -74,21 +78,27 @@ class WizardLLMStoryTeller:
bnb_config = BitsAndBytesConfig(**quantization_config)
try:
self.tokenizer = AutoTokenizer.from_pretrained(self.model_id)
self.tokenizer = AutoTokenizer.from_pretrained(
self.model_id
)
self.model = AutoModelForCausalLM.from_pretrained(
self.model_id, quantization_config=bnb_config
)
self.model # .to(self.device)
except Exception as e:
self.logger.error(f"Failed to load the model or the tokenizer: {e}")
self.logger.error(
f"Failed to load the model or the tokenizer: {e}"
)
raise
def load_model(self):
"""Load the model"""
if not self.model or not self.tokenizer:
try:
self.tokenizer = AutoTokenizer.from_pretrained(self.model_id)
self.tokenizer = AutoTokenizer.from_pretrained(
self.model_id
)
bnb_config = (
BitsAndBytesConfig(**self.quantization_config)
@ -104,7 +114,8 @@ class WizardLLMStoryTeller:
self.model = DDP(self.model)
except Exception as error:
self.logger.error(
f"Failed to load the model or the tokenizer: {error}"
"Failed to load the model or the tokenizer:"
f" {error}"
)
raise
@ -124,9 +135,9 @@ class WizardLLMStoryTeller:
max_length = self.max_length
try:
inputs = self.tokenizer.encode(prompt_text, return_tensors="pt").to(
self.device
)
inputs = self.tokenizer.encode(
prompt_text, return_tensors="pt"
).to(self.device)
# self.log.start()
@ -136,7 +147,9 @@ class WizardLLMStoryTeller:
output_sequence = []
outputs = self.model.generate(
inputs, max_length=len(inputs) + 1, do_sample=True
inputs,
max_length=len(inputs) + 1,
do_sample=True,
)
output_tokens = outputs[0][-1]
output_sequence.append(output_tokens.item())
@ -144,7 +157,8 @@ class WizardLLMStoryTeller:
# print token in real-time
print(
self.tokenizer.decode(
[output_tokens], skip_special_tokens=True
[output_tokens],
skip_special_tokens=True,
),
end="",
flush=True,
@ -157,7 +171,9 @@ class WizardLLMStoryTeller:
)
del inputs
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
return self.tokenizer.decode(
outputs[0], skip_special_tokens=True
)
except Exception as e:
self.logger.error(f"Failed to generate the text: {e}")
raise
@ -178,9 +194,9 @@ class WizardLLMStoryTeller:
max_length = self.max_
try:
inputs = self.tokenizer.encode(prompt_text, return_tensors="pt").to(
self.device
)
inputs = self.tokenizer.encode(
prompt_text, return_tensors="pt"
).to(self.device)
# self.log.start()
@ -190,7 +206,9 @@ class WizardLLMStoryTeller:
output_sequence = []
outputs = self.model.generate(
inputs, max_length=len(inputs) + 1, do_sample=True
inputs,
max_length=len(inputs) + 1,
do_sample=True,
)
output_tokens = outputs[0][-1]
output_sequence.append(output_tokens.item())
@ -198,7 +216,8 @@ class WizardLLMStoryTeller:
# print token in real-time
print(
self.tokenizer.decode(
[output_tokens], skip_special_tokens=True
[output_tokens],
skip_special_tokens=True,
),
end="",
flush=True,
@ -212,7 +231,9 @@ class WizardLLMStoryTeller:
del inputs
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
return self.tokenizer.decode(
outputs[0], skip_special_tokens=True
)
except Exception as e:
self.logger.error(f"Failed to generate the text: {e}")
raise

@ -2,7 +2,11 @@ import logging
import torch
from torch.nn.parallel import DistributedDataParallel as DDP
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
)
class YarnMistral128:
@ -74,7 +78,9 @@ class YarnMistral128:
bnb_config = BitsAndBytesConfig(**quantization_config)
try:
self.tokenizer = AutoTokenizer.from_pretrained(self.model_id)
self.tokenizer = AutoTokenizer.from_pretrained(
self.model_id
)
self.model = AutoModelForCausalLM.from_pretrained(
self.model_id,
quantization_config=bnb_config,
@ -86,14 +92,18 @@ class YarnMistral128:
self.model # .to(self.device)
except Exception as e:
self.logger.error(f"Failed to load the model or the tokenizer: {e}")
self.logger.error(
f"Failed to load the model or the tokenizer: {e}"
)
raise
def load_model(self):
"""Load the model"""
if not self.model or not self.tokenizer:
try:
self.tokenizer = AutoTokenizer.from_pretrained(self.model_id)
self.tokenizer = AutoTokenizer.from_pretrained(
self.model_id
)
bnb_config = (
BitsAndBytesConfig(**self.quantization_config)
@ -109,7 +119,8 @@ class YarnMistral128:
self.model = DDP(self.model)
except Exception as error:
self.logger.error(
f"Failed to load the model or the tokenizer: {error}"
"Failed to load the model or the tokenizer:"
f" {error}"
)
raise
@ -129,9 +140,9 @@ class YarnMistral128:
max_length = self.max_length
try:
inputs = self.tokenizer.encode(prompt_text, return_tensors="pt").to(
self.device
)
inputs = self.tokenizer.encode(
prompt_text, return_tensors="pt"
).to(self.device)
# self.log.start()
@ -141,7 +152,9 @@ class YarnMistral128:
output_sequence = []
outputs = self.model.generate(
inputs, max_length=len(inputs) + 1, do_sample=True
inputs,
max_length=len(inputs) + 1,
do_sample=True,
)
output_tokens = outputs[0][-1]
output_sequence.append(output_tokens.item())
@ -149,7 +162,8 @@ class YarnMistral128:
# print token in real-time
print(
self.tokenizer.decode(
[output_tokens], skip_special_tokens=True
[output_tokens],
skip_special_tokens=True,
),
end="",
flush=True,
@ -162,7 +176,9 @@ class YarnMistral128:
)
del inputs
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
return self.tokenizer.decode(
outputs[0], skip_special_tokens=True
)
except Exception as e:
self.logger.error(f"Failed to generate the text: {e}")
raise
@ -206,9 +222,9 @@ class YarnMistral128:
max_length = self.max_
try:
inputs = self.tokenizer.encode(prompt_text, return_tensors="pt").to(
self.device
)
inputs = self.tokenizer.encode(
prompt_text, return_tensors="pt"
).to(self.device)
# self.log.start()
@ -218,7 +234,9 @@ class YarnMistral128:
output_sequence = []
outputs = self.model.generate(
inputs, max_length=len(inputs) + 1, do_sample=True
inputs,
max_length=len(inputs) + 1,
do_sample=True,
)
output_tokens = outputs[0][-1]
output_sequence.append(output_tokens.item())
@ -226,7 +244,8 @@ class YarnMistral128:
# print token in real-time
print(
self.tokenizer.decode(
[output_tokens], skip_special_tokens=True
[output_tokens],
skip_special_tokens=True,
),
end="",
flush=True,
@ -240,7 +259,9 @@ class YarnMistral128:
del inputs
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
return self.tokenizer.decode(
outputs[0], skip_special_tokens=True
)
except Exception as e:
self.logger.error(f"Failed to generate the text: {e}")
raise

@ -87,7 +87,9 @@ class Yi34B200k:
top_k=self.top_k,
top_p=self.top_p,
)
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
return self.tokenizer.decode(
outputs[0], skip_special_tokens=True
)
# # Example usage

@ -68,7 +68,9 @@ class Zephyr:
tokenize=self.tokenize,
add_generation_prompt=self.add_generation_prompt,
)
outputs = self.pipe(prompt) # max_new_token=self.max_new_tokens)
outputs = self.pipe(
prompt
) # max_new_token=self.max_new_tokens)
print(outputs[0]["generated_text"])
def chat(self, message: str):

@ -2,7 +2,9 @@ from swarms.prompts.code_interpreter import CODE_INTERPRETER
from swarms.prompts.finance_agent_prompt import FINANCE_AGENT_PROMPT
from swarms.prompts.growth_agent_prompt import GROWTH_AGENT_PROMPT
from swarms.prompts.legal_agent_prompt import LEGAL_AGENT_PROMPT
from swarms.prompts.operations_agent_prompt import OPERATIONS_AGENT_PROMPT
from swarms.prompts.operations_agent_prompt import (
OPERATIONS_AGENT_PROMPT,
)
from swarms.prompts.product_agent_prompt import PRODUCT_AGENT_PROMPT

@ -25,7 +25,9 @@ class AgentOutputParser(BaseAgentOutputParser):
@staticmethod
def _preprocess_json_input(input_str: str) -> str:
corrected_str = re.sub(
r'(?<!\\)\\(?!["\\/bfnrt]|u[0-9a-fA-F]{4})', r"\\\\", input_str
r'(?<!\\)\\(?!["\\/bfnrt]|u[0-9a-fA-F]{4})',
r"\\\\",
input_str,
)
return corrected_str

@ -16,12 +16,16 @@ class PromptGenerator:
"text": "thought",
"reasoning": "reasoning",
"plan": (
"- short bulleted\n- list that conveys\n- long-term plan"
"- short bulleted\n- list that conveys\n-"
" long-term plan"
),
"criticism": "constructive self-criticism",
"speak": "thoughts summary to say to user",
},
"command": {"name": "command name", "args": {"arg name": "value"}},
"command": {
"name": "command name",
"args": {"arg name": "value"},
},
}
def add_constraint(self, constraint: str) -> None:
@ -66,13 +70,16 @@ class PromptGenerator:
Returns:
str: The generated prompt string.
"""
formatted_response_format = json.dumps(self.response_format, indent=4)
formatted_response_format = json.dumps(
self.response_format, indent=4
)
prompt_string = (
f"Constraints:\n{''.join(self.constraints)}\n\nCommands:\n{''.join(self.commands)}\n\nResources:\n{''.join(self.resources)}\n\nPerformance"
f" Evaluation:\n{''.join(self.performance_evaluation)}\n\nYou"
" should only respond in JSON format as described below \nResponse"
f" Format: \n{formatted_response_format} \nEnsure the response can"
" be parsed by Python json.loads"
" should only respond in JSON format as described below"
" \nResponse Format:"
f" \n{formatted_response_format} \nEnsure the response"
" can be parsed by Python json.loads"
)
return prompt_string

@ -5,26 +5,30 @@ def generate_agent_role_prompt(agent):
"""
prompts = {
"Finance Agent": (
"You are a seasoned finance analyst AI assistant. Your primary goal"
" is to compose comprehensive, astute, impartial, and methodically"
" arranged financial reports based on provided data and trends."
"You are a seasoned finance analyst AI assistant. Your"
" primary goal is to compose comprehensive, astute,"
" impartial, and methodically arranged financial reports"
" based on provided data and trends."
),
"Travel Agent": (
"You are a world-travelled AI tour guide assistant. Your main"
" purpose is to draft engaging, insightful, unbiased, and"
" well-structured travel reports on given locations, including"
" history, attractions, and cultural insights."
"You are a world-travelled AI tour guide assistant. Your"
" main purpose is to draft engaging, insightful,"
" unbiased, and well-structured travel reports on given"
" locations, including history, attractions, and cultural"
" insights."
),
"Academic Research Agent": (
"You are an AI academic research assistant. Your primary"
" responsibility is to create thorough, academically rigorous,"
" unbiased, and systematically organized reports on a given"
" research topic, following the standards of scholarly work."
" responsibility is to create thorough, academically"
" rigorous, unbiased, and systematically organized"
" reports on a given research topic, following the"
" standards of scholarly work."
),
"Default Agent": (
"You are an AI critical thinker research assistant. Your sole"
" purpose is to write well written, critically acclaimed, objective"
" and structured reports on given text."
"You are an AI critical thinker research assistant. Your"
" sole purpose is to write well written, critically"
" acclaimed, objective and structured reports on given"
" text."
),
}
@ -39,12 +43,14 @@ def generate_report_prompt(question, research_summary):
"""
return (
f'"""{research_summary}""" Using the above information, answer the'
f' following question or topic: "{question}" in a detailed report --'
" The report should focus on the answer to the question, should be"
" well structured, informative, in depth, with facts and numbers if"
" available, a minimum of 1,200 words and with markdown syntax and apa"
" format. Write all source urls at the end of the report in apa format"
f'"""{research_summary}""" Using the above information,'
f' answer the following question or topic: "{question}" in a'
" detailed report -- The report should focus on the answer"
" to the question, should be well structured, informative,"
" in depth, with facts and numbers if available, a minimum"
" of 1,200 words and with markdown syntax and apa format."
" Write all source urls at the end of the report in apa"
" format"
)
@ -55,10 +61,10 @@ def generate_search_queries_prompt(question):
"""
return (
"Write 4 google search queries to search online that form an objective"
f' opinion from the following: "{question}"You must respond with a list'
' of strings in the following format: ["query 1", "query 2", "query'
' 3", "query 4"]'
"Write 4 google search queries to search online that form an"
f' objective opinion from the following: "{question}"You must'
" respond with a list of strings in the following format:"
' ["query 1", "query 2", "query 3", "query 4"]'
)
@ -73,16 +79,17 @@ def generate_resource_report_prompt(question, research_summary):
str: The resource report prompt for the given question and research summary.
"""
return (
f'"""{research_summary}""" Based on the above information, generate a'
" bibliography recommendation report for the following question or"
f' topic: "{question}". The report should provide a detailed analysis'
" of each recommended resource, explaining how each source can"
" contribute to finding answers to the research question. Focus on the"
" relevance, reliability, and significance of each source. Ensure that"
" the report is well-structured, informative, in-depth, and follows"
" Markdown syntax. Include relevant facts, figures, and numbers"
" whenever available. The report should have a minimum length of 1,200"
" words."
f'"""{research_summary}""" Based on the above information,'
" generate a bibliography recommendation report for the"
f' following question or topic: "{question}". The report'
" should provide a detailed analysis of each recommended"
" resource, explaining how each source can contribute to"
" finding answers to the research question. Focus on the"
" relevance, reliability, and significance of each source."
" Ensure that the report is well-structured, informative,"
" in-depth, and follows Markdown syntax. Include relevant"
" facts, figures, and numbers whenever available. The report"
" should have a minimum length of 1,200 words."
)
@ -94,14 +101,15 @@ def generate_outline_report_prompt(question, research_summary):
"""
return (
f'"""{research_summary}""" Using the above information, generate an'
" outline for a research report in Markdown syntax for the following"
f' question or topic: "{question}". The outline should provide a'
" well-structured framework for the research report, including the"
" main sections, subsections, and key points to be covered. The"
" research report should be detailed, informative, in-depth, and a"
" minimum of 1,200 words. Use appropriate Markdown syntax to format"
" the outline and ensure readability."
f'"""{research_summary}""" Using the above information,'
" generate an outline for a research report in Markdown"
f' syntax for the following question or topic: "{question}".'
" The outline should provide a well-structured framework for"
" the research report, including the main sections,"
" subsections, and key points to be covered. The research"
" report should be detailed, informative, in-depth, and a"
" minimum of 1,200 words. Use appropriate Markdown syntax to"
" format the outline and ensure readability."
)
@ -113,11 +121,12 @@ def generate_concepts_prompt(question, research_summary):
"""
return (
f'"""{research_summary}""" Using the above information, generate a list'
" of 5 main concepts to learn for a research report on the following"
f' question or topic: "{question}". The outline should provide a'
" well-structured frameworkYou must respond with a list of strings in"
' the following format: ["concepts 1", "concepts 2", "concepts 3",'
f'"""{research_summary}""" Using the above information,'
" generate a list of 5 main concepts to learn for a research"
f' report on the following question or topic: "{question}".'
" The outline should provide a well-structured frameworkYou"
" must respond with a list of strings in the following"
' format: ["concepts 1", "concepts 2", "concepts 3",'
' "concepts 4, concepts 5"]'
)
@ -132,10 +141,11 @@ def generate_lesson_prompt(concept):
"""
prompt = (
f"generate a comprehensive lesson about {concept} in Markdown syntax."
f" This should include the definitionof {concept}, its historical"
" background and development, its applications or uses in"
f" differentfields, and notable events or facts related to {concept}."
f"generate a comprehensive lesson about {concept} in Markdown"
f" syntax. This should include the definitionof {concept},"
" its historical background and development, its"
" applications or uses in differentfields, and notable"
f" events or facts related to {concept}."
)
return prompt

@ -53,7 +53,10 @@ def get_buffer_string(
else:
raise ValueError(f"Got unsupported message type: {m}")
message = f"{role}: {m.content}"
if isinstance(m, AIMessage) and "function_call" in m.additional_kwargs:
if (
isinstance(m, AIMessage)
and "function_call" in m.additional_kwargs
):
message += f"{m.additional_kwargs['function_call']}"
string_messages.append(message)
@ -100,8 +103,8 @@ class BaseMessageChunk(BaseMessage):
merged[k] = v
elif not isinstance(merged[k], type(v)):
raise ValueError(
f'additional_kwargs["{k}"] already exists in this message,'
" but with a different type."
f'additional_kwargs["{k}"] already exists in this'
" message, but with a different type."
)
elif isinstance(merged[k], str):
merged[k] += v
@ -109,7 +112,8 @@ class BaseMessageChunk(BaseMessage):
merged[k] = self._merge_kwargs_dict(merged[k], v)
else:
raise ValueError(
f"Additional kwargs key {k} already exists in this message."
f"Additional kwargs key {k} already exists in"
" this message."
)
return merged

@ -10,10 +10,14 @@ class Message:
Messages are the inputs and outputs of ChatModels.
"""
def __init__(self, content: str, role: str, additional_kwargs: Dict = None):
def __init__(
self, content: str, role: str, additional_kwargs: Dict = None
):
self.content = content
self.role = role
self.additional_kwargs = additional_kwargs if additional_kwargs else {}
self.additional_kwargs = (
additional_kwargs if additional_kwargs else {}
)
@abstractmethod
def get_type(self) -> str:
@ -65,7 +69,10 @@ class SystemMessage(Message):
"""
def __init__(
self, content: str, role: str = "System", additional_kwargs: Dict = None
self,
content: str,
role: str = "System",
additional_kwargs: Dict = None,
):
super().__init__(content, role, additional_kwargs)
@ -97,7 +104,9 @@ class ChatMessage(Message):
A Message that can be assigned an arbitrary speaker (i.e. role).
"""
def __init__(self, content: str, role: str, additional_kwargs: Dict = None):
def __init__(
self, content: str, role: str, additional_kwargs: Dict = None
):
super().__init__(content, role, additional_kwargs)
def get_type(self) -> str:
@ -112,7 +121,10 @@ def get_buffer_string(
string_messages = []
for m in messages:
message = f"{m.role}: {m.content}"
if isinstance(m, AIMessage) and "function_call" in m.additional_kwargs:
if (
isinstance(m, AIMessage)
and "function_call" in m.additional_kwargs
):
message += f"{m.additional_kwargs['function_call']}"
string_messages.append(message)

@ -1,6 +1,6 @@
ERROR_PROMPT = (
"An error has occurred for the following text: \n{promptedQuery} Please"
" explain this error.\n {e}"
"An error has occurred for the following text: \n{promptedQuery}"
" Please explain this error.\n {e}"
)
IMAGE_PROMPT = """

@ -1,17 +1,19 @@
PY_SIMPLE_COMPLETION_INSTRUCTION = "# Write the body of this function only."
PY_SIMPLE_COMPLETION_INSTRUCTION = (
"# Write the body of this function only."
)
PY_REFLEXION_COMPLETION_INSTRUCTION = (
"You are a Python writing assistant. You will be given your past function"
" implementation, a series of unit tests, and a hint to change the"
" implementation appropriately. Write your full implementation (restate the"
" function signature).\n\n-----"
"You are a Python writing assistant. You will be given your past"
" function implementation, a series of unit tests, and a hint to"
" change the implementation appropriately. Write your full"
" implementation (restate the function signature).\n\n-----"
)
PY_SELF_REFLECTION_COMPLETION_INSTRUCTION = (
"You are a Python writing assistant. You will be given a function"
" implementation and a series of unit tests. Your goal is to write a few"
" sentences to explain why your implementation is wrong as indicated by the"
" tests. You will need this as a hint when you try again later. Only"
" provide the few sentence description in your answer, not the"
" implementation.\n\n-----"
" implementation and a series of unit tests. Your goal is to"
" write a few sentences to explain why your implementation is"
" wrong as indicated by the tests. You will need this as a hint"
" when you try again later. Only provide the few sentence"
" description in your answer, not the implementation.\n\n-----"
)
USE_PYTHON_CODEBLOCK_INSTRUCTION = (
"Use a Python code block to write your response. For"
@ -19,26 +21,28 @@ USE_PYTHON_CODEBLOCK_INSTRUCTION = (
)
PY_SIMPLE_CHAT_INSTRUCTION = (
"You are an AI that only responds with python code, NOT ENGLISH. You will"
" be given a function signature and its docstring by the user. Write your"
" full implementation (restate the function signature)."
"You are an AI that only responds with python code, NOT ENGLISH."
" You will be given a function signature and its docstring by the"
" user. Write your full implementation (restate the function"
" signature)."
)
PY_SIMPLE_CHAT_INSTRUCTION_V2 = (
"You are an AI that only responds with only python code. You will be given"
" a function signature and its docstring by the user. Write your full"
" implementation (restate the function signature)."
"You are an AI that only responds with only python code. You will"
" be given a function signature and its docstring by the user."
" Write your full implementation (restate the function"
" signature)."
)
PY_REFLEXION_CHAT_INSTRUCTION = (
"You are an AI Python assistant. You will be given your past function"
" implementation, a series of unit tests, and a hint to change the"
" implementation appropriately. Write your full implementation (restate the"
" function signature)."
"You are an AI Python assistant. You will be given your past"
" function implementation, a series of unit tests, and a hint to"
" change the implementation appropriately. Write your full"
" implementation (restate the function signature)."
)
PY_REFLEXION_CHAT_INSTRUCTION_V2 = (
"You are an AI Python assistant. You will be given your previous"
" implementation of a function, a series of unit tests results, and your"
" self-reflection on your previous implementation. Write your full"
" implementation (restate the function signature)."
" implementation of a function, a series of unit tests results,"
" and your self-reflection on your previous implementation. Write"
" your full implementation (restate the function signature)."
)
PY_REFLEXION_FEW_SHOT_ADD = '''Example 1:
[previous impl]:
@ -173,19 +177,20 @@ END EXAMPLES
'''
PY_SELF_REFLECTION_CHAT_INSTRUCTION = (
"You are a Python programming assistant. You will be given a function"
" implementation and a series of unit tests. Your goal is to write a few"
" sentences to explain why your implementation is wrong as indicated by the"
" tests. You will need this as a hint when you try again later. Only"
" provide the few sentence description in your answer, not the"
" implementation."
"You are a Python programming assistant. You will be given a"
" function implementation and a series of unit tests. Your goal"
" is to write a few sentences to explain why your implementation"
" is wrong as indicated by the tests. You will need this as a"
" hint when you try again later. Only provide the few sentence"
" description in your answer, not the implementation."
)
PY_SELF_REFLECTION_CHAT_INSTRUCTION_V2 = (
"You are a Python programming assistant. You will be given a function"
" implementation and a series of unit test results. Your goal is to write a"
" few sentences to explain why your implementation is wrong as indicated by"
" the tests. You will need this as guidance when you try again later. Only"
" provide the few sentence description in your answer, not the"
"You are a Python programming assistant. You will be given a"
" function implementation and a series of unit test results. Your"
" goal is to write a few sentences to explain why your"
" implementation is wrong as indicated by the tests. You will"
" need this as guidance when you try again later. Only provide"
" the few sentence description in your answer, not the"
" implementation. You will be given a few examples by the user."
)
PY_SELF_REFLECTION_FEW_SHOT = """Example 1:

@ -1,40 +1,43 @@
conversation_stages = {
"1": (
"Introduction: Start the conversation by introducing yourself and your"
" company. Be polite and respectful while keeping the tone of the"
" conversation professional. Your greeting should be welcoming. Always"
" clarify in your greeting the reason why you are contacting the"
" prospect."
"Introduction: Start the conversation by introducing yourself"
" and your company. Be polite and respectful while keeping"
" the tone of the conversation professional. Your greeting"
" should be welcoming. Always clarify in your greeting the"
" reason why you are contacting the prospect."
),
"2": (
"Qualification: Qualify the prospect by confirming if they are the"
" right person to talk to regarding your product/service. Ensure that"
" they have the authority to make purchasing decisions."
"Qualification: Qualify the prospect by confirming if they"
" are the right person to talk to regarding your"
" product/service. Ensure that they have the authority to"
" make purchasing decisions."
),
"3": (
"Value proposition: Briefly explain how your product/service can"
" benefit the prospect. Focus on the unique selling points and value"
" proposition of your product/service that sets it apart from"
" competitors."
"Value proposition: Briefly explain how your product/service"
" can benefit the prospect. Focus on the unique selling"
" points and value proposition of your product/service that"
" sets it apart from competitors."
),
"4": (
"Needs analysis: Ask open-ended questions to uncover the prospect's"
" needs and pain points. Listen carefully to their responses and take"
" notes."
"Needs analysis: Ask open-ended questions to uncover the"
" prospect's needs and pain points. Listen carefully to their"
" responses and take notes."
),
"5": (
"Solution presentation: Based on the prospect's needs, present your"
" product/service as the solution that can address their pain points."
"Solution presentation: Based on the prospect's needs,"
" present your product/service as the solution that can"
" address their pain points."
),
"6": (
"Objection handling: Address any objections that the prospect may have"
" regarding your product/service. Be prepared to provide evidence or"
" testimonials to support your claims."
"Objection handling: Address any objections that the prospect"
" may have regarding your product/service. Be prepared to"
" provide evidence or testimonials to support your claims."
),
"7": (
"Close: Ask for the sale by proposing a next step. This could be a"
" demo, a trial or a meeting with decision-makers. Ensure to summarize"
" what has been discussed and reiterate the benefits."
"Close: Ask for the sale by proposing a next step. This could"
" be a demo, a trial or a meeting with decision-makers."
" Ensure to summarize what has been discussed and reiterate"
" the benefits."
),
}

@ -46,40 +46,43 @@ Conversation history:
conversation_stages = {
"1": (
"Introduction: Start the conversation by introducing yourself and your"
" company. Be polite and respectful while keeping the tone of the"
" conversation professional. Your greeting should be welcoming. Always"
" clarify in your greeting the reason why you are contacting the"
" prospect."
"Introduction: Start the conversation by introducing yourself"
" and your company. Be polite and respectful while keeping"
" the tone of the conversation professional. Your greeting"
" should be welcoming. Always clarify in your greeting the"
" reason why you are contacting the prospect."
),
"2": (
"Qualification: Qualify the prospect by confirming if they are the"
" right person to talk to regarding your product/service. Ensure that"
" they have the authority to make purchasing decisions."
"Qualification: Qualify the prospect by confirming if they"
" are the right person to talk to regarding your"
" product/service. Ensure that they have the authority to"
" make purchasing decisions."
),
"3": (
"Value proposition: Briefly explain how your product/service can"
" benefit the prospect. Focus on the unique selling points and value"
" proposition of your product/service that sets it apart from"
" competitors."
"Value proposition: Briefly explain how your product/service"
" can benefit the prospect. Focus on the unique selling"
" points and value proposition of your product/service that"
" sets it apart from competitors."
),
"4": (
"Needs analysis: Ask open-ended questions to uncover the prospect's"
" needs and pain points. Listen carefully to their responses and take"
" notes."
"Needs analysis: Ask open-ended questions to uncover the"
" prospect's needs and pain points. Listen carefully to their"
" responses and take notes."
),
"5": (
"Solution presentation: Based on the prospect's needs, present your"
" product/service as the solution that can address their pain points."
"Solution presentation: Based on the prospect's needs,"
" present your product/service as the solution that can"
" address their pain points."
),
"6": (
"Objection handling: Address any objections that the prospect may have"
" regarding your product/service. Be prepared to provide evidence or"
" testimonials to support your claims."
"Objection handling: Address any objections that the prospect"
" may have regarding your product/service. Be prepared to"
" provide evidence or testimonials to support your claims."
),
"7": (
"Close: Ask for the sale by proposing a next step. This could be a"
" demo, a trial or a meeting with decision-makers. Ensure to summarize"
" what has been discussed and reiterate the benefits."
"Close: Ask for the sale by proposing a next step. This could"
" be a demo, a trial or a meeting with decision-makers."
" Ensure to summarize what has been discussed and reiterate"
" the benefits."
),
}

@ -54,7 +54,9 @@ IMPORTANT: Avoid repeating actions such as doing the same CLICK event twice in a
Objective: {objective}
"""
USER_QUESTION = "Hello, I can help you with anything. What would you like done?"
USER_QUESTION = (
"Hello, I can help you with anything. What would you like done?"
)
SUMMARY_PROMPT = """
You are a Self-Operating Computer. You just completed a request from a user by operating the computer. Now you need to share the results.
@ -89,7 +91,8 @@ def format_vision_prompt(objective, previous_action):
"""
if previous_action:
previous_action = (
f"Here was the previous action you took: {previous_action}"
"Here was the previous action you took:"
f" {previous_action}"
)
else:
previous_action = ""

@ -21,7 +21,9 @@ from swarms.prompts.tools import (
)
from swarms.tools.tool import BaseTool
from swarms.utils.code_interpreter import SubprocessCodeInterpreter
from swarms.utils.parse_code import extract_code_in_backticks_in_string
from swarms.utils.parse_code import (
extract_code_in_backticks_in_string,
)
from swarms.utils.pdf_to_text import pdf_to_text
@ -140,7 +142,7 @@ class Agent:
get_llm_init_params(): Get LLM init params
get_tool_description(): Get the tool description
find_tool_by_name(name: str): Find a tool by name
Example:
>>> from swarms.models import OpenAIChat
@ -180,6 +182,7 @@ class Agent:
dynamic_temperature_enabled: Optional[bool] = False,
sop: Optional[str] = None,
sop_list: Optional[List[str]] = None,
# memory: Optional[Vectorstore] = None,
saved_state_path: Optional[str] = "flow_state.json",
autosave: Optional[bool] = False,
context_length: Optional[int] = 8192,
@ -189,7 +192,7 @@ class Agent:
multi_modal: Optional[bool] = None,
pdf_path: Optional[str] = None,
list_of_pdf: Optional[str] = None,
tokenizer: Optional[str] = None,
tokenizer: Optional[Any] = None,
*args,
**kwargs: Any,
):
@ -305,8 +308,9 @@ class Agent:
return "\n".join(tool_descriptions)
except Exception as error:
print(
f"Error getting tool description: {error} try adding a"
" description to the tool or removing the tool"
f"Error getting tool description: {error} try"
" adding a description to the tool or removing"
" the tool"
)
else:
return "No tools available"
@ -322,7 +326,9 @@ class Agent:
"""Construct the dynamic prompt"""
tools_description = self.get_tool_description()
tool_prompt = self.tool_prompt_prep(tools_description, SCENARIOS)
tool_prompt = self.tool_prompt_prep(
tools_description, SCENARIOS
)
return tool_prompt
@ -435,27 +441,36 @@ class Agent:
def activate_autonomous_agent(self):
"""Print the autonomous agent activation message"""
try:
print(colored("Initializing Autonomous Agent...", "yellow"))
print(
colored("Initializing Autonomous Agent...", "yellow")
)
# print(colored("Loading modules...", "yellow"))
# print(colored("Modules loaded successfully.", "green"))
print(
colored("Autonomous Agent Activated.", "cyan", attrs=["bold"])
colored(
"Autonomous Agent Activated.",
"cyan",
attrs=["bold"],
)
)
print(
colored("All systems operational. Executing task...", "green")
colored(
"All systems operational. Executing task...",
"green",
)
)
except Exception as error:
print(
colored(
(
"Error activating autonomous agent. Try optimizing your"
" parameters..."
"Error activating autonomous agent. Try"
" optimizing your parameters..."
),
"red",
)
)
print(error)
def loop_count_print(self, loop_count, max_loops):
"""loop_count_print summary
@ -463,11 +478,9 @@ class Agent:
loop_count (_type_): _description_
max_loops (_type_): _description_
"""
print(
colored(f"\nLoop {loop_count} of {max_loops}", "cyan")
)
print(colored(f"\nLoop {loop_count} of {max_loops}", "cyan"))
print("\n")
def _history(self, user_name: str, task: str) -> str:
"""Generate the history for the history prompt
@ -480,8 +493,10 @@ class Agent:
"""
history = [f"{user_name}: {task}"]
return history
def _dynamic_prompt_setup(self, dynamic_prompt: str, task: str) -> str:
def _dynamic_prompt_setup(
self, dynamic_prompt: str, task: str
) -> str:
"""_dynamic_prompt_setup summary
Args:
@ -491,11 +506,15 @@ class Agent:
Returns:
str: _description_
"""
dynamic_prompt = dynamic_prompt or self.construct_dynamic_prompt()
dynamic_prompt = (
dynamic_prompt or self.construct_dynamic_prompt()
)
combined_prompt = f"{dynamic_prompt}\n{task}"
return combined_prompt
def run(self, task: Optional[str], img: Optional[str] = None, **kwargs):
def run(
self, task: Optional[str], img: Optional[str] = None, **kwargs
):
"""
Run the autonomous agent loop
@ -524,7 +543,10 @@ class Agent:
loop_count = 0
# While the max_loops is auto or the loop count is less than the max_loops
while self.max_loops == "auto" or loop_count < self.max_loops:
while (
self.max_loops == "auto"
or loop_count < self.max_loops
):
# Loop count
loop_count += 1
self.loop_count_print(loop_count, self.max_loops)
@ -542,7 +564,9 @@ class Agent:
self.dynamic_temperature()
# Preparing the prompt
task = self.agent_history_prompt(FLOW_SYSTEM_PROMPT, response)
task = self.agent_history_prompt(
FLOW_SYSTEM_PROMPT, response
)
attempt = 0
while attempt < self.retry_attempts:
@ -581,7 +605,9 @@ class Agent:
# print(response)
break
except Exception as e:
logging.error(f"Error generating response: {e}")
logging.error(
f"Error generating response: {e}"
)
attempt += 1
time.sleep(self.retry_interval)
# Add the response to the history
@ -595,7 +621,10 @@ class Agent:
if self.autosave:
save_path = self.saved_state_path or "flow_state.json"
print(
colored(f"Autosaving agent state to {save_path}", "green")
colored(
f"Autosaving agent state to {save_path}",
"green",
)
)
self.save_state(save_path)
@ -637,12 +666,16 @@ class Agent:
# for i in range(self.max_loops):
while self.max_loops == "auto" or loop_count < self.max_loops:
loop_count += 1
print(colored(f"\nLoop {loop_count} of {self.max_loops}", "blue"))
print(
colored(
f"\nLoop {loop_count} of {self.max_loops}", "blue"
)
)
print("\n")
if self._check_stopping_condition(response) or parse_done_token(
if self._check_stopping_condition(
response
):
) or parse_done_token(response):
break
# Adjust temperature, comment if no work
@ -650,7 +683,9 @@ class Agent:
self.dynamic_temperature()
# Preparing the prompt
task = self.agent_history_prompt(FLOW_SYSTEM_PROMPT, response)
task = self.agent_history_prompt(
FLOW_SYSTEM_PROMPT, response
)
attempt = 0
while attempt < self.retry_attempts:
@ -678,7 +713,11 @@ class Agent:
if self.autosave:
save_path = self.saved_state_path or "flow_state.json"
print(colored(f"Autosaving agent state to {save_path}", "green"))
print(
colored(
f"Autosaving agent state to {save_path}", "green"
)
)
self.save_state(save_path)
if self.return_history:
@ -737,7 +776,9 @@ class Agent:
Args:
tasks (List[str]): A list of tasks to run.
"""
task_coroutines = [self.run_async(task, **kwargs) for task in tasks]
task_coroutines = [
self.run_async(task, **kwargs) for task in tasks
]
completed_tasks = await asyncio.gather(*task_coroutines)
return completed_tasks
@ -751,7 +792,9 @@ class Agent:
return Agent(llm=llm, template=template)
@staticmethod
def from_llm_and_template_file(llm: Any, template_file: str) -> "Agent":
def from_llm_and_template_file(
llm: Any, template_file: str
) -> "Agent":
"""Create AgentStream from LLM and a template file."""
with open(template_file, "r") as f:
template = f.read()
@ -785,16 +828,34 @@ class Agent:
Prints the entire history and memory of the agent.
Each message is colored and formatted for better readability.
"""
print(colored("Agent History and Memory", "cyan", attrs=["bold"]))
print(colored("========================", "cyan", attrs=["bold"]))
print(
colored(
"Agent History and Memory", "cyan", attrs=["bold"]
)
)
print(
colored(
"========================", "cyan", attrs=["bold"]
)
)
for loop_index, history in enumerate(self.memory, start=1):
print(colored(f"\nLoop {loop_index}:", "yellow", attrs=["bold"]))
print(
colored(
f"\nLoop {loop_index}:", "yellow", attrs=["bold"]
)
)
for message in history:
speaker, _, message_text = message.partition(": ")
if "Human" in speaker:
print(colored(f"{speaker}:", "green") + f" {message_text}")
print(
colored(f"{speaker}:", "green")
+ f" {message_text}"
)
else:
print(colored(f"{speaker}:", "blue") + f" {message_text}")
print(
colored(f"{speaker}:", "blue")
+ f" {message_text}"
)
print(colored("------------------------", "cyan"))
print(colored("End of Agent History", "cyan", attrs=["bold"]))
@ -963,7 +1024,16 @@ class Agent:
value = getattr(self.llm, name)
if isinstance(
value,
(str, int, float, bool, list, dict, tuple, type(None)),
(
str,
int,
float,
bool,
list,
dict,
tuple,
type(None),
),
):
llm_params[name] = value
else:
@ -1110,7 +1180,9 @@ class Agent:
text = text or self.pdf_connector()
pass
def tools_prompt_prep(self, docs: str = None, scenarios: str = None):
def tools_prompt_prep(
self, docs: str = None, scenarios: str = None
):
"""
Prepare the tool prompt
"""

@ -62,7 +62,9 @@ class AutoScaler:
agent=None,
):
self.agent = agent or Agent
self.agents_pool = [self.agent() for _ in range(initial_agents)]
self.agents_pool = [
self.agent() for _ in range(initial_agents)
]
self.task_queue = queue.Queue()
self.scale_up_factor = scale_up_factor
self.idle_threshold = idle_threshold
@ -74,7 +76,8 @@ class AutoScaler:
self.tasks_queue.put(task)
except Exception as error:
print(
f"Error adding task to queue: {error} try again with a new task"
f"Error adding task to queue: {error} try again with"
" a new task"
)
@log_decorator
@ -84,20 +87,29 @@ class AutoScaler:
"""Add more agents"""
try:
with self.lock:
new_agents_counts = len(self.agents_pool) * self.scale_up_factor
new_agents_counts = (
len(self.agents_pool) * self.scale_up_factor
)
for _ in range(new_agents_counts):
self.agents_pool.append(Agent())
except Exception as error:
print(f"Error scaling up: {error} try again with a new task")
print(
f"Error scaling up: {error} try again with a new task"
)
def scale_down(self):
"""scale down"""
try:
with self.lock:
if len(self.agents_pool) > 10: # ensure minmum of 10 agents
if (
len(self.agents_pool) > 10
): # ensure minmum of 10 agents
del self.agents_pool[-1] # remove last agent
except Exception as error:
print(f"Error scaling down: {error} try again with a new task")
print(
f"Error scaling down: {error} try again with a new"
" task"
)
@log_decorator
@error_decorator
@ -109,19 +121,27 @@ class AutoScaler:
sleep(60) # check minute
pending_tasks = self.task_queue.qsize()
active_agents = sum(
[1 for agent in self.agents_pool if agent.is_busy()]
[
1
for agent in self.agents_pool
if agent.is_busy()
]
)
if pending_tasks / len(self.agents_pool) > self.busy_threshold:
if (
pending_tasks / len(self.agents_pool)
> self.busy_threshold
):
self.scale_up()
elif (
active_agents / len(self.agents_pool) < self.idle_threshold
active_agents / len(self.agents_pool)
< self.idle_threshold
):
self.scale_down()
except Exception as error:
print(
f"Error monitoring and scaling: {error} try again with a new"
" task"
f"Error monitoring and scaling: {error} try again"
" with a new task"
)
@log_decorator
@ -130,7 +150,9 @@ class AutoScaler:
def start(self):
"""Start scaling"""
try:
monitor_thread = threading.Thread(target=self.monitor_and_scale)
monitor_thread = threading.Thread(
target=self.monitor_and_scale
)
monitor_thread.start()
while True:
@ -142,13 +164,17 @@ class AutoScaler:
if available_agent:
available_agent.run(task)
except Exception as error:
print(f"Error starting: {error} try again with a new task")
print(
f"Error starting: {error} try again with a new task"
)
def check_agent_health(self):
"""Checks the health of each agent and replaces unhealthy agents."""
for i, agent in enumerate(self.agents_pool):
if not agent.is_healthy():
logging.warning(f"Replacing unhealthy agent at index {i}")
logging.warning(
f"Replacing unhealthy agent at index {i}"
)
self.agents_pool[i] = self.agent()
def balance_load(self):
@ -159,7 +185,9 @@ class AutoScaler:
task = self.task_queue.get()
agent.run(task)
def set_scaling_strategy(self, strategy: Callable[[int, int], int]):
def set_scaling_strategy(
self, strategy: Callable[[int, int], int]
):
"""Set a custom scaling strategy."""
self.custom_scale_strategy = strategy
@ -179,7 +207,11 @@ class AutoScaler:
def report_agent_metrics(self) -> Dict[str, List[float]]:
"""Collects and reports metrics from each agent."""
metrics = {"completion_time": [], "success_rate": [], "error_rate": []}
metrics = {
"completion_time": [],
"success_rate": [],
"error_rate": [],
}
for agent in self.agents_pool:
agent_metrics = agent.get_metrics()
for key in metrics.keys():

@ -87,5 +87,7 @@ class BaseDocumentTransformer(ABC):
A list of transformed Documents.
"""
return await asyncio.get_running_loop().run_in_executor(
None, partial(self.transform_documents, **kwargs), documents
None,
partial(self.transform_documents, **kwargs),
documents,
)

@ -69,11 +69,18 @@ class Task:
# Add a prompt to notify the Agent of the sequential workflow
if "prompt" in self.kwargs:
self.kwargs["prompt"] += (
f"\n\nPrevious output: {self.result}" if self.result else ""
f"\n\nPrevious output: {self.result}"
if self.result
else ""
)
else:
self.kwargs["prompt"] = f"Main task: {self.description}" + (
f"\n\nPrevious output: {self.result}" if self.result else ""
self.kwargs["prompt"] = (
f"Main task: {self.description}"
+ (
f"\n\nPrevious output: {self.result}"
if self.result
else ""
)
)
self.result = self.agent.run(*self.args, **self.kwargs)
else:
@ -116,7 +123,9 @@ class SequentialWorkflow:
autosave: bool = False
name: str = (None,)
description: str = (None,)
saved_state_filepath: Optional[str] = "sequential_workflow_state.json"
saved_state_filepath: Optional[str] = (
"sequential_workflow_state.json"
)
restore_state_filepath: Optional[str] = None
dashboard: bool = False
@ -181,7 +190,9 @@ class SequentialWorkflow:
def remove_task(self, task: str) -> None:
"""Remove tasks from sequential workflow"""
self.tasks = [task for task in self.tasks if task.description != task]
self.tasks = [
task for task in self.tasks if task.description != task
]
def update_task(self, task: str, **updates) -> None:
"""
@ -330,7 +341,9 @@ class SequentialWorkflow:
)
self.tasks.append(task)
def load_workflow_state(self, filepath: str = None, **kwargs) -> None:
def load_workflow_state(
self, filepath: str = None, **kwargs
) -> None:
"""
Loads the workflow state from a json file and restores the workflow state.
@ -384,18 +397,22 @@ class SequentialWorkflow:
# Ensure that 'task' is provided in the kwargs
if "task" not in task.kwargs:
raise ValueError(
"The 'task' argument is required for the"
" Agent agent execution in"
f" '{task.description}'"
"The 'task' argument is required"
" for the Agent agent execution"
f" in '{task.description}'"
)
# Separate the 'task' argument from other kwargs
flow_task_arg = task.kwargs.pop("task")
task.result = task.agent.run(
flow_task_arg, *task.args, **task.kwargs
flow_task_arg,
*task.args,
**task.kwargs,
)
else:
# If it's not a Agent instance, call the agent directly
task.result = task.agent(*task.args, **task.kwargs)
task.result = task.agent(
*task.args, **task.kwargs
)
# Pass the result as an argument to the next task if it exists
next_task_index = self.tasks.index(task) + 1
@ -417,9 +434,9 @@ class SequentialWorkflow:
print(
colored(
(
f"Error initializing the Sequential workflow: {e} try"
" optimizing your inputs like the agent class and task"
" description"
"Error initializing the Sequential workflow:"
f" {e} try optimizing your inputs like the"
" agent class and task description"
),
"red",
attrs=["bold", "underline"],
@ -443,8 +460,9 @@ class SequentialWorkflow:
# Ensure that 'task' is provided in the kwargs
if "task" not in task.kwargs:
raise ValueError(
"The 'task' argument is required for the Agent"
f" agent execution in '{task.description}'"
"The 'task' argument is required for"
" the Agent agent execution in"
f" '{task.description}'"
)
# Separate the 'task' argument from other kwargs
flow_task_arg = task.kwargs.pop("task")

@ -144,7 +144,9 @@ class AbstractSwarm(ABC):
pass
@abstractmethod
def autoscaler(self, num_workers: int, worker: ["AbstractWorker"]):
def autoscaler(
self, num_workers: int, worker: ["AbstractWorker"]
):
"""Autoscaler that acts like kubernetes for autonomous agents"""
pass
@ -159,7 +161,9 @@ class AbstractSwarm(ABC):
pass
@abstractmethod
def assign_task(self, worker: "AbstractWorker", task: Any) -> Dict:
def assign_task(
self, worker: "AbstractWorker", task: Any
) -> Dict:
"""Assign a task to a worker"""
pass

@ -24,7 +24,10 @@ class DialogueSimulator:
"""
def __init__(
self, agents: List[Callable], max_iters: int = 10, name: str = None
self,
agents: List[Callable],
max_iters: int = 10,
name: str = None,
):
self.agents = agents
self.max_iters = max_iters
@ -60,7 +63,8 @@ class DialogueSimulator:
def __repr__(self):
return (
f"DialogueSimulator({self.agents}, {self.max_iters}, {self.name})"
f"DialogueSimulator({self.agents}, {self.max_iters},"
f" {self.name})"
)
def save_state(self):

@ -64,7 +64,11 @@ class GodMode:
table.append([f"LLM {i+1}", response])
print(
colored(
tabulate(table, headers=["LLM", "Response"], tablefmt="pretty"),
tabulate(
table,
headers=["LLM", "Response"],
tablefmt="pretty",
),
"cyan",
)
)
@ -84,7 +88,11 @@ class GodMode:
table.append([f"LLM {i+1}", response])
print(
colored(
tabulate(table, headers=["LLM", "Response"], tablefmt="pretty"),
tabulate(
table,
headers=["LLM", "Response"],
tablefmt="pretty",
),
"cyan",
)
)
@ -122,7 +130,11 @@ class GodMode:
]
print(
colored(
tabulate(table, headers=["LLM", "Response"], tablefmt="pretty"),
tabulate(
table,
headers=["LLM", "Response"],
tablefmt="pretty",
),
"cyan",
)
)
@ -159,8 +171,8 @@ class GodMode:
responses.append(future.result())
except Exception as error:
print(
f"{future_to_llm[future]} generated an exception:"
f" {error}"
f"{future_to_llm[future]} generated an"
f" exception: {error}"
)
self.last_responses = responses
self.task_history.append(task)

@ -43,12 +43,15 @@ class GroupChat:
for agent in self.agents:
if agent.name in name:
return agent
raise ValueError(f"No agent found with a name contained in '{name}'.")
raise ValueError(
f"No agent found with a name contained in '{name}'."
)
def next_agent(self, agent: Agent) -> Agent:
"""Return the next agent in the list."""
return self.agents[
(self.agent_names.index(agent.name) + 1) % len(self.agents)
(self.agent_names.index(agent.name) + 1)
% len(self.agents)
]
def select_speaker_msg(self):
@ -69,8 +72,8 @@ class GroupChat:
n_agents = len(self.agent_names)
if n_agents < 3:
logger.warning(
f"GroupChat is underpopulated with {n_agents} agents. Direct"
" communication would be more efficient."
f"GroupChat is underpopulated with {n_agents} agents."
" Direct communication would be more efficient."
)
name = selector.generate_reply(
@ -80,9 +83,10 @@ class GroupChat:
{
"role": "system",
"content": (
"Read the above conversation. Then select the next"
f" most suitable role from {self.agent_names} to"
" play. Only return the role."
"Read the above conversation. Then"
" select the next most suitable role"
f" from {self.agent_names} to play. Only"
" return the role."
),
}
]
@ -95,13 +99,18 @@ class GroupChat:
def _participant_roles(self):
return "\n".join(
[f"{agent.name}: {agent.system_message}" for agent in self.agents]
[
f"{agent.name}: {agent.system_message}"
for agent in self.agents
]
)
def format_history(self, messages: List[Dict]) -> str:
formatted_messages = []
for message in messages:
formatted_message = f"'{message['role']}:{message['content']}"
formatted_message = (
f"'{message['role']}:{message['content']}"
)
formatted_messages.append(formatted_message)
return "\n".join(formatted_messages)

@ -13,8 +13,8 @@ from swarms.utils.logger import logger
class BidOutputParser(RegexParser):
def get_format_instructions(self) -> str:
return (
"Your response should be an integrater delimited by angled brackets"
" like this: <int>"
"Your response should be an integrater delimited by"
" angled brackets like this: <int>"
)
@ -123,7 +123,9 @@ class MultiAgentCollaboration:
def step(self) -> tuple[str, str]:
"""Steps through the multi-agent collaboration."""
speaker_idx = self.select_next_speaker(self._step, self.agents)
speaker_idx = self.select_next_speaker(
self._step, self.agents
)
speaker = self.agents[speaker_idx]
message = speaker.send()
message = speaker.send()
@ -146,7 +148,8 @@ class MultiAgentCollaboration:
wait=tenacity.wait_none(),
retry=tenacity.retry_if_exception_type(ValueError),
before_sleep=lambda retry_state: print(
f"ValueError occured: {retry_state.outcome.exception()}, retying..."
f"ValueError occured: {retry_state.outcome.exception()},"
" retying..."
),
retry_error_callback=lambda retry_state: 0,
)
@ -167,7 +170,9 @@ class MultiAgentCollaboration:
bid = self.ask_for_bid(agent)
bids.append(bid)
max_value = max(bids)
max_indices = [i for i, x in enumerate(bids) if x == max_value]
max_indices = [
i for i, x in enumerate(bids) if x == max_value
]
idx = random.choice(max_indices)
return idx
@ -176,7 +181,8 @@ class MultiAgentCollaboration:
wait=tenacity.wait_none(),
retry=tenacity.retry_if_exception_type(ValueError),
before_sleep=lambda retry_state: print(
f"ValueError occured: {retry_state.outcome.exception()}, retying..."
f"ValueError occured: {retry_state.outcome.exception()},"
" retying..."
),
retry_error_callback=lambda retry_state: 0,
)
@ -256,7 +262,9 @@ class MultiAgentCollaboration:
for _ in range(self.max_iters):
for agent in self.agents:
result = agent.run(conversation)
self.results.append({"agent": agent, "response": result})
self.results.append(
{"agent": agent, "response": result}
)
conversation += result
if self.autosave:
@ -309,7 +317,9 @@ class MultiAgentCollaboration:
"""Tracks and reports the performance of each agent"""
performance_data = {}
for agent in self.agents:
performance_data[agent.name] = agent.get_performance_metrics()
performance_data[agent.name] = (
agent.get_performance_metrics()
)
return performance_data
def set_interaction_rules(self, rules):

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save