crystal-mistral

Mistral API client for Crystal programming language

⚡ Crystal Mistral Client

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      crystal-mistral:
        github: Skrebnevf/crystal-mistral
    
  2. Run shards install

🖥️ Usage

require "crystal-mistral"

# API key can be passed directly or loaded from ENV["MISTRAL_API_KEY"]
client = CrystalMistral::Client.new

💬 Chat Completion

# Use custom Messages type
messages = [
  Messages.new(role: Role::User, content: "Hello!")
]
# or String type
messages = %([{"role": "user", "content": "Best place in Norilsk"}])

response = client.chat(
  model: "mistral-large-latest",
  messages: messages,
  temperature: 0.7_f32
)

puts response.choices[0].message.content

💻 Code Completion

 client = CrystalMistral::Client.new

  response = client.code(
    model: "codestral-2405",
    prompt: "def hello(name : String)",
    suffix: "",
    temperature: 0.7
  )

  puts "Generated code:\n#{puts resp.choices[0].message.content}"

🧠 Embeddings

texts = ["Let's compare London and Paris", "Compare?!"]

response = client.embeddings(
  model: "mistral-embed",
  input: texts
)

response.data.each_with_index do |embedding, idx|
  puts "Embedding ##{idx}: #{embedding.embedding[0..4]}"
end

📊 Classifiers

# Moderation
response = client.moderation(
  model: "mistral-moderation-latest",
  input: "I hate you and want to hurt people"
)

puts "Content: #{response.results[0].categories}"

# Chat moderation
# Use custom Messages type
messages = [
  Messages.new(role: "user", content: "I want to hate someone."),
  Messages.new(role: "assistant", content: "That doesn't sound good."),
]
# or String
messages = %([{"role": "user", "content": "I want to hate someone."}])

response = client.chat_moderations(
  model: "mistral-chat-moderation-latest",
  input: messages
)

puts "Content: #{response.results[0].categories}"

✔️ TODO

  • Add Chat Completion
  • Add Embeddings
  • Add Tests
  • Add FIM
  • Add Agents
  • Add Classifiers
  • Add Files
  • Add Fine Tuning

🤝 Contributing

  1. Fork it (https://github.com/Skrebnevf/crystal-mistral/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

🏠 Contributors

Repository

crystal-mistral

Owner
Statistic
  • 1
  • 0
  • 0
  • 0
  • 0
  • about 3 hours ago
  • June 22, 2025
License

MIT License

Links
Synced at

Mon, 23 Jun 2025 19:02:07 GMT

Languages