|
7 | 7 | config.openai_api_key = ENV.fetch("OPENAI_API_KEY", nil) |
8 | 8 | end |
9 | 9 |
|
| 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}¤t=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 | + |
10 | 44 | # Create the LLMs |
11 | 45 | llm1 = Mars::Agent.new( |
12 | 46 | 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." |
14 | 49 | ) |
15 | 50 |
|
16 | 51 | llm2 = Mars::Agent.new(name: "LLM 2", options: { model: "gpt-4o" }, |
17 | 52 | instructions: "You are a helpful assistant that can answer questions and help with tasks. |
18 | 53 | Return information about the typical food of the country.") |
19 | 54 |
|
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, |
21 | 56 | instructions: "You are a helpful assistant that can answer questions and help with tasks. |
22 | 57 | Return information about the popular sports of the country.") |
23 | 58 |
|
| 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 | + |
24 | 63 | parallel_workflow = Mars::Workflows::Parallel.new( |
25 | 64 | "Parallel workflow", |
26 | | - steps: [llm2, llm3] |
| 65 | + steps: [llm2, llm3, llm4] |
27 | 66 | ) |
28 | 67 |
|
29 | 68 | gate = Mars::Gate.new( |
|
39 | 78 | steps: [llm1, gate] |
40 | 79 | ) |
41 | 80 |
|
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?") |
0 commit comments