Class: Durable::Llm::Providers::Together

Inherits:
Base
  • Object
show all
Defined in:
lib/durable/llm/providers/together.rb

Overview

Together AI provider for accessing various language models through the Together API.

Provides completion, embedding, and streaming capabilities with authentication handling, error management, and response normalization. It establishes HTTP connections to Together’s API endpoint, processes chat completions and embeddings, handles various API error responses, and includes comprehensive response classes to format Together’s API responses into a consistent interface.

Defined Under Namespace

Classes: TogetherChoice, TogetherEmbeddingResponse, TogetherMessage, TogetherResponse, TogetherStreamChoice, TogetherStreamDelta, TogetherStreamResponse

Constant Summary collapse

BASE_URL =
'https://api.together.xyz/v1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

models, options, #stream?

Constructor Details

#initialize(api_key: nil) ⇒ Together

Initializes the Together provider with an API key.

Parameters:

  • api_key (String, nil) (defaults to: nil)

    The API key to use. If nil, uses default_api_key



38
39
40
41
42
43
44
45
# File 'lib/durable/llm/providers/together.rb', line 38

def initialize(api_key: nil)
  super
  @conn = Faraday.new(url: BASE_URL) do |faraday|
    faraday.request :json
    faraday.response :json
    faraday.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



33
34
35
# File 'lib/durable/llm/providers/together.rb', line 33

def api_key
  @api_key
end

Class Method Details

.stream?Boolean

Indicates whether this provider supports streaming.

Returns:

  • (Boolean)

    Always true for Together



89
90
91
# File 'lib/durable/llm/providers/together.rb', line 89

def self.stream?
  true
end

Instance Method Details

#completion(options) ⇒ TogetherResponse

Completes a chat conversation using the Together API.

Parameters:

  • options (Hash)

    The options for the completion request

Returns:



51
52
53
54
55
56
57
58
# File 'lib/durable/llm/providers/together.rb', line 51

def completion(options)
  response = @conn.post('chat/completions') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.body = options
  end

  handle_response(response)
end

#default_api_keyString?

Returns the default API key for Together AI.

Returns:

  • (String, nil)

    The API key from configuration or environment variable



25
26
27
28
29
30
31
# File 'lib/durable/llm/providers/together.rb', line 25

def default_api_key
  begin
    Durable::Llm.configuration.together&.api_key
  rescue NoMethodError
    nil
  end || ENV['TOGETHER_API_KEY']
end

#embedding(model:, input:, **options) ⇒ TogetherEmbeddingResponse

Generates embeddings for the given input using the Together API.

Parameters:

  • model (String)

    The model to use for embedding

  • input (String, Array<String>)

    The input text(s) to embed

  • options (Hash)

    Additional options for the embedding request

Returns:



66
67
68
69
70
71
72
73
# File 'lib/durable/llm/providers/together.rb', line 66

def embedding(model:, input:, **options)
  response = @conn.post('embeddings') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.body = { model: model, input: input, **options }
  end

  handle_response(response, TogetherEmbeddingResponse)
end

#modelsArray<String>

Retrieves the list of available models from the Together API.

Returns:

  • (Array<String>)

    Array of model IDs



78
79
80
81
82
83
84
# File 'lib/durable/llm/providers/together.rb', line 78

def models
  response = @conn.get('models') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
  end

  handle_response(response).data.map { |model| model['id'] }
end

#stream(options) {|TogetherStreamResponse| ... } ⇒ Object

Streams a chat completion using the Together API.

Parameters:

  • options (Hash)

    The options for the streaming request

Yields:



97
98
99
100
101
102
103
104
105
106
# File 'lib/durable/llm/providers/together.rb', line 97

def stream(options)
  options = prepare_stream_options(options)

  @conn.post('chat/completions') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.headers['Accept'] = 'text/event-stream'
    req.body = options
    req.options.on_data = stream_proc { |chunk| yield TogetherStreamResponse.new(chunk) }
  end
end