Class: Durable::Llm::Providers::DeepSeek

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

Overview

DeepSeek provider for language model API interactions

Defined Under Namespace

Classes: DeepSeekChoice, DeepSeekEmbeddingResponse, DeepSeekMessage, DeepSeekResponse, DeepSeekStreamChoice, DeepSeekStreamDelta, DeepSeekStreamResponse

Constant Summary collapse

BASE_URL =
'https://api.deepseek.com'

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) ⇒ DeepSeek

Returns a new instance of DeepSeek.



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

def initialize(api_key: nil)
  super()
  @api_key = api_key || default_api_key
  @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.



22
23
24
# File 'lib/durable/llm/providers/deepseek.rb', line 22

def api_key
  @api_key
end

Class Method Details

.stream?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/durable/llm/providers/deepseek.rb', line 60

def self.stream?
  true
end

Instance Method Details

#completion(options) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/durable/llm/providers/deepseek.rb', line 34

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_keyObject



18
19
20
# File 'lib/durable/llm/providers/deepseek.rb', line 18

def default_api_key
  Durable::Llm.configuration.deepseek&.api_key || ENV['DEEPSEEK_API_KEY']
end

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



43
44
45
46
47
48
49
50
# File 'lib/durable/llm/providers/deepseek.rb', line 43

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, DeepSeekEmbeddingResponse)
end

#modelsObject



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

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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/durable/llm/providers/deepseek.rb', line 64

def stream(options)
  opts = options.dup
  opts[:stream] = true
  opts['temperature'] = opts['temperature'].to_f if opts['temperature']

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