Class: Legion::LLM::Call::LexLLMAdapter

Inherits:
Object
  • Object
show all
Includes:
Legion::Logging::Helper
Defined in:
lib/legion/llm/call/lex_llm_adapter.rb

Overview

Adapts a lex-llm provider class to legion-llm’s native dispatch contract.

Instance Method Summary collapse

Constructor Details

#initialize(provider_name, provider_class) ⇒ LexLLMAdapter

Returns a new instance of LexLLMAdapter.



12
13
14
15
16
# File 'lib/legion/llm/call/lex_llm_adapter.rb', line 12

def initialize(provider_name, provider_class)
  @provider_name = provider_name.to_sym
  @provider_class = provider_class
  @lex_llm_namespace = resolve_lex_llm_namespace
end

Instance Method Details

#chat(model:, messages:, **opts) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/legion/llm/call/lex_llm_adapter.rb', line 18

def chat(model:, messages:, **opts)
  response = provider.complete(
    normalize_messages(messages, system: opts[:system]),
    tools:       normalize_tools(opts[:tools]),
    temperature: opts[:temperature],
    params:      opts[:params] || {},
    headers:     opts[:headers] || {},
    schema:      opts[:schema],
    thinking:    opts[:thinking],
    tool_prefs:  opts[:tool_prefs],
    model:       model_info(model, offering_metadata: opts[:offering_metadata])
  )

  message_response(response, offering_metadata: opts[:offering_metadata])
end

#count_tokens(model:, messages:) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/legion/llm/call/lex_llm_adapter.rb', line 65

def count_tokens(model:, messages:, **)
  {
    result: estimate_tokens(messages),
    model:  model,
    usage:  {}
  }
end

#embed(model:, text:, dimensions: nil, **opts) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/legion/llm/call/lex_llm_adapter.rb', line 54

def embed(model:, text:, dimensions: nil, **opts)
  response = provider.embed(text, model: model, dimensions: dimensions)

  {
    result:   response.vectors,
    model:    response.model,
    usage:    { input_tokens: response.input_tokens.to_i, output_tokens: 0 },
    metadata: (offering_metadata: opts[:offering_metadata])
  }
end

#offerings(live: false, **filters) ⇒ Object



73
74
75
76
77
# File 'lib/legion/llm/call/lex_llm_adapter.rb', line 73

def offerings(live: false, **filters)
  return [] unless provider.respond_to?(:discover_offerings)

  provider.discover_offerings(live: live, **filters)
end

#stream(model:, messages:, **opts, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/legion/llm/call/lex_llm_adapter.rb', line 34

def stream(model:, messages:, **opts, &block)
  chunks = []
  provider.complete(
    normalize_messages(messages, system: opts[:system]),
    tools:       normalize_tools(opts[:tools]),
    temperature: opts[:temperature],
    params:      opts[:params] || {},
    headers:     opts[:headers] || {},
    schema:      opts[:schema],
    thinking:    opts[:thinking],
    tool_prefs:  opts[:tool_prefs],
    model:       model_info(model, offering_metadata: opts[:offering_metadata])
  ) do |chunk|
    chunks << chunk
    block&.call(chunk)
  end

  chunk_response(chunks, offering_metadata: opts[:offering_metadata])
end