Skip to content

Commit 8eea8ce

Browse files
authored
Add tools and schema to example (#13)
1 parent 30c7df1 commit 8eea8ce

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
```mermaid
2+
flowchart LR
3+
in((In))
4+
out((Out))
5+
llm_1[LLM 1]
6+
gate{Gate}
7+
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
16+
gate -->|default| out
17+
llm_2 --> parallel_workflow_aggregator
18+
parallel_workflow_aggregator --> out
19+
llm_3 --> parallel_workflow_aggregator
20+
llm_4 --> parallel_workflow_aggregator
21+
```

examples/complex_llm_workflow/generator.rb

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,62 @@
77
config.openai_api_key = ENV.fetch("OPENAI_API_KEY", nil)
88
end
99

10+
# Define schema for sports
11+
class SportsSchema < RubyLLM::Schema
12+
array :sports do
13+
object do
14+
string :name
15+
array :top_3_players do
16+
string :name
17+
end
18+
end
19+
end
20+
end
21+
22+
sports_schema = SportsSchema.new
23+
24+
# Define weather tool
25+
class Weather < RubyLLM::Tool
26+
description "Gets current weather for a location"
27+
28+
params do
29+
string :latitude, description: "Latitude (e.g., 52.5200)"
30+
string :longitude, description: "Longitude (e.g., 13.4050)"
31+
end
32+
33+
def execute(latitude:, longitude:)
34+
url = "https://api.open-meteo.com/v1/forecast?latitude=#{latitude}&longitude=#{longitude}&current=temperature_2m,wind_speed_10m"
35+
response = Net::HTTP.get_response(URI(url))
36+
JSON.parse(response.body)
37+
rescue StandardError => e
38+
{ error: e.message }
39+
end
40+
end
41+
42+
weather_tool = Weather.new
43+
1044
# Create the LLMs
1145
llm1 = Mars::Agent.new(
1246
name: "LLM 1", options: { model: "gpt-4o" },
13-
instructions: "You are a helpful assistant that can answer questions and help with tasks. Only answer with the result"
47+
instructions: "You are a helpful assistant that can answer questions.
48+
When asked about a country, only answer with its name."
1449
)
1550

1651
llm2 = Mars::Agent.new(name: "LLM 2", options: { model: "gpt-4o" },
1752
instructions: "You are a helpful assistant that can answer questions and help with tasks.
1853
Return information about the typical food of the country.")
1954

20-
llm3 = Mars::Agent.new(name: "LLM 3", options: { model: "gpt-4o" },
55+
llm3 = Mars::Agent.new(name: "LLM 3", options: { model: "gpt-4o" }, schema: sports_schema,
2156
instructions: "You are a helpful assistant that can answer questions and help with tasks.
2257
Return information about the popular sports of the country.")
2358

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.")
62+
2463
parallel_workflow = Mars::Workflows::Parallel.new(
2564
"Parallel workflow",
26-
steps: [llm2, llm3]
65+
steps: [llm2, llm3, llm4]
2766
)
2867

2968
gate = Mars::Gate.new(
@@ -39,4 +78,10 @@
3978
steps: [llm1, gate]
4079
)
4180

42-
puts sequential_workflow.run("Which is the last country to declare independence?")
81+
# Generate and save the diagram
82+
diagram = Mars::Rendering::Mermaid.new(sequential_workflow).render
83+
File.write("examples/complex_llm_workflow/diagram.md", diagram)
84+
puts "Complex workflow diagram saved to: examples/complex_llm_workflow/diagram.md"
85+
86+
# Run the workflow
87+
puts sequential_workflow.run("Which is the largest country in South America?")

lib/mars.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "zeitwerk"
44
require "async"
55
require "ruby_llm"
6+
require "ruby_llm/schema"
67

78
loader = Zeitwerk::Loader.for_gem
89
loader.setup

mars.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Gem::Specification.new do |spec|
3636

3737
# Uncomment to register a new dependency of your gem
3838
spec.add_dependency "async", "~> 2.34"
39-
spec.add_dependency "ruby_llm", "~> 1.0"
39+
spec.add_dependency "ruby_llm", "~> 1.9"
4040
spec.add_dependency "zeitwerk", "~> 2.7"
4141

4242
# For more information and examples about making a new gem, check out our

0 commit comments

Comments
 (0)