Class: Rager::Context

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/rager/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(tags, T::Array[String])
  @max_retries = T.let(max_retries || 0, Integer)
  @backoff = T.let(backoff || 1.0, Numeric)
end

Instance Attribute Details

#backoffObject (readonly)

Returns the value of attribute backoff.



25
26
27
# File 'lib/rager/context.rb', line 25

def backoff
  @backoff
end

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/rager/context.rb', line 13

def id
  @id
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



22
23
24
# File 'lib/rager/context.rb', line 22

def max_retries
  @max_retries
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/rager/context.rb', line 16

def name
  @name
end

#tagsObject (readonly)

Returns the value of attribute tags.



19
20
21
# File 'lib/rager/context.rb', line 19

def tags
  @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(messages, **kwargs)
  context_options = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs))
  options = Rager::Chat::Options.new(**kwargs)
  options.validate

  normalized_input, input_ids = normalize(messages, context_options.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: options,
    context_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 embed(text, **kwargs)
  context_options = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs))
  options = Rager::Embed::Options.new(**kwargs)
  options.validate

  normalized_input, input_ids = normalize(text, context_options.input_ids)

  normalized_input = [normalized_input] if normalized_input.is_a?(String)

  execute(
    operation: Rager::Operation::Embed,
    input: normalized_input,
    options: options,
    context_options: context_options,
    input_ids: input_ids
  ) do |input, opts|
    Rager::Providers
      .get_provider(Rager::Operation::Embed, opts.provider, opts)
      .embed(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)
  context_options = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs))
  options = Rager::Image::Options.new(**kwargs)
  options.validate

  normalized_input, input_ids = normalize(prompt, context_options.input_ids)

  execute(
    operation: Rager::Operation::Image,
    input: normalized_input,
    options: options,
    context_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)
  context_options = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs))
  options = Rager::Mesh::Options.new(**kwargs)
  options.validate

  normalized_input, input_ids = normalize(prompt, context_options.input_ids)

  execute(
    operation: Rager::Operation::Mesh,
    input: normalized_input,
    options: options,
    context_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: 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)
  context_options = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs))
  options = Rager::Rerank::Options.new(**kwargs)
  options.validate

  normalized_query, input_ids = normalize(query, context_options.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: options,
    context_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)
  context_options = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs))
  options = Rager::Search::Options.new(**kwargs)
  options.validate

  normalized_input, input_ids = normalize(query, context_options.input_ids)

  execute(
    operation: Rager::Operation::Search,
    input: normalized_input,
    options: options,
    context_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)
  context_options = Rager::ContextOptions.new(**Rager::ContextOptions.extract_kwargs(kwargs))
  options = Rager::Template::Options.new(**kwargs)
  options.validate

  normalized_template, input_ids = normalize(template, context_options.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: options,
    context_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_hObject



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

#with_options(max_retries: nil, backoff: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rager/context.rb', line 50

def with_options(max_retries: nil, backoff: nil)
  Context.new(
    id: @id,
    name: @name,
    tags: @tags,
    max_retries: max_retries || @max_retries,
    backoff: backoff || @backoff
  )
end