Class: Rager::Context

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, name: nil, max_retries: nil, backoff: nil) ⇒ Context

Returns a new instance of Context.



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

def initialize(id: nil, name: nil, max_retries: nil, backoff: nil)
  @id = T.let(id || SecureRandom.uuid, String)
  @name = T.let(name, T.nilable(String))
  @max_retries = T.let(max_retries || 0, Integer)
  @backoff = T.let(backoff || 1.0, Float)
end

Instance Attribute Details

#backoffObject (readonly)

Returns the value of attribute backoff.



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

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.



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

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

Instance Method Details

#chat(messages, **kwargs) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rager/context.rb', line 38

def chat(messages, **kwargs)
  execute(
    Rager::Operation::Chat,
    Rager::Chat::Options,
    kwargs,
    messages
  ) { |options, normalized_input|
    final_input = if normalized_input.is_a?(String)
      [
        Rager::Chat::Message.new(
          role: Rager::Chat::MessageRole::User,
          content: normalized_input
        )
      ]
    else
      normalized_input
    end

    provider = Rager::Providers.get_provider(:chat, options.provider, options)
    provider.chat(final_input, options)
  }
end

#embed(text, **kwargs) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rager/context.rb', line 67

def embed(text, **kwargs)
  execute(
    Rager::Operation::Embed,
    Rager::Embed::Options,
    kwargs,
    text
  ) { |options, normalized_input|
    final_input = if normalized_input.is_a?(String)
      [normalized_input]
    else
      normalized_input
    end

    provider = Rager::Providers.get_provider(:embed, options.provider, options)
    provider.embed(final_input, options)
  }
end

#image_gen(prompt, **kwargs) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rager/context.rb', line 91

def image_gen(prompt, **kwargs)
  execute(
    Rager::Operation::ImageGen,
    Rager::ImageGen::Options,
    kwargs,
    prompt
  ) { |options, normalized_input|
    provider = Rager::Providers.get_provider(:image_gen, options.provider, options)
    provider.image_gen(T.cast(normalized_input, Rager::Types::ImageGenInput), options)
  }
end

#mesh_gen(prompt, **kwargs) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rager/context.rb', line 109

def mesh_gen(prompt, **kwargs)
  execute(
    Rager::Operation::MeshGen,
    Rager::MeshGen::Options,
    kwargs,
    prompt
  ) { |options, normalized_input|
    provider = Rager::Providers.get_provider(:mesh_gen, options.provider, options)
    provider.mesh_gen(T.cast(normalized_input, Rager::Types::MeshGenInput), options)
  }
end

#rerank(query, documents, **kwargs) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rager/context.rb', line 128

def rerank(query, documents, **kwargs)
  execute(
    Rager::Operation::Rerank,
    Rager::Rerank::Options,
    kwargs,
    {query: query, documents: documents}
  ) { |options, normalized_input|
    provider = Rager::Providers.get_provider(:rerank, options.provider, options)
    input_hash = T.cast(normalized_input, T::Hash[Symbol, T.untyped])
    provider.rerank(input_hash[:query], input_hash[:documents], options)
  }
end

#search(query, **kwargs) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rager/context.rb', line 147

def search(query, **kwargs)
  execute(
    Rager::Operation::Search,
    Rager::Search::Options,
    kwargs,
    query
  ) { |options, normalized_input|
    provider = Rager::Providers.get_provider(:search, options.provider, options)
    provider.search(T.cast(normalized_input, Rager::Types::SearchInput), options)
  }
end

#template(template, variables, **kwargs) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rager/context.rb', line 166

def template(template, variables, **kwargs)
  execute(
    Rager::Operation::Template,
    Rager::Template::Options,
    kwargs,
    {template: template, variables: variables}
  ) { |options, normalized_input|
    provider = Rager::Providers.get_provider(:template, options.provider, options)
    input_hash = T.cast(normalized_input, T::Hash[Symbol, T.untyped])
    provider.template(input_hash[:template], input_hash[:variables], options)
  }
end