Class: BxBuilderChain::Llm::Base Abstract

Inherits:
Object
  • Object
show all
Includes:
DependencyHelper
Defined in:
lib/bx_builder_chain/llm/base.rb

Overview

This class is abstract.

A LLM is a language model consisting of a neural network with many parameters (typically billions of weights or more), trained on large quantities of unlabeled text using self-supervised learning or semi-supervised learning.

BxBuilderChain.rb provides a common interface to interact with all supported LLMs:

  • OpenAI

Direct Known Subclasses

OpenAi

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DependencyHelper

#depends_on

Instance Attribute Details

#clientObject (readonly)

A client for communicating with the LLM



17
18
19
# File 'lib/bx_builder_chain/llm/base.rb', line 17

def client
  @client
end

Instance Method Details

#chat(**kwargs) ⇒ Object

Generate a chat completion for a given prompt. Parameters will depend on the LLM

Raises:

  • NotImplementedError if not supported by the LLM



27
28
29
# File 'lib/bx_builder_chain/llm/base.rb', line 27

def chat(**kwargs)
  raise NotImplementedError, "#{self.class.name} does not support chat"
end

#complete(**kwargs) ⇒ Object

Generate a completion for a given prompt. Parameters will depend on the LLM.

Raises:

  • NotImplementedError if not supported by the LLM



35
36
37
# File 'lib/bx_builder_chain/llm/base.rb', line 35

def complete(**kwargs)
  raise NotImplementedError, "#{self.class.name} does not support completion"
end

#count_tokens(string) ⇒ Object



57
58
59
60
61
62
# File 'lib/bx_builder_chain/llm/base.rb', line 57

def count_tokens(string)
  tokens = string.scan(/[\w]+|[\W]/)
  tokens = tokens.flat_map { |token| token.split(/(?<=[\W])$/) }

  return (tokens.length*1.07).to_i
end

#default_dimensionObject



19
20
21
# File 'lib/bx_builder_chain/llm/base.rb', line 19

def default_dimension
  self.class.const_get(:DEFAULTS).dig(:dimension)
end

#embed(**kwargs) ⇒ Object

Generate an embedding for a given text. Parameters depends on the LLM.

Raises:

  • NotImplementedError if not supported by the LLM



44
45
46
# File 'lib/bx_builder_chain/llm/base.rb', line 44

def embed(**kwargs)
  raise NotImplementedError, "#{self.class.name} does not support generating embeddings"
end

#summarize(**kwargs) ⇒ Object

Generate a summary for a given text. Parameters depends on the LLM.

Raises:

  • NotImplementedError if not supported by the LLM



53
54
55
# File 'lib/bx_builder_chain/llm/base.rb', line 53

def summarize(**kwargs)
  raise NotImplementedError, "#{self.class.name} does not support summarization"
end