Class: Rager::Context
- Inherits:
-
Object
- Object
- Rager::Context
- Extended by:
- T::Sig
- Defined in:
- lib/rager/context.rb
Instance Attribute Summary collapse
-
#backoff ⇒ Object
readonly
Returns the value of attribute backoff.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#max_retries ⇒ Object
readonly
Returns the value of attribute max_retries.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Class Method Summary collapse
Instance Method Summary collapse
- #chat(messages, **kwargs) ⇒ Object
- #embed(text, **kwargs) ⇒ Object
- #image(prompt, **kwargs) ⇒ Object
-
#initialize(id: nil, name: nil, tags: [], max_retries: nil, backoff: nil) ⇒ Context
constructor
A new instance of Context.
- #mesh(prompt, **kwargs) ⇒ Object
- #outcome(value, feedback: nil, tags: []) ⇒ Object
- #rerank(query, documents, **kwargs) ⇒ Object
- #search(query, **kwargs) ⇒ Object
- #template(template, variables, **kwargs) ⇒ Object
- #to_h ⇒ Object
- #with_options(max_retries: nil, backoff: nil) ⇒ Object
Constructor Details
#initialize(id: nil, name: nil, tags: [], max_retries: nil, backoff: nil) ⇒ Context
Returns a new instance of Context.
36 37 38 39 40 41 42 |
# File 'lib/rager/context.rb', line 36 def initialize(id: nil, name: nil, tags: [], max_retries: nil, backoff: nil) @id = T.let(id || SecureRandom.uuid, String) @name = T.let(name, T.nilable(String)) @tags = T.let(, T::Array[String]) @max_retries = T.let(max_retries || 0, Integer) @backoff = T.let(backoff || 1.0, Numeric) end |
Instance Attribute Details
#backoff ⇒ Object (readonly)
Returns the value of attribute backoff.
25 26 27 |
# File 'lib/rager/context.rb', line 25 def backoff @backoff end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
13 14 15 |
# File 'lib/rager/context.rb', line 13 def id @id end |
#max_retries ⇒ Object (readonly)
Returns the value of attribute max_retries.
22 23 24 |
# File 'lib/rager/context.rb', line 22 def max_retries @max_retries end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/rager/context.rb', line 16 def name @name end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
19 20 21 |
# File 'lib/rager/context.rb', line 19 def @tags end |
Class Method Details
.from_h(hash, max_retries: nil, backoff: nil) ⇒ Object
301 302 303 304 305 306 307 308 309 |
# File 'lib/rager/context.rb', line 301 def self.from_h(hash, max_retries: nil, backoff: nil) new( id: hash[:id], name: hash[:name], tags: hash[:tags], max_retries: max_retries || hash[:max_retries], backoff: backoff || hash[:backoff] ) end |
Instance Method Details
#chat(messages, **kwargs) ⇒ Object
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 |
# File 'lib/rager/context.rb', line 66 def chat(, **kwargs) = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs)) = Rager::Chat::Options.new(**kwargs) .validate normalized_input, input_ids = normalize(, .input_ids) if normalized_input.is_a?(String) normalized_input = [ Rager::Chat::Message.new( role: Rager::Chat::MessageRole::User, content: normalized_input ) ] end execute( operation: Rager::Operation::Chat, input: normalized_input, options: , context_options: , input_ids: input_ids ) do |input, opts| Rager::Providers .get_provider(Rager::Operation::Chat, opts.provider, opts) .chat(input, opts) end end |
#embed(text, **kwargs) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rager/context.rb', line 101 def (text, **kwargs) = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs)) = Rager::Embed::Options.new(**kwargs) .validate normalized_input, input_ids = normalize(text, .input_ids) normalized_input = [normalized_input] if normalized_input.is_a?(String) execute( operation: Rager::Operation::Embed, input: normalized_input, options: , context_options: , input_ids: input_ids ) do |input, opts| Rager::Providers .get_provider(Rager::Operation::Embed, opts.provider, opts) .(input, opts) end end |
#image(prompt, **kwargs) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/rager/context.rb', line 129 def image(prompt, **kwargs) = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs)) = Rager::Image::Options.new(**kwargs) .validate normalized_input, input_ids = normalize(prompt, .input_ids) execute( operation: Rager::Operation::Image, input: normalized_input, options: , context_options: , input_ids: input_ids ) do |input, opts| Rager::Providers .get_provider(Rager::Operation::Image, opts.provider, opts) .image(input, opts) end end |
#mesh(prompt, **kwargs) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/rager/context.rb', line 155 def mesh(prompt, **kwargs) = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs)) = Rager::Mesh::Options.new(**kwargs) .validate normalized_input, input_ids = normalize(prompt, .input_ids) execute( operation: Rager::Operation::Mesh, input: normalized_input, options: , context_options: , input_ids: input_ids ) do |input, opts| Rager::Providers .get_provider(Rager::Operation::Mesh, opts.provider, opts) .mesh(input, opts) end end |
#outcome(value, feedback: nil, tags: []) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/rager/context.rb', line 274 def outcome(value, feedback: nil, tags: []) Rager::Utils::Http.log_remote( "outcomes", { version: "1", data: Rager::Outcome.new( context_id: @id, value: value, feedback: feedback, tags: ) }.to_json ) end |
#rerank(query, documents, **kwargs) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/rager/context.rb', line 182 def rerank(query, documents, **kwargs) = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs)) = Rager::Rerank::Options.new(**kwargs) .validate normalized_query, input_ids = normalize(query, .input_ids) normalized_documents, input_ids = normalize(documents, input_ids) rerank_input = Rager::Rerank::Input.new( query: normalized_query, documents: normalized_documents ) execute( operation: Rager::Operation::Rerank, input: rerank_input, options: , context_options: , input_ids: input_ids ) do |input, opts| Rager::Providers .get_provider(Rager::Operation::Rerank, opts.provider, opts) .rerank(input, opts) end end |
#search(query, **kwargs) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/rager/context.rb', line 214 def search(query, **kwargs) = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs)) = Rager::Search::Options.new(**kwargs) .validate normalized_input, input_ids = normalize(query, .input_ids) execute( operation: Rager::Operation::Search, input: normalized_input, options: , context_options: , input_ids: input_ids ) do |input, opts| Rager::Providers .get_provider(Rager::Operation::Search, opts.provider, opts) .search(input, opts) end end |
#template(template, variables, **kwargs) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/rager/context.rb', line 241 def template(template, variables, **kwargs) = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs)) = Rager::Template::Options.new(**kwargs) .validate normalized_template, input_ids = normalize(template, .input_ids) normalized_variables, input_ids = normalize(variables, input_ids) template_input = Rager::Template::Input.new( template: normalized_template, variables: normalized_variables ) execute( operation: Rager::Operation::Template, input: template_input, options: , context_options: , input_ids: input_ids ) do |input, opts| Rager::Providers .get_provider(Rager::Operation::Template, opts.provider, opts) .template(input, opts) end end |
#to_h ⇒ Object
290 291 292 293 294 295 296 297 298 |
# File 'lib/rager/context.rb', line 290 def to_h { id: @id, name: @name, tags: @tags, max_retries: @max_retries, backoff: @backoff } end |