Wilson Silva
Wilson Silva
All Posts May 5 2024

Mistral Ruby gem

Why I ported Mistral’s client to Ruby

Python is everywhere

I am taking several AI courses to become proficient in AI development techniques like Retrieval Augmented Generation (RAG), DSPy, and agentic workflows. Most AI libraries, courses, and tutorials use Python as the primary language.

I finished a course recently called Getting Started With Mistral. It taught me effective prompting techniques, function calling, JSON mode, and RAG.

To reinforce my learning, I began transcribing the course. I wrote all code samples in the same language they were taught: Python.

Sharing a common language

What if developers could learn AI using Python and then easily apply their knowledge to their language of choice?

For this to happen in the Ruby ecosystem, we need libraries that match those in Python, with similar features and APIs. That’s why I created a Mistral gem that closely mirrors the official Python client. It was also a great exercise to practice Python.

Take a look at the code samples below to see how easy it is to translate between the two libraries thanks to their similar APIs.

client = MistralClient(api_key=os.environ["MISTRAL_API_KEY"])

response = client.chat_stream(
  model="mistral-small",
  messages=[ChatMessage(role="user", content="Hi")],
)

for chunk in response:
  print(chunk.choices[0].delta.content)
client = Mistral::Client.new(api_key: ENV["MISTRAL_API_KEY"])

response = client.chat_stream(
  model: "mistral-small",
  messages: [Mistral::ChatMessage.new(role: "user", content: "Hi")]
)

for chunk in response do
  print(chunk.choices[0].delta.content)
end

Other gems

When this was written, only two mistral gems existed: mistral_rb and mistral-ai. Here’s how they compare to each other and my gem.

Feature mistral-ai mistral_rb mistral
Completion without streaming
Completion with streaming
Embeddings
Response formats
List models
Function calling
Type safety
Familiar interface
Automatic retries
Configurable timeouts
Code examples
Runtime dependencies 3 11 2

Key takeaways

  • Python’s built-in dependency management is brittle. But tools like Poetry offer a more robust solution, similar to Bundler in the Ruby ecosystem.
  • Gained experience with Pydantic for data validation and pytest for testing in Python
  • Experimented with minitest as a lightweight alternative to RSpec for testing Ruby code
  • Deepened my understanding of implementing enumerators effectively in Ruby
  • Compared to Ruby, Python is ugly 😂