docs fix for the swarmns api fix

pull/925/head
Kye Gomez 6 days ago
parent a24cc89cba
commit 2770b8c7bf

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

File diff suppressed because it is too large Load Diff

@ -1,7 +1,4 @@
from swarms import Agent from swarms import Agent
from swarms.prompts.logistics import (
Quality_Control_Agent_Prompt,
)
# Image for analysis # Image for analysis
@ -12,8 +9,8 @@ quality_control_agent = Agent(
agent_name="Quality Control Agent", agent_name="Quality Control Agent",
agent_description="A quality control agent that analyzes images and provides a detailed report on the quality of the product in the image.", agent_description="A quality control agent that analyzes images and provides a detailed report on the quality of the product in the image.",
model_name="claude-3-5-sonnet-20240620", model_name="claude-3-5-sonnet-20240620",
system_prompt=Quality_Control_Agent_Prompt, # system_prompt=Quality_Control_Agent_Prompt,
multi_modal=True, # multi_modal=True,
max_loops=1, max_loops=1,
output_type="str-all-except-first", output_type="str-all-except-first",
summarize_multiple_images=True, summarize_multiple_images=True,
@ -22,7 +19,7 @@ quality_control_agent = Agent(
response = quality_control_agent.run( response = quality_control_agent.run(
task="what is in the image?", task="what is in the image?",
imgs=[factory_image, factory_image], imgs=[factory_image, "burning_image.jpg"],
) )
print(response) print(response)

@ -985,7 +985,7 @@ class Agent:
self.short_memory.add(role=self.user_name, content=task) self.short_memory.add(role=self.user_name, content=task)
if self.plan_enabled or self.planning_prompt is not None: if self.plan_enabled is True:
self.plan(task) self.plan(task)
# Set the loop count # Set the loop count
@ -1360,9 +1360,14 @@ class Agent:
# Get the current conversation history # Get the current conversation history
history = self.short_memory.get_str() history = self.short_memory.get_str()
plan_prompt = f"Create a comprehensive step-by-step plan to complete the following task: \n\n {task}"
# Construct the planning prompt by combining history, planning prompt, and task # Construct the planning prompt by combining history, planning prompt, and task
if exists(self.planning_prompt):
planning_prompt = f"{history}\n\n{self.planning_prompt}\n\nTask: {task}"
else:
planning_prompt = ( planning_prompt = (
f"{history}\n\n{self.planning_prompt}\n\nTask: {task}" f"{history}\n\n{plan_prompt}\n\nTask: {task}"
) )
# Generate the plan using the LLM # Generate the plan using the LLM
@ -1371,9 +1376,6 @@ class Agent:
# Store the generated plan in short-term memory # Store the generated plan in short-term memory
self.short_memory.add(role=self.agent_name, content=plan) self.short_memory.add(role=self.agent_name, content=plan)
logger.info(
f"Successfully created plan for task: {task[:50]}..."
)
return None return None
except Exception as error: except Exception as error:
@ -2501,6 +2503,7 @@ class Agent:
task: Optional[Union[str, Any]] = None, task: Optional[Union[str, Any]] = None,
img: Optional[str] = None, img: Optional[str] = None,
imgs: Optional[List[str]] = None, imgs: Optional[List[str]] = None,
correct_answer: Optional[str] = None,
*args, *args,
**kwargs, **kwargs,
) -> Any: ) -> Any:
@ -2534,6 +2537,14 @@ class Agent:
output = self.run_multiple_images( output = self.run_multiple_images(
task=task, imgs=imgs, *args, **kwargs task=task, imgs=imgs, *args, **kwargs
) )
elif exists(correct_answer):
output = self.continuous_run_with_answer(
task=task,
img=img,
correct_answer=correct_answer,
*args,
**kwargs,
)
else: else:
output = self._run( output = self._run(
task=task, task=task,
@ -2909,7 +2920,7 @@ class Agent:
self, task: str, imgs: List[str], *args, **kwargs self, task: str, imgs: List[str], *args, **kwargs
): ):
""" """
Run the agent with multiple images. Run the agent with multiple images using concurrent processing.
Args: Args:
task (str): The task to be performed on each image. task (str): The task to be performed on each image.
@ -2932,12 +2943,33 @@ class Agent:
Raises: Raises:
Exception: If an error occurs while processing any of the images. Exception: If an error occurs while processing any of the images.
""" """
# Calculate number of workers as 95% of available CPU cores
cpu_count = os.cpu_count()
max_workers = max(1, int(cpu_count * 0.95))
# Use ThreadPoolExecutor for concurrent processing
with ThreadPoolExecutor(max_workers=max_workers) as executor:
# Submit all image processing tasks
future_to_img = {
executor.submit(
self.run, task=task, img=img, *args, **kwargs
): img
for img in imgs
}
# Collect results in order
outputs = [] outputs = []
for img in imgs: for future in future_to_img:
output = self.run(task=task, img=img, *args, **kwargs) try:
output = future.result()
outputs.append(output) outputs.append(output)
except Exception as e:
logger.error(f"Error processing image: {e}")
outputs.append(
None
) # or raise the exception based on your preference
# Combine the outputs into a single string # Combine the outputs into a single string if summarization is enabled
if self.summarize_multiple_images is True: if self.summarize_multiple_images is True:
output = "\n".join(outputs) output = "\n".join(outputs)
@ -2959,3 +2991,58 @@ class Agent:
outputs = self.run(task=prompt, *args, **kwargs) outputs = self.run(task=prompt, *args, **kwargs)
return outputs return outputs
def continuous_run_with_answer(
self,
task: str,
img: Optional[str] = None,
correct_answer: str = None,
max_attempts: int = 10,
):
"""
Run the agent with the task until the correct answer is provided.
Args:
task (str): The task to be performed
correct_answer (str): The correct answer that must be found in the response
max_attempts (int): Maximum number of attempts before giving up (default: 10)
Returns:
str: The response containing the correct answer
Raises:
Exception: If max_attempts is reached without finding the correct answer
"""
attempts = 0
while attempts < max_attempts:
attempts += 1
if self.verbose:
logger.info(
f"Attempt {attempts}/{max_attempts} to find correct answer"
)
response = self._run(task=task, img=img)
# Check if the correct answer is in the response (case-insensitive)
if correct_answer.lower() in response.lower():
if self.verbose:
logger.info(
f"Correct answer found on attempt {attempts}"
)
return response
else:
# Add feedback to help guide the agent
feedback = "Your previous response was incorrect. Think carefully about the question and ensure your response directly addresses what was asked."
self.short_memory.add(role="User", content=feedback)
if self.verbose:
logger.info(
f"Correct answer not found. Expected: '{correct_answer}'"
)
# If we reach here, we've exceeded max_attempts
raise Exception(
f"Failed to find correct answer '{correct_answer}' after {max_attempts} attempts"
)

