Skip to content

Commit 245e83d

Browse files
authored
Add complex example with LLM usage (#12)
1 parent 19491d1 commit 245e83d

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require_relative "../../lib/mars"
5+
6+
RubyLLM.configure do |config|
7+
config.openai_api_key = ENV.fetch("OPENAI_API_KEY", nil)
8+
end
9+
10+
# Create the LLMs
11+
llm1 = Mars::Agent.new(
12+
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"
14+
)
15+
16+
llm2 = Mars::Agent.new(name: "LLM 2", options: { model: "gpt-4o" },
17+
instructions: "You are a helpful assistant that can answer questions and help with tasks.
18+
Return information about the typical food of the country.")
19+
20+
llm3 = Mars::Agent.new(name: "LLM 3", options: { model: "gpt-4o" },
21+
instructions: "You are a helpful assistant that can answer questions and help with tasks.
22+
Return information about the popular sports of the country.")
23+
24+
parallel_workflow = Mars::Workflows::Parallel.new(
25+
"Parallel workflow",
26+
steps: [llm2, llm3]
27+
)
28+
29+
gate = Mars::Gate.new(
30+
name: "Gate",
31+
condition: ->(input) { input.split.length < 10 ? :success : :error },
32+
branches: {
33+
success: parallel_workflow
34+
}
35+
)
36+
37+
sequential_workflow = Mars::Workflows::Sequential.new(
38+
"Sequential workflow",
39+
steps: [llm1, gate]
40+
)
41+
42+
puts sequential_workflow.run("Which is the last country to declare independence?")

lib/mars.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "zeitwerk"
44
require "async"
5+
require "ruby_llm"
56

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

lib/mars/agent.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ module Mars
44
class Agent < Runnable
55
attr_reader :name
66

7-
def initialize(name:, options: {}, tools: [], schema: nil)
7+
def initialize(name:, options: {}, tools: [], schema: nil, instructions: nil)
88
@name = name
99
@tools = Array(tools)
1010
@schema = schema
1111
@options = options
12+
@instructions = instructions
1213
end
1314

1415
def run(input)
15-
chat.ask(input)
16+
chat.ask(input).content
1617
end
1718

1819
private
1920

20-
attr_reader :tools, :schema, :options
21+
attr_reader :tools, :schema, :options, :instructions
2122

2223
def chat
23-
@chat ||= RubyLLM::Chat.new(**options).with_tools(tools).with_schema(schema)
24+
@chat ||= RubyLLM::Chat.new(**options)
25+
.with_instructions(instructions)
26+
.with_tools(*tools)
27+
.with_schema(schema)
2428
end
2529
end
2630
end

lib/mars/gate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(name:, condition:, branches:)
1313
def run(input)
1414
result = condition.call(input)
1515

16-
branches[result].run(input)
16+
branches[result]&.run(input) || input
1717
end
1818

1919
private

0 commit comments

Comments
 (0)