Class: Ticuna::LLM

Inherits:
Object
  • Object
show all
Defined in:
lib/ticuna/llm.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(provider = nil) ⇒ Object



10
11
12
13
14
# File 'lib/ticuna/llm.rb', line 10

def self.new(provider = nil)
  provider_key = detect_provider(provider)
  provider_client = Ticuna::Providers::CLIENTS[provider_key].call
  allocate.tap { |instance| instance.send(:initialize_llm, provider_key, provider_client) }
end

Instance Method Details

#ask(message, stream: false, model: :gpt_4_1, output_format: :text, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ticuna/llm.rb', line 34

def ask(message, stream: false, model: :gpt_4_1, output_format: :text, &block)
  tool_contexts = @tools.map(&:context).compact
  direct_contexts = @contexts.compact
  combined_contexts = (tool_contexts + direct_contexts).reject { |ctx| ctx.to_s.strip.empty? }

  system_message = if combined_contexts.empty?
                     nil
                   else
                     { role: "developer", content: combined_contexts.join("\n\n") }
                   end

  messages = if system_message
               [system_message, { role: "user", content: message }]
             else
               [{ role: "user", content: message }]
             end

  model_string = detect_llm_model(model)

  Ticuna::Response.new(
    @provider.ask_with_messages(messages, stream: stream, model: model_string, output_format: output_format, &block),
    provider: @provider_key
  )
end

#context(value = nil, &block) ⇒ Object



28
29
30
31
32
# File 'lib/ticuna/llm.rb', line 28

def context(value = nil, &block)
  value = block.call if block
  @contexts << value if value
  self
end

#initialize_llm(provider_key, provider_client) ⇒ Object



16
17
18
19
20
21
# File 'lib/ticuna/llm.rb', line 16

def initialize_llm(provider_key, provider_client)
  @provider_key = provider_key
  @provider = provider_client
  @tools = []
  @contexts = []
end

#tool(klass) ⇒ Object



23
24
25
26
# File 'lib/ticuna/llm.rb', line 23

def tool(klass)
  @tools << klass.new
  self
end