Class: FlowEngine::LLM::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/flowengine/llm/client.rb

Overview

High-level LLM client that parses introduction text into pre-filled answers. Wraps an Adapter and a model name, builds the system prompt from the flow Definition, and parses the structured JSON response.

Direct Known Subclasses

AutoClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:, model: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • adapter (Adapter) —

    LLM provider adapter (e.g. Adapters::OpenAIAdapter)

  • model (String, nil) (defaults to: nil) —

    model identifier; defaults to the adapter's model



15
16
17
18
# File 'lib/flowengine/llm/client.rb', line 15

def initialize(adapter:, model: nil)
  @adapter = adapter
  @model = model || adapter.model
end

Instance Attribute Details

#adapter ⇒ Object (readonly)

Returns the value of attribute adapter.



11
12
13
# File 'lib/flowengine/llm/client.rb', line 11

def adapter
  @adapter
end

#model ⇒ Object (readonly)

Returns the value of attribute model.



11
12
13
# File 'lib/flowengine/llm/client.rb', line 11

def model
  @model
end

Instance Method Details

#parse_ai_intake(definition:, user_text:, answered: {}, conversation_history: []) ⇒ Hash

Sends user text to the LLM for an AI intake step and returns both extracted answers and an optional follow-up question.

Parameters:

  • definition (Definition) —

    flow definition

  • user_text (String) —

    user's free-form text

  • answered (Hash<Symbol, Object>) (defaults to: {}) —

    already-answered steps

  • conversation_history (Array<Hash>) (defaults to: []) —

    prior rounds [text:]

Returns:

  • (Hash) —

    { answers: Hash<Symbol, Object>, follow_up: String|nil }

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/flowengine/llm/client.rb', line 46

def parse_ai_intake(definition:, user_text:, answered: {}, conversation_history: [])
  system_prompt = IntakePromptBuilder.new(
    definition,
    answered: answered,
    conversation_history: conversation_history
  ).build

  response_text = adapter.chat(
    system_prompt: system_prompt,
    user_prompt: user_text,
    model: model
  )

  parse_intake_response(response_text, definition)
end

#parse_introduction(definition:, introduction_text:) ⇒ Hash<Symbol, Object>

Sends the introduction text to the LLM with a system prompt built from the Definition, and returns a hash of extracted step answers.

Parameters:

  • definition (Definition) —

    flow definition (used to build system prompt)

  • introduction_text (String) —

    user's free-form introduction

Returns:

  • (Hash<Symbol, Object>) —

    step_id => extracted value

Raises:



27
28
29
30
31
32
33
34
35
# File 'lib/flowengine/llm/client.rb', line 27

def parse_introduction(definition:, introduction_text:)
  system_prompt = SystemPromptBuilder.new(definition).build
  response_text = adapter.chat(
    system_prompt: system_prompt,
    user_prompt: introduction_text,
    model: model
  )
  parse_response(response_text, definition)
end

#to_s ⇒ Object



62
63
64
# File 'lib/flowengine/llm/client.rb', line 62

def to_s
  "#<#{self.class.name} adapter=#{adapter} model=#{model}>"
end