Class: Boxcars::VectorAnswer

Inherits:
EngineBoxcar show all
Defined in:
lib/boxcars/boxcar/vector_answer.rb

Overview

A Boxcar that answers questions using vector-search context.

Constant Summary collapse

DESC =

Default description for this boxcar.

"useful for when you need to answer questions from vector search results."
CTEMPLATE =

Prompt template used by this boxcar.

[
  syst("You are tasked with answering a question using these possibly relevant excerpts from a large volume of text:\n" \
       "```text\n%<search_content>s\n```\n\n",
       "Using the above, just answer the question as if you were answering directly."),
  user("%<question>s")
].freeze

Constants inherited from Boxcar

Boxcar::SCHEMA_KEY_ALIASES, Boxcar::TYPE_ALIASES

Instance Attribute Summary collapse

Attributes inherited from EngineBoxcar

#engine, #prompt, #stop, #top_k

Attributes inherited from Boxcar

#description, #name, #parameters, #return_direct

Instance Method Summary collapse

Methods inherited from EngineBoxcar

#apply, #call, #extract_code, #generate, #input_keys, #output_key, #output_keys, #predict, #prediction_variables

Methods inherited from Boxcar

#apply, assi, #call, #conduct, #conduct_result, hist, #input_keys, #output_keys, #parameters_json_schema, #run, #run_result, #schema, syst, #tool_call_name, #tool_definition, #tool_spec, user, #validate_inputs, #validate_outputs

Constructor Details

#initialize(embeddings:, vector_documents:, engine: nil, prompt: nil, **kwargs) ⇒ VectorAnswer

Returns a new instance of VectorAnswer.

Parameters:

  • embeddings (Hash)

    The vector embeddings to use for this boxcar.

  • vector_documents (Hash)

    The vector documents to use for this boxcar.

  • engine (Boxcars::Engine) (defaults to: nil)

    The engine to use for this boxcar. Can be inherited from a train if nil.

  • prompt (Boxcars::Prompt) (defaults to: nil)

    The prompt to use for this boxcar. Defaults to built-in prompt.

  • kwargs (Hash)

    Any other keyword arguments to pass to the parent class.



17
18
19
20
21
22
23
24
25
# File 'lib/boxcars/boxcar/vector_answer.rb', line 17

def initialize(embeddings:, vector_documents:, engine: nil, prompt: nil, **kwargs)
  the_prompt = prompt || my_prompt
  @embeddings = embeddings
  @vector_documents = vector_documents
  kwargs[:stop] ||= ["```output"]
  kwargs[:name] ||= "VectorAnswer"
  kwargs[:description] ||= DESC
  super(engine:, prompt: the_prompt, **kwargs)
end

Instance Attribute Details

#embeddingsObject (readonly)

Returns the value of attribute embeddings.



10
11
12
# File 'lib/boxcars/boxcar/vector_answer.rb', line 10

def embeddings
  @embeddings
end

#search_contentObject (readonly)

Returns the value of attribute search_content.



10
11
12
# File 'lib/boxcars/boxcar/vector_answer.rb', line 10

def search_content
  @search_content
end

#vector_documentsObject (readonly)

Returns the value of attribute vector_documents.



10
11
12
# File 'lib/boxcars/boxcar/vector_answer.rb', line 10

def vector_documents
  @vector_documents
end

Instance Method Details

#prediction_additional(inputs) ⇒ Hash

Returns The additional variables for this boxcar.

Parameters:

  • inputs (Hash)

    The inputs to use for the prediction.

Returns:

  • (Hash)

    The additional variables for this boxcar.



29
30
31
# File 'lib/boxcars/boxcar/vector_answer.rb', line 29

def prediction_additional(inputs)
  { search_content: get_search_content(inputs[:question]) }.merge super
end