dify_llm 1.9.1 → 1.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +27 -8
- data/lib/generators/ruby_llm/agent/agent_generator.rb +36 -0
- data/lib/generators/ruby_llm/agent/templates/agent.rb.tt +6 -0
- data/lib/generators/ruby_llm/agent/templates/instructions.txt.erb.tt +0 -0
- data/lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb +110 -41
- data/lib/generators/ruby_llm/chat_ui/templates/controllers/chats_controller.rb.tt +14 -15
- data/lib/generators/ruby_llm/chat_ui/templates/controllers/messages_controller.rb.tt +8 -11
- data/lib/generators/ruby_llm/chat_ui/templates/controllers/models_controller.rb.tt +2 -2
- data/lib/generators/ruby_llm/chat_ui/templates/helpers/messages_helper.rb.tt +25 -0
- data/lib/generators/ruby_llm/chat_ui/templates/jobs/chat_response_job.rb.tt +2 -2
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/chats/_chat.html.erb.tt +16 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/chats/_form.html.erb.tt +31 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/chats/index.html.erb.tt +31 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/chats/new.html.erb.tt +9 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/chats/show.html.erb.tt +27 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_assistant.html.erb.tt +14 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_content.html.erb.tt +1 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_error.html.erb.tt +13 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_form.html.erb.tt +23 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_system.html.erb.tt +10 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_tool.html.erb.tt +2 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_tool_calls.html.erb.tt +4 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_user.html.erb.tt +14 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/tool_calls/_default.html.erb.tt +13 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/tool_results/_default.html.erb.tt +21 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/models/_model.html.erb.tt +17 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/models/index.html.erb.tt +40 -0
- data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/models/show.html.erb.tt +27 -0
- data/lib/generators/ruby_llm/chat_ui/templates/views/chats/_chat.html.erb.tt +2 -2
- data/lib/generators/ruby_llm/chat_ui/templates/views/chats/_form.html.erb.tt +2 -2
- data/lib/generators/ruby_llm/chat_ui/templates/views/chats/index.html.erb.tt +19 -7
- data/lib/generators/ruby_llm/chat_ui/templates/views/chats/new.html.erb.tt +1 -1
- data/lib/generators/ruby_llm/chat_ui/templates/views/chats/show.html.erb.tt +5 -3
- data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_assistant.html.erb.tt +9 -0
- data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_content.html.erb.tt +1 -1
- data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_error.html.erb.tt +8 -0
- data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_form.html.erb.tt +1 -1
- data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_system.html.erb.tt +6 -0
- data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_tool.html.erb.tt +2 -0
- data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_tool_calls.html.erb.tt +4 -7
- data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_user.html.erb.tt +9 -0
- data/lib/generators/ruby_llm/chat_ui/templates/views/messages/create.turbo_stream.erb.tt +5 -7
- data/lib/generators/ruby_llm/chat_ui/templates/views/messages/tool_calls/_default.html.erb.tt +8 -0
- data/lib/generators/ruby_llm/chat_ui/templates/views/messages/tool_results/_default.html.erb.tt +16 -0
- data/lib/generators/ruby_llm/chat_ui/templates/views/models/_model.html.erb.tt +11 -12
- data/lib/generators/ruby_llm/chat_ui/templates/views/models/index.html.erb.tt +27 -17
- data/lib/generators/ruby_llm/chat_ui/templates/views/models/show.html.erb.tt +3 -4
- data/lib/generators/ruby_llm/generator_helpers.rb +37 -17
- data/lib/generators/ruby_llm/install/install_generator.rb +22 -18
- data/lib/generators/ruby_llm/install/templates/create_chats_migration.rb.tt +1 -1
- data/lib/generators/ruby_llm/install/templates/create_messages_migration.rb.tt +4 -1
- data/lib/generators/ruby_llm/install/templates/create_models_migration.rb.tt +4 -10
- data/lib/generators/ruby_llm/install/templates/create_tool_calls_migration.rb.tt +2 -1
- data/lib/generators/ruby_llm/install/templates/initializer.rb.tt +2 -2
- data/lib/generators/ruby_llm/schema/schema_generator.rb +26 -0
- data/lib/generators/ruby_llm/schema/templates/schema.rb.tt +2 -0
- data/lib/generators/ruby_llm/tool/templates/tool.rb.tt +9 -0
- data/lib/generators/ruby_llm/tool/templates/tool_call.html.erb.tt +13 -0
- data/lib/generators/ruby_llm/tool/templates/tool_result.html.erb.tt +13 -0
- data/lib/generators/ruby_llm/tool/tool_generator.rb +96 -0
- data/lib/generators/ruby_llm/upgrade_to_v1_10/templates/add_v1_10_message_columns.rb.tt +19 -0
- data/lib/generators/ruby_llm/upgrade_to_v1_10/upgrade_to_v1_10_generator.rb +50 -0
- data/lib/generators/ruby_llm/upgrade_to_v1_14/templates/add_v1_14_tool_call_columns.rb.tt +7 -0
- data/lib/generators/ruby_llm/upgrade_to_v1_14/upgrade_to_v1_14_generator.rb +49 -0
- data/lib/generators/ruby_llm/upgrade_to_v1_7/templates/migration.rb.tt +1 -1
- data/lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb +2 -4
- data/lib/generators/ruby_llm/upgrade_to_v1_9/upgrade_to_v1_9_generator.rb +1 -1
- data/lib/ruby_llm/active_record/acts_as.rb +10 -4
- data/lib/ruby_llm/active_record/acts_as_legacy.rb +132 -27
- data/lib/ruby_llm/active_record/chat_methods.rb +132 -28
- data/lib/ruby_llm/active_record/message_methods.rb +58 -8
- data/lib/ruby_llm/active_record/model_methods.rb +1 -1
- data/lib/ruby_llm/active_record/payload_helpers.rb +26 -0
- data/lib/ruby_llm/active_record/tool_call_methods.rb +15 -0
- data/lib/ruby_llm/agent.rb +365 -0
- data/lib/ruby_llm/aliases.json +199 -62
- data/lib/ruby_llm/attachment.rb +15 -4
- data/lib/ruby_llm/chat.rb +150 -22
- data/lib/ruby_llm/configuration.rb +65 -65
- data/lib/ruby_llm/connection.rb +11 -7
- data/lib/ruby_llm/content.rb +6 -2
- data/lib/ruby_llm/error.rb +37 -1
- data/lib/ruby_llm/message.rb +43 -15
- data/lib/ruby_llm/model/info.rb +15 -13
- data/lib/ruby_llm/models.json +37560 -14094
- data/lib/ruby_llm/models.rb +321 -38
- data/lib/ruby_llm/models_schema.json +2 -2
- data/lib/ruby_llm/provider.rb +26 -4
- data/lib/ruby_llm/providers/anthropic/capabilities.rb +5 -119
- data/lib/ruby_llm/providers/anthropic/chat.rb +149 -17
- data/lib/ruby_llm/providers/anthropic/media.rb +2 -2
- data/lib/ruby_llm/providers/anthropic/models.rb +3 -9
- data/lib/ruby_llm/providers/anthropic/streaming.rb +25 -1
- data/lib/ruby_llm/providers/anthropic/tools.rb +20 -0
- data/lib/ruby_llm/providers/anthropic.rb +5 -1
- data/lib/ruby_llm/providers/azure/chat.rb +29 -0
- data/lib/ruby_llm/providers/azure/embeddings.rb +24 -0
- data/lib/ruby_llm/providers/azure/media.rb +45 -0
- data/lib/ruby_llm/providers/azure/models.rb +14 -0
- data/lib/ruby_llm/providers/azure.rb +148 -0
- data/lib/ruby_llm/providers/bedrock/auth.rb +122 -0
- data/lib/ruby_llm/providers/bedrock/chat.rb +357 -28
- data/lib/ruby_llm/providers/bedrock/media.rb +62 -33
- data/lib/ruby_llm/providers/bedrock/models.rb +107 -62
- data/lib/ruby_llm/providers/bedrock/streaming.rb +309 -8
- data/lib/ruby_llm/providers/bedrock.rb +69 -52
- data/lib/ruby_llm/providers/deepseek/capabilities.rb +4 -114
- data/lib/ruby_llm/providers/deepseek.rb +5 -1
- data/lib/ruby_llm/providers/dify/chat.rb +82 -7
- data/lib/ruby_llm/providers/dify/media.rb +2 -2
- data/lib/ruby_llm/providers/dify/streaming.rb +26 -4
- data/lib/ruby_llm/providers/dify.rb +4 -0
- data/lib/ruby_llm/providers/gemini/capabilities.rb +45 -207
- data/lib/ruby_llm/providers/gemini/chat.rb +88 -6
- data/lib/ruby_llm/providers/gemini/images.rb +1 -1
- data/lib/ruby_llm/providers/gemini/models.rb +2 -4
- data/lib/ruby_llm/providers/gemini/streaming.rb +34 -2
- data/lib/ruby_llm/providers/gemini/tools.rb +35 -3
- data/lib/ruby_llm/providers/gemini.rb +4 -0
- data/lib/ruby_llm/providers/gpustack/capabilities.rb +20 -0
- data/lib/ruby_llm/providers/gpustack/chat.rb +1 -1
- data/lib/ruby_llm/providers/gpustack.rb +8 -0
- data/lib/ruby_llm/providers/mistral/capabilities.rb +8 -0
- data/lib/ruby_llm/providers/mistral/chat.rb +59 -1
- data/lib/ruby_llm/providers/mistral.rb +4 -0
- data/lib/ruby_llm/providers/ollama/capabilities.rb +20 -0
- data/lib/ruby_llm/providers/ollama/chat.rb +1 -1
- data/lib/ruby_llm/providers/ollama.rb +11 -1
- data/lib/ruby_llm/providers/openai/capabilities.rb +96 -192
- data/lib/ruby_llm/providers/openai/chat.rb +101 -7
- data/lib/ruby_llm/providers/openai/media.rb +5 -2
- data/lib/ruby_llm/providers/openai/models.rb +2 -4
- data/lib/ruby_llm/providers/openai/streaming.rb +11 -3
- data/lib/ruby_llm/providers/openai/temperature.rb +28 -0
- data/lib/ruby_llm/providers/openai/tools.rb +27 -2
- data/lib/ruby_llm/providers/openai.rb +11 -1
- data/lib/ruby_llm/providers/openrouter/chat.rb +168 -0
- data/lib/ruby_llm/providers/openrouter/images.rb +69 -0
- data/lib/ruby_llm/providers/openrouter/streaming.rb +74 -0
- data/lib/ruby_llm/providers/openrouter.rb +37 -1
- data/lib/ruby_llm/providers/perplexity/capabilities.rb +34 -99
- data/lib/ruby_llm/providers/perplexity/models.rb +12 -14
- data/lib/ruby_llm/providers/perplexity.rb +4 -0
- data/lib/ruby_llm/providers/vertexai/models.rb +1 -1
- data/lib/ruby_llm/providers/vertexai.rb +23 -7
- data/lib/ruby_llm/providers/xai/chat.rb +15 -0
- data/lib/ruby_llm/providers/xai/models.rb +75 -0
- data/lib/ruby_llm/providers/xai.rb +32 -0
- data/lib/ruby_llm/stream_accumulator.rb +120 -18
- data/lib/ruby_llm/streaming.rb +82 -60
- data/lib/ruby_llm/thinking.rb +49 -0
- data/lib/ruby_llm/tokens.rb +47 -0
- data/lib/ruby_llm/tool.rb +49 -4
- data/lib/ruby_llm/tool_call.rb +6 -3
- data/lib/ruby_llm/version.rb +1 -1
- data/lib/ruby_llm.rb +14 -8
- data/lib/tasks/models.rake +62 -23
- data/lib/tasks/release.rake +1 -1
- data/lib/tasks/ruby_llm.rake +9 -1
- data/lib/tasks/vcr.rake +33 -1
- metadata +67 -16
- data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_message.html.erb.tt +0 -13
- data/lib/ruby_llm/providers/bedrock/capabilities.rb +0 -167
- data/lib/ruby_llm/providers/bedrock/signing.rb +0 -831
- data/lib/ruby_llm/providers/bedrock/streaming/base.rb +0 -51
- data/lib/ruby_llm/providers/bedrock/streaming/content_extraction.rb +0 -71
- data/lib/ruby_llm/providers/bedrock/streaming/message_processing.rb +0 -67
- data/lib/ruby_llm/providers/bedrock/streaming/payload_processing.rb +0 -80
- data/lib/ruby_llm/providers/bedrock/streaming/prelude_handling.rb +0 -78
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'erb'
|
|
4
|
+
require 'forwardable'
|
|
5
|
+
require 'pathname'
|
|
6
|
+
require 'ruby_llm/schema'
|
|
7
|
+
|
|
8
|
+
module RubyLLM
|
|
9
|
+
# Base class for simple, class-configured agents.
|
|
10
|
+
class Agent
|
|
11
|
+
extend Forwardable
|
|
12
|
+
include Enumerable
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
def inherited(subclass)
|
|
16
|
+
super
|
|
17
|
+
subclass.instance_variable_set(:@chat_kwargs, (@chat_kwargs || {}).dup)
|
|
18
|
+
subclass.instance_variable_set(:@tools, (@tools || []).dup)
|
|
19
|
+
subclass.instance_variable_set(:@instructions, @instructions)
|
|
20
|
+
subclass.instance_variable_set(:@temperature, @temperature)
|
|
21
|
+
subclass.instance_variable_set(:@thinking, @thinking)
|
|
22
|
+
subclass.instance_variable_set(:@params, (@params || {}).dup)
|
|
23
|
+
subclass.instance_variable_set(:@headers, (@headers || {}).dup)
|
|
24
|
+
subclass.instance_variable_set(:@schema, @schema)
|
|
25
|
+
subclass.instance_variable_set(:@context, @context)
|
|
26
|
+
subclass.instance_variable_set(:@chat_model, @chat_model)
|
|
27
|
+
subclass.instance_variable_set(:@input_names, (@input_names || []).dup)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def model(model_id = nil, **options)
|
|
31
|
+
options[:model] = model_id unless model_id.nil?
|
|
32
|
+
@chat_kwargs = options
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def tools(*tools, &block)
|
|
36
|
+
return @tools || [] if tools.empty? && !block_given?
|
|
37
|
+
|
|
38
|
+
@tools = block_given? ? block : tools.flatten
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def instructions(text = nil, **prompt_locals, &block)
|
|
42
|
+
if text.nil? && prompt_locals.empty? && !block_given?
|
|
43
|
+
@instructions ||= { prompt: 'instructions', locals: {} }
|
|
44
|
+
return @instructions
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
@instructions = block || text || { prompt: 'instructions', locals: prompt_locals }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def temperature(value = nil)
|
|
51
|
+
return @temperature if value.nil?
|
|
52
|
+
|
|
53
|
+
@temperature = value
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def thinking(effort: nil, budget: nil)
|
|
57
|
+
return @thinking if effort.nil? && budget.nil?
|
|
58
|
+
|
|
59
|
+
@thinking = { effort: effort, budget: budget }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def params(**params, &block)
|
|
63
|
+
return @params || {} if params.empty? && !block_given?
|
|
64
|
+
|
|
65
|
+
@params = block_given? ? block : params
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def headers(**headers, &block)
|
|
69
|
+
return @headers || {} if headers.empty? && !block_given?
|
|
70
|
+
|
|
71
|
+
@headers = block_given? ? block : headers
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def schema(value = nil, &block)
|
|
75
|
+
return @schema if value.nil? && !block_given?
|
|
76
|
+
|
|
77
|
+
@schema = block_given? ? block : value
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def context(value = nil)
|
|
81
|
+
return @context if value.nil?
|
|
82
|
+
|
|
83
|
+
@context = value
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def chat_model(value = nil)
|
|
87
|
+
return @chat_model if value.nil?
|
|
88
|
+
|
|
89
|
+
@chat_model = value
|
|
90
|
+
remove_instance_variable(:@resolved_chat_model) if instance_variable_defined?(:@resolved_chat_model)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def inputs(*names)
|
|
94
|
+
return @input_names || [] if names.empty?
|
|
95
|
+
|
|
96
|
+
@input_names = names.flatten.map(&:to_sym)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def chat_kwargs
|
|
100
|
+
@chat_kwargs || {}
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def chat(**kwargs)
|
|
104
|
+
input_values, chat_options = partition_inputs(kwargs)
|
|
105
|
+
chat = RubyLLM.chat(**chat_kwargs, **chat_options)
|
|
106
|
+
apply_configuration(chat, input_values:, persist_instructions: true)
|
|
107
|
+
chat
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def create(**kwargs)
|
|
111
|
+
with_rails_chat_record(:create, **kwargs)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def create!(**kwargs)
|
|
115
|
+
with_rails_chat_record(:create!, **kwargs)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def find(id, **kwargs)
|
|
119
|
+
raise ArgumentError, 'chat_model must be configured to use find' unless resolved_chat_model
|
|
120
|
+
|
|
121
|
+
input_values, = partition_inputs(kwargs)
|
|
122
|
+
record = resolved_chat_model.find(id)
|
|
123
|
+
apply_configuration(record, input_values:, persist_instructions: false)
|
|
124
|
+
|
|
125
|
+
record
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def sync_instructions!(chat_or_id, **kwargs)
|
|
129
|
+
raise ArgumentError, 'chat_model must be configured to use sync_instructions!' unless resolved_chat_model
|
|
130
|
+
|
|
131
|
+
input_values, = partition_inputs(kwargs)
|
|
132
|
+
record = chat_or_id.is_a?(resolved_chat_model) ? chat_or_id : resolved_chat_model.find(chat_or_id)
|
|
133
|
+
apply_assume_model_exists(record)
|
|
134
|
+
runtime = runtime_context(chat: record, inputs: input_values)
|
|
135
|
+
instructions_value = resolved_instructions_value(record, runtime, inputs: input_values)
|
|
136
|
+
return record if instructions_value.nil?
|
|
137
|
+
|
|
138
|
+
record.with_instructions(instructions_value)
|
|
139
|
+
record
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def render_prompt(name, chat:, inputs:, locals:)
|
|
143
|
+
path = prompt_path_for(name)
|
|
144
|
+
unless File.exist?(path)
|
|
145
|
+
raise RubyLLM::PromptNotFoundError,
|
|
146
|
+
"Prompt file not found for #{self}: #{path}. Create the file or use inline instructions."
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
resolved_locals = resolve_prompt_locals(locals, runtime: runtime_context(chat:, inputs:), chat:, inputs:)
|
|
150
|
+
ERB.new(File.read(path)).result_with_hash(resolved_locals)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
private
|
|
154
|
+
|
|
155
|
+
def with_rails_chat_record(method_name, **kwargs)
|
|
156
|
+
raise ArgumentError, 'chat_model must be configured to use create/create!' unless resolved_chat_model
|
|
157
|
+
|
|
158
|
+
input_values, chat_options = partition_inputs(kwargs)
|
|
159
|
+
record = resolved_chat_model.public_send(method_name, **chat_kwargs, **chat_options)
|
|
160
|
+
apply_configuration(record, input_values:, persist_instructions: true) if record
|
|
161
|
+
record
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def apply_configuration(chat_object, input_values:, persist_instructions:)
|
|
165
|
+
runtime = runtime_context(chat: chat_object, inputs: input_values)
|
|
166
|
+
llm_chat = llm_chat_for(chat_object)
|
|
167
|
+
|
|
168
|
+
apply_context(llm_chat)
|
|
169
|
+
apply_instructions(chat_object, runtime, inputs: input_values, persist: persist_instructions)
|
|
170
|
+
apply_tools(llm_chat, runtime)
|
|
171
|
+
apply_temperature(llm_chat)
|
|
172
|
+
apply_thinking(llm_chat)
|
|
173
|
+
apply_params(llm_chat, runtime)
|
|
174
|
+
apply_headers(llm_chat, runtime)
|
|
175
|
+
apply_schema(llm_chat, runtime)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def apply_context(llm_chat)
|
|
179
|
+
llm_chat.with_context(context) if context
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def apply_instructions(chat_object, runtime, inputs:, persist:)
|
|
183
|
+
value = resolved_instructions_value(chat_object, runtime, inputs:)
|
|
184
|
+
return if value.nil?
|
|
185
|
+
|
|
186
|
+
target = instruction_target(chat_object, persist:)
|
|
187
|
+
return target.with_runtime_instructions(value) if use_runtime_instructions?(target, persist:)
|
|
188
|
+
|
|
189
|
+
target.with_instructions(value)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def apply_tools(llm_chat, runtime)
|
|
193
|
+
tools_to_apply = Array(evaluate(tools, runtime))
|
|
194
|
+
llm_chat.with_tools(*tools_to_apply) unless tools_to_apply.empty?
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def apply_temperature(llm_chat)
|
|
198
|
+
llm_chat.with_temperature(temperature) unless temperature.nil?
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def apply_thinking(llm_chat)
|
|
202
|
+
llm_chat.with_thinking(**thinking) if thinking
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def apply_params(llm_chat, runtime)
|
|
206
|
+
value = evaluate(params, runtime)
|
|
207
|
+
llm_chat.with_params(**value) if value && !value.empty?
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def apply_headers(llm_chat, runtime)
|
|
211
|
+
value = evaluate(headers, runtime)
|
|
212
|
+
llm_chat.with_headers(**value) if value && !value.empty?
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def apply_schema(llm_chat, runtime)
|
|
216
|
+
value = resolved_schema_value(runtime)
|
|
217
|
+
llm_chat.with_schema(value) if value
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def resolved_schema_value(runtime)
|
|
221
|
+
value = schema
|
|
222
|
+
return value unless value.is_a?(Proc)
|
|
223
|
+
|
|
224
|
+
evaluate(value, runtime)
|
|
225
|
+
rescue NoMethodError => e
|
|
226
|
+
raise unless e.receiver.equal?(runtime)
|
|
227
|
+
|
|
228
|
+
RubyLLM::Schema.create(&value)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def llm_chat_for(chat_object)
|
|
232
|
+
apply_assume_model_exists(chat_object)
|
|
233
|
+
chat_object.respond_to?(:to_llm) ? chat_object.to_llm : chat_object
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def apply_assume_model_exists(chat_object)
|
|
237
|
+
return unless chat_kwargs.key?(:assume_model_exists) &&
|
|
238
|
+
resolved_chat_model &&
|
|
239
|
+
chat_object.is_a?(resolved_chat_model)
|
|
240
|
+
|
|
241
|
+
chat_object.assume_model_exists = chat_kwargs[:assume_model_exists]
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def evaluate(value, runtime)
|
|
245
|
+
value.is_a?(Proc) ? runtime.instance_exec(&value) : value
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def resolved_instructions_value(chat_object, runtime, inputs:)
|
|
249
|
+
value = evaluate(@instructions, runtime)
|
|
250
|
+
return value unless prompt_instruction?(value)
|
|
251
|
+
|
|
252
|
+
runtime.prompt(
|
|
253
|
+
value[:prompt],
|
|
254
|
+
**resolve_prompt_locals(value[:locals] || {}, runtime:, chat: chat_object, inputs:)
|
|
255
|
+
)
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def prompt_instruction?(value)
|
|
259
|
+
value.is_a?(Hash) && value[:prompt]
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def instruction_target(chat_object, persist:)
|
|
263
|
+
if persist || !chat_object.respond_to?(:to_llm)
|
|
264
|
+
chat_object
|
|
265
|
+
else
|
|
266
|
+
runtime_instruction_target(chat_object)
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def runtime_instruction_target(chat_object)
|
|
271
|
+
return chat_object if chat_object.respond_to?(:with_runtime_instructions)
|
|
272
|
+
|
|
273
|
+
chat_object.to_llm
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def use_runtime_instructions?(target, persist:)
|
|
277
|
+
!persist && target.respond_to?(:with_runtime_instructions)
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def resolve_prompt_locals(locals, runtime:, chat:, inputs:)
|
|
281
|
+
base = { chat: chat }.merge(inputs)
|
|
282
|
+
evaluated = locals.each_with_object({}) do |(key, value), acc|
|
|
283
|
+
acc[key.to_sym] = value.is_a?(Proc) ? runtime.instance_exec(&value) : value
|
|
284
|
+
end
|
|
285
|
+
base.merge(evaluated)
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def partition_inputs(kwargs)
|
|
289
|
+
input_values = {}
|
|
290
|
+
chat_options = {}
|
|
291
|
+
|
|
292
|
+
kwargs.each do |key, value|
|
|
293
|
+
symbolized_key = key.to_sym
|
|
294
|
+
if inputs.include?(symbolized_key)
|
|
295
|
+
input_values[symbolized_key] = value
|
|
296
|
+
else
|
|
297
|
+
chat_options[symbolized_key] = value
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
[input_values, chat_options]
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
def runtime_context(chat:, inputs:)
|
|
305
|
+
agent_class = self
|
|
306
|
+
Object.new.tap do |runtime|
|
|
307
|
+
runtime.define_singleton_method(:chat) { chat }
|
|
308
|
+
runtime.define_singleton_method(:prompt) do |name, **locals|
|
|
309
|
+
agent_class.render_prompt(name, chat:, inputs:, locals:)
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
inputs.each do |name, value|
|
|
313
|
+
runtime.define_singleton_method(name) { value }
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def prompt_path_for(name)
|
|
319
|
+
filename = name.to_s
|
|
320
|
+
filename += '.txt.erb' unless filename.end_with?('.txt.erb')
|
|
321
|
+
prompt_root.join(prompt_agent_path, filename)
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def prompt_agent_path
|
|
325
|
+
class_name = name || 'agent'
|
|
326
|
+
class_name.gsub('::', '/')
|
|
327
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
|
328
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
|
329
|
+
.tr('-', '_')
|
|
330
|
+
.downcase
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
def prompt_root
|
|
334
|
+
if defined?(Rails) && Rails.respond_to?(:root) && Rails.root
|
|
335
|
+
Rails.root.join('app/prompts')
|
|
336
|
+
else
|
|
337
|
+
Pathname.new(Dir.pwd).join('app/prompts')
|
|
338
|
+
end
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
def resolved_chat_model
|
|
342
|
+
return @resolved_chat_model if defined?(@resolved_chat_model)
|
|
343
|
+
|
|
344
|
+
@resolved_chat_model = case @chat_model
|
|
345
|
+
when String then Object.const_get(@chat_model)
|
|
346
|
+
else @chat_model
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
def initialize(chat: nil, inputs: nil, persist_instructions: true, **kwargs)
|
|
352
|
+
input_values, chat_options = self.class.send(:partition_inputs, kwargs)
|
|
353
|
+
@chat = chat || RubyLLM.chat(**self.class.chat_kwargs, **chat_options)
|
|
354
|
+
self.class.send(:apply_configuration, @chat, input_values: input_values.merge(inputs || {}),
|
|
355
|
+
persist_instructions:)
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
attr_reader :chat
|
|
359
|
+
|
|
360
|
+
def_delegators :chat, :model, :messages, :tools, :params, :headers, :schema, :ask, :say, :with_tool, :with_tools,
|
|
361
|
+
:with_model, :with_temperature, :with_thinking, :with_context, :with_params, :with_headers,
|
|
362
|
+
:with_schema, :on_new_message, :on_end_message, :on_tool_call, :on_tool_result, :each, :complete,
|
|
363
|
+
:add_message, :reset_messages!
|
|
364
|
+
end
|
|
365
|
+
end
|