Class: Rubagent::Llms::BaseLlm

Inherits:
Object
  • Object
show all
Defined in:
lib/rubagent/llms/base_llm.rb

Overview

Base class for LLM implementations.

Direct Known Subclasses

OpenAI

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/rubagent/llms/base_llm.rb', line 9

def api_key
  @api_key
end

#context_lengthObject

Returns the value of attribute context_length.



9
10
11
# File 'lib/rubagent/llms/base_llm.rb', line 9

def context_length
  @context_length
end

#modelObject

Returns the value of attribute model.



9
10
11
# File 'lib/rubagent/llms/base_llm.rb', line 9

def model
  @model
end

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/rubagent/llms/base_llm.rb', line 9

def url
  @url
end

Instance Method Details

#ask(prompt) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubagent/llms/base_llm.rb', line 11

def ask(prompt)
  authorization = api_key ? { 'Authorization' => "Bearer #{api_key}" } : {}
  HttpClient.post(
    url: url,
    headers: {
      'Content-Type' => 'application/json'
    }.merge(authorization),
    body: {
      model: model, # or "gpt-3.5-turbo"
      messages: [
        { role: 'user', content: prompt }
      ],
      stream: true
    },
    stream: true
  ) do |chunk|
    chunk.lines.each do |line|
      next unless line.start_with?('data: ')

      data = line.sub('data: ', '').strip
      break if data == '[DONE]'

      begin
        parsed = JSON.parse(data)
        content = parsed.dig('choices', 0, 'delta', 'content')
        print content if content
      rescue JSON::ParserError
        warn "Failed to parse chunk: #{data}"
      end
    end
  end
end