Class: Aiko::LLM::OpenAICompatible
- Defined in:
- lib/aiko/llm/openai_compatible.rb
Constant Summary collapse
- OPEN_TIMEOUT =
10- READ_TIMEOUT =
300- RETRY_WAITS =
[1, 4].freeze
Instance Method Summary collapse
- #chat(messages:, tools: []) ⇒ Object
-
#initialize(api_key:, base_url:, model:, sleeper: ->(sec) { sleep(sec) }) ⇒ OpenAICompatible
constructor
A new instance of OpenAICompatible.
Constructor Details
#initialize(api_key:, base_url:, model:, sleeper: ->(sec) { sleep(sec) }) ⇒ OpenAICompatible
Returns a new instance of OpenAICompatible.
14 15 16 17 18 19 20 |
# File 'lib/aiko/llm/openai_compatible.rb', line 14 def initialize(api_key:, base_url:, model:, sleeper: ->(sec) { sleep(sec) }) super() @api_key = api_key @endpoint = URI.parse("#{base_url.sub(%r{/+\z}, "")}/chat/completions") @model = model @sleeper = sleeper end |
Instance Method Details
#chat(messages:, tools: []) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/aiko/llm/openai_compatible.rb', line 22 def chat(messages:, tools: []) body = { "model" => @model, "messages" => } body["tools"] = tools unless tools.empty? status, response_body = post_with_retry(body) unless (200..299).cover?(status) raise APIError, "API request failed with status #{status}: #{response_body.to_s[0, 500]}" end to_response(parse_json(response_body)) end |