Class: Durable::Llm::Providers::Together
- 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
-
#api_key ⇒ Object
Returns the value of attribute api_key.
Class Method Summary collapse
-
.stream? ⇒ Boolean
Indicates whether this provider supports streaming.
Instance Method Summary collapse
-
#completion(options) ⇒ TogetherResponse
Completes a chat conversation using the Together API.
-
#default_api_key ⇒ String?
Returns the default API key for Together AI.
-
#embedding(model:, input:, **options) ⇒ TogetherEmbeddingResponse
Generates embeddings for the given input using the Together API.
-
#initialize(api_key: nil) ⇒ Together
constructor
Initializes the Together provider with an API key.
-
#models ⇒ Array<String>
Retrieves the list of available models from the Together API.
-
#stream(options) {|TogetherStreamResponse| ... } ⇒ Object
Streams a chat completion using the Together API.
Methods inherited from Base
Constructor Details
#initialize(api_key: nil) ⇒ Together
Initializes the Together provider with an 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_key ⇒ Object
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.
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.
51 52 53 54 55 56 57 58 |
# File 'lib/durable/llm/providers/together.rb', line 51 def completion() response = @conn.post('chat/completions') do |req| req.headers['Authorization'] = "Bearer #{@api_key}" req.body = end handle_response(response) end |
#default_api_key ⇒ String?
Returns the default API key for Together AI.
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.
66 67 68 69 70 71 72 73 |
# File 'lib/durable/llm/providers/together.rb', line 66 def (model:, input:, **) response = @conn.post('embeddings') do |req| req.headers['Authorization'] = "Bearer #{@api_key}" req.body = { model: model, input: input, ** } end handle_response(response, TogetherEmbeddingResponse) end |
#models ⇒ Array<String>
Retrieves the list of available models from the Together API.
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.
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/durable/llm/providers/together.rb', line 97 def stream() = () @conn.post('chat/completions') do |req| req.headers['Authorization'] = "Bearer #{@api_key}" req.headers['Accept'] = 'text/event-stream' req.body = req..on_data = stream_proc { |chunk| yield TogetherStreamResponse.new(chunk) } end end |