Skip to content

gbaptista/maritaca-ai

Repository files navigation

Maritaca AI

A Ruby gem for interacting with MariTalk from Maritaca AI.

The image features a minimalist logo combining a red ruby and a green Brazilian maritaca bird. The left side shows a flat, smooth red ruby, while the right side transitions to a vibrant green maritaca bird, both depicted in a simple, stylized manner. The background is a subtle gradient, enhancing the logo's modern and clean design..

This Gem is designed to provide low-level access to MariTalk, enabling people to build abstractions on top of it. If you are interested in more high-level abstractions or more user-friendly tools, you may want to consider Nano Bots 💎 🤖.

TL;DR and Quick Start

gem 'maritaca-ai', '~> 1.2.0'
require 'maritaca-ai'

client = Maritaca.new(
  credentials: { api_key: ENV['MARITACA_API_KEY'] },
  options: { server_sent_events: true }
)

result = client.chat_inference(
  { model: 'sabia-2-medium',
    chat_mode: true,
    messages: [ { role: 'user', content: 'Oi!' } ] }
)

Result:

{ 'answer' => ' Oi! Como posso ajudar você hoje?',
  'usage' => {
    'completion_tokens' => 15,
    'prompt_tokens' => 3,
    'total_tokens' => 18
  },
  'model' => 'sabia-2-medium' }

Index

Setup

Installing

gem install maritaca-ai -v 1.2.0
gem 'maritaca-ai', '~> 1.2.0'

Credentials

You can obtain your API key at MariTalk.

Enclose credentials in single quotes when using environment variables to prevent issues with the $ character in the API key:

MARITACA_API_KEY='123...$a12...'

Usage

Client

Ensure that you have an API Key for authentication.

Create a new client:

require 'maritaca-ai'

client = Maritaca.new(
  credentials: { api_key: ENV['MARITACA_API_KEY'] },
  options: { server_sent_events: true }
)

Custom Address

You can use a custom address:

require 'maritaca-ai'

client = Maritaca.new(
  credentials: {
    address: 'https://chat.maritaca.ai',
    api_key: ENV['MARITACA_API_KEY']
  }
)

Methods

chat_inference

Without Streaming Events
Chat
result = client.chat_inference(
  { model: 'sabia-2-medium',
    chat_mode: true,
    messages: [ { role: 'user', content: 'Oi!' } ] }
)

Result:

{ 'answer' => ' Oi! Como posso ajudar você hoje?',
  'usage' => {
    'completion_tokens' => 15,
    'prompt_tokens' => 3,
    'total_tokens' => 18
  },
  'model' => 'sabia-2-medium' }
Back-and-Forth Conversations

To maintain a back-and-forth conversation, you need to append the received responses and build a history for your requests:

result = client.chat_inference(
  { model: 'sabia-2-medium',
    chat_mode: true,
    messages: [
      { role: 'user', content: 'Oi, meu nome é Tamanduá.' },
      { role: 'assistant', content: 'Oi Tamanduá, como posso ajudá-lo hoje?' },
      { role: 'user', content: 'Qual é o meu nome?' }
    ] }
)

Result:

{ 'answer' => ' Seu nome é Tamanduá. É um prazer conhecê-lo! Como posso ajudá-lo hoje?',
  'usage' => {
    'completion_tokens' => 35,
    'prompt_tokens' => 39,
    'total_tokens' => 74
  },
  'model' => 'sabia-2-medium' }
Without Chat

You can prompt the model without using chat mode:

result = client.chat_inference(
  { model: 'sabia-2-medium',
    chat_mode: false,
    messages: "Minha terra tem palmeiras,\nOnde canta o Sabiá;\n",
    stopping_tokens: ['.'] }
)

Result:

{ 'answer' =>
    "As aves, que aqui gorjeiam,\n" \
    'Não gorjeiam como lá.',
  'usage' => {
    'completion_tokens' => 21,
    'prompt_tokens' => 21,
    'total_tokens' => 42
  },
  'model' => 'sabia-2-medium' }
Receiving Stream Events

Ensure that you have enabled Server-Sent Events before using blocks for streaming. You also need to add stream: true in your payload:

client.chat_inference(
  { model: 'sabia-2-medium',
    stream: true,
    chat_mode: true,
    messages: [ { role: 'user', content: 'Oi!' } ] }
) do |event, parsed, raw|
  puts event
end

Event:

{ 'text' => ' Oi! Com' }

You can get all the receive events at once as an array:

result = client.chat_inference(
  { model: 'sabia-2-medium',
    stream: true,
    chat_mode: true,
    messages: [ { role: 'user', content: 'Oi!' } ] }
)

Result:

[{ 'text' => ' Oi! Com' },
 { 'text' => 'o posso a' },
 { 'text' => 'judar você' },
 { 'text' => ' hoje?' },
 { 'completion_tokens' => 15,
   'prompt_tokens' => 74,
   'total_tokens' => 89,
   'model' => 'sabia-2-medium' }]

You can mix both as well:

