Class: Boxcars::Gpt4allEng

Inherits:
Engine
  • Object
show all
Defined in:
lib/boxcars/engine/gpt4all_eng.rb

Overview

A engine that uses local GPT4All API.

Constant Summary collapse

DEFAULT_NAME =

the default name of the engine

"Gpt4all engine"
DEFAULT_DESCRIPTION =

the default description of the engine

"useful for when you need to use local AI to answer questions. " \
"You should ask targeted questions"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Engine

#generate, #generation_info, #get_num_tokens

Constructor Details

#initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, prompts: [], batch_size: 2, **_kwargs) ⇒ Gpt4allEng

A engine is a container for a single tool to run.

Parameters:

  • name (String) (defaults to: DEFAULT_NAME)

    The name of the engine. Defaults to “OpenAI engine”.

  • description (String) (defaults to: DEFAULT_DESCRIPTION)

    A description of the engine. Defaults to: useful for when you need to use AI to answer questions. You should ask targeted questions“.

  • prompts (Array<String>) (defaults to: [])

    The prompts to use when asking the engine. Defaults to [].

  • batch_size (Integer) (defaults to: 2)

    The number of prompts to send to the engine at once. Defaults to 2.



22
23
24
25
26
# File 'lib/boxcars/engine/gpt4all_eng.rb', line 22

def initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, prompts: [], batch_size: 2, **_kwargs)
  @prompts = prompts
  @batch_size = batch_size
  super(description: description, name: name)
end

Instance Attribute Details

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



8
9
10
# File 'lib/boxcars/engine/gpt4all_eng.rb', line 8

def batch_size
  @batch_size
end

#model_kwargsObject (readonly)

Returns the value of attribute model_kwargs.



8
9
10
# File 'lib/boxcars/engine/gpt4all_eng.rb', line 8

def model_kwargs
  @model_kwargs
end

#promptsObject (readonly)

Returns the value of attribute prompts.



8
9
10
# File 'lib/boxcars/engine/gpt4all_eng.rb', line 8

def prompts
  @prompts
end

Instance Method Details

#client(prompt:, inputs: {}, **_kwargs) ⇒ Object

Get an answer from the engine.

Parameters:

  • prompt (String)

    The prompt to use when asking the engine.

  • openai_access_token (String)

    The access token to use when asking the engine. Defaults to Boxcars.configuration.openai_access_token.

  • kwargs (Hash)

    Additional parameters to pass to the engine if wanted.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/boxcars/engine/gpt4all_eng.rb', line 33

def client(prompt:, inputs: {}, **_kwargs)
  gpt4all = Gpt4all::ConversationalAI.new
  gpt4all.prepare_resources(force_download: false)
  gpt4all.start_bot
  input_text = prompt.as_prompt(inputs: inputs)[:prompt]
  Boxcars.debug("Prompt after formatting:\n#{input_text}", :cyan) if Boxcars.configuration.log_prompts
  gpt4all.prompt(input_text)
rescue StandardError => e
  Boxcars.error(["Error from gpt4all engine: #{e}", e.backtrace[-5..-1]].flatten.join("\n   "))
ensure
  gpt4all.stop_bot
end

#run(question, **kwargs) ⇒ Object

get an answer from the engine for a question.

Parameters:

  • question (String)

    The question to ask the engine.

  • kwargs (Hash)

    Additional parameters to pass to the engine if wanted.



49
50
51
52
53
54
# File 'lib/boxcars/engine/gpt4all_eng.rb', line 49

def run(question, **kwargs)
  prompt = Prompt.new(template: question)
  answer = client(prompt: prompt, **kwargs)
  Boxcars.debug("Answer: #{answer}", :cyan)
  answer
end