Class: Agentic::LlmClient

Inherits:
Object
  • Object
show all
Defined in:
lib/agentic/llm_client.rb

Overview

Generic wrapper for LLM API clients

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ LlmClient

Initializes a new LlmClient

Parameters:

  • config (LlmConfig)

    The configuration for the LLM



13
14
15
16
17
# File 'lib/agentic/llm_client.rb', line 13

def initialize(config)
  @client = OpenAI::Client.new(access_token: Agentic.configuration.access_token)
  @config = config
  @last_response = nil
end

Instance Attribute Details

#clientOpenAI::Client (readonly)

Returns The underlying LLM client instance.

Returns:

  • (OpenAI::Client)

    The underlying LLM client instance



9
10
11
# File 'lib/agentic/llm_client.rb', line 9

def client
  @client
end

#last_responseOpenAI::Client (readonly)

Returns The underlying LLM client instance.

Returns:

  • (OpenAI::Client)

    The underlying LLM client instance



9
10
11
# File 'lib/agentic/llm_client.rb', line 9

def last_response
  @last_response
end

Instance Method Details

#complete(messages, output_schema: nil) ⇒ Hash

Sends a completion request to the LLM

Parameters:

  • messages (Array<Hash>)

    The messages to send

Returns:

  • (Hash)

    The response from the LLM



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/agentic/llm_client.rb', line 22

def complete(messages, output_schema: nil)
  parameters = {model: @config.model, messages: messages}
  if output_schema
    parameters[:response_format] = {
      type: "json_schema",
      json_schema: output_schema.to_hash
    }
  end

  @last_response = client.chat(parameters: parameters)

  if output_schema
    content = JSON.parse(@last_response.dig("choices", 0, "message", "content"))

    if (refusal = @last_response.dig("choices", 0, "message", "refusal"))
      {refusal: refusal, content: nil}
    else
      {content: content}
    end
  else
    @last_response.dig("choices", 0, "message", "content")
  end
end

#modelsArray<Hash>

Fetches available models from the LLM provider

Returns:

  • (Array<Hash>)

    The list of available models



48
49
50
# File 'lib/agentic/llm_client.rb', line 48

def models
  client.models.list&.dig("data")
end

#query_generation_stats(generation_id) ⇒ Hash

Queries generation stats for a given generation ID

Parameters:

  • generation_id (String)

    The ID of the generation

Returns:

  • (Hash)

    The generation stats



55
56
57
# File 'lib/agentic/llm_client.rb', line 55

def query_generation_stats(generation_id)
  client.query_generation_stats(generation_id)
end