Class: Deepseek
- Inherits:
-
LastLLM::Provider
- Object
- LastLLM::Provider
- Deepseek
- Defined in:
- lib/last_llm/providers/deepseek.rb
Overview
Deepseek provider implementation
Constant Summary collapse
- BASE_ENDPOINT =
'https://api.deepseek.com'
Instance Attribute Summary
Attributes inherited from LastLLM::Provider
Instance Method Summary collapse
- #generate_object(prompt, schema, options = {}) ⇒ Object
- #generate_text(prompt, options = {}) ⇒ Object
-
#initialize(config) ⇒ Deepseek
constructor
A new instance of Deepseek.
Methods inherited from LastLLM::Provider
#handle_request_error, #parse_response
Constructor Details
#initialize(config) ⇒ Deepseek
Returns a new instance of Deepseek.
9 10 11 12 |
# File 'lib/last_llm/providers/deepseek.rb', line 9 def initialize(config) super(Constants::DEEPSEEK, config) @conn = connection(config[:base_url] || BASE_ENDPOINT) end |
Instance Method Details
#generate_object(prompt, schema, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/last_llm/providers/deepseek.rb', line 36 def generate_object(prompt, schema, = {}) system_prompt = 'You are a helpful assistant that responds with valid JSON.' formatted_prompt = LastLLM::StructuredOutput.format_prompt(prompt, schema) = [ { role: 'system', content: system_prompt }, { role: 'user', content: formatted_prompt } ] response = @conn.post('/v1/chat/completions') do |req| req.body = { model: [:model] || @config[:model] || 'deepseek-chat', messages: , temperature: [:temperature] || 0.2, top_p: [:top_p] || 0.8, stream: false }.compact end result = parse_response(response) content = result.dig(:choices, 0, :message, :content) begin JSON.parse(content, symbolize_names: true) rescue JSON::ParserError => e # Try to clean markdown code blocks and parse again content.gsub!("```json\n", '').gsub!("\n```", '') begin JSON.parse(content, symbolize_names: true) rescue JSON::ParserError raise LastLLM::ApiError, "Invalid JSON response: #{e.}" end end rescue Faraday::Error => e handle_request_error(e) end |
#generate_text(prompt, options = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/last_llm/providers/deepseek.rb', line 14 def generate_text(prompt, = {}) = (prompt, ) response = @conn.post('/v1/chat/completions') do |req| req.body = { model: [:model] || @config[:model] || 'deepseek-chat', messages: , temperature: [:temperature] || 0.7, top_p: [:top_p] || 0.8, max_tokens: [:max_tokens], stream: false }.compact end result = parse_response(response) content = result.dig(:choices, 0, :message, :content) content.to_s rescue Faraday::Error => e handle_request_error(e) end |