Module: Zuno
- Defined in:
- lib/zuno.rb,
lib/zuno/version.rb
Defined Under Namespace
Modules: Providers Classes: CallbackContext, CallbackControl, Error, MaxIterationsExceeded, ModelDescriptor, ProviderError, SseParser, StreamingError, ToolDefinition, ToolError
Constant Summary collapse
- OPENROUTER_ADAPTER_CONFIG_KEYS =
i[api_key app_url title timeout].freeze
- AI_GATEWAY_ADAPTER_CONFIG_KEYS =
i[api_key timeout base_url].freeze
- REPLICATE_ADAPTER_CONFIG_KEYS =
i[api_key timeout].freeze
- ELEVENLABS_ADAPTER_CONFIG_KEYS =
i[api_key timeout base_url].freeze
- DEFAULT_MAX_ITERATIONS =
1- REPLICATE_PREFER_WAIT_SECONDS =
60- REPLICATE_POLL_INTERVAL_SECONDS =
1- REPLICATE_WAIT_TIMEOUT_SECONDS =
600- REPLICATE_TERMINAL_STATUSES =
%w[succeeded failed canceled aborted].freeze
- VERSION =
"1.1.3"
Class Method Summary collapse
- .ai_gateway(api_key: nil, timeout: Providers::AIGateway::DEFAULT_TIMEOUT, base_url: Providers::AIGateway::DEFAULT_BASE_URL) ⇒ Object
- .default_provider_options ⇒ Object
- .default_provider_options=(options) ⇒ Object
- .elevenlabs(api_key: nil, timeout: Providers::ElevenLabs::DEFAULT_TIMEOUT, base_url: Providers::ElevenLabs::DEFAULT_BASE_URL) ⇒ Object
- .embed(model:, content:, dimensions: nil, encoding_format: nil, provider_options: {}, **options) ⇒ Object
- .generate(model:, messages: nil, system: nil, prompt: nil, input: nil, tools: {}, tool_choice: nil, temperature: nil, max_tokens: nil, provider_options: {}, before_tool_execution: nil, after_tool_execution: nil, before_generation: nil, after_generation: nil) ⇒ Object
- .loop(model:, messages: nil, system: nil, prompt: nil, tools: {}, tool_choice: nil, stop_when: nil, max_iterations: DEFAULT_MAX_ITERATIONS, temperature: nil, max_tokens: nil, provider_options: {}, before_tool_execution: nil, after_tool_execution: nil, before_iteration: nil, after_iteration: nil, before_generation: nil, after_generation: nil) ⇒ Object
- .model(id, provider: :openrouter, provider_options: {}) ⇒ Object
- .openrouter(api_key: nil, app_url: nil, title: nil, timeout: Providers::OpenRouter::DEFAULT_TIMEOUT) ⇒ Object
- .replicate(api_key: nil, timeout: Providers::Replicate::DEFAULT_TIMEOUT) ⇒ Object
- .stream(model:, messages: nil, system: nil, prompt: nil, temperature: nil, max_tokens: nil, provider_options: {}, &block) ⇒ Object
- .tool(name:, description:, input_schema:, &execute) ⇒ Object
- .transcribe(model_id: "scribe_v2", file: nil, cloud_storage_url: nil, source_url: nil, provider_options: {}, **options) ⇒ Object
Class Method Details
.ai_gateway(api_key: nil, timeout: Providers::AIGateway::DEFAULT_TIMEOUT, base_url: Providers::AIGateway::DEFAULT_BASE_URL) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/zuno.rb', line 205 def ai_gateway( api_key: nil, timeout: Providers::AIGateway::DEFAULT_TIMEOUT, base_url: Providers::AIGateway::DEFAULT_BASE_URL ) Providers::AIGateway.new( api_key: api_key, timeout: timeout, base_url: base_url ) end |
.default_provider_options ⇒ Object
177 178 179 |
# File 'lib/zuno.rb', line 177 def ||= {} end |
.default_provider_options=(options) ⇒ Object
181 182 183 |
# File 'lib/zuno.rb', line 181 def () = .is_a?(Hash) ? : {} end |
.elevenlabs(api_key: nil, timeout: Providers::ElevenLabs::DEFAULT_TIMEOUT, base_url: Providers::ElevenLabs::DEFAULT_BASE_URL) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/zuno.rb', line 217 def elevenlabs( api_key: nil, timeout: Providers::ElevenLabs::DEFAULT_TIMEOUT, base_url: Providers::ElevenLabs::DEFAULT_BASE_URL ) Providers::ElevenLabs.new( api_key: api_key, timeout: timeout, base_url: base_url ) end |
.embed(model:, content:, dimensions: nil, encoding_format: nil, provider_options: {}, **options) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/zuno.rb', line 280 def ( model:, content:, dimensions: nil, encoding_format: nil, provider_options: {}, ** ) (content: content) model_descriptor = normalize_model(model) = ( model_descriptor., ) provider = model_descriptor.provider.to_sym case provider when :openrouter, :ai_gateway adapter = provider_adapter(provider, ) response = adapter.( ( model_id: model_descriptor.id, provider: provider, content: content, dimensions: dimensions, encoding_format: encoding_format, provider_options: , options: ) ) = Array(response["data"]).filter_map do |item| item["embedding"] if item.is_a?(Hash) end result = { embeddings: , usage: response["usage"], model: response["model"], raw_response: response } result[:embedding] = .first if .length == 1 result else raise ProviderError, "embed only supports openrouter or ai_gateway provider" end rescue ProviderError raise rescue StandardError => e raise Error, e. end |
.generate(model:, messages: nil, system: nil, prompt: nil, input: nil, tools: {}, tool_choice: nil, temperature: nil, max_tokens: nil, provider_options: {}, before_tool_execution: nil, after_tool_execution: nil, before_generation: nil, after_generation: nil) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/zuno.rb', line 332 def generate( model:, messages: nil, system: nil, prompt: nil, input: nil, tools: {}, tool_choice: nil, temperature: nil, max_tokens: nil, provider_options: {}, before_tool_execution: nil, after_tool_execution: nil, before_generation: nil, after_generation: nil ) callback_control = nil after_generation_called = false callback_control = CallbackControl.new model_descriptor = normalize_model(model) = ( model_descriptor., ) provider = model_descriptor.provider.to_sym call_callback!( before_generation, { model: model_descriptor, mode: "single", provider: provider }, callback_control ) if callback_control.stopped? result = callback_stopped_result( control: callback_control, iterations: [], message: {}, usage: nil, raw_response: nil ) after_generation_called = true call_callback!(after_generation, { ok: true, result: result }, callback_control) return result end result = case provider when :openrouter, :ai_gateway adapter = provider_adapter(provider, ) generate_openrouter_single( model_descriptor: model_descriptor, adapter: adapter, messages: , system: system, prompt: prompt, tools: tools, tool_choice: tool_choice, temperature: temperature, max_tokens: max_tokens, provider_options: , before_tool_execution: before_tool_execution, after_tool_execution: after_tool_execution ) when :replicate raise Error, "tools are not supported for replicate generate" unless normalize_tools(tools).empty? raise Error, "tool_choice is not supported for replicate generate" unless tool_choice.nil? validate_no_webhook_support!() adapter = provider_adapter(provider, ) generate_replicate_single( model_descriptor: model_descriptor, adapter: adapter, input: input, provider_options: ) else raise ProviderError, "Unsupported provider: #{provider}" end after_generation_called = true call_callback!(after_generation, { ok: true, result: result }, callback_control) result rescue ProviderError => e unless after_generation_called after_generation_called = true call_callback!(after_generation, { ok: false, error: e }, callback_control) end raise rescue StandardError => e unless after_generation_called after_generation_called = true call_callback!(after_generation, { ok: false, error: e }, callback_control) end raise Error, e. end |
.loop(model:, messages: nil, system: nil, prompt: nil, tools: {}, tool_choice: nil, stop_when: nil, max_iterations: DEFAULT_MAX_ITERATIONS, temperature: nil, max_tokens: nil, provider_options: {}, before_tool_execution: nil, after_tool_execution: nil, before_iteration: nil, after_iteration: nil, before_generation: nil, after_generation: nil) ⇒ Object
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 |
# File 'lib/zuno.rb', line 433 def loop( model:, messages: nil, system: nil, prompt: nil, tools: {}, tool_choice: nil, stop_when: nil, max_iterations: DEFAULT_MAX_ITERATIONS, temperature: nil, max_tokens: nil, provider_options: {}, before_tool_execution: nil, after_tool_execution: nil, before_iteration: nil, after_iteration: nil, before_generation: nil, after_generation: nil ) callback_control = nil callback_context = nil model_descriptor = normalize_model(model) unless i[openrouter ai_gateway].include?(model_descriptor.provider.to_sym) raise Error, "loop only supports openrouter or ai_gateway provider" end = ( model_descriptor., ) adapter = provider_adapter(model_descriptor.provider, ) callback_context = build_callback_context( messages: , system: system, prompt: prompt, tools: tools ) resolved_stop_when = normalize_stop_when(stop_when) resolved_max_iterations = normalize_max_iterations(max_iterations) after_generation_called = false callback_control = CallbackControl.new call_callback!( before_generation, { model: model_descriptor, messages: (messages: callback_context., system: callback_context.system), tool_names: callback_context.tools.keys, tool_choice: normalize_tool_choice( explicit_tool_choice: tool_choice, provider_options: , tools: callback_context.tools ), max_iterations: resolved_max_iterations, stop_when: resolved_stop_when }, callback_control, callback_context ) normalize_callback_context!(callback_context) if callback_control.stopped? result = callback_stopped_result( control: callback_control, iterations: [], message: {}, usage: nil, raw_response: nil ) after_generation_called = true call_callback!(after_generation, { ok: true, result: result }, callback_control, callback_context) return result end iterations = [] iteration_count = 0 infinite_iterations = resolved_max_iterations == :infinite while infinite_iterations || iteration_count < resolved_max_iterations current_iteration = iteration_count + 1 call_callback!( before_iteration, { iteration_index: current_iteration, messages: (messages: callback_context., system: callback_context.system) }, callback_control, callback_context ) normalize_callback_context!(callback_context) if callback_control.stopped? result = callback_stopped_result( control: callback_control, iterations: iterations, message: {}, usage: nil, raw_response: nil ) after_generation_called = true call_callback!(after_generation, { ok: true, result: result }, callback_control, callback_context) return result end resolved_tool_choice = normalize_tool_choice( explicit_tool_choice: tool_choice, provider_options: , tools: callback_context.tools ) payload = build_payload( model_id: model_descriptor.id, provider: model_descriptor.provider, messages: (messages: callback_context., system: callback_context.system), tools: callback_context.tools, tool_choice: resolved_tool_choice, temperature: temperature, max_tokens: max_tokens, provider_options: ) response = adapter.chat(payload) = response.dig("choices", 0, "message") || {} tool_calls = Array(["tool_calls"]) iteration_record = { index: current_iteration, message: , tool_calls: tool_calls, usage: response["usage"], finish_reason: response.dig("choices", 0, "finish_reason"), tool_results: [] } if tool_calls.empty? iterations << iteration_record call_callback!( after_iteration, { iteration_index: current_iteration, iteration: iteration_record }, callback_control, callback_context ) normalize_callback_context!(callback_context) if callback_control.stopped? result = callback_stopped_result( control: callback_control, iterations: iterations, message: , usage: response["usage"], raw_response: response ) after_generation_called = true call_callback!(after_generation, { ok: true, result: result }, callback_control, callback_context) return result end result = { text: (), message: , usage: response["usage"], finish_reason: response.dig("choices", 0, "finish_reason"), iterations: iterations, raw_response: response } after_generation_called = true call_callback!(after_generation, { ok: true, result: result }, callback_control, callback_context) return result end callback_context.((message: , tool_calls: tool_calls)) stop_triggered = false stop_triggered_tool_name = nil tool_calls.each do |tool_call| tool_call_id = normalize_tool_call_id(tool_call["id"]) arguments = parse_arguments(tool_call.dig("function", "arguments")) tool_name = tool_call.dig("function", "name").to_s call_callback!( before_tool_execution, { iteration_index: current_iteration, tool_call_id: tool_call_id, tool_name: tool_name, input: arguments, raw_tool_call: tool_call }, callback_control, callback_context ) normalize_callback_context!(callback_context) tool_result = execute_tool_call( tool_call: tool_call, tools: callback_context.tools, tool_call_id: tool_call_id, arguments: arguments ) iteration_record[:tool_results] << tool_result call_callback!( after_tool_execution, tool_result.merge(iteration_index: current_iteration), callback_control, callback_context ) normalize_callback_context!(callback_context) if tool_stop_condition_met?(resolved_stop_when, tool_result) stop_triggered = true stop_triggered_tool_name ||= tool_result[:tool_name] end callback_context.( "role" => "tool", "tool_call_id" => tool_result[:tool_call_id], "content" => serialize_tool_content(tool_result[:output]) ) end iterations << iteration_record call_callback!( after_iteration, { iteration_index: current_iteration, iteration: iteration_record }, callback_control, callback_context ) normalize_callback_context!(callback_context) if callback_control.stopped? result = callback_stopped_result( control: callback_control, iterations: iterations, message: , usage: response["usage"], raw_response: response ) after_generation_called = true call_callback!(after_generation, { ok: true, result: result }, callback_control, callback_context) return result end if stop_triggered result = { text: (), message: , usage: response["usage"], finish_reason: "stop_when_tool_called", stop_reason: { type: "tool_called", tool_name: stop_triggered_tool_name }, iterations: iterations, raw_response: response } after_generation_called = true call_callback!(after_generation, { ok: true, result: result }, callback_control, callback_context) return result end iteration_count += 1 end raise MaxIterationsExceeded, "Reached max_iterations=#{resolved_max_iterations} without a final assistant response" unless infinite_iterations rescue ProviderError, MaxIterationsExceeded => e unless after_generation_called after_generation_called = true call_callback!(after_generation, { ok: false, error: e }, callback_control, callback_context) end raise rescue StandardError => e unless after_generation_called after_generation_called = true call_callback!(after_generation, { ok: false, error: e }, callback_control, callback_context) end raise Error, e. end |
.model(id, provider: :openrouter, provider_options: {}) ⇒ Object
185 186 187 |
# File 'lib/zuno.rb', line 185 def model(id, provider: :openrouter, provider_options: {}) ModelDescriptor.new(id: id, provider: provider, provider_options: ) end |
.openrouter(api_key: nil, app_url: nil, title: nil, timeout: Providers::OpenRouter::DEFAULT_TIMEOUT) ⇒ Object
189 190 191 192 193 194 195 196 |
# File 'lib/zuno.rb', line 189 def openrouter(api_key: nil, app_url: nil, title: nil, timeout: Providers::OpenRouter::DEFAULT_TIMEOUT) Providers::OpenRouter.new( api_key: api_key, app_url: app_url, title: title, timeout: timeout ) end |
.replicate(api_key: nil, timeout: Providers::Replicate::DEFAULT_TIMEOUT) ⇒ Object
198 199 200 201 202 203 |
# File 'lib/zuno.rb', line 198 def replicate(api_key: nil, timeout: Providers::Replicate::DEFAULT_TIMEOUT) Providers::Replicate.new( api_key: api_key, timeout: timeout ) end |
.stream(model:, messages: nil, system: nil, prompt: nil, temperature: nil, max_tokens: nil, provider_options: {}, &block) ⇒ Object
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 |
# File 'lib/zuno.rb', line 716 def stream( model:, messages: nil, system: nil, prompt: nil, temperature: nil, max_tokens: nil, provider_options: {}, &block ) raise ArgumentError, "stream requires a block callback" unless block_given? model_descriptor = normalize_model(model) unless i[openrouter ai_gateway].include?(model_descriptor.provider.to_sym) raise ProviderError, "stream only supports openrouter or ai_gateway provider" end = ( model_descriptor., ) adapter = provider_adapter(model_descriptor.provider, ) = (messages: , system: system, prompt: prompt) payload = build_payload( model_id: model_descriptor.id, provider: model_descriptor.provider, messages: , tools: {}, tool_choice: nil, temperature: temperature, max_tokens: max_tokens, provider_options: ).merge("stream" => true) block.call(type: :start, model: model_descriptor.id, provider: model_descriptor.provider) emitted_finish = false adapter.stream(payload) do |raw_data| next if raw_data == "[DONE]" parsed = parse_json(raw_data) raise StreamingError, "Malformed SSE payload: #{raw_data}" if parsed.nil? usage = parsed["usage"] block.call(type: :usage, usage: usage, raw: parsed) if usage.is_a?(Hash) choice = Array(parsed["choices"]).first || {} delta = choice["delta"] || {} text_delta = extract_text_delta(delta) block.call(type: :text_delta, text: text_delta, raw: parsed) if text_delta && !text_delta.empty? Array(delta["tool_calls"]).each do |tool_call_delta| tool_delta = { index: tool_call_delta["index"], id: tool_call_delta["id"], type: tool_call_delta["type"], name: tool_call_delta.dig("function", "name"), arguments_delta: tool_call_delta.dig("function", "arguments") } block.call(type: :tool_call_delta, tool_call: tool_delta, raw: parsed) end finish_reason = choice["finish_reason"] next if finish_reason.nil? || finish_reason.to_s.empty? emitted_finish = true block.call(type: :finish, finish_reason: finish_reason, raw: parsed) end block.call(type: :finish, finish_reason: "stop") unless emitted_finish true rescue ProviderError => e wrapped_error = StreamingError.new(e.) block.call(type: :error, error: wrapped_error.) if block_given? raise wrapped_error rescue StreamingError => e block.call(type: :error, error: e.) if block_given? raise rescue StandardError => e wrapped_error = StreamingError.new(e.) block.call(type: :error, error: wrapped_error.) if block_given? raise wrapped_error end |
.tool(name:, description:, input_schema:, &execute) ⇒ Object
229 230 231 232 233 234 235 236 237 238 |
# File 'lib/zuno.rb', line 229 def tool(name:, description:, input_schema:, &execute) raise ToolError, "A block is required for tool '#{name}'" unless block_given? ToolDefinition.new( name: name, description: description, input_schema: input_schema, execute_proc: execute ) end |
.transcribe(model_id: "scribe_v2", file: nil, cloud_storage_url: nil, source_url: nil, provider_options: {}, **options) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/zuno.rb', line 240 def transcribe( model_id: "scribe_v2", file: nil, cloud_storage_url: nil, source_url: nil, provider_options: {}, ** ) validate_transcription_input!( model_id: model_id, file: file, cloud_storage_url: cloud_storage_url, source_url: source_url ) = ({}, ) adapter = provider_adapter(:elevenlabs, ) response = adapter.transcribe( model_id: model_id, file: file, cloud_storage_url: cloud_storage_url, source_url: source_url, options: ) result = { text: response["text"], language_code: response["language_code"], language_probability: response["language_probability"], words: response["words"], transcripts: response["transcripts"], transcription_id: response["transcription_id"], raw_response: response } result.reject { |_key, value| value.nil? } rescue ProviderError raise rescue StandardError => e raise Error, e. end |