Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions examples/complex_llm_workflow/generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/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

# Create the LLMs
llm1 = Mars::Agent.new(
name: "LLM 1", options: { model: "gpt-4o" },
instructions: "You are a helpful assistant that can answer questions and help with tasks. Only answer with the result"
)

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

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

parallel_workflow = Mars::Workflows::Parallel.new(
"Parallel workflow",
steps: [llm2, llm3]
)

gate = Mars::Gate.new(
name: "Gate",
condition: ->(input) { input.split.length < 10 ? :success : :error },
branches: {
success: parallel_workflow
}
)

sequential_workflow = Mars::Workflows::Sequential.new(
"Sequential workflow",
steps: [llm1, gate]
)

puts sequential_workflow.run("Which is the last country to declare independence?")
1 change: 1 addition & 0 deletions lib/mars.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "zeitwerk"
require "async"
require "ruby_llm"

loader = Zeitwerk::Loader.for_gem
loader.setup
Expand Down
12 changes: 8 additions & 4 deletions lib/mars/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@ module Mars
class Agent < Runnable
attr_reader :name

def initialize(name:, options: {}, tools: [], schema: nil)
def initialize(name:, options: {}, tools: [], schema: nil, instructions: nil)
@name = name
@tools = Array(tools)
@schema = schema
@options = options
@instructions = instructions
end

def run(input)
chat.ask(input)
chat.ask(input).content
end

private

attr_reader :tools, :schema, :options
attr_reader :tools, :schema, :options, :instructions

def chat
@chat ||= RubyLLM::Chat.new(**options).with_tools(tools).with_schema(schema)
@chat ||= RubyLLM::Chat.new(**options)
.with_instructions(instructions)
.with_tools(*tools)
.with_schema(schema)
end
end
end
2 changes: 1 addition & 1 deletion lib/mars/gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(name:, condition:, branches:)
def run(input)
result = condition.call(input)

branches[result].run(input)
branches[result]&.run(input) || input
end

private
Expand Down