@ -0,0 +1,68 @@
from swarms.structs import Agent
from swarms.prompts.logistics import (
Quality_Control_Agent_Prompt,
)
# Image for analysis
factory_image = "image.jpg"
def security_analysis(danger_level: str) -> str:
"""
Analyzes the security danger level and returns an appropriate response.
Args:
danger_level (str, optional): The level of danger to analyze.
Can be "low", "medium", "high", or None. Defaults to None.
Returns:
str: A string describing the danger level assessment.
- "No danger level provided" if danger_level is None
- "No danger" if danger_level is "low"
- "Medium danger" if danger_level is "medium"
- "High danger" if danger_level is "high"
- "Unknown danger level" for any other value
"""
if danger_level is None:
return "No danger level provided"
if danger_level == "low":
return "No danger"
if danger_level == "medium":
return "Medium danger"
if danger_level == "high":
return "High danger"
return "Unknown danger level"
custom_system_prompt = f"""
{Quality_Control_Agent_Prompt}
You have access to tools that can help you with your analysis. When you need to perform a security analysis, you MUST use the security_analysis function with an appropriate danger level (low, medium, or high) based on your observations.
Always use the available tools when they are relevant to the task. If you determine there is any level of danger or security concern, call the security_analysis function with the appropriate danger level.
"""
# Quality control agent
quality_control_agent = Agent(
agent_name="Quality Control Agent",
agent_description="A quality control agent that analyzes images and provides a detailed report on the quality of the product in the image.",
# model_name="anthropic/claude-3-opus-20240229",
model_name="gpt-4o-mini",
system_prompt=custom_system_prompt,
multi_modal=True,
max_loops=1,
output_type="str-all-except-first",
# tools_list_dictionary=[schema],
tools=[security_analysis],
)
response = quality_control_agent.run(
task="Analyze the image and then perform a security analysis. Based on what you see in the image, determine if there is a low, medium, or high danger level and call the security_analysis function with that danger level",
img=factory_image,
)
Loading…
Cancel
Save