[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) 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?") img = node.run_img("/image1", "What is this image about?")
chat = node.chat( chat = node.chat(
( (
"What is your name? Generate a picture of yourself. What is this image" "What is your name? Generate a picture of yourself. What is"
" about?" " this image about?"
), ),
streaming=True, streaming=True,
) )

@ -53,7 +53,8 @@ decision_making_support_agent = Agent(
pdf_path = "bankstatement.pdf" pdf_path = "bankstatement.pdf"
fraud_detection_instructions = "Detect fraud in the document" fraud_detection_instructions = "Detect fraud in the document"
summary_agent_instructions = ( 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 = ( decision_making_support_agent_instructions = (
"Provide decision making support to the business owner:" "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 # Provide decision making support to the accountant
decision_making_support_agent_output = decision_making_support_agent.run( 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__() super().__init__()
self.pdf_path = pdf_path self.pdf_path = pdf_path
self.list_pdfs = list_pdfs 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.summary_agent_instructions = summary_agent_instructions
self.decision_making_support_agent_instructions = ( self.decision_making_support_agent_instructions = (
decision_making_support_agent_instructions decision_making_support_agent_instructions
@ -98,7 +100,8 @@ class AccountantSwarms:
# Generate an actionable summary of the document # Generate an actionable summary of the document
summary_agent_output = summary_generator_agent.run( 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 # Provide decision making support to the accountant
@ -113,7 +116,9 @@ class AccountantSwarms:
swarm = AccountantSwarms( swarm = AccountantSwarms(
pdf_path="tesla.pdf", pdf_path="tesla.pdf",
fraud_detection_instructions="Detect fraud in the document", 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=( decision_making_support_agent_instructions=(
"Provide decision making support to the business owner:" "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 luxurious setting", "in a playful and colorful background", "in an ice cave setting",
"in a serene and calm landscape" "in a serene and calm landscape"
] ]
<<<<<<< HEAD
=======
self.contexts = [
"high realism product ad (extremely creative)"
]
>>>>>>> 831147e ([CODE QUALITY])
def generate_concept(self): def generate_concept(self):
theme = random.choice(self.themes) theme = random.choice(self.themes)
context = random.choice(self.contexts) context = random.choice(self.contexts)
<<<<<<< HEAD
return f"An ad for {self.product_name} that embodies a {theme} theme {context}" return f"An ad for {self.product_name} that embodies a {theme} theme {context}"
# User input # User input
product_name = input("Enter a product name for ad creation (e.g., 'PS5', 'AirPods', 'Kirkland Vodka'): ") 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'): ") 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 # Generate creative concept
concept_generator = ProductAdConceptGenerator(product_name) 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) ad_copy = ad_copy_agent.run(task=ad_copy_prompt)
# Output the results # Output the results
<<<<<<< HEAD
print("Creative Concept:", creative_concept) print("Creative Concept:", creative_concept)
print("Image Path:", image_paths[0] if image_paths else "No image generated") print("Image Path:", image_paths[0] if image_paths else "No image generated")
print("Ad Copy:", ad_copy) 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) paper = pdf_to_text(PDF_PATH)
algorithmic_psuedocode_agent = paper_summarizer_agent.run( algorithmic_psuedocode_agent = paper_summarizer_agent.run(
"Focus on creating the algorithmic pseudocode for the novel method in this" "Focus on creating the algorithmic pseudocode for the novel"
f" paper: {paper}" 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() llm = GPT4VisionAPI()
task = ( task = (
"Analyze this image of an assembly line and identify any issues such as" "Analyze this image of an assembly line and identify any issues"
" misaligned parts, defects, or deviations from the standard assembly" " such as misaligned parts, defects, or deviations from the"
" process. IF there is anything unsafe in the image, explain why it is" " standard assembly process. IF there is anything unsafe in the"
" unsafe and how it could be improved." " image, explain why it is unsafe and how it could be improved."
) )
img = "assembly_line.jpg" img = "assembly_line.jpg"

@ -9,7 +9,8 @@ from swarms.prompts.autobloggen import (
# Prompts # Prompts
topic_selection_task = ( 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.llm = llm()
self.topic_selection_task = topic_selection_task 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.objective = objective
self.iterations = iterations self.iterations = iterations
self.max_retries = max_retries self.max_retries = max_retries
@ -90,7 +93,9 @@ class AutoBlogGenSwarm:
def step(self): def step(self):
"""Steps through the task""" """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 = self.print_beautifully(
"Topic Selection Agent", topic_selection_agent "Topic Selection Agent", topic_selection_agent
) )
@ -100,7 +105,9 @@ class AutoBlogGenSwarm:
# Agent that reviews the draft # Agent that reviews the draft
review_agent = self.llm(self.get_review_prompt(draft_blog)) 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 # Agent that publishes on social media
distribution_agent = self.llm( distribution_agent = self.llm(
@ -119,7 +126,11 @@ class AutoBlogGenSwarm:
except Exception as error: except Exception as error:
print( print(
colored( colored(
f"Error while running AutoBlogGenSwarm {error}", "red" (
"Error while running AutoBlogGenSwarm"
f" {error}"
),
"red",
) )
) )
if attempt == self.retry_attempts - 1: if attempt == self.retry_attempts - 1:

@ -47,7 +47,11 @@ class AutoTemp:
""" """
score_text = self.llm(eval_prompt, temperature=0.5) score_text = self.llm(eval_prompt, temperature=0.5)
score_match = re.search(r"\b\d+(\.\d)?\b", score_text) 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): def run(self, prompt, temperature_string):
print("Starting generation process...") print("Starting generation process...")

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

@ -5,7 +5,9 @@ from blog_gen import BlogGen
def main(): def main():
api_key = os.getenv("OPENAI_API_KEY") api_key = os.getenv("OPENAI_API_KEY")
if not 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: ") 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
documentation_agent = Agent( documentation_agent = Agent(
llm=llm, sop=DOCUMENTATION_SOP, max_loops=1, llm=llm,
sop=DOCUMENTATION_SOP,
max_loops=1,
) )
# Tests agent # 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 # Run the documentation agent
@ -52,6 +58,6 @@ documentation = documentation_agent.run(
# Run the tests agent # Run the tests agent
tests = tests_agent.run( tests = tests_agent.run(
f"Write tests for the following code:{TASK} here is the documentation:" f"Write tests for the following code:{TASK} here is the"
f" {documentation}" f" documentation: {documentation}"
) )

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

@ -41,7 +41,9 @@ def get_review_prompt(article):
return prompt 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( prompt = SOCIAL_MEDIA_SYSTEM_PROMPT_AGENT.replace(
"{{ARTICLE}}", article "{{ARTICLE}}", article
).replace("{{GOAL}}", goal) ).replace("{{GOAL}}", goal)
@ -50,11 +52,12 @@ def social_media_prompt(article: str, goal: str = "Clicks and engagement"):
# Agent that generates topics # Agent that generates topics
topic_selection_task = ( 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( topics = llm(
f"Your System Instructions: {TOPIC_GENERATOR_SYSTEM_PROMPT}, Your current" f"Your System Instructions: {TOPIC_GENERATOR_SYSTEM_PROMPT}, Your"
f" task: {topic_selection_task}" f" current task: {topic_selection_task}"
) )
dashboard = print( dashboard = print(

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

@ -2,7 +2,8 @@ from langchain.document_loaders import CSVLoader
from swarms.memory import qdrant from swarms.memory import qdrant
loader = CSVLoader( 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() docs = loader.load()

@ -24,7 +24,9 @@ llm = OpenAIChat(
) )
# Initialize the Worker with the custom tool # 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 # Use the worker to process a task
task = "Hello, my name is ChatGPT" task = "Hello, my name is ChatGPT"

@ -17,5 +17,8 @@ labels = [
] ]
result = clip("swarms.jpeg", 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) clip.plot_image_with_metadata("swarms.jpeg", metadata)

@ -7,4 +7,6 @@ model_wrapper = DistilWhisperModel()
transcription = model_wrapper("path/to/audio.mp3") transcription = model_wrapper("path/to/audio.mp3")
# For async usage # 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 from swarms.models.mpt import MPT
mpt_instance = 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...") 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() text = soup.get_text()
lines = (line.strip() for line in text.splitlines()) lines = (line.strip() for line in text.splitlines())
chunks = ( 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) results = "\n".join(chunk for chunk in chunks if chunk)
except Exception as e: except Exception as e:
@ -60,6 +62,6 @@ agent = Agent(
) )
out = agent.run( out = agent.run(
"Generate a 10,000 word blog on mental clarity and the benefits of" "Generate a 10,000 word blog on mental clarity and the benefits"
" meditation." " of meditation."
) )

@ -18,7 +18,9 @@ flow2 = Agent(llm=llm, max_loops=1, dashboard=False)
workflow = SequentialWorkflow(max_loops=1) workflow = SequentialWorkflow(max_loops=1)
# Add tasks to the workflow # 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 # Suppose the next task takes the output of the first task as input
workflow.add("Summarize the generated blog", flow2) workflow.add("Summarize the generated blog", flow2)

@ -1,7 +1,11 @@
from swarms import Orchestrator, Worker from swarms import Orchestrator, Worker
# Instantiate the Orchestrator with 10 agents # 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 # 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, self.system_message,
HumanMessage( 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]: def step(self) -> tuple[str, str]:
# 1. choose the next speaker # 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] speaker = self.agents[speaker_idx]
# 2. next speaker sends message # 2. next speaker sends message
@ -114,7 +118,9 @@ class BiddingDialogueAgent(DialogueAgent):
message_history="\n".join(self.message_history), message_history="\n".join(self.message_history),
recent_message=self.message_history[-1], 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 return bid_string
@ -127,7 +133,8 @@ The presidential candidates are: {', '.join(character_names)}."""
player_descriptor_system_message = SystemMessage( player_descriptor_system_message = SystemMessage(
content=( 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} return SystemMessage(content=f"""{character_header}
You will speak in the style of {character_name}, and exaggerate their personality. You will speak in the style of {character_name}, and exaggerate their personality.
You will come up with creative ideas related to {topic}. You will come up with creative ideas related to {topic}.
@ -183,7 +192,9 @@ character_headers = [
) )
] ]
character_system_messages = [ 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( for character_name, character_headers in zip(
character_names, character_headers character_names, character_headers
) )
@ -209,8 +220,8 @@ for (
class BidOutputParser(RegexParser): class BidOutputParser(RegexParser):
def get_format_instructions(self) -> str: def get_format_instructions(self) -> str:
return ( return (
"Your response should be an integer delimited by angled brackets," "Your response should be an integer delimited by angled"
" like this: <int>." " brackets, like this: <int>."
) )
@ -262,7 +273,9 @@ topic_specifier_prompt = [
Speak directly to the presidential candidates: {*character_names,}. Speak directly to the presidential candidates: {*character_names,}.
Do not add anything else."""), 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"Original topic:\n{topic}\n")
print(f"Detailed topic:\n{specified_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 wait=tenacity.wait_none(), # No waiting time between retries
retry=tenacity.retry_if_exception_type(ValueError), retry=tenacity.retry_if_exception_type(ValueError),
before_sleep=lambda retry_state: print( 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, retry_error_callback=lambda retry_state: 0,
) # Default value when all retries are exhausted ) # Default value when all retries are exhausted
@ -286,7 +300,9 @@ def ask_for_bid(agent) -> str:
return bid return bid
def select_next_speaker(step: int, agents: List[DialogueAgent]) -> int: def select_next_speaker(
step: int, agents: List[DialogueAgent]
) -> int:
bids = [] bids = []
for agent in agents: for agent in agents:
bid = ask_for_bid(agent) bid = ask_for_bid(agent)
@ -309,7 +325,9 @@ def select_next_speaker(step: int, agents: List[DialogueAgent]) -> int:
characters = [] characters = []
for character_name, character_system_message, bidding_template in zip( 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( characters.append(
BiddingDialogueAgent( BiddingDialogueAgent(

@ -2,7 +2,9 @@ from swarms.swarms import DialogueSimulator
from swarms.workers.worker import Worker from swarms.workers.worker import Worker
from swarms.models import OpenAIChat 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( worker1 = Worker(
llm=llm, llm=llm,

@ -45,5 +45,7 @@ manager = Agent(
agents = [flow1, flow2, flow3] agents = [flow1, flow2, flow3]
group_chat = GroupChat(agents=agents, messages=[], max_round=10) 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") 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.workers.worker import Worker
from swarms.models import OpenAIChat from swarms.models import OpenAIChat
@ -37,9 +40,9 @@ debate = MultiAgentDebate(agents, select_speaker)
# Run task # Run task
task = ( task = (
"What were the winning boston marathon times for the past 5 years (ending" "What were the winning boston marathon times for the past 5 years"
" in 2022)? Generate a table of the year, name, country of origin, and" " (ending in 2022)? Generate a table of the year, name, country"
" times." " of origin, and times."
) )
results = debate.run(task, max_iters=4) results = debate.run(task, max_iters=4)

@ -7,9 +7,13 @@ node = Worker(
# Instantiate the Orchestrator with 10 agents # 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 # Agent 7 sends a message to Agent 9
orchestrator.chat( 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 # 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 # Agent 7 sends a message to Agent 9
orchestrator.chat( 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 # Define an objective
objective = ( objective = (
"Find 20 potential customers for a HierarchicalSwarm based AI Agent" "Find 20 potential customers for a HierarchicalSwarm based AI"
" automation infrastructure" " Agent automation infrastructure"
) )
# Run HierarchicalSwarm # Run HierarchicalSwarm

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

@ -33,12 +33,16 @@ agent3 = Agent(llm=biochat, max_loops=1, dashboard=False)
workflow = SequentialWorkflow(max_loops=1) workflow = SequentialWorkflow(max_loops=1)
# Add tasks to the workflow # 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 # Suppose the next task takes the output of the first task as input
workflow.add("Summarize the generated blog", agent2) 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 # Run the workflow
workflow.run() workflow.run()

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

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

@ -18,8 +18,8 @@ def cosine_similarity(X: Matrix, Y: Matrix) -> np.ndarray:
Y = np.array(Y) Y = np.array(Y)
if X.shape[1] != Y.shape[1]: if X.shape[1] != Y.shape[1]:
raise ValueError( raise ValueError(
"Number of columns in X and Y must be the same. X has shape" "Number of columns in X and Y must be the same. X has"
f" {X.shape} and Y has shape {Y.shape}." f" shape {X.shape} and Y has shape {Y.shape}."
) )
try: try:
import simsimd as simd import simsimd as simd
@ -32,9 +32,9 @@ def cosine_similarity(X: Matrix, Y: Matrix) -> np.ndarray:
return Z return Z
except ImportError: except ImportError:
logger.info( logger.info(
"Unable to import simsimd, defaulting to NumPy implementation. If" "Unable to import simsimd, defaulting to NumPy"
" you want to use simsimd please install with `pip install" " implementation. If you want to use simsimd please"
" simsimd`." " install with `pip install simsimd`."
) )
X_norm = np.linalg.norm(X, axis=1) X_norm = np.linalg.norm(X, axis=1)
Y_norm = np.linalg.norm(Y, 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_array = cosine_similarity(X, Y)
score_threshold = score_threshold or -1.0 score_threshold = score_threshold or -1.0
score_array[score_array < score_threshold] = 0 score_array[score_array < score_threshold] = 0
top_k = min(top_k or len(score_array), np.count_nonzero(score_array)) top_k = min(
top_k_idxs = np.argpartition(score_array, -top_k, axis=None)[-top_k:] top_k or len(score_array), np.count_nonzero(score_array)
top_k_idxs = top_k_idxs[np.argsort(score_array.ravel()[top_k_idxs])][::-1] )
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) ret_idxs = np.unravel_index(top_k_idxs, score_array.shape)
scores = score_array.ravel()[top_k_idxs].tolist() scores = score_array.ravel()[top_k_idxs].tolist()
return list(zip(*ret_idxs)), scores # type: ignore 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) create_engine_params: dict = field(factory=dict, kw_only=True)
engine: Optional[Engine] = field(default=None, kw_only=True) engine: Optional[Engine] = field(default=None, kw_only=True)
table_name: str = field(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 an engine is not provided, a connection string is required.
if connection_string is None: 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://"): if not connection_string.startswith("postgresql://"):
raise ValueError( raise ValueError(
"The connection string must describe a Postgres database" "The connection string must describe a Postgres"
" connection" " database connection"
) )
@engine.validator @engine.validator
@ -120,7 +124,9 @@ class PgVectorVectorStore(BaseVectorStore):
# If a connection string is not provided, an engine is required. # If a connection string is not provided, an engine is required.
if engine is None: 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: def __attrs_post_init__(self) -> None:
"""If a an engine is provided, it will be used to connect to the database. """If a an engine is provided, it will be used to connect to the database.
@ -139,10 +145,14 @@ class PgVectorVectorStore(BaseVectorStore):
) -> None: ) -> None:
"""Provides a mechanism to initialize the database schema and extensions.""" """Provides a mechanism to initialize the database schema and extensions."""
if install_uuid_extension: 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: 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: if create_schema:
self._model.metadata.create_all(self.engine) self._model.metadata.create_all(self.engine)
@ -246,7 +256,9 @@ class PgVectorVectorStore(BaseVectorStore):
return [ return [
BaseVectorStore.QueryResult( BaseVectorStore.QueryResult(
id=str(result[0].id), 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], score=result[1],
meta=result[0].meta, meta=result[0].meta,
namespace=result[0].namespace, namespace=result[0].namespace,

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

@ -2,7 +2,11 @@ from typing import List
from sentence_transformers import SentenceTransformer from sentence_transformers import SentenceTransformer
from httpx import RequestError from httpx import RequestError
from qdrant_client import QdrantClient 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: class Qdrant:
@ -33,7 +37,9 @@ class Qdrant:
https: bool = True, https: bool = True,
): ):
try: 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.collection_name = collection_name
self._load_embedding_model(model_name) self._load_embedding_model(model_name)
self._setup_collection() self._setup_collection()
@ -56,7 +62,10 @@ class Qdrant:
try: try:
exists = self.client.get_collection(self.collection_name) exists = self.client.get_collection(self.collection_name)
if exists: if exists:
print(f"Collection '{self.collection_name}' already exists.") print(
f"Collection '{self.collection_name}' already"
" exists."
)
except Exception as e: except Exception as e:
self.client.create_collection( self.client.create_collection(
collection_name=self.collection_name, collection_name=self.collection_name,
@ -93,7 +102,8 @@ class Qdrant:
) )
else: else:
print( 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: except Exception as e:
print(f"Error processing document at index {i}: {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. SearchResult or None: Returns the search results if successful, otherwise None.
""" """
try: try:
query_vector = self.model.encode(query, normalize_embeddings=True) query_vector = self.model.encode(
query, normalize_embeddings=True
)
search_result = self.client.search( search_result = self.client.search(
collection_name=self.collection_name, collection_name=self.collection_name,
query_vector=query_vector, query_vector=query_vector,

@ -9,7 +9,9 @@ from pydantic import BaseModel, Field
class TaskInput(BaseModel): class TaskInput(BaseModel):
__root__: Any = Field( __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}', example='{\n"debug": false,\n"mode": "benchmarks"\n}',
) )
@ -25,7 +27,9 @@ class Artifact(BaseModel):
) )
relative_path: Optional[str] = Field( relative_path: Optional[str] = Field(
None, 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/", example="python/code/",
) )
@ -34,7 +38,9 @@ class ArtifactUpload(BaseModel):
file: bytes = Field(..., description="File to upload") file: bytes = Field(..., description="File to upload")
relative_path: Optional[str] = Field( relative_path: Optional[str] = Field(
None, 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/", example="python/code/",
) )
@ -42,7 +48,10 @@ class ArtifactUpload(BaseModel):
class StepInput(BaseModel): class StepInput(BaseModel):
__root__: Any = Field( __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}', example='{\n"file_to_refactor": "models.py"\n}',
) )
@ -51,7 +60,8 @@ class StepOutput(BaseModel):
__root__: Any = Field( __root__: Any = Field(
..., ...,
description=( 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}', example='{\n"tokens": 7894,\n"estimated_cost": "0,24$"\n}',
) )
@ -61,7 +71,9 @@ class TaskRequestBody(BaseModel):
input: Optional[str] = Field( input: Optional[str] = Field(
None, None,
description="Input prompt for the task.", 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 additional_input: Optional[TaskInput] = None
@ -84,7 +96,9 @@ class Task(TaskRequestBody):
class StepRequestBody(BaseModel): class StepRequestBody(BaseModel):
input: Optional[str] = Field( 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 additional_input: Optional[StepInput] = None
@ -107,22 +121,28 @@ class Step(StepRequestBody):
example="6bb1801a-fd80-45e8-899a-4dd723cc602e", example="6bb1801a-fd80-45e8-899a-4dd723cc602e",
) )
name: Optional[str] = Field( 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( output: Optional[str] = Field(
None, None,
description="Output of the task step.", description="Output of the task step.",
example=( example=(
"I am going to use the write_to_file command and write Washington" "I am going to use the write_to_file command and write"
" to a file called output.txt <write_to_file('output.txt'," " Washington to a file called output.txt"
" 'Washington')" " <write_to_file('output.txt', 'Washington')"
), ),
) )
additional_output: Optional[StepOutput] = None additional_output: Optional[StepOutput] = None
artifacts: List[Artifact] = Field( 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( 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 [] return []
if query_embedding.ndim == 1: if query_embedding.ndim == 1:
query_embedding = np.expand_dims(query_embedding, axis=0) 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)) most_similar = int(np.argmax(similarity_to_query))
idxs = [most_similar] idxs = [most_similar]
selected = np.array([embedding_list[most_similar]]) selected = np.array([embedding_list[most_similar]])
while len(idxs) < min(k, len(embedding_list)): while len(idxs) < min(k, len(embedding_list)):
best_score = -np.inf best_score = -np.inf
idx_to_add = -1 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): for i, query_score in enumerate(similarity_to_query):
if i in idxs: if i in idxs:
continue continue
redundant_score = max(similarity_to_selected[i]) redundant_score = max(similarity_to_selected[i])
equation_score = ( 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: if equation_score > best_score:
best_score = equation_score best_score = equation_score
idx_to_add = i idx_to_add = i
idxs.append(idx_to_add) 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 return idxs

@ -10,14 +10,18 @@ from swarms.models.openai_models import (
from swarms.models.zephyr import Zephyr # noqa: E402 from swarms.models.zephyr import Zephyr # noqa: E402
from swarms.models.biogpt import BioGPT # noqa: E402 from swarms.models.biogpt import BioGPT # noqa: E402
from swarms.models.huggingface import HuggingfaceLLM # 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 from swarms.models.mpt import MPT7B # noqa: E402
# MultiModal Models # MultiModal Models
from swarms.models.idefics import Idefics # noqa: E402 from swarms.models.idefics import Idefics # noqa: E402
from swarms.models.vilt import Vilt # noqa: E402 from swarms.models.vilt import Vilt # noqa: E402
from swarms.models.nougat import Nougat # 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.gpt4_vision_api import GPT4VisionAPI # noqa: E402
# from swarms.models.gpt4v import GPT4Vision # 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: def wrapper(*args: Any, **kwargs: Any) -> Any:
"""Validate exactly one arg in each group is not None.""" """Validate exactly one arg in each group is not None."""
counts = [ 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 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: if invalid_groups:
invalid_group_names = [ invalid_group_names = [
", ".join(arg_groups[i]) for i in invalid_groups ", ".join(arg_groups[i]) for i in invalid_groups
@ -119,8 +125,9 @@ def guard_import(
module = importlib.import_module(module_name, package) module = importlib.import_module(module_name, package)
except ImportError: except ImportError:
raise ImportError( raise ImportError(
f"Could not import {module_name} python package. " f"Could not import {module_name} python package. Please"
f"Please install it with `pip install {pip_name or module_name}`." " install it with `pip install"
f" {pip_name or module_name}`."
) )
return module return module
@ -134,25 +141,33 @@ def check_package_version(
) -> None: ) -> None:
"""Check the version of a package.""" """Check the version of a package."""
imported_version = parse(version(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( raise ValueError(
f"Expected {package} version to be < {lt_version}. Received " f"Expected {package} version to be < {lt_version}."
f"{imported_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( raise ValueError(
f"Expected {package} version to be <= {lte_version}. Received " f"Expected {package} version to be <= {lte_version}."
f"{imported_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( raise ValueError(
f"Expected {package} version to be > {gt_version}. Received " f"Expected {package} version to be > {gt_version}."
f"{imported_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( raise ValueError(
f"Expected {package} version to be >= {gte_version}. Received " f"Expected {package} version to be >= {gte_version}."
f"{imported_version}." f" Received {imported_version}."
) )
@ -185,9 +200,11 @@ def build_extra_kwargs(
if field_name in extra_kwargs: if field_name in extra_kwargs:
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: 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. {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) extra_kwargs[field_name] = values.pop(field_name)
invalid_model_kwargs = all_required_field_names.intersection( invalid_model_kwargs = all_required_field_names.intersection(
@ -195,8 +212,9 @@ def build_extra_kwargs(
) )
if invalid_model_kwargs: if invalid_model_kwargs:
raise ValueError( raise ValueError(
f"Parameters {invalid_model_kwargs} should be specified explicitly." f"Parameters {invalid_model_kwargs} should be specified"
" Instead they were passed in as part of `model_kwargs` parameter." " explicitly. Instead they were passed in as part of"
" `model_kwargs` parameter."
) )
return extra_kwargs return extra_kwargs
@ -273,12 +291,16 @@ class _AnthropicCommon(BaseLanguageModel):
check_package_version("anthropic", gte_version="0.3") check_package_version("anthropic", gte_version="0.3")
values["client"] = anthropic.Anthropic( values["client"] = anthropic.Anthropic(
base_url=values["anthropic_api_url"], 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"], timeout=values["default_request_timeout"],
) )
values["async_client"] = anthropic.AsyncAnthropic( values["async_client"] = anthropic.AsyncAnthropic(
base_url=values["anthropic_api_url"], 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"], timeout=values["default_request_timeout"],
) )
values["HUMAN_PROMPT"] = anthropic.HUMAN_PROMPT values["HUMAN_PROMPT"] = anthropic.HUMAN_PROMPT
@ -316,7 +338,9 @@ class _AnthropicCommon(BaseLanguageModel):
self, stop: Optional[List[str]] = None self, stop: Optional[List[str]] = None
) -> List[str]: ) -> List[str]:
if not self.HUMAN_PROMPT or not self.AI_PROMPT: 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: if stop is None:
stop = [] stop = []
@ -375,7 +399,9 @@ class Anthropic(LLM, _AnthropicCommon):
def _wrap_prompt(self, prompt: str) -> str: def _wrap_prompt(self, prompt: str) -> str:
if not self.HUMAN_PROMPT or not self.AI_PROMPT: 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): if prompt.startswith(self.HUMAN_PROMPT):
return prompt # Already wrapped. return prompt # Already wrapped.
@ -389,7 +415,8 @@ class Anthropic(LLM, _AnthropicCommon):
# As a last resort, wrap the prompt ourselves to emulate instruct-style. # As a last resort, wrap the prompt ourselves to emulate instruct-style.
return ( 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( def _call(
@ -419,7 +446,10 @@ class Anthropic(LLM, _AnthropicCommon):
if self.streaming: if self.streaming:
completion = "" completion = ""
for chunk in self._stream( 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 completion += chunk.text
return completion return completion
@ -447,7 +477,10 @@ class Anthropic(LLM, _AnthropicCommon):
if self.streaming: if self.streaming:
completion = "" completion = ""
async for chunk in self._astream( 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 completion += chunk.text
return completion return completion
@ -533,10 +566,14 @@ class Anthropic(LLM, _AnthropicCommon):
chunk = GenerationChunk(text=token.completion) chunk = GenerationChunk(text=token.completion)
yield chunk yield chunk
if run_manager: 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: def get_num_tokens(self, text: str) -> int:
"""Calculate number of tokens.""" """Calculate number of tokens."""
if not self.count_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) return self.count_tokens(text)

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

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

@ -112,11 +112,12 @@ class BioClip:
template: str = "this is a photo of ", template: str = "this is a photo of ",
context_length: int = 256, context_length: int = 256,
): ):
image = torch.stack([self.preprocess_val(Image.open(img_path))]).to( image = torch.stack(
self.device [self.preprocess_val(Image.open(img_path))]
) ).to(self.device)
texts = self.tokenizer( 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) ).to(self.device)
with torch.no_grad(): with torch.no_grad():
@ -128,7 +129,9 @@ class BioClip:
.detach() .detach()
.softmax(dim=-1) .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() logits = logits.cpu().numpy()
sorted_indices = sorted_indices.cpu().numpy() sorted_indices = sorted_indices.cpu().numpy()
@ -149,7 +152,10 @@ class BioClip:
metadata["filename"] metadata["filename"]
+ "\n" + "\n"
+ "\n".join( + "\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) ax.set_title(title, fontsize=14)

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

@ -32,7 +32,9 @@ def _create_retry_decorator(llm) -> Callable[[Any], Any]:
return retry( return retry(
reraise=True, reraise=True,
stop=stop_after_attempt(llm.max_retries), 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)), retry=(retry_if_exception_type(cohere.error.CohereError)),
before_sleep=before_sleep_log(logger, logging.WARNING), before_sleep=before_sleep_log(logger, logging.WARNING),
) )
@ -65,7 +67,9 @@ class BaseCohere(Serializable):
client: Any #: :meta private: client: Any #: :meta private:
async_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.""" """Model name to use."""
temperature: float = 0.75 temperature: float = 0.75

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

@ -6,7 +6,11 @@ from typing import Union
import torch import torch
from termcolor import colored 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): 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: if retries <= 0:
raise raise
print( print(
f"Retry after exception: {e}, Attempts remaining:" f"Retry after exception: {e}, Attempts"
f" {retries}" f" remaining: {retries}"
) )
await asyncio.sleep(delay) await asyncio.sleep(delay)
@ -66,7 +70,9 @@ class DistilWhisperModel:
def __init__(self, model_id="distil-whisper/distil-large-v2"): def __init__(self, model_id="distil-whisper/distil-large-v2"):
self.device = "cuda:0" if torch.cuda.is_available() else "cpu" self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
self.torch_dtype = ( 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_id = model_id
self.model = AutoModelForSpeechSeq2Seq.from_pretrained( self.model = AutoModelForSpeechSeq2Seq.from_pretrained(
@ -106,7 +112,9 @@ class DistilWhisperModel:
:return: The transcribed text. :return: The transcribed text.
""" """
loop = asyncio.get_event_loop() 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): def real_time_transcribe(self, audio_file_path, chunk_duration=5):
""" """
@ -130,13 +138,21 @@ class DistilWhisperModel:
sample_rate = audio_input.sampling_rate sample_rate = audio_input.sampling_rate
len(audio_input.array) / sample_rate len(audio_input.array) / sample_rate
chunks = [ chunks = [
audio_input.array[i : i + sample_rate * chunk_duration] audio_input.array[
i : i + sample_rate * chunk_duration
]
for i in range( 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): for i, chunk in enumerate(chunks):
# Process the current chunk # Process the current chunk
@ -146,8 +162,8 @@ class DistilWhisperModel:
return_tensors="pt", return_tensors="pt",
padding=True, padding=True,
) )
processed_inputs = processed_inputs.input_values.to( processed_inputs = (
self.device processed_inputs.input_values.to(self.device)
) )
# Generate transcription for the chunk # Generate transcription for the chunk
@ -158,7 +174,9 @@ class DistilWhisperModel:
# Print the chunk's transcription # Print the chunk's transcription
print( print(
colored(f"Chunk {i+1}/{len(chunks)}: ", "yellow") colored(
f"Chunk {i+1}/{len(chunks)}: ", "yellow"
)
+ transcription + transcription
) )
@ -167,5 +185,8 @@ class DistilWhisperModel:
except Exception as e: except Exception as e:
print( 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 import elevenlabs
except ImportError as e: except ImportError as e:
raise ImportError( raise ImportError(
"Cannot import elevenlabs, please install `pip install elevenlabs`." "Cannot import elevenlabs, please install `pip install"
" elevenlabs`."
) from e ) from e
return elevenlabs return elevenlabs
@ -52,16 +53,18 @@ class ElevenLabsText2SpeechTool(BaseTool):
name: str = "eleven_labs_text2speech" name: str = "eleven_labs_text2speech"
description: str = ( description: str = (
"A wrapper around Eleven Labs Text2Speech. " "A wrapper around Eleven Labs Text2Speech. Useful for when"
"Useful for when you need to convert text to speech. " " you need to convert text to speech. It supports multiple"
"It supports multiple languages, including English, German, Polish, " " languages, including English, German, Polish, Spanish,"
"Spanish, Italian, French, Portuguese, and Hindi. " " Italian, French, Portuguese, and Hindi. "
) )
@root_validator(pre=True) @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key exists in environment.""" """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 return values
@ -102,7 +105,9 @@ class ElevenLabsText2SpeechTool(BaseTool):
def save(self, speech_file: str, path: str) -> None: def save(self, speech_file: str, path: str) -> None:
"""Save the speech file to a path.""" """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): def __str__(self):
return "ElevenLabsText2SpeechTool" return "ElevenLabsText2SpeechTool"

@ -14,7 +14,9 @@ class Embeddings(ABC):
def embed_query(self, text: str) -> List[float]: def embed_query(self, text: str) -> List[float]:
"""Embed query text.""" """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.""" """Embed search docs."""
raise NotImplementedError raise NotImplementedError

@ -83,4 +83,6 @@ class FastViT:
top_classes = top_classes.cpu().numpy().tolist() 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 # 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.device_map = device_map
self.max_new_tokens = max_new_tokens 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.image_processor = FuyuImageProcessor()
self.processor = FuyuProcessor( self.processor = FuyuProcessor(
image_processor=self.image_processor, image_processor=self.image_processor,
@ -87,5 +89,7 @@ class Fuyu:
image_pil = Image.open(BytesIO(response.content)) image_pil = Image.open(BytesIO(response.content))
return image_pil return image_pil
except requests.RequestException as error: 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 return None

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

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

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

@ -100,10 +100,14 @@ class Idefics:
""" """
inputs = ( inputs = (
self.processor( 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) ).to(self.device)
if batched_mode 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( exit_condition = self.processor.tokenizer(
@ -111,7 +115,8 @@ class Idefics:
).input_ids ).input_ids
bad_words_ids = self.processor.tokenizer( 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 ).input_ids
generated_ids = self.model.generate( generated_ids = self.model.generate(
@ -145,10 +150,14 @@ class Idefics:
""" """
inputs = ( inputs = (
self.processor( 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) ).to(self.device)
if batched_mode 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( exit_condition = self.processor.tokenizer(
@ -156,7 +165,8 @@ class Idefics:
).input_ids ).input_ids
bad_words_ids = self.processor.tokenizer( 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 ).input_ids
generated_ids = self.model.generate( generated_ids = self.model.generate(

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

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

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

@ -170,7 +170,9 @@ class LlamaFunctionCaller:
prompt = f"{task}\n\n" prompt = f"{task}\n\n"
# Encode and send to the model # 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) streamer = TextStreamer(self.tokenizer)

@ -70,7 +70,10 @@ class MultiModalLlava:
def chat(self): def chat(self):
"""Interactive chat in terminal""" """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: while True:
user_input = input("You: ") user_input = input("You: ")
if user_input.lower() == "exit": if user_input.lower() == "exit":

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

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

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

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

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

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

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

@ -38,6 +38,8 @@ class Petals:
def __call__(self, prompt): def __call__(self, prompt):
"""Generate text using the Petals API.""" """Generate text using the Petals API."""
params = self._default_params() 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) outputs = self.model.generate(inputs, **params)
return self.tokenizer.decode(outputs[0]) return self.tokenizer.decode(outputs[0])

@ -1,7 +1,12 @@
import cv2 import cv2
import numpy as np import numpy as np
from PIL import Image from PIL import Image
from transformers import SamImageProcessor, SamModel, SamProcessor, pipeline from transformers import (
SamImageProcessor,
SamModel,
SamProcessor,
pipeline,
)
try: try:
import cv2 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): if np.any(masks.sum(axis=(1, 2)) == 0):
raise ValueError( raise ValueError(
"One or more masks are empty. Please filter out empty masks before" "One or more masks are empty. Please filter out empty"
" using `compute_iou_vectorized` function." " masks before using `compute_iou_vectorized` function."
) )
masks_bool = masks.astype(bool) masks_bool = masks.astype(bool)
masks_flat = masks_bool.reshape(masks.shape[0], -1) masks_flat = masks_bool.reshape(masks.shape[0], -1)
intersection = np.logical_and(masks_flat[:, None], masks_flat[None, :]).sum( intersection = np.logical_and(
axis=2 masks_flat[:, None], masks_flat[None, :]
) ).sum(axis=2)
union = np.logical_or(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 iou_matrix = intersection / union
return iou_matrix return iou_matrix
@ -96,7 +103,9 @@ def mask_non_max_suppression(
def filter_masks_by_relative_area( 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: ) -> np.ndarray:
""" """
Filters masks based on their relative area within the total area of each mask. 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): if not (0 <= minimum_area <= 1) or not (0 <= maximum_area <= 1):
raise ValueError( 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: if minimum_area > maximum_area:
raise ValueError( 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] total_area = masks.shape[1] * masks.shape[2]
relative_areas = masks.sum(axis=(1, 2)) / total_area relative_areas = masks.sum(axis=(1, 2)) / total_area
return masks[ 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 if feature_type == FeatureType.ISLAND
else cv2.RETR_CCOMP 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: for contour in contours:
area = cv2.contourArea(contour) area = cv2.contourArea(contour)
@ -180,7 +194,9 @@ def adjust_mask_features_by_relative_area(
image=mask, image=mask,
contours=[contour], contours=[contour],
contourIdx=-1, contourIdx=-1,
color=(0 if feature_type == FeatureType.ISLAND else 255), color=(
0 if feature_type == FeatureType.ISLAND else 255
),
thickness=-1, thickness=-1,
) )
return np.where(mask > 0, 1, 0).astype(bool) 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 sv.Detections: An object containing the masks and their bounding box
coordinates. 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( def refine_marks(
@ -262,11 +280,15 @@ class SegmentAnythingMarkGenerator:
""" """
def __init__( 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.model = SamModel.from_pretrained(model_name).to(device)
self.processor = SamProcessor.from_pretrained(model_name) 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( self.pipeline = pipeline(
task="mask-generation", task="mask-generation",
model=self.model, model=self.model,
@ -285,7 +307,9 @@ class SegmentAnythingMarkGenerator:
sv.Detections: An object containing the segmentation masks and their sv.Detections: An object containing the segmentation masks and their
corresponding bounding box coordinates. 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) outputs = self.pipeline(image, points_per_batch=64)
masks = np.array(outputs["masks"]) masks = np.array(outputs["masks"])
return masks_to_marks(masks=masks) return masks_to_marks(masks=masks)

@ -4,7 +4,9 @@ from openai import OpenAI
client = 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 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", " ") text = text.replace("\n", " ")
return client.embeddings.create(input=[text], model=model)["data"][0][ return client.embeddings.create(input=[text], model=model)[
"embedding" "data"
] ][0]["embedding"]

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

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

@ -34,7 +34,9 @@ class TimmModel:
"""Retrieve the list of supported models from timm.""" """Retrieve the list of supported models from timm."""
return timm.list_models() 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. Create a model instance from timm with specified parameters.

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

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

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

@ -87,7 +87,9 @@ class Yi34B200k:
top_k=self.top_k, top_k=self.top_k,
top_p=self.top_p, 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 # # Example usage

@ -68,7 +68,9 @@ class Zephyr:
tokenize=self.tokenize, tokenize=self.tokenize,
add_generation_prompt=self.add_generation_prompt, 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"]) print(outputs[0]["generated_text"])
def chat(self, message: str): 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.finance_agent_prompt import FINANCE_AGENT_PROMPT
from swarms.prompts.growth_agent_prompt import GROWTH_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.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 from swarms.prompts.product_agent_prompt import PRODUCT_AGENT_PROMPT

@ -25,7 +25,9 @@ class AgentOutputParser(BaseAgentOutputParser):
@staticmethod @staticmethod
def _preprocess_json_input(input_str: str) -> str: def _preprocess_json_input(input_str: str) -> str:
corrected_str = re.sub( 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 return corrected_str

@ -16,12 +16,16 @@ class PromptGenerator:
"text": "thought", "text": "thought",
"reasoning": "reasoning", "reasoning": "reasoning",
"plan": ( "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", "criticism": "constructive self-criticism",
"speak": "thoughts summary to say to user", "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: def add_constraint(self, constraint: str) -> None:
@ -66,13 +70,16 @@ class PromptGenerator:
Returns: Returns:
str: The generated prompt string. 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 = ( prompt_string = (
f"Constraints:\n{''.join(self.constraints)}\n\nCommands:\n{''.join(self.commands)}\n\nResources:\n{''.join(self.resources)}\n\nPerformance" 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" f" Evaluation:\n{''.join(self.performance_evaluation)}\n\nYou"
" should only respond in JSON format as described below \nResponse" " should only respond in JSON format as described below"
f" Format: \n{formatted_response_format} \nEnsure the response can" " \nResponse Format:"
" be parsed by Python json.loads" f" \n{formatted_response_format} \nEnsure the response"
" can be parsed by Python json.loads"
) )
return prompt_string return prompt_string

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

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

@ -10,10 +10,14 @@ class Message:
Messages are the inputs and outputs of ChatModels. 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.content = content
self.role = role self.role = role
self.additional_kwargs = additional_kwargs if additional_kwargs else {} self.additional_kwargs = (
additional_kwargs if additional_kwargs else {}
)
@abstractmethod @abstractmethod
def get_type(self) -> str: def get_type(self) -> str:
@ -65,7 +69,10 @@ class SystemMessage(Message):
""" """
def __init__( 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) 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). 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) super().__init__(content, role, additional_kwargs)
def get_type(self) -> str: def get_type(self) -> str:
@ -112,7 +121,10 @@ def get_buffer_string(
string_messages = [] string_messages = []
for m in messages: for m in messages:
message = f"{m.role}: {m.content}" 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']}" message += f"{m.additional_kwargs['function_call']}"
string_messages.append(message) string_messages.append(message)

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

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

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

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

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

@ -87,5 +87,7 @@ class BaseDocumentTransformer(ABC):
A list of transformed Documents. A list of transformed Documents.
""" """
return await asyncio.get_running_loop().run_in_executor( 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 # Add a prompt to notify the Agent of the sequential workflow
if "prompt" in self.kwargs: if "prompt" in self.kwargs:
self.kwargs["prompt"] += ( self.kwargs["prompt"] += (
f"\n\nPrevious output: {self.result}" if self.result else "" f"\n\nPrevious output: {self.result}"
if self.result
else ""
) )
else: else:
self.kwargs["prompt"] = f"Main task: {self.description}" + ( self.kwargs["prompt"] = (
f"\n\nPrevious output: {self.result}" if self.result else "" f"Main task: {self.description}"
+ (
f"\n\nPrevious output: {self.result}"
if self.result
else ""
)
) )
self.result = self.agent.run(*self.args, **self.kwargs) self.result = self.agent.run(*self.args, **self.kwargs)
else: else:
@ -116,7 +123,9 @@ class SequentialWorkflow:
autosave: bool = False autosave: bool = False
name: str = (None,) name: str = (None,)
description: 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 restore_state_filepath: Optional[str] = None
dashboard: bool = False dashboard: bool = False
@ -181,7 +190,9 @@ class SequentialWorkflow:
def remove_task(self, task: str) -> None: def remove_task(self, task: str) -> None:
"""Remove tasks from sequential workflow""" """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: def update_task(self, task: str, **updates) -> None:
""" """
@ -330,7 +341,9 @@ class SequentialWorkflow:
) )
self.tasks.append(task) 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. 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 # Ensure that 'task' is provided in the kwargs
if "task" not in task.kwargs: if "task" not in task.kwargs:
raise ValueError( raise ValueError(
"The 'task' argument is required for the" "The 'task' argument is required"
" Agent agent execution in" " for the Agent agent execution"
f" '{task.description}'" f" in '{task.description}'"
) )
# Separate the 'task' argument from other kwargs # Separate the 'task' argument from other kwargs
flow_task_arg = task.kwargs.pop("task") flow_task_arg = task.kwargs.pop("task")
task.result = task.agent.run( task.result = task.agent.run(
flow_task_arg, *task.args, **task.kwargs flow_task_arg,
*task.args,
**task.kwargs,
) )
else: else:
# If it's not a Agent instance, call the agent directly # 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 # Pass the result as an argument to the next task if it exists
next_task_index = self.tasks.index(task) + 1 next_task_index = self.tasks.index(task) + 1
@ -417,9 +434,9 @@ class SequentialWorkflow:
print( print(
colored( colored(
( (
f"Error initializing the Sequential workflow: {e} try" "Error initializing the Sequential workflow:"
" optimizing your inputs like the agent class and task" f" {e} try optimizing your inputs like the"
" description" " agent class and task description"
), ),
"red", "red",
attrs=["bold", "underline"], attrs=["bold", "underline"],
@ -443,8 +460,9 @@ class SequentialWorkflow:
# Ensure that 'task' is provided in the kwargs # Ensure that 'task' is provided in the kwargs
if "task" not in task.kwargs: if "task" not in task.kwargs:
raise ValueError( raise ValueError(
"The 'task' argument is required for the Agent" "The 'task' argument is required for"
f" agent execution in '{task.description}'" " the Agent agent execution in"
f" '{task.description}'"
) )
# Separate the 'task' argument from other kwargs # Separate the 'task' argument from other kwargs
flow_task_arg = task.kwargs.pop("task") flow_task_arg = task.kwargs.pop("task")

@ -144,7 +144,9 @@ class AbstractSwarm(ABC):
pass pass
@abstractmethod @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""" """Autoscaler that acts like kubernetes for autonomous agents"""
pass pass
@ -159,7 +161,9 @@ class AbstractSwarm(ABC):
pass pass
@abstractmethod @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""" """Assign a task to a worker"""
pass pass

@ -24,7 +24,10 @@ class DialogueSimulator:
""" """
def __init__( 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.agents = agents
self.max_iters = max_iters self.max_iters = max_iters
@ -60,7 +63,8 @@ class DialogueSimulator:
def __repr__(self): def __repr__(self):
return ( return (
f"DialogueSimulator({self.agents}, {self.max_iters}, {self.name})" f"DialogueSimulator({self.agents}, {self.max_iters},"
f" {self.name})"
) )
def save_state(self): def save_state(self):

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

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

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