result = client.chat_inference(
  { model: 'sabia-2-medium',
    stream: true,
    chat_mode: true,
    messages: [ { role: 'user', content: 'Oi!' } ] }
) do |event, parsed, raw|
  puts event
end

Streaming and Server-Sent Events (SSE)

Server-Sent Events (SSE) is a technology that allows certain endpoints to offer streaming capabilities, such as creating the impression that "the model is typing along with you," rather than delivering the entire answer all at once.

You can set up the client to use Server-Sent Events (SSE) for all supported endpoints:

client = Maritaca.new(
  credentials: { api_key: ENV['MARITACA_API_KEY'] },
  options: { server_sent_events: true }
)

Or, you can decide on a request basis:

client.chat_inference(
  { model: 'sabia-2-medium',
    stream: true,
    chat_mode: true,
    messages: [ { role: 'user', content: 'Oi!' } ] },
  server_sent_events: true
) do |event, parsed, raw|
  puts event
end

With Server-Sent Events (SSE) enabled, you can use a block to receive partial results via events. This feature is particularly useful for methods that offer streaming capabilities, such as chat_inference: Receiving Stream Events

Server-Sent Events (SSE) Hang

Method calls will hang until the server-sent events finish, so even without providing a block, you can obtain the final results of the received events: Receiving Stream Events

New Functionalities and APIs

Maritaca may launch a new endpoint that we haven't covered in the Gem yet. If that's the case, you may still be able to use it through the request method. For example, chat_inference is just a wrapper for api/chat/inference, which you can call directly like this:

result = client.request(
  'api/chat/inference',
  { model: 'sabia-2-medium',
    chat_mode: true,
    messages: [{ role: 'user', content: 'Oi!' }] },
  request_method: 'POST'
)

Request Options

Adapter

The gem uses Faraday with the Typhoeus adapter by default.

You can use a different adapter if you want:

require 'faraday/net_http'

client = Maritaca.new(
  credentials: { api_key: ENV['MARITACA_API_KEY'] },
  options: { connection: { adapter: :net_http } }
)

Timeout

You can set the maximum number of seconds to wait for the request to complete with the timeout option:

client = Maritaca.new(
  credentials: { api_key: ENV['MARITACA_API_KEY'] },
  options: { connection: { request: { timeout: 5 } } }
)

You can also have more fine-grained control over Faraday's Request Options if you prefer:

client = Maritaca.new(
  credentials: { api_key: ENV['MARITACA_API_KEY'] },
  options: {
    connection: {
      request: {
        timeout: 5,
        open_timeout: 5,
        read_timeout: 5,
        write_timeout: 5
      }
    }
  }
)

Error Handling

Rescuing

require 'maritaca-ai'

begin
  client.chat_inference(
    { model: 'sabia-2-medium',
      chat_mode: true,
      messages: [ { role: 'user', content: 'Oi!' } ] }
  )
rescue Maritaca::Errors::MaritacaError => error
  puts error.class # Maritaca::Errors::RequestError
  puts error.message # 'the server responded with status 500'

  puts error.payload
  # { model: 'sabia-2-medium',
  #   chat_mode: true,
  #   ...
  # }

  puts error.request
  # #<Faraday::ServerError response={:status=>500, :headers...
end

For Short

require 'maritaca-ai/errors'

begin
  client.chat_inference(
    { model: 'sabia-2-medium',
      chat_mode: true,
      messages: [ { role: 'user', content: 'Oi!' } ] }
  )
rescue MaritacaError => error
  puts error.class # Maritaca::Errors::RequestError
end

Errors

MaritacaError

MissingAPIKeyError

RequestError

Development

bundle
rubocop -A

bundle exec ruby spec/tasks/run-client.rb

Purpose

This Gem is designed to provide low-level access to MariTalk, enabling people to build abstractions on top of it. If you are interested in more high-level abstractions or more user-friendly tools, you may want to consider Nano Bots 💎 🤖.

Publish to RubyGems

gem build maritaca-ai.gemspec

gem signin

gem push maritaca-ai-1.2.0.gem

Updating the README

Install Babashka:

curl -s https://raw.githubusercontent.com/babashka/babashka/master/install | sudo bash

Update the template.md file and then:

bb tasks/generate-readme.clj

Trick for automatically updating the README.md when template.md changes:

sudo pacman -S inotify-tools # Arch / Manjaro
sudo apt-get install inotify-tools # Debian / Ubuntu / Raspberry Pi OS
sudo dnf install inotify-tools # Fedora / CentOS / RHEL

while inotifywait -e modify template.md; do bb tasks/generate-readme.clj; done

Trick for Markdown Live Preview:

pip install -U markdown_live_preview

mlp README.md -p 8076

Resources and References

These resources and references may be useful throughout your learning process.

Disclaimer

This is not an official Maritaca AI project, nor is it affiliated with Maritaca AI in any way.

This software is distributed under the MIT License. This license includes a disclaimer of warranty. Moreover, the authors assume no responsibility for any damage or costs that may result from using this project. Use the Maritaca AI Ruby Gem at your own risk.