Class: SimpleLLM::Yandex::Sync

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/simple_llm.rb

Instance Method Summary collapse

Methods included from Common

#initialize

Instance Method Details

#call(project, api_key, input, max_output_tokens = 500, temperature = 0, model: "yandexgpt/rc", instructions: nil, max_input_chars: 1000, debug: false) ⇒ String

TODO: ассертить размер input-а при image_input как-то иначе https://aistudio.yandex.ru/docs/ru/ai-studio/api/Responses/createResponse.html

Parameters:

  • project
  • api_key
  • input (Object)
  • max_output_tokens (Integer) (defaults to: 500)

    "An upper bound for the number of tokens that can be generated for a response, including visible output tokens and reasoning tokens."

  • temperature (Integer) (defaults to: 0)

    "What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic."

  • model (String) (defaults to: "yandexgpt/rc")
  • instructions (defaults to: nil)
  • max_input_chars (Integer) (defaults to: 1000)

    предохранительный ассерт, чтобы метод не вызвали со слишком тяжелым большим input-ом

  • debug (true, false) (defaults to: false)

    печатать запросы и ответы

Returns:

  • (String)

    текст из ["output"]["content"][0]["text"]

Raises:

  • (::ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/simple_llm.rb', line 50

def call project, api_key, input, max_output_tokens = 500, temperature = 0, model: "yandexgpt/rc", instructions: nil, max_input_chars: 1000, debug: false
  raise ::ArgumentError, "max_input_chars assertion: #{input.to_s.size}" if input.to_s.size > max_input_chars
  request = lambda do
    ::NetHTTPUtils.request_data(
      "https://ai.api.cloud.yandex.net/v1/responses", :post, :json, header: {
        "OpenAI-Project" => project,
        "Authorization" => "Api-Key #{api_key}",
        "x-data-logging-enabled" => "false",  # https://aistudio.yandex.ru/docs/ru/ai-studio/operations/disable-logging.html
      }, form: {
        model: "gpt://#{project}/#{model}",
        instructions: instructions,
        input: input,
        temperature: temperature,
        max_output_tokens: max_output_tokens,
        # reasoning: {effort: "low"},
      }.compact.tap{ |_| ::STDERR.puts ::JSON.pretty_generate _ if debug }
    ).force_encoding("utf-8")
  end
  ::JSON.parse(
    if @cache
      @cache[ @cache_key_string_function.(
    self.class,
    ::Time.now,
    ::Digest::MD5.hexdigest(api_key),
    ::Process::pid,
    model,
    input,
    max_output_tokens,
    temperature,
    instructions,
      ) ] ||= request.call
    else
      request.call
    end
  ).tap{ |_| ::STDERR.puts ::JSON.pretty_generate _ if debug }["output"].select{ |_| "message" == _["type"] }.map do |message|
    message["content"].assert_one.fetch("text").strip
  end.assert_one_or_less or raise Error, "empty message assertion"
end