Class: Zuno::Providers::AIGateway
- Inherits:
-
Object
- Object
- Zuno::Providers::AIGateway
- Defined in:
- lib/zuno.rb
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://ai-gateway.vercel.sh/v1".freeze
- DEFAULT_TIMEOUT =
120_000
Instance Method Summary collapse
- #chat(payload) ⇒ Object
- #embed(payload) ⇒ Object
-
#initialize(api_key: nil, timeout: DEFAULT_TIMEOUT, base_url: DEFAULT_BASE_URL) ⇒ AIGateway
constructor
A new instance of AIGateway.
- #model(model_id) ⇒ Object
- #stream(payload) ⇒ Object
Constructor Details
#initialize(api_key: nil, timeout: DEFAULT_TIMEOUT, base_url: DEFAULT_BASE_URL) ⇒ AIGateway
Returns a new instance of AIGateway.
1623 1624 1625 1626 1627 1628 1629 |
# File 'lib/zuno.rb', line 1623 def initialize(api_key: nil, timeout: DEFAULT_TIMEOUT, base_url: DEFAULT_BASE_URL) @api_key = api_key raise ProviderError, "Vercel Gateway API key not configured" if @api_key.nil? || @api_key.to_s.empty? @timeout = timeout @base_url = base_url.to_s.empty? ? DEFAULT_BASE_URL : base_url.to_s end |
Instance Method Details
#chat(payload) ⇒ Object
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 |
# File 'lib/zuno.rb', line 1639 def chat(payload) response = Typhoeus.post( chat_completions_url, headers: headers, body: JSON.generate(payload), timeout: @timeout ) validate_response!(response) parsed = JSON.parse(response.body) raise ProviderError, "Vercel Gateway returned invalid JSON" unless parsed.is_a?(Hash) parsed rescue JSON::ParserError => e raise ProviderError, "Failed to parse Vercel Gateway response: #{e.}" end |
#embed(payload) ⇒ Object
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 |
# File 'lib/zuno.rb', line 1656 def (payload) response = Typhoeus.post( , headers: headers, body: JSON.generate(payload), timeout: @timeout ) validate_response!(response) parsed = JSON.parse(response.body) raise ProviderError, "Vercel Gateway returned invalid JSON" unless parsed.is_a?(Hash) parsed rescue JSON::ParserError => e raise ProviderError, "Failed to parse Vercel Gateway response: #{e.}" end |
#model(model_id) ⇒ Object
1631 1632 1633 1634 1635 1636 1637 |
# File 'lib/zuno.rb', line 1631 def model(model_id) ModelDescriptor.new( id: model_id, provider: :ai_gateway, provider_options: ) end |
#stream(payload) ⇒ Object
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 |
# File 'lib/zuno.rb', line 1673 def stream(payload) raise ArgumentError, "stream requires a block callback" unless block_given? request = Typhoeus::Request.new( chat_completions_url, method: :post, headers: headers, body: JSON.generate(payload), timeout: @timeout ) parser = SseParser.new { |data| yield(data) } request.on_body do |chunk| parser.push(chunk) nil end request.run validate_response!(request.response) parser.flush end |