Class: LastLLM::Providers::Deepseek

Inherits:
LastLLM::Provider show all
Defined in:
lib/last_llm/providers/deepseek.rb

Overview

Deepseek provider implementation

Constant Summary collapse

BASE_ENDPOINT =

API Configuration

'https://api.deepseek.com'
DEFAULT_MODEL =
'deepseek-chat'
DEFAULT_TEMPERATURE =

LLM Default Parameters

0.7
DEFAULT_TOP_P =
0.8
DEFAULT_TEMPERATURE_OBJECT =
0.2
SUCCESS_STATUS =

Response Configuration

200
UNAUTHORIZED_STATUS =

Error Status Codes

401
BAD_REQUEST_STATUS =
400

Instance Attribute Summary

Attributes inherited from LastLLM::Provider

#config, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LastLLM::Provider

#parse_response

Constructor Details

#initialize(config) ⇒ Deepseek

Returns a new instance of Deepseek.



25
26
27
28
# File 'lib/last_llm/providers/deepseek.rb', line 25

def initialize(config)
  super(Constants::DEEPSEEK, config)
  @conn = connection(config[:base_url] || BASE_ENDPOINT)
end

Class Method Details

.execute_tool(tool, response) ⇒ Hash?

Execute a tool from a Deepseek response

Parameters:

  • tool (LastLLM::Tool)

    The tool to execute

  • response (Hash)

    The Deepseek response containing tool call information

Returns:

  • (Hash, nil)

    The result of the function call or nil if the tool wasn’t called



68
69
70
71
72
73
74
# File 'lib/last_llm/providers/deepseek.rb', line 68

def self.execute_tool(tool, response)
  tool_call = response.dig(:choices, 0, :message, :tool_calls)&.first
  return nil unless tool_call && tool_call[:function][:name] == tool.name

  arguments = JSON.parse(tool_call[:function][:arguments], symbolize_names: true)
  tool.call(arguments)
end

.format_tool(tool) ⇒ Hash

Format a tool for Deepseek function calling

Parameters:

Returns:

  • (Hash)

    The tool in Deepseek format



53
54
55
56
57
58
59
60
61
62
# File 'lib/last_llm/providers/deepseek.rb', line 53

def self.format_tool(tool)
  {
    type: 'function',
    function: {
      name: tool.name,
      description: tool.description,
      parameters: tool.parameters
    }
  }
end

Instance Method Details

#generate_object(prompt, schema, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/last_llm/providers/deepseek.rb', line 36

def generate_object(prompt, schema, options = {})
  system_prompt = 'You are a helpful assistant that responds with valid JSON.'
  formatted_prompt = LastLLM::StructuredOutput.format_prompt(prompt, schema)

  options = options.dup
  options[:system_prompt] = system_prompt
  options[:temperature] ||= DEFAULT_TEMPERATURE_OBJECT

  make_request(formatted_prompt, options) do |result|
    content = result.dig(:choices, 0, :message, :content)
    parse_json_response(content)
  end
end

#generate_text(prompt, options = {}) ⇒ Object



30
31
32
33
34
# File 'lib/last_llm/providers/deepseek.rb', line 30

def generate_text(prompt, options = {})
  make_request(prompt, options) do |result|
    result.dig(:choices, 0, :message, :content).to_s
  end
end