Skip to content

Commit 06a200c

Browse files
authored
Update api (#19)
1 parent e854013 commit 06a200c

File tree

15 files changed

+197
-110
lines changed

15 files changed

+197
-110
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@ RSpec/ExampleLength:
3434
RSpec/MultipleExpectations:
3535
Enabled: false
3636

37+
RSpec/MultipleMemoizedHelpers:
38+
Enabled: false
39+
3740
RSpec/VerifiedDoubleReference:
3841
Enabled: false

README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,20 @@ Here's a simple example to get you started:
4747
```ruby
4848
require 'mars'
4949

50+
# Define agents
51+
class Agent1 < Mars::Agent
52+
end
53+
54+
class Agent2 < Mars::Agent
55+
end
56+
57+
class Agent3 < Mars::Agent
58+
end
59+
5060
# Create agents
51-
agent1 = Mars::Agent.new(name: "Agent 1")
52-
agent2 = Mars::Agent.new(name: "Agent 2")
53-
agent3 = Mars::Agent.new(name: "Agent 3")
61+
agent1 = Agent1.new
62+
agent2 = Agent2.new
63+
agent3 = Agent3.new
5464

5565
# Create a sequential workflow
5666
workflow = Mars::Workflows::Sequential.new(
@@ -69,9 +79,13 @@ result = workflow.run("Your input here")
6979
Agents are the basic building blocks of MARS. They represent individual units of work:
7080

7181
```ruby
72-
agent = Mars::Agent.new(
73-
name: "My Agent",
74-
instructions: "You are a helpful assistant",
82+
class CustomAgent < Mars::Agent
83+
def system_prompt
84+
"You are a helpful assistant"
85+
end
86+
end
87+
88+
agent = CustomAgent.new(
7589
options: { model: "gpt-4o" }
7690
)
7791
```
@@ -110,7 +124,7 @@ Create conditional branching in your workflows:
110124

111125
```ruby
112126
gate = Mars::Gate.new(
113-
name: "Decision Gate",
127+
"Decision Gate",
114128
condition: ->(input) { input[:score] > 0.5 ? :success : :failure },
115129
branches: {
116130
success: success_workflow,

examples/complex_llm_workflow/diagram.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
flowchart LR
33
in((In))
44
out((Out))
5-
llm_1[LLM 1]
5+
agent1[Agent1]
66
gate{Gate}
77
parallel_workflow_aggregator[Parallel workflow Aggregator]
8-
llm_2[LLM 2]
9-
llm_3[LLM 3]
10-
llm_4[LLM 4]
11-
in --> llm_1
12-
llm_1 --> gate
13-
gate -->|success| llm_2
14-
gate -->|success| llm_3
15-
gate -->|success| llm_4
8+
agent2[Agent2]
9+
agent3[Agent3]
10+
agent4[Agent4]
11+
in --> agent1
12+
agent1 --> gate
13+
gate -->|success| agent2
14+
gate -->|success| agent3
15+
gate -->|success| agent4
1616
gate -->|default| out
17-
llm_2 --> parallel_workflow_aggregator
17+
agent2 --> parallel_workflow_aggregator
1818
parallel_workflow_aggregator --> out
19-
llm_3 --> parallel_workflow_aggregator
20-
llm_4 --> parallel_workflow_aggregator
19+
agent3 --> parallel_workflow_aggregator
20+
agent4 --> parallel_workflow_aggregator
2121
```

examples/complex_llm_workflow/generator.rb

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ class SportsSchema < RubyLLM::Schema
1919
end
2020
end
2121

22-
sports_schema = SportsSchema.new
23-
2422
# Define weather tool
2523
class Weather < RubyLLM::Tool
2624
description "Gets current weather for a location"
@@ -39,34 +37,55 @@ def execute(latitude:, longitude:)
3937
end
4038
end
4139

42-
weather_tool = Weather.new
40+
# Define LLMs
41+
class Agent1 < Mars::Agent
42+
def system_prompt
43+
"You are a helpful assistant that can answer questions.
44+
When asked about a country, only answer with its name."
45+
end
46+
end
4347

44-
# Create the LLMs
45-
llm1 = Mars::Agent.new(
46-
name: "LLM 1", options: { model: "gpt-4o" },
47-
instructions: "You are a helpful assistant that can answer questions.
48-
When asked about a country, only answer with its name."
49-
)
48+
class Agent2 < Mars::Agent
49+
def system_prompt
50+
"You are a helpful assistant that can answer questions and help with tasks.
51+
Return information about the typical food of the country."
52+
end
53+
end
54+
55+
class Agent3 < Mars::Agent
56+
def system_prompt
57+
"You are a helpful assistant that can answer questions and help with tasks.
58+
Return information about the popular sports of the country."
59+
end
60+
61+
def schema
62+
SportsSchema.new
63+
end
64+
end
5065

51-
llm2 = Mars::Agent.new(name: "LLM 2", options: { model: "gpt-4o" },
52-
instructions: "You are a helpful assistant that can answer questions and help with tasks.
53-
Return information about the typical food of the country.")
66+
class Agent4 < Mars::Agent
67+
def system_prompt
68+
"You are a helpful assistant that can answer questions and help with tasks.
69+
Return the current weather of the country's capital."
70+
end
5471

55-
llm3 = Mars::Agent.new(name: "LLM 3", options: { model: "gpt-4o" }, schema: sports_schema,
56-
instructions: "You are a helpful assistant that can answer questions and help with tasks.
57-
Return information about the popular sports of the country.")
72+
def tools
73+
[Weather.new]
74+
end
75+
end
5876

59-
llm4 = Mars::Agent.new(name: "LLM 4", options: { model: "gpt-4o" }, tools: [weather_tool],
60-
instructions: "You are a helpful assistant that can answer questions and help with tasks.
61-
Return the current weather of the country's capital.")
77+
# Create the LLMs
78+
llm1 = Agent1.new(options: { model: "gpt-4o" })
79+
llm2 = Agent2.new(options: { model: "gpt-4o" })
80+
llm3 = Agent3.new(options: { model: "gpt-4o" })
81+
llm4 = Agent4.new(options: { model: "gpt-4o" })
6282

6383
parallel_workflow = Mars::Workflows::Parallel.new(
6484
"Parallel workflow",
6585
steps: [llm2, llm3, llm4]
6686
)
6787

6888
gate = Mars::Gate.new(
69-
name: "Gate",
7089
condition: ->(input) { input.split.length < 10 ? :success : :error },
7190
branches: {
7291
success: parallel_workflow
@@ -84,4 +103,4 @@ def execute(latitude:, longitude:)
84103
puts "Complex workflow diagram saved to: examples/complex_llm_workflow/diagram.md"
85104

86105
# Run the workflow
87-
puts sequential_workflow.run("Which is the largest country in South America?")
106+
puts sequential_workflow.run("Which is the largest country in Europe?")

examples/complex_workflow/diagram.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22
flowchart LR
33
in((In))
44
out((Out))
5-
llm_1[LLM 1]
5+
agent1[Agent1]
66
gate{Gate}
77
parallel_workflow_2_aggregator[Parallel workflow 2 Aggregator]
8-
llm_4[LLM 4]
8+
agent4[Agent4]
99
parallel_workflow_aggregator[Parallel workflow Aggregator]
10-
llm_2[LLM 2]
11-
llm_3[LLM 3]
12-
llm_5[LLM 5]
13-
in --> llm_1
14-
llm_1 --> gate
15-
gate -->|success| llm_4
16-
gate -->|success| llm_5
17-
gate -->|warning| llm_4
18-
gate -->|error| llm_2
19-
gate -->|error| llm_3
10+
agent2[Agent2]
11+
agent3[Agent3]
12+
agent5[Agent5]
13+
in --> agent1
14+
agent1 --> gate
15+
gate -->|success| agent4
16+
gate -->|success| agent5
17+
gate -->|warning| agent4
18+
gate -->|error| agent2
19+
gate -->|error| agent3
2020
gate -->|default| out
21-
llm_4 --> llm_2
22-
llm_4 --> llm_3
23-
llm_2 --> parallel_workflow_aggregator
21+
agent4 --> agent2
22+
agent4 --> agent3
23+
agent2 --> parallel_workflow_aggregator
2424
parallel_workflow_aggregator --> parallel_workflow_2_aggregator
2525
parallel_workflow_aggregator --> out
26-
llm_3 --> parallel_workflow_aggregator
26+
agent3 --> parallel_workflow_aggregator
2727
parallel_workflow_2_aggregator --> out
28-
llm_5 --> parallel_workflow_2_aggregator
28+
agent5 --> parallel_workflow_2_aggregator
2929
```

examples/complex_workflow/generator.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,28 @@
33

44
require_relative "../../lib/mars"
55

6-
# Create the LLMs
7-
llm1 = Mars::Agent.new(name: "LLM 1")
6+
# Define LLMs
7+
class Agent1 < Mars::Agent
8+
end
9+
10+
class Agent2 < Mars::Agent
11+
end
812

9-
llm2 = Mars::Agent.new(name: "LLM 2")
13+
class Agent3 < Mars::Agent
14+
end
1015

11-
llm3 = Mars::Agent.new(name: "LLM 3")
16+
class Agent4 < Mars::Agent
17+
end
1218

13-
llm4 = Mars::Agent.new(name: "LLM 4")
19+
class Agent5 < Mars::Agent
20+
end
1421

15-
llm5 = Mars::Agent.new(name: "LLM 5")
22+
# Create the LLMs
23+
llm1 = Agent1.new
24+
llm2 = Agent2.new
25+
llm3 = Agent3.new
26+
llm4 = Agent4.new
27+
llm5 = Agent5.new
1628

1729
# Create a parallel workflow (LLM 2 x LLM 3)
1830
parallel_workflow = Mars::Workflows::Parallel.new(
@@ -34,7 +46,6 @@
3446

3547
# Create the gate that decides between exit or continue
3648
gate = Mars::Gate.new(
37-
name: "Gate",
3849
condition: ->(input) { input[:result] },
3950
branches: {
4051
success: parallel_workflow2,

examples/parallel_workflow/diagram.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ flowchart LR
33
in((In))
44
out((Out))
55
aggregator[Aggregator]
6-
llm_1[LLM 1]
7-
llm_2[LLM 2]
8-
llm_3[LLM 3]
9-
in --> llm_1
10-
in --> llm_2
11-
in --> llm_3
12-
llm_1 --> aggregator
6+
agent1[Agent1]
7+
agent2[Agent2]
8+
agent3[Agent3]
9+
in --> agent1
10+
in --> agent2
11+
in --> agent3
12+
agent1 --> aggregator
1313
aggregator --> out
14-
llm_2 --> aggregator
15-
llm_3 --> aggregator
14+
agent2 --> aggregator
15+
agent3 --> aggregator
1616
```

examples/parallel_workflow/generator.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33

44
require_relative "../../lib/mars"
55

6-
# Create the LLMs
7-
llm1 = Mars::Agent.new(name: "LLM 1")
6+
# Define the LLMs
7+
class Agent1 < Mars::Agent
8+
end
9+
10+
class Agent2 < Mars::Agent
11+
end
812

9-
llm2 = Mars::Agent.new(name: "LLM 2")
13+
class Agent3 < Mars::Agent
14+
end
1015

11-
llm3 = Mars::Agent.new(name: "LLM 3")
16+
# Create the LLMs
17+
llm1 = Agent1.new
18+
llm2 = Agent2.new
19+
llm3 = Agent3.new
1220

1321
aggregator = Mars::Aggregator.new("Aggregator", operation: lambda(&:sum))
1422

examples/simple_workflow/diagram.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
flowchart LR
33
in((In))
44
out((Out))
5-
llm_1[LLM 1]
5+
agent1[Agent1]
66
gate{Gate}
7-
llm_2[LLM 2]
8-
llm_3[LLM 3]
9-
in --> llm_1
10-
llm_1 --> gate
11-
gate -->|success| llm_2
7+
agent2[Agent2]
8+
agent3[Agent3]
9+
in --> agent1
10+
agent1 --> gate
11+
gate -->|success| agent2
1212
gate -->|default| out
13-
llm_2 --> llm_3
14-
llm_3 --> out
13+
agent2 --> agent3
14+
agent3 --> out
1515
```

examples/simple_workflow/generator.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33

44
require_relative "../../lib/mars"
55

6-
# Create the LLMs
7-
llm1 = Mars::Agent.new(name: "LLM 1")
6+
# Define the LLMs
7+
class Agent1 < Mars::Agent
8+
end
9+
10+
class Agent2 < Mars::Agent
11+
end
812

9-
llm2 = Mars::Agent.new(name: "LLM 2")
13+
class Agent3 < Mars::Agent
14+
end
1015

11-
llm3 = Mars::Agent.new(name: "LLM 3")
16+
# Create the LLMs
17+
llm1 = Agent1.new
18+
llm2 = Agent2.new
19+
llm3 = Agent3.new
1220

1321
# Create the success workflow (LLM 2 -> LLM 3)
1422
success_workflow = Mars::Workflows::Sequential.new(
@@ -18,7 +26,6 @@
1826

1927
# Create the gate that decides between exit or continue
2028
gate = Mars::Gate.new(
21-
name: "Gate",
2229
condition: ->(input) { input[:result] },
2330
branches: {
2431
success: success_workflow

0 commit comments

Comments
 (0)