-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.rb
More file actions
executable file
·112 lines (91 loc) · 2.77 KB
/
Copy pathgenerator.rb
File metadata and controls
executable file
·112 lines (91 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative "../../lib/mars"
RubyLLM.configure do |config|
config.openai_api_key = ENV.fetch("OPENAI_API_KEY", nil)
end
# Define schema for sports
class SportsSchema < RubyLLM::Schema
array :sports do
object do
string :name
array :top_3_players do
string :name
end
end
end
end
# Define weather tool
class Weather < RubyLLM::Tool
description "Gets current weather for a location"
params do
string :latitude, description: "Latitude (e.g., 52.5200)"
string :longitude, description: "Longitude (e.g., 13.4050)"
end
def execute(latitude:, longitude:)
url = "https://api.open-meteo.com/v1/forecast?latitude=#{latitude}&longitude=#{longitude}¤t=temperature_2m,wind_speed_10m"
response = Net::HTTP.get_response(URI(url))
JSON.parse(response.body)
rescue StandardError => e
{ error: e.message }
end
end
class ExampleAgent < RubyLLM::Agent
instructions "You are a helpful assistant that can answer questions.
When asked about a country, only answer with its name."
end
class Agent1 < MARS::AgentStep
agent ExampleAgent
end
class FoodAgent < RubyLLM::Agent
instructions "You are a helpful assistant that can answer questions and help with tasks.
Return information about the typical food of the country."
end
class Agent2 < MARS::AgentStep
agent FoodAgent
end
class SportsAgent < RubyLLM::Agent
instructions "You are a helpful assistant that can answer questions and help with tasks.
Return information about the popular sports of the country."
schema SportsSchema
end
class Agent3 < MARS::AgentStep
agent SportsAgent
end
class WeatherAgent < RubyLLM::Agent
instructions "You are a helpful assistant that can answer questions and help with tasks.
Return the current weather of the country's capital."
tools Weather
end
class Agent4 < MARS::AgentStep
agent WeatherAgent
end
# Create the LLMs
llm1 = Agent1.new
llm2 = Agent2.new
llm3 = Agent3.new
llm4 = Agent4.new
parallel_workflow = MARS::Workflows::Parallel.new(
"Parallel workflow",
steps: [llm2, llm3, llm4]
)
error_workflow = MARS::Workflows::Sequential.new(
"Error workflow",
steps: []
)
gate = MARS::Gate.new(
check: ->(input) { :failure unless input.split.length < 10 },
fallbacks: {
failure: error_workflow
}
)
sequential_workflow = MARS::Workflows::Sequential.new(
"Sequential workflow",
steps: [llm1, gate, parallel_workflow]
)
# Generate and save the diagram
diagram = MARS::Rendering::Mermaid.new(sequential_workflow).render
File.write("examples/complex_llm_workflow/diagram.md", diagram)
puts "Complex workflow diagram saved to: examples/complex_llm_workflow/diagram.md"
# Run the workflow
puts sequential_workflow.run("Which is the largest country in Europe?")