Class: Rubagent::Llms::BaseLlm
- Inherits:
-
Object
- Object
- Rubagent::Llms::BaseLlm
- Defined in:
- lib/rubagent/llms/base_llm.rb
Overview
Base class for LLM implementations.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#context_length ⇒ Object
Returns the value of attribute context_length.
-
#model ⇒ Object
Returns the value of attribute model.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
Instance Attribute Details
#api_key ⇒ Object
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_length ⇒ Object
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 |
#model ⇒ Object
Returns the value of attribute model.
9 10 11 |
# File 'lib/rubagent/llms/base_llm.rb', line 9 def model @model end |
#url ⇒ Object
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) = api_key ? { 'Authorization' => "Bearer #{api_key}" } : {} HttpClient.post( url: url, headers: { 'Content-Type' => 'application/json' }.merge(), 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 |