parent
871bc77713
commit
a0075c3690
@ -1,51 +1,44 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""
|
|
||||||
Basic Graph Workflow Example
|
|
||||||
|
|
||||||
A minimal example showing how to use GraphWorkflow with backend selection.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from swarms.structs.graph_workflow import GraphWorkflow
|
from swarms.structs.graph_workflow import GraphWorkflow
|
||||||
from swarms.structs.agent import Agent
|
from swarms.structs.agent import Agent
|
||||||
|
|
||||||
agent_one = Agent(agent_name="research_agent", model="gpt-4o-mini")
|
agent_one = Agent(
|
||||||
|
agent_name="research_agent",
|
||||||
|
model_name="gpt-4o-mini",
|
||||||
|
name="Research Agent",
|
||||||
|
agent_description="Agent responsible for gathering and summarizing research information."
|
||||||
|
)
|
||||||
agent_two = Agent(
|
agent_two = Agent(
|
||||||
agent_name="research_agent_two", model="gpt-4o-mini"
|
agent_name="research_agent_two",
|
||||||
|
model_name="gpt-4o-mini",
|
||||||
|
name="Analysis Agent",
|
||||||
|
agent_description="Agent that analyzes the research data provided and processes insights."
|
||||||
)
|
)
|
||||||
agent_three = Agent(
|
agent_three = Agent(
|
||||||
agent_name="research_agent_three", model="gpt-4o-mini"
|
agent_name="research_agent_three",
|
||||||
|
model_name="gpt-4o-mini",
|
||||||
|
agent_description="Agent tasked with structuring analysis into a final report or output."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
"""
|
|
||||||
Run a basic graph workflow example without print statements.
|
|
||||||
"""
|
|
||||||
# Create agents
|
|
||||||
|
|
||||||
# Create workflow with backend selection
|
# Create workflow with backend selection
|
||||||
workflow = GraphWorkflow(
|
workflow = GraphWorkflow(
|
||||||
name="Basic Example",
|
name="Basic Example",
|
||||||
verbose=True,
|
verbose=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add agents to workflow
|
workflow.add_nodes([agent_one, agent_two, agent_three])
|
||||||
workflow.add_node(agent_one)
|
|
||||||
workflow.add_node(agent_two)
|
|
||||||
workflow.add_node(agent_three)
|
|
||||||
|
|
||||||
# Create simple chain using the actual agent names
|
# Create simple chain using the actual agent names
|
||||||
workflow.add_edge("research_agent", "research_agent_two")
|
workflow.add_edge("research_agent", "research_agent_two")
|
||||||
workflow.add_edge("research_agent_two", "research_agent_three")
|
workflow.add_edge("research_agent_two", "research_agent_three")
|
||||||
|
|
||||||
|
workflow.visualize()
|
||||||
|
|
||||||
# Compile the workflow
|
# Compile the workflow
|
||||||
workflow.compile()
|
workflow.compile()
|
||||||
|
|
||||||
# Run the workflow
|
# Run the workflow
|
||||||
task = "Complete a simple task"
|
task = "Complete a simple task"
|
||||||
results = workflow.run(task)
|
results = workflow.run(task)
|
||||||
return results
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
print(results)
|
||||||
main()
|
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
|
||||||
|
from swarms.structs.graph_workflow import GraphWorkflow
|
||||||
|
from swarms.structs.agent import Agent
|
||||||
|
|
||||||
|
agent_one = Agent(
|
||||||
|
agent_name="research_agent",
|
||||||
|
model_name="claude-haiku-4-5",
|
||||||
|
top_p=None,
|
||||||
|
temperature=None,
|
||||||
|
agent_description="Agent responsible for gathering and summarizing research information."
|
||||||
|
)
|
||||||
|
agent_two = Agent(
|
||||||
|
agent_name="research_agent_two",
|
||||||
|
model_name="claude-haiku-4-5",
|
||||||
|
top_p=None,
|
||||||
|
temperature=None,
|
||||||
|
agent_description="Agent that analyzes the research data provided and processes insights."
|
||||||
|
)
|
||||||
|
agent_three = Agent(
|
||||||
|
agent_name="research_agent_three",
|
||||||
|
model_name="claude-haiku-4-5",
|
||||||
|
top_p=None,
|
||||||
|
temperature=None,
|
||||||
|
agent_description="Agent tasked with structuring analysis into a final report or output."
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create workflow with backend selection
|
||||||
|
workflow = GraphWorkflow(
|
||||||
|
name="Basic Example",
|
||||||
|
verbose=True,
|
||||||
|
backend="rustworkx",
|
||||||
|
)
|
||||||
|
|
||||||
|
agents = [agent_one, agent_two, agent_three]
|
||||||
|
workflow.add_nodes(agents, batch_size=3)
|
||||||
|
|
||||||
|
workflow.add_edge("research_agent", "research_agent_two")
|
||||||
|
workflow.add_edge("research_agent_two", "research_agent_three")
|
||||||
|
workflow.visualize()
|
||||||
|
|
||||||
|
# Compile the workflow
|
||||||
|
workflow.compile()
|
||||||
|
|
||||||
|
# Run the workflow
|
||||||
|
task = "Analyze the best mining companies in the US"
|
||||||
|
results = workflow.run(task)
|
||||||
|
|
||||||
|
print(results)
|
||||||
@ -1,55 +0,0 @@
|
|||||||
import re
|
|
||||||
|
|
||||||
from swarms.structs.maker import MAKER
|
|
||||||
|
|
||||||
|
|
||||||
# Define task-specific functions for a counting task
|
|
||||||
def format_counting_prompt(
|
|
||||||
task, state, step_idx, previous_result
|
|
||||||
):
|
|
||||||
"""Format prompt for counting task."""
|
|
||||||
if previous_result is None:
|
|
||||||
return f"{task}\nThis is step 1. What is the first number? Reply with just the number."
|
|
||||||
return f"{task}\nThe previous number was {previous_result}. What is the next number? Reply with just the number."
|
|
||||||
|
|
||||||
|
|
||||||
def parse_counting_response(response):
|
|
||||||
"""Parse the counting response to extract the number."""
|
|
||||||
numbers = re.findall(r"\d+", response)
|
|
||||||
if numbers:
|
|
||||||
return int(numbers[0])
|
|
||||||
return response.strip()
|
|
||||||
|
|
||||||
|
|
||||||
def validate_counting_response(response, max_tokens):
|
|
||||||
"""Validate counting response."""
|
|
||||||
if len(response) > max_tokens * 4:
|
|
||||||
return False
|
|
||||||
return bool(re.search(r"\d+", response))
|
|
||||||
|
|
||||||
|
|
||||||
# Create MAKER instance
|
|
||||||
maker = MAKER(
|
|
||||||
name="CountingExample",
|
|
||||||
description="MAKER example: counting numbers",
|
|
||||||
model_name="gpt-4o-mini",
|
|
||||||
system_prompt="You are a helpful assistant. When asked to count, respond with just the number, nothing else.",
|
|
||||||
format_prompt=format_counting_prompt,
|
|
||||||
parse_response=parse_counting_response,
|
|
||||||
validate_response=validate_counting_response,
|
|
||||||
k=2,
|
|
||||||
max_tokens=100,
|
|
||||||
temperature=0.1,
|
|
||||||
verbose=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Run the solver with the task as the main input
|
|
||||||
results = maker.run(
|
|
||||||
task="Count from 1 to 10, one number at a time",
|
|
||||||
max_steps=5,
|
|
||||||
)
|
|
||||||
|
|
||||||
print(results)
|
|
||||||
|
|
||||||
# Show statistics
|
|
||||||
stats = maker.get_statistics()
|
|
||||||
Loading…
Reference in new issue