Class: ActiveGenie::Providers::AnthropicProvider

Inherits:
BaseProvider
  • Object
show all
Defined in:
lib/active_genie/providers/anthropic_provider.rb

Overview

Provider for interacting with the Anthropic (Claude) API with json response

Constant Summary collapse

ANTHROPIC_ENDPOINT =
'/v1/messages'

Constants inherited from BaseProvider

BaseProvider::DEFAULT_HEADERS, BaseProvider::DEFAULT_MAX_RETRIES, BaseProvider::DEFAULT_OPEN_TIMEOUT, BaseProvider::DEFAULT_RETRY_DELAY, BaseProvider::DEFAULT_TIMEOUT

Instance Method Summary collapse

Methods inherited from BaseProvider

#delete, #get, #initialize, #post, #put

Constructor Details

This class inherits a constructor from ActiveGenie::Providers::BaseProvider

Instance Method Details

#function_calling(messages, function) ⇒ Hash?

Requests structured JSON output from the Anthropic Claude model based on a schema.

Parameters:

  • messages (Array<Hash>)

    A list of messages representing the conversation history. Each hash should have :role (‘user’, ‘assistant’, or ‘system’) and :content (String). Claude uses ‘user’, ‘assistant’, and ‘system’ roles.

  • function (Hash)

    A JSON schema definition describing the desired output format.

Returns:

  • (Hash, nil)

    The parsed JSON object matching the schema, or nil if parsing fails or content is empty.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_genie/providers/anthropic_provider.rb', line 19

def function_calling(messages, function)
  system_message, user_messages = split_messages(messages)
  tool = function_to_tool(function)

  payload = {
    model:,
    system: system_message,
    messages: user_messages,
    tools: [tool],
    tool_choice: { name: tool[:name], type: 'tool' },
    max_tokens: @config.llm.max_tokens,
    temperature: @config.llm.temperature || 0
  }

  response = retry_with_backoff do
    request(payload)
  end

  response.dig('content', 0, 'input')